blob: 9e7ece0e5e5ecc81750f3db0977c1831b70fd547 [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 = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .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 Malinen36aedc92008-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 Berg55682962007-09-20 13:09:35 -0400373};
374
Johannes Berge31b8212010-10-05 19:39:30 +0200375/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000376static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200377 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200378 [NL80211_KEY_IDX] = { .type = NLA_U8 },
379 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200380 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200381 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
382 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200383 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100384 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
385};
386
387/* policy for the key default flags */
388static const struct nla_policy
389nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
390 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
391 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200392};
393
Johannes Bergff1b6e62011-05-04 15:37:28 +0200394/* policy for WoWLAN attributes */
395static const struct nla_policy
396nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
397 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
398 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
400 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200401 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
402 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
403 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
404 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100405 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
406};
407
408static const struct nla_policy
409nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
410 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
411 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
412 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
413 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
414 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
415 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
416 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
417 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
418 },
419 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
420 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
421 },
422 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
423 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200425};
426
Johannes Berge5497d72011-07-05 16:35:40 +0200427/* policy for GTK rekey offload attributes */
428static const struct nla_policy
429nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
430 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
431 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
432 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
433};
434
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300435static const struct nla_policy
436nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200437 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300438 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700439 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300440};
441
Holger Schuriga0438972009-11-11 11:30:02 +0100442/* ifidx get helper */
443static int nl80211_get_ifidx(struct netlink_callback *cb)
444{
445 int res;
446
447 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
448 nl80211_fam.attrbuf, nl80211_fam.maxattr,
449 nl80211_policy);
450 if (res)
451 return res;
452
453 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
454 return -EINVAL;
455
456 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
457 if (!res)
458 return -EINVAL;
459 return res;
460}
461
Johannes Berg67748892010-10-04 21:14:06 +0200462static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
463 struct netlink_callback *cb,
464 struct cfg80211_registered_device **rdev,
465 struct net_device **dev)
466{
467 int ifidx = cb->args[0];
468 int err;
469
470 if (!ifidx)
471 ifidx = nl80211_get_ifidx(cb);
472 if (ifidx < 0)
473 return ifidx;
474
475 cb->args[0] = ifidx;
476
477 rtnl_lock();
478
479 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
480 if (!*dev) {
481 err = -ENODEV;
482 goto out_rtnl;
483 }
484
485 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100486 if (IS_ERR(*rdev)) {
487 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200488 goto out_rtnl;
489 }
490
491 return 0;
492 out_rtnl:
493 rtnl_unlock();
494 return err;
495}
496
497static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
498{
499 cfg80211_unlock_rdev(rdev);
500 rtnl_unlock();
501}
502
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100503/* IE validation */
504static bool is_valid_ie_attr(const struct nlattr *attr)
505{
506 const u8 *pos;
507 int len;
508
509 if (!attr)
510 return true;
511
512 pos = nla_data(attr);
513 len = nla_len(attr);
514
515 while (len) {
516 u8 elemlen;
517
518 if (len < 2)
519 return false;
520 len -= 2;
521
522 elemlen = pos[1];
523 if (elemlen > len)
524 return false;
525
526 len -= elemlen;
527 pos += 2 + elemlen;
528 }
529
530 return true;
531}
532
Johannes Berg55682962007-09-20 13:09:35 -0400533/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000534static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400535 int flags, u8 cmd)
536{
537 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000538 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400539}
540
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400541static int nl80211_msg_put_channel(struct sk_buff *msg,
542 struct ieee80211_channel *chan)
543{
David S. Miller9360ffd2012-03-29 04:41:26 -0400544 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
545 chan->center_freq))
546 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400547
David S. Miller9360ffd2012-03-29 04:41:26 -0400548 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
549 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
550 goto nla_put_failure;
551 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
552 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
553 goto nla_put_failure;
554 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
555 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
556 goto nla_put_failure;
Johannes Berg1c33a052013-02-18 23:44:38 +0100557 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
558 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
559 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400560
David S. Miller9360ffd2012-03-29 04:41:26 -0400561 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
562 DBM_TO_MBM(chan->max_power)))
563 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400564
565 return 0;
566
567 nla_put_failure:
568 return -ENOBUFS;
569}
570
Johannes Berg55682962007-09-20 13:09:35 -0400571/* netlink command implementations */
572
Johannes Bergb9454e82009-07-08 13:29:08 +0200573struct key_parse {
574 struct key_params p;
575 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200576 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200577 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100578 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200579};
580
581static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
582{
583 struct nlattr *tb[NL80211_KEY_MAX + 1];
584 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
585 nl80211_key_policy);
586 if (err)
587 return err;
588
589 k->def = !!tb[NL80211_KEY_DEFAULT];
590 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
591
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100592 if (k->def) {
593 k->def_uni = true;
594 k->def_multi = true;
595 }
596 if (k->defmgmt)
597 k->def_multi = true;
598
Johannes Bergb9454e82009-07-08 13:29:08 +0200599 if (tb[NL80211_KEY_IDX])
600 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
601
602 if (tb[NL80211_KEY_DATA]) {
603 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
604 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
605 }
606
607 if (tb[NL80211_KEY_SEQ]) {
608 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
609 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
610 }
611
612 if (tb[NL80211_KEY_CIPHER])
613 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
614
Johannes Berge31b8212010-10-05 19:39:30 +0200615 if (tb[NL80211_KEY_TYPE]) {
616 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
617 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
618 return -EINVAL;
619 }
620
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100621 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
622 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100623 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
624 tb[NL80211_KEY_DEFAULT_TYPES],
625 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100626 if (err)
627 return err;
628
629 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
630 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
631 }
632
Johannes Bergb9454e82009-07-08 13:29:08 +0200633 return 0;
634}
635
636static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
637{
638 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
639 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
640 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
641 }
642
643 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
644 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
645 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
646 }
647
648 if (info->attrs[NL80211_ATTR_KEY_IDX])
649 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
650
651 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
652 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
653
654 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
655 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
656
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100657 if (k->def) {
658 k->def_uni = true;
659 k->def_multi = true;
660 }
661 if (k->defmgmt)
662 k->def_multi = true;
663
Johannes Berge31b8212010-10-05 19:39:30 +0200664 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
665 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
666 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
667 return -EINVAL;
668 }
669
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100670 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
671 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
672 int err = nla_parse_nested(
673 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
674 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
675 nl80211_key_default_policy);
676 if (err)
677 return err;
678
679 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
680 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
681 }
682
Johannes Bergb9454e82009-07-08 13:29:08 +0200683 return 0;
684}
685
686static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
687{
688 int err;
689
690 memset(k, 0, sizeof(*k));
691 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200692 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200693
694 if (info->attrs[NL80211_ATTR_KEY])
695 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
696 else
697 err = nl80211_parse_key_old(info, k);
698
699 if (err)
700 return err;
701
702 if (k->def && k->defmgmt)
703 return -EINVAL;
704
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100705 if (k->defmgmt) {
706 if (k->def_uni || !k->def_multi)
707 return -EINVAL;
708 }
709
Johannes Bergb9454e82009-07-08 13:29:08 +0200710 if (k->idx != -1) {
711 if (k->defmgmt) {
712 if (k->idx < 4 || k->idx > 5)
713 return -EINVAL;
714 } else if (k->def) {
715 if (k->idx < 0 || k->idx > 3)
716 return -EINVAL;
717 } else {
718 if (k->idx < 0 || k->idx > 5)
719 return -EINVAL;
720 }
721 }
722
723 return 0;
724}
725
Johannes Bergfffd0932009-07-08 14:22:54 +0200726static struct cfg80211_cached_keys *
727nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530728 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200729{
730 struct key_parse parse;
731 struct nlattr *key;
732 struct cfg80211_cached_keys *result;
733 int rem, err, def = 0;
734
735 result = kzalloc(sizeof(*result), GFP_KERNEL);
736 if (!result)
737 return ERR_PTR(-ENOMEM);
738
739 result->def = -1;
740 result->defmgmt = -1;
741
742 nla_for_each_nested(key, keys, rem) {
743 memset(&parse, 0, sizeof(parse));
744 parse.idx = -1;
745
746 err = nl80211_parse_key_new(key, &parse);
747 if (err)
748 goto error;
749 err = -EINVAL;
750 if (!parse.p.key)
751 goto error;
752 if (parse.idx < 0 || parse.idx > 4)
753 goto error;
754 if (parse.def) {
755 if (def)
756 goto error;
757 def = 1;
758 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100759 if (!parse.def_uni || !parse.def_multi)
760 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200761 } else if (parse.defmgmt)
762 goto error;
763 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200764 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200765 if (err)
766 goto error;
767 result->params[parse.idx].cipher = parse.p.cipher;
768 result->params[parse.idx].key_len = parse.p.key_len;
769 result->params[parse.idx].key = result->data[parse.idx];
770 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530771
772 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
773 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
774 if (no_ht)
775 *no_ht = true;
776 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200777 }
778
779 return result;
780 error:
781 kfree(result);
782 return ERR_PTR(err);
783}
784
785static int nl80211_key_allowed(struct wireless_dev *wdev)
786{
787 ASSERT_WDEV_LOCK(wdev);
788
Johannes Bergfffd0932009-07-08 14:22:54 +0200789 switch (wdev->iftype) {
790 case NL80211_IFTYPE_AP:
791 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200792 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700793 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200794 break;
795 case NL80211_IFTYPE_ADHOC:
796 if (!wdev->current_bss)
797 return -ENOLINK;
798 break;
799 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200800 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200801 if (wdev->sme_state != CFG80211_SME_CONNECTED)
802 return -ENOLINK;
803 break;
804 default:
805 return -EINVAL;
806 }
807
808 return 0;
809}
810
Johannes Berg7527a782011-05-13 10:58:57 +0200811static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
812{
813 struct nlattr *nl_modes = nla_nest_start(msg, attr);
814 int i;
815
816 if (!nl_modes)
817 goto nla_put_failure;
818
819 i = 0;
820 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400821 if ((ifmodes & 1) && nla_put_flag(msg, i))
822 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200823 ifmodes >>= 1;
824 i++;
825 }
826
827 nla_nest_end(msg, nl_modes);
828 return 0;
829
830nla_put_failure:
831 return -ENOBUFS;
832}
833
834static int nl80211_put_iface_combinations(struct wiphy *wiphy,
835 struct sk_buff *msg)
836{
837 struct nlattr *nl_combis;
838 int i, j;
839
840 nl_combis = nla_nest_start(msg,
841 NL80211_ATTR_INTERFACE_COMBINATIONS);
842 if (!nl_combis)
843 goto nla_put_failure;
844
845 for (i = 0; i < wiphy->n_iface_combinations; i++) {
846 const struct ieee80211_iface_combination *c;
847 struct nlattr *nl_combi, *nl_limits;
848
849 c = &wiphy->iface_combinations[i];
850
851 nl_combi = nla_nest_start(msg, i + 1);
852 if (!nl_combi)
853 goto nla_put_failure;
854
855 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
856 if (!nl_limits)
857 goto nla_put_failure;
858
859 for (j = 0; j < c->n_limits; j++) {
860 struct nlattr *nl_limit;
861
862 nl_limit = nla_nest_start(msg, j + 1);
863 if (!nl_limit)
864 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400865 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
866 c->limits[j].max))
867 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200868 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
869 c->limits[j].types))
870 goto nla_put_failure;
871 nla_nest_end(msg, nl_limit);
872 }
873
874 nla_nest_end(msg, nl_limits);
875
David S. Miller9360ffd2012-03-29 04:41:26 -0400876 if (c->beacon_int_infra_match &&
877 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
878 goto nla_put_failure;
879 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
880 c->num_different_channels) ||
881 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
882 c->max_interfaces))
883 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200884
885 nla_nest_end(msg, nl_combi);
886 }
887
888 nla_nest_end(msg, nl_combis);
889
890 return 0;
891nla_put_failure:
892 return -ENOBUFS;
893}
894
Eric W. Biederman15e47302012-09-07 20:12:54 +0000895static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400896 struct cfg80211_registered_device *dev)
897{
898 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100899 struct nlattr *nl_bands, *nl_band;
900 struct nlattr *nl_freqs, *nl_freq;
901 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100902 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100903 enum ieee80211_band band;
904 struct ieee80211_channel *chan;
905 struct ieee80211_rate *rate;
906 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200907 const struct ieee80211_txrx_stypes *mgmt_stypes =
908 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400909
Eric W. Biederman15e47302012-09-07 20:12:54 +0000910 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400911 if (!hdr)
912 return -1;
913
David S. Miller9360ffd2012-03-29 04:41:26 -0400914 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
915 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
916 nla_put_u32(msg, NL80211_ATTR_GENERATION,
917 cfg80211_rdev_list_generation) ||
918 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
919 dev->wiphy.retry_short) ||
920 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
921 dev->wiphy.retry_long) ||
922 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
923 dev->wiphy.frag_threshold) ||
924 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
925 dev->wiphy.rts_threshold) ||
926 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
927 dev->wiphy.coverage_class) ||
928 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
929 dev->wiphy.max_scan_ssids) ||
930 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
931 dev->wiphy.max_sched_scan_ssids) ||
932 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
933 dev->wiphy.max_scan_ie_len) ||
934 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
935 dev->wiphy.max_sched_scan_ie_len) ||
936 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
937 dev->wiphy.max_match_sets))
938 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200939
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
941 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
942 goto nla_put_failure;
943 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
944 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
945 goto nla_put_failure;
946 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
947 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
948 goto nla_put_failure;
949 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
950 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
951 goto nla_put_failure;
952 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
953 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
954 goto nla_put_failure;
955 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
956 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
957 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200958
David S. Miller9360ffd2012-03-29 04:41:26 -0400959 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
960 sizeof(u32) * dev->wiphy.n_cipher_suites,
961 dev->wiphy.cipher_suites))
962 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100963
David S. Miller9360ffd2012-03-29 04:41:26 -0400964 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
965 dev->wiphy.max_num_pmkids))
966 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530967
David S. Miller9360ffd2012-03-29 04:41:26 -0400968 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
969 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
970 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200971
David S. Miller9360ffd2012-03-29 04:41:26 -0400972 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
973 dev->wiphy.available_antennas_tx) ||
974 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
975 dev->wiphy.available_antennas_rx))
976 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100977
David S. Miller9360ffd2012-03-29 04:41:26 -0400978 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
979 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
980 dev->wiphy.probe_resp_offload))
981 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200982
Bruno Randolf7f531e02010-12-16 11:30:22 +0900983 if ((dev->wiphy.available_antennas_tx ||
984 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900985 u32 tx_ant = 0, rx_ant = 0;
986 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300987 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900988 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400989 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
990 tx_ant) ||
991 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
992 rx_ant))
993 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900994 }
995 }
996
Johannes Berg7527a782011-05-13 10:58:57 +0200997 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
998 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700999 goto nla_put_failure;
1000
Johannes Bergee688b002008-01-24 19:38:39 +01001001 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1002 if (!nl_bands)
1003 goto nla_put_failure;
1004
1005 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1006 if (!dev->wiphy.bands[band])
1007 continue;
1008
1009 nl_band = nla_nest_start(msg, band);
1010 if (!nl_band)
1011 goto nla_put_failure;
1012
Johannes Bergd51626d2008-10-09 12:20:13 +02001013 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -04001014 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
1015 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1016 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
1017 &dev->wiphy.bands[band]->ht_cap.mcs) ||
1018 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1019 dev->wiphy.bands[band]->ht_cap.cap) ||
1020 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1021 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
1022 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1023 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
1024 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001025
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001026 /* add VHT info */
1027 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1028 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1029 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1030 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1031 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1032 dev->wiphy.bands[band]->vht_cap.cap)))
1033 goto nla_put_failure;
1034
Johannes Bergee688b002008-01-24 19:38:39 +01001035 /* add frequencies */
1036 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1037 if (!nl_freqs)
1038 goto nla_put_failure;
1039
1040 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1041 nl_freq = nla_nest_start(msg, i);
1042 if (!nl_freq)
1043 goto nla_put_failure;
1044
1045 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001046
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001047 if (nl80211_msg_put_channel(msg, chan))
1048 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001049
Johannes Bergee688b002008-01-24 19:38:39 +01001050 nla_nest_end(msg, nl_freq);
1051 }
1052
1053 nla_nest_end(msg, nl_freqs);
1054
1055 /* add bitrates */
1056 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1057 if (!nl_rates)
1058 goto nla_put_failure;
1059
1060 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1061 nl_rate = nla_nest_start(msg, i);
1062 if (!nl_rate)
1063 goto nla_put_failure;
1064
1065 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001066 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1067 rate->bitrate))
1068 goto nla_put_failure;
1069 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1070 nla_put_flag(msg,
1071 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1072 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001073
1074 nla_nest_end(msg, nl_rate);
1075 }
1076
1077 nla_nest_end(msg, nl_rates);
1078
1079 nla_nest_end(msg, nl_band);
1080 }
1081 nla_nest_end(msg, nl_bands);
1082
Johannes Berg8fdc6212009-03-14 09:34:01 +01001083 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1084 if (!nl_cmds)
1085 goto nla_put_failure;
1086
1087 i = 0;
1088#define CMD(op, n) \
1089 do { \
1090 if (dev->ops->op) { \
1091 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001092 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1093 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001094 } \
1095 } while (0)
1096
1097 CMD(add_virtual_intf, NEW_INTERFACE);
1098 CMD(change_virtual_intf, SET_INTERFACE);
1099 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001100 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001101 CMD(add_station, NEW_STATION);
1102 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001103 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001104 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001105 CMD(auth, AUTHENTICATE);
1106 CMD(assoc, ASSOCIATE);
1107 CMD(deauth, DEAUTHENTICATE);
1108 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001109 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001110 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001111 CMD(set_pmksa, SET_PMKSA);
1112 CMD(del_pmksa, DEL_PMKSA);
1113 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001114 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1115 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001116 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001117 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001118 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001119 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001120 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001121 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1122 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001123 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001124 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001125 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001126 i++;
1127 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1128 goto nla_put_failure;
1129 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001130 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001131 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1132 CMD(tdls_mgmt, TDLS_MGMT);
1133 CMD(tdls_oper, TDLS_OPER);
1134 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001135 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1136 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001137 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001138 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001139 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1140 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001141 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1142 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001143 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001144 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001145 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001146
Kalle Valo4745fc02011-11-17 19:06:10 +02001147#ifdef CONFIG_NL80211_TESTMODE
1148 CMD(testmode_cmd, TESTMODE);
1149#endif
1150
Johannes Berg8fdc6212009-03-14 09:34:01 +01001151#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001152
Johannes Berg6829c872009-07-02 09:13:27 +02001153 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001154 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001155 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1156 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001157 }
1158
Johannes Berg6829c872009-07-02 09:13:27 +02001159 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001160 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001161 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1162 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001163 }
1164
Johannes Berg8fdc6212009-03-14 09:34:01 +01001165 nla_nest_end(msg, nl_cmds);
1166
Johannes Berg7c4ef712011-11-18 15:33:48 +01001167 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001168 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1169 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1170 dev->wiphy.max_remain_on_channel_duration))
1171 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001172
David S. Miller9360ffd2012-03-29 04:41:26 -04001173 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1174 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1175 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001176
Johannes Berg2e161f72010-08-12 15:38:38 +02001177 if (mgmt_stypes) {
1178 u16 stypes;
1179 struct nlattr *nl_ftypes, *nl_ifs;
1180 enum nl80211_iftype ift;
1181
1182 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1183 if (!nl_ifs)
1184 goto nla_put_failure;
1185
1186 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1187 nl_ftypes = nla_nest_start(msg, ift);
1188 if (!nl_ftypes)
1189 goto nla_put_failure;
1190 i = 0;
1191 stypes = mgmt_stypes[ift].tx;
1192 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001193 if ((stypes & 1) &&
1194 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1195 (i << 4) | IEEE80211_FTYPE_MGMT))
1196 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001197 stypes >>= 1;
1198 i++;
1199 }
1200 nla_nest_end(msg, nl_ftypes);
1201 }
1202
Johannes Berg74b70a42010-08-24 12:15:53 +02001203 nla_nest_end(msg, nl_ifs);
1204
Johannes Berg2e161f72010-08-12 15:38:38 +02001205 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1206 if (!nl_ifs)
1207 goto nla_put_failure;
1208
1209 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1210 nl_ftypes = nla_nest_start(msg, ift);
1211 if (!nl_ftypes)
1212 goto nla_put_failure;
1213 i = 0;
1214 stypes = mgmt_stypes[ift].rx;
1215 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001216 if ((stypes & 1) &&
1217 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1218 (i << 4) | IEEE80211_FTYPE_MGMT))
1219 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001220 stypes >>= 1;
1221 i++;
1222 }
1223 nla_nest_end(msg, nl_ftypes);
1224 }
1225 nla_nest_end(msg, nl_ifs);
1226 }
1227
Johannes Bergdfb89c52012-06-27 09:23:48 +02001228#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001229 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1230 struct nlattr *nl_wowlan;
1231
1232 nl_wowlan = nla_nest_start(msg,
1233 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1234 if (!nl_wowlan)
1235 goto nla_put_failure;
1236
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1238 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1239 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1240 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1241 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1242 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1243 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1244 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1245 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1246 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1247 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1248 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1249 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1250 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1251 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1252 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1253 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001254 if (dev->wiphy.wowlan.n_patterns) {
1255 struct nl80211_wowlan_pattern_support pat = {
1256 .max_patterns = dev->wiphy.wowlan.n_patterns,
1257 .min_pattern_len =
1258 dev->wiphy.wowlan.pattern_min_len,
1259 .max_pattern_len =
1260 dev->wiphy.wowlan.pattern_max_len,
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08001261 .max_pkt_offset =
1262 dev->wiphy.wowlan.max_pkt_offset,
Johannes Bergff1b6e62011-05-04 15:37:28 +02001263 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001264 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1265 sizeof(pat), &pat))
1266 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001267 }
1268
1269 nla_nest_end(msg, nl_wowlan);
1270 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001271#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001272
Johannes Berg7527a782011-05-13 10:58:57 +02001273 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1274 dev->wiphy.software_iftypes))
1275 goto nla_put_failure;
1276
1277 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1278 goto nla_put_failure;
1279
David S. Miller9360ffd2012-03-29 04:41:26 -04001280 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1281 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1282 dev->wiphy.ap_sme_capa))
1283 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001284
David S. Miller9360ffd2012-03-29 04:41:26 -04001285 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1286 dev->wiphy.features))
1287 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001288
David S. Miller9360ffd2012-03-29 04:41:26 -04001289 if (dev->wiphy.ht_capa_mod_mask &&
1290 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1291 sizeof(*dev->wiphy.ht_capa_mod_mask),
1292 dev->wiphy.ht_capa_mod_mask))
1293 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001294
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05301295 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1296 dev->wiphy.max_acl_mac_addrs &&
1297 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1298 dev->wiphy.max_acl_mac_addrs))
1299 goto nla_put_failure;
1300
Johannes Berg55682962007-09-20 13:09:35 -04001301 return genlmsg_end(msg, hdr);
1302
1303 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001304 genlmsg_cancel(msg, hdr);
1305 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001306}
1307
1308static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1309{
Johannes Berg645e77d2013-03-01 14:03:49 +01001310 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001311 int start = cb->args[0];
1312 struct cfg80211_registered_device *dev;
1313
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001314 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001315 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001316 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1317 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001318 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001319 continue;
Johannes Berg645e77d2013-03-01 14:03:49 +01001320 ret = nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
1321 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1322 dev);
1323 if (ret < 0) {
1324 /*
1325 * If sending the wiphy data didn't fit (ENOBUFS or
1326 * EMSGSIZE returned), this SKB is still empty (so
1327 * it's not too big because another wiphy dataset is
1328 * already in the skb) and we've not tried to adjust
1329 * the dump allocation yet ... then adjust the alloc
1330 * size to be bigger, and return 1 but with the empty
1331 * skb. This results in an empty message being RX'ed
1332 * in userspace, but that is ignored.
1333 *
1334 * We can then retry with the larger buffer.
1335 */
1336 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1337 !skb->len &&
1338 cb->min_dump_alloc < 4096) {
1339 cb->min_dump_alloc = 4096;
1340 mutex_unlock(&cfg80211_mutex);
1341 return 1;
1342 }
Julius Volzb4637272008-07-08 14:02:19 +02001343 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001344 break;
Julius Volzb4637272008-07-08 14:02:19 +02001345 }
Johannes Berg55682962007-09-20 13:09:35 -04001346 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001347 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001348
1349 cb->args[0] = idx;
1350
1351 return skb->len;
1352}
1353
1354static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1355{
1356 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001357 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001358
Johannes Berg645e77d2013-03-01 14:03:49 +01001359 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001360 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001361 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001362
Eric W. Biederman15e47302012-09-07 20:12:54 +00001363 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001364 nlmsg_free(msg);
1365 return -ENOBUFS;
1366 }
Johannes Berg55682962007-09-20 13:09:35 -04001367
Johannes Berg134e6372009-07-10 09:51:34 +00001368 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001369}
1370
Jouni Malinen31888482008-10-30 16:59:24 +02001371static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1372 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1373 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1374 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1375 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1376 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1377};
1378
1379static int parse_txq_params(struct nlattr *tb[],
1380 struct ieee80211_txq_params *txq_params)
1381{
Johannes Berga3304b02012-03-28 11:04:24 +02001382 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001383 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1384 !tb[NL80211_TXQ_ATTR_AIFS])
1385 return -EINVAL;
1386
Johannes Berga3304b02012-03-28 11:04:24 +02001387 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001388 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1389 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1390 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1391 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1392
Johannes Berga3304b02012-03-28 11:04:24 +02001393 if (txq_params->ac >= NL80211_NUM_ACS)
1394 return -EINVAL;
1395
Jouni Malinen31888482008-10-30 16:59:24 +02001396 return 0;
1397}
1398
Johannes Bergf444de02010-05-05 15:25:02 +02001399static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1400{
1401 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001402 * You can only set the channel explicitly for WDS interfaces,
1403 * all others have their channel managed via their respective
1404 * "establish a connection" command (connect, join, ...)
1405 *
1406 * For AP/GO and mesh mode, the channel can be set with the
1407 * channel userspace API, but is only stored and passed to the
1408 * low-level driver when the AP starts or the mesh is joined.
1409 * This is for backward compatibility, userspace can also give
1410 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001411 *
1412 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001413 * whatever else is going on, so they have their own special
1414 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001415 */
1416 return !wdev ||
1417 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001418 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001419 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1420 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001421}
1422
Johannes Berg683b6d32012-11-08 21:25:48 +01001423static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1424 struct genl_info *info,
1425 struct cfg80211_chan_def *chandef)
1426{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301427 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001428
1429 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1430 return -EINVAL;
1431
1432 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1433
1434 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001435 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1436 chandef->center_freq1 = control_freq;
1437 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001438
1439 /* Primary channel not allowed */
1440 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1441 return -EINVAL;
1442
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001443 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1444 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001445
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001446 chantype = nla_get_u32(
1447 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1448
1449 switch (chantype) {
1450 case NL80211_CHAN_NO_HT:
1451 case NL80211_CHAN_HT20:
1452 case NL80211_CHAN_HT40PLUS:
1453 case NL80211_CHAN_HT40MINUS:
1454 cfg80211_chandef_create(chandef, chandef->chan,
1455 chantype);
1456 break;
1457 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001458 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001459 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001460 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1461 chandef->width =
1462 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1463 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1464 chandef->center_freq1 =
1465 nla_get_u32(
1466 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1467 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1468 chandef->center_freq2 =
1469 nla_get_u32(
1470 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1471 }
1472
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001473 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001474 return -EINVAL;
1475
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001476 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1477 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001478 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001479
Johannes Berg683b6d32012-11-08 21:25:48 +01001480 return 0;
1481}
1482
Johannes Bergf444de02010-05-05 15:25:02 +02001483static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1484 struct wireless_dev *wdev,
1485 struct genl_info *info)
1486{
Johannes Berg683b6d32012-11-08 21:25:48 +01001487 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001488 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001489 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1490
1491 if (wdev)
1492 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001493
Johannes Bergf444de02010-05-05 15:25:02 +02001494 if (!nl80211_can_set_dev_channel(wdev))
1495 return -EOPNOTSUPP;
1496
Johannes Berg683b6d32012-11-08 21:25:48 +01001497 result = nl80211_parse_chandef(rdev, info, &chandef);
1498 if (result)
1499 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001500
1501 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001502 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001503 case NL80211_IFTYPE_AP:
1504 case NL80211_IFTYPE_P2P_GO:
1505 if (wdev->beacon_interval) {
1506 result = -EBUSY;
1507 break;
1508 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001509 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001510 result = -EINVAL;
1511 break;
1512 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001513 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001514 result = 0;
1515 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001516 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001517 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001518 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001519 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001520 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001521 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001522 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001523 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001524 }
1525 mutex_unlock(&rdev->devlist_mtx);
1526
1527 return result;
1528}
1529
1530static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1531{
Johannes Berg4c476992010-10-04 21:36:35 +02001532 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1533 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001534
Johannes Berg4c476992010-10-04 21:36:35 +02001535 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001536}
1537
Bill Jordane8347eb2010-10-01 13:54:28 -04001538static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1539{
Johannes Berg43b19952010-10-07 13:10:30 +02001540 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1541 struct net_device *dev = info->user_ptr[1];
1542 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001543 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001544
1545 if (!info->attrs[NL80211_ATTR_MAC])
1546 return -EINVAL;
1547
Johannes Berg43b19952010-10-07 13:10:30 +02001548 if (netif_running(dev))
1549 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001550
Johannes Berg43b19952010-10-07 13:10:30 +02001551 if (!rdev->ops->set_wds_peer)
1552 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001553
Johannes Berg43b19952010-10-07 13:10:30 +02001554 if (wdev->iftype != NL80211_IFTYPE_WDS)
1555 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001556
1557 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001558 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001559}
1560
1561
Johannes Berg55682962007-09-20 13:09:35 -04001562static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1563{
1564 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001565 struct net_device *netdev = NULL;
1566 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001567 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001568 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001569 u32 changed;
1570 u8 retry_short = 0, retry_long = 0;
1571 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001572 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001573
Johannes Bergf444de02010-05-05 15:25:02 +02001574 /*
1575 * Try to find the wiphy and netdev. Normally this
1576 * function shouldn't need the netdev, but this is
1577 * done for backward compatibility -- previously
1578 * setting the channel was done per wiphy, but now
1579 * it is per netdev. Previous userland like hostapd
1580 * also passed a netdev to set_wiphy, so that it is
1581 * possible to let that go to the right netdev!
1582 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001583 mutex_lock(&cfg80211_mutex);
1584
Johannes Bergf444de02010-05-05 15:25:02 +02001585 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1586 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1587
1588 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1589 if (netdev && netdev->ieee80211_ptr) {
1590 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1591 mutex_lock(&rdev->mtx);
1592 } else
1593 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001594 }
1595
Johannes Bergf444de02010-05-05 15:25:02 +02001596 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001597 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1598 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001599 if (IS_ERR(rdev)) {
1600 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001601 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001602 }
1603 wdev = NULL;
1604 netdev = NULL;
1605 result = 0;
1606
1607 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001608 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001609 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001610
1611 /*
1612 * end workaround code, by now the rdev is available
1613 * and locked, and wdev may or may not be NULL.
1614 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001615
1616 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001617 result = cfg80211_dev_rename(
1618 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001619
1620 mutex_unlock(&cfg80211_mutex);
1621
1622 if (result)
1623 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001624
Jouni Malinen31888482008-10-30 16:59:24 +02001625 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1626 struct ieee80211_txq_params txq_params;
1627 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1628
1629 if (!rdev->ops->set_txq_params) {
1630 result = -EOPNOTSUPP;
1631 goto bad_res;
1632 }
1633
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001634 if (!netdev) {
1635 result = -EINVAL;
1636 goto bad_res;
1637 }
1638
Johannes Berg133a3ff2011-11-03 14:50:13 +01001639 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1640 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1641 result = -EINVAL;
1642 goto bad_res;
1643 }
1644
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001645 if (!netif_running(netdev)) {
1646 result = -ENETDOWN;
1647 goto bad_res;
1648 }
1649
Jouni Malinen31888482008-10-30 16:59:24 +02001650 nla_for_each_nested(nl_txq_params,
1651 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1652 rem_txq_params) {
1653 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1654 nla_data(nl_txq_params),
1655 nla_len(nl_txq_params),
1656 txq_params_policy);
1657 result = parse_txq_params(tb, &txq_params);
1658 if (result)
1659 goto bad_res;
1660
Hila Gonene35e4d22012-06-27 17:19:42 +03001661 result = rdev_set_txq_params(rdev, netdev,
1662 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001663 if (result)
1664 goto bad_res;
1665 }
1666 }
1667
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001668 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001669 result = __nl80211_set_channel(rdev,
1670 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1671 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001672 if (result)
1673 goto bad_res;
1674 }
1675
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001676 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001677 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001678 enum nl80211_tx_power_setting type;
1679 int idx, mbm = 0;
1680
Johannes Bergc8442112012-10-24 10:17:18 +02001681 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1682 txp_wdev = NULL;
1683
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001684 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001685 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001686 goto bad_res;
1687 }
1688
1689 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1690 type = nla_get_u32(info->attrs[idx]);
1691
1692 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1693 (type != NL80211_TX_POWER_AUTOMATIC)) {
1694 result = -EINVAL;
1695 goto bad_res;
1696 }
1697
1698 if (type != NL80211_TX_POWER_AUTOMATIC) {
1699 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1700 mbm = nla_get_u32(info->attrs[idx]);
1701 }
1702
Johannes Bergc8442112012-10-24 10:17:18 +02001703 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001704 if (result)
1705 goto bad_res;
1706 }
1707
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001708 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1709 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1710 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001711 if ((!rdev->wiphy.available_antennas_tx &&
1712 !rdev->wiphy.available_antennas_rx) ||
1713 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001714 result = -EOPNOTSUPP;
1715 goto bad_res;
1716 }
1717
1718 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1719 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1720
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001721 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001722 * available antenna masks, except for the "all" mask */
1723 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1724 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001725 result = -EINVAL;
1726 goto bad_res;
1727 }
1728
Bruno Randolf7f531e02010-12-16 11:30:22 +09001729 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1730 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001731
Hila Gonene35e4d22012-06-27 17:19:42 +03001732 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001733 if (result)
1734 goto bad_res;
1735 }
1736
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001737 changed = 0;
1738
1739 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1740 retry_short = nla_get_u8(
1741 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1742 if (retry_short == 0) {
1743 result = -EINVAL;
1744 goto bad_res;
1745 }
1746 changed |= WIPHY_PARAM_RETRY_SHORT;
1747 }
1748
1749 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1750 retry_long = nla_get_u8(
1751 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1752 if (retry_long == 0) {
1753 result = -EINVAL;
1754 goto bad_res;
1755 }
1756 changed |= WIPHY_PARAM_RETRY_LONG;
1757 }
1758
1759 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1760 frag_threshold = nla_get_u32(
1761 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1762 if (frag_threshold < 256) {
1763 result = -EINVAL;
1764 goto bad_res;
1765 }
1766 if (frag_threshold != (u32) -1) {
1767 /*
1768 * Fragments (apart from the last one) are required to
1769 * have even length. Make the fragmentation code
1770 * simpler by stripping LSB should someone try to use
1771 * odd threshold value.
1772 */
1773 frag_threshold &= ~0x1;
1774 }
1775 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1776 }
1777
1778 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1779 rts_threshold = nla_get_u32(
1780 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1781 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1782 }
1783
Lukáš Turek81077e82009-12-21 22:50:47 +01001784 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1785 coverage_class = nla_get_u8(
1786 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1787 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1788 }
1789
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001790 if (changed) {
1791 u8 old_retry_short, old_retry_long;
1792 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001793 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001794
1795 if (!rdev->ops->set_wiphy_params) {
1796 result = -EOPNOTSUPP;
1797 goto bad_res;
1798 }
1799
1800 old_retry_short = rdev->wiphy.retry_short;
1801 old_retry_long = rdev->wiphy.retry_long;
1802 old_frag_threshold = rdev->wiphy.frag_threshold;
1803 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001804 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001805
1806 if (changed & WIPHY_PARAM_RETRY_SHORT)
1807 rdev->wiphy.retry_short = retry_short;
1808 if (changed & WIPHY_PARAM_RETRY_LONG)
1809 rdev->wiphy.retry_long = retry_long;
1810 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1811 rdev->wiphy.frag_threshold = frag_threshold;
1812 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1813 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001814 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1815 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001816
Hila Gonene35e4d22012-06-27 17:19:42 +03001817 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001818 if (result) {
1819 rdev->wiphy.retry_short = old_retry_short;
1820 rdev->wiphy.retry_long = old_retry_long;
1821 rdev->wiphy.frag_threshold = old_frag_threshold;
1822 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001823 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001824 }
1825 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001826
Johannes Berg306d6112008-12-08 12:39:04 +01001827 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001828 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001829 if (netdev)
1830 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001831 return result;
1832}
1833
Johannes Berg71bbc992012-06-15 15:30:18 +02001834static inline u64 wdev_id(struct wireless_dev *wdev)
1835{
1836 return (u64)wdev->identifier |
1837 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1838}
Johannes Berg55682962007-09-20 13:09:35 -04001839
Johannes Berg683b6d32012-11-08 21:25:48 +01001840static int nl80211_send_chandef(struct sk_buff *msg,
1841 struct cfg80211_chan_def *chandef)
1842{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001843 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001844
Johannes Berg683b6d32012-11-08 21:25:48 +01001845 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1846 chandef->chan->center_freq))
1847 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001848 switch (chandef->width) {
1849 case NL80211_CHAN_WIDTH_20_NOHT:
1850 case NL80211_CHAN_WIDTH_20:
1851 case NL80211_CHAN_WIDTH_40:
1852 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1853 cfg80211_get_chandef_type(chandef)))
1854 return -ENOBUFS;
1855 break;
1856 default:
1857 break;
1858 }
1859 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1860 return -ENOBUFS;
1861 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1862 return -ENOBUFS;
1863 if (chandef->center_freq2 &&
1864 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001865 return -ENOBUFS;
1866 return 0;
1867}
1868
Eric W. Biederman15e47302012-09-07 20:12:54 +00001869static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001870 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001871 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001872{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001873 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001874 void *hdr;
1875
Eric W. Biederman15e47302012-09-07 20:12:54 +00001876 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001877 if (!hdr)
1878 return -1;
1879
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001880 if (dev &&
1881 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001882 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001883 goto nla_put_failure;
1884
1885 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1886 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001887 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001888 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001889 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1890 rdev->devlist_generation ^
1891 (cfg80211_rdev_list_generation << 2)))
1892 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001893
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001894 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001895 int ret;
1896 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001897
Johannes Berg683b6d32012-11-08 21:25:48 +01001898 ret = rdev_get_channel(rdev, wdev, &chandef);
1899 if (ret == 0) {
1900 if (nl80211_send_chandef(msg, &chandef))
1901 goto nla_put_failure;
1902 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001903 }
1904
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001905 if (wdev->ssid_len) {
1906 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1907 goto nla_put_failure;
1908 }
1909
Johannes Berg55682962007-09-20 13:09:35 -04001910 return genlmsg_end(msg, hdr);
1911
1912 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001913 genlmsg_cancel(msg, hdr);
1914 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001915}
1916
1917static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1918{
1919 int wp_idx = 0;
1920 int if_idx = 0;
1921 int wp_start = cb->args[0];
1922 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001923 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001924 struct wireless_dev *wdev;
1925
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001926 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001927 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1928 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001929 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001930 if (wp_idx < wp_start) {
1931 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001932 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001933 }
Johannes Berg55682962007-09-20 13:09:35 -04001934 if_idx = 0;
1935
Johannes Bergf5ea9122009-08-07 16:17:38 +02001936 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001937 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001938 if (if_idx < if_start) {
1939 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001940 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001941 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001942 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001943 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001944 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001945 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001946 goto out;
1947 }
1948 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001949 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001950 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001951
1952 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001953 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001954 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001955 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001956
1957 cb->args[0] = wp_idx;
1958 cb->args[1] = if_idx;
1959
1960 return skb->len;
1961}
1962
1963static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1964{
1965 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001966 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001967 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001968
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001969 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001970 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001971 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001972
Eric W. Biederman15e47302012-09-07 20:12:54 +00001973 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001974 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001975 nlmsg_free(msg);
1976 return -ENOBUFS;
1977 }
Johannes Berg55682962007-09-20 13:09:35 -04001978
Johannes Berg134e6372009-07-10 09:51:34 +00001979 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001980}
1981
Michael Wu66f7ac52008-01-31 19:48:22 +01001982static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1983 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1984 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1985 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1986 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1987 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1988};
1989
1990static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1991{
1992 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1993 int flag;
1994
1995 *mntrflags = 0;
1996
1997 if (!nla)
1998 return -EINVAL;
1999
2000 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2001 nla, mntr_flags_policy))
2002 return -EINVAL;
2003
2004 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2005 if (flags[flag])
2006 *mntrflags |= (1<<flag);
2007
2008 return 0;
2009}
2010
Johannes Berg9bc383d2009-11-19 11:55:19 +01002011static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002012 struct net_device *netdev, u8 use_4addr,
2013 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002014{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002015 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002016 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002017 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002018 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002019 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002020
2021 switch (iftype) {
2022 case NL80211_IFTYPE_AP_VLAN:
2023 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2024 return 0;
2025 break;
2026 case NL80211_IFTYPE_STATION:
2027 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2028 return 0;
2029 break;
2030 default:
2031 break;
2032 }
2033
2034 return -EOPNOTSUPP;
2035}
2036
Johannes Berg55682962007-09-20 13:09:35 -04002037static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2038{
Johannes Berg4c476992010-10-04 21:36:35 +02002039 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002040 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002041 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002042 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002043 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002044 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002045 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002046
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002047 memset(&params, 0, sizeof(params));
2048
Johannes Berg04a773a2009-04-19 21:24:32 +02002049 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002050
Johannes Berg723b0382008-09-16 20:22:09 +02002051 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002052 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002053 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002054 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002055 if (ntype > NL80211_IFTYPE_MAX)
2056 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002057 }
2058
Johannes Berg92ffe052008-09-16 20:39:36 +02002059 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002060 struct wireless_dev *wdev = dev->ieee80211_ptr;
2061
Johannes Berg4c476992010-10-04 21:36:35 +02002062 if (ntype != NL80211_IFTYPE_MESH_POINT)
2063 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002064 if (netif_running(dev))
2065 return -EBUSY;
2066
2067 wdev_lock(wdev);
2068 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2069 IEEE80211_MAX_MESH_ID_LEN);
2070 wdev->mesh_id_up_len =
2071 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2072 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2073 wdev->mesh_id_up_len);
2074 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002075 }
2076
Felix Fietkau8b787642009-11-10 18:53:10 +01002077 if (info->attrs[NL80211_ATTR_4ADDR]) {
2078 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2079 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002080 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002081 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002082 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002083 } else {
2084 params.use_4addr = -1;
2085 }
2086
Johannes Berg92ffe052008-09-16 20:39:36 +02002087 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002088 if (ntype != NL80211_IFTYPE_MONITOR)
2089 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002090 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2091 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002092 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002093 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002094
2095 flags = &_flags;
2096 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002097 }
Johannes Berg3b858752009-03-12 09:55:09 +01002098
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002099 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002100 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002101 else
2102 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002103
Johannes Berg9bc383d2009-11-19 11:55:19 +01002104 if (!err && params.use_4addr != -1)
2105 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2106
Johannes Berg55682962007-09-20 13:09:35 -04002107 return err;
2108}
2109
2110static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2111{
Johannes Berg4c476992010-10-04 21:36:35 +02002112 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002113 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002114 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002115 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002116 int err;
2117 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002118 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002119
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002120 memset(&params, 0, sizeof(params));
2121
Johannes Berg55682962007-09-20 13:09:35 -04002122 if (!info->attrs[NL80211_ATTR_IFNAME])
2123 return -EINVAL;
2124
2125 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2126 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2127 if (type > NL80211_IFTYPE_MAX)
2128 return -EINVAL;
2129 }
2130
Johannes Berg79c97e92009-07-07 03:56:12 +02002131 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002132 !(rdev->wiphy.interface_modes & (1 << type)))
2133 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002134
Arend van Spriel1c18f142013-01-08 10:17:27 +01002135 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2136 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2137 ETH_ALEN);
2138 if (!is_valid_ether_addr(params.macaddr))
2139 return -EADDRNOTAVAIL;
2140 }
2141
Johannes Berg9bc383d2009-11-19 11:55:19 +01002142 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002143 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002144 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002145 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002146 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002147 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002148
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002149 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2150 if (!msg)
2151 return -ENOMEM;
2152
Michael Wu66f7ac52008-01-31 19:48:22 +01002153 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2154 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2155 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002156 wdev = rdev_add_virtual_intf(rdev,
2157 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2158 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002159 if (IS_ERR(wdev)) {
2160 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002161 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002162 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002163
Johannes Berg98104fde2012-06-16 00:19:54 +02002164 switch (type) {
2165 case NL80211_IFTYPE_MESH_POINT:
2166 if (!info->attrs[NL80211_ATTR_MESH_ID])
2167 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002168 wdev_lock(wdev);
2169 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2170 IEEE80211_MAX_MESH_ID_LEN);
2171 wdev->mesh_id_up_len =
2172 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2173 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2174 wdev->mesh_id_up_len);
2175 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002176 break;
2177 case NL80211_IFTYPE_P2P_DEVICE:
2178 /*
2179 * P2P Device doesn't have a netdev, so doesn't go
2180 * through the netdev notifier and must be added here
2181 */
2182 mutex_init(&wdev->mtx);
2183 INIT_LIST_HEAD(&wdev->event_list);
2184 spin_lock_init(&wdev->event_lock);
2185 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2186 spin_lock_init(&wdev->mgmt_registrations_lock);
2187
2188 mutex_lock(&rdev->devlist_mtx);
2189 wdev->identifier = ++rdev->wdev_id;
2190 list_add_rcu(&wdev->list, &rdev->wdev_list);
2191 rdev->devlist_generation++;
2192 mutex_unlock(&rdev->devlist_mtx);
2193 break;
2194 default:
2195 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002196 }
2197
Eric W. Biederman15e47302012-09-07 20:12:54 +00002198 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002199 rdev, wdev) < 0) {
2200 nlmsg_free(msg);
2201 return -ENOBUFS;
2202 }
2203
2204 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002205}
2206
2207static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2208{
Johannes Berg4c476992010-10-04 21:36:35 +02002209 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002210 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002211
Johannes Berg4c476992010-10-04 21:36:35 +02002212 if (!rdev->ops->del_virtual_intf)
2213 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002214
Johannes Berg84efbb82012-06-16 00:00:26 +02002215 /*
2216 * If we remove a wireless device without a netdev then clear
2217 * user_ptr[1] so that nl80211_post_doit won't dereference it
2218 * to check if it needs to do dev_put(). Otherwise it crashes
2219 * since the wdev has been freed, unlike with a netdev where
2220 * we need the dev_put() for the netdev to really be freed.
2221 */
2222 if (!wdev->netdev)
2223 info->user_ptr[1] = NULL;
2224
Hila Gonene35e4d22012-06-27 17:19:42 +03002225 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002226}
2227
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002228static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2229{
2230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2231 struct net_device *dev = info->user_ptr[1];
2232 u16 noack_map;
2233
2234 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2235 return -EINVAL;
2236
2237 if (!rdev->ops->set_noack_map)
2238 return -EOPNOTSUPP;
2239
2240 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2241
Hila Gonene35e4d22012-06-27 17:19:42 +03002242 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002243}
2244
Johannes Berg41ade002007-12-19 02:03:29 +01002245struct get_key_cookie {
2246 struct sk_buff *msg;
2247 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002248 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002249};
2250
2251static void get_key_callback(void *c, struct key_params *params)
2252{
Johannes Bergb9454e82009-07-08 13:29:08 +02002253 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002254 struct get_key_cookie *cookie = c;
2255
David S. Miller9360ffd2012-03-29 04:41:26 -04002256 if ((params->key &&
2257 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2258 params->key_len, params->key)) ||
2259 (params->seq &&
2260 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2261 params->seq_len, params->seq)) ||
2262 (params->cipher &&
2263 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2264 params->cipher)))
2265 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002266
Johannes Bergb9454e82009-07-08 13:29:08 +02002267 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2268 if (!key)
2269 goto nla_put_failure;
2270
David S. Miller9360ffd2012-03-29 04:41:26 -04002271 if ((params->key &&
2272 nla_put(cookie->msg, NL80211_KEY_DATA,
2273 params->key_len, params->key)) ||
2274 (params->seq &&
2275 nla_put(cookie->msg, NL80211_KEY_SEQ,
2276 params->seq_len, params->seq)) ||
2277 (params->cipher &&
2278 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2279 params->cipher)))
2280 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002281
David S. Miller9360ffd2012-03-29 04:41:26 -04002282 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2283 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002284
2285 nla_nest_end(cookie->msg, key);
2286
Johannes Berg41ade002007-12-19 02:03:29 +01002287 return;
2288 nla_put_failure:
2289 cookie->error = 1;
2290}
2291
2292static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2293{
Johannes Berg4c476992010-10-04 21:36:35 +02002294 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002295 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002296 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002297 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002298 const u8 *mac_addr = NULL;
2299 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002300 struct get_key_cookie cookie = {
2301 .error = 0,
2302 };
2303 void *hdr;
2304 struct sk_buff *msg;
2305
2306 if (info->attrs[NL80211_ATTR_KEY_IDX])
2307 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2308
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002309 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002310 return -EINVAL;
2311
2312 if (info->attrs[NL80211_ATTR_MAC])
2313 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2314
Johannes Berge31b8212010-10-05 19:39:30 +02002315 pairwise = !!mac_addr;
2316 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2317 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2318 if (kt >= NUM_NL80211_KEYTYPES)
2319 return -EINVAL;
2320 if (kt != NL80211_KEYTYPE_GROUP &&
2321 kt != NL80211_KEYTYPE_PAIRWISE)
2322 return -EINVAL;
2323 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2324 }
2325
Johannes Berg4c476992010-10-04 21:36:35 +02002326 if (!rdev->ops->get_key)
2327 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002328
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002329 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002330 if (!msg)
2331 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002332
Eric W. Biederman15e47302012-09-07 20:12:54 +00002333 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002334 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002335 if (IS_ERR(hdr))
2336 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002337
2338 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002339 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002340
David S. Miller9360ffd2012-03-29 04:41:26 -04002341 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2342 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2343 goto nla_put_failure;
2344 if (mac_addr &&
2345 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2346 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002347
Johannes Berge31b8212010-10-05 19:39:30 +02002348 if (pairwise && mac_addr &&
2349 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2350 return -ENOENT;
2351
Hila Gonene35e4d22012-06-27 17:19:42 +03002352 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2353 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002354
2355 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002356 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002357
2358 if (cookie.error)
2359 goto nla_put_failure;
2360
2361 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002362 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002363
2364 nla_put_failure:
2365 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002366 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002367 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002368 return err;
2369}
2370
2371static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2372{
Johannes Berg4c476992010-10-04 21:36:35 +02002373 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002374 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002375 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002376 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002377
Johannes Bergb9454e82009-07-08 13:29:08 +02002378 err = nl80211_parse_key(info, &key);
2379 if (err)
2380 return err;
2381
2382 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002383 return -EINVAL;
2384
Johannes Bergb9454e82009-07-08 13:29:08 +02002385 /* only support setting default key */
2386 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002387 return -EINVAL;
2388
Johannes Bergfffd0932009-07-08 14:22:54 +02002389 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002390
2391 if (key.def) {
2392 if (!rdev->ops->set_default_key) {
2393 err = -EOPNOTSUPP;
2394 goto out;
2395 }
2396
2397 err = nl80211_key_allowed(dev->ieee80211_ptr);
2398 if (err)
2399 goto out;
2400
Hila Gonene35e4d22012-06-27 17:19:42 +03002401 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002402 key.def_uni, key.def_multi);
2403
2404 if (err)
2405 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002406
Johannes Berg3d23e342009-09-29 23:27:28 +02002407#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002408 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002409#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002410 } else {
2411 if (key.def_uni || !key.def_multi) {
2412 err = -EINVAL;
2413 goto out;
2414 }
2415
2416 if (!rdev->ops->set_default_mgmt_key) {
2417 err = -EOPNOTSUPP;
2418 goto out;
2419 }
2420
2421 err = nl80211_key_allowed(dev->ieee80211_ptr);
2422 if (err)
2423 goto out;
2424
Hila Gonene35e4d22012-06-27 17:19:42 +03002425 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002426 if (err)
2427 goto out;
2428
2429#ifdef CONFIG_CFG80211_WEXT
2430 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2431#endif
2432 }
2433
2434 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002435 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002436
Johannes Berg41ade002007-12-19 02:03:29 +01002437 return err;
2438}
2439
2440static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2441{
Johannes Berg4c476992010-10-04 21:36:35 +02002442 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002443 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002444 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002445 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002446 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002447
Johannes Bergb9454e82009-07-08 13:29:08 +02002448 err = nl80211_parse_key(info, &key);
2449 if (err)
2450 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002451
Johannes Bergb9454e82009-07-08 13:29:08 +02002452 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002453 return -EINVAL;
2454
Johannes Berg41ade002007-12-19 02:03:29 +01002455 if (info->attrs[NL80211_ATTR_MAC])
2456 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2457
Johannes Berge31b8212010-10-05 19:39:30 +02002458 if (key.type == -1) {
2459 if (mac_addr)
2460 key.type = NL80211_KEYTYPE_PAIRWISE;
2461 else
2462 key.type = NL80211_KEYTYPE_GROUP;
2463 }
2464
2465 /* for now */
2466 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2467 key.type != NL80211_KEYTYPE_GROUP)
2468 return -EINVAL;
2469
Johannes Berg4c476992010-10-04 21:36:35 +02002470 if (!rdev->ops->add_key)
2471 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002472
Johannes Berge31b8212010-10-05 19:39:30 +02002473 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2474 key.type == NL80211_KEYTYPE_PAIRWISE,
2475 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002476 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002477
2478 wdev_lock(dev->ieee80211_ptr);
2479 err = nl80211_key_allowed(dev->ieee80211_ptr);
2480 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002481 err = rdev_add_key(rdev, dev, key.idx,
2482 key.type == NL80211_KEYTYPE_PAIRWISE,
2483 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002484 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002485
Johannes Berg41ade002007-12-19 02:03:29 +01002486 return err;
2487}
2488
2489static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2490{
Johannes Berg4c476992010-10-04 21:36:35 +02002491 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002492 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002493 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002494 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002495 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002496
Johannes Bergb9454e82009-07-08 13:29:08 +02002497 err = nl80211_parse_key(info, &key);
2498 if (err)
2499 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002500
2501 if (info->attrs[NL80211_ATTR_MAC])
2502 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2503
Johannes Berge31b8212010-10-05 19:39:30 +02002504 if (key.type == -1) {
2505 if (mac_addr)
2506 key.type = NL80211_KEYTYPE_PAIRWISE;
2507 else
2508 key.type = NL80211_KEYTYPE_GROUP;
2509 }
2510
2511 /* for now */
2512 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2513 key.type != NL80211_KEYTYPE_GROUP)
2514 return -EINVAL;
2515
Johannes Berg4c476992010-10-04 21:36:35 +02002516 if (!rdev->ops->del_key)
2517 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002518
Johannes Bergfffd0932009-07-08 14:22:54 +02002519 wdev_lock(dev->ieee80211_ptr);
2520 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002521
2522 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2523 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2524 err = -ENOENT;
2525
Johannes Bergfffd0932009-07-08 14:22:54 +02002526 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002527 err = rdev_del_key(rdev, dev, key.idx,
2528 key.type == NL80211_KEYTYPE_PAIRWISE,
2529 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002530
Johannes Berg3d23e342009-09-29 23:27:28 +02002531#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002532 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002533 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002534 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002535 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002536 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2537 }
2538#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002539 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002540
Johannes Berg41ade002007-12-19 02:03:29 +01002541 return err;
2542}
2543
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302544/* This function returns an error or the number of nested attributes */
2545static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2546{
2547 struct nlattr *attr;
2548 int n_entries = 0, tmp;
2549
2550 nla_for_each_nested(attr, nl_attr, tmp) {
2551 if (nla_len(attr) != ETH_ALEN)
2552 return -EINVAL;
2553
2554 n_entries++;
2555 }
2556
2557 return n_entries;
2558}
2559
2560/*
2561 * This function parses ACL information and allocates memory for ACL data.
2562 * On successful return, the calling function is responsible to free the
2563 * ACL buffer returned by this function.
2564 */
2565static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2566 struct genl_info *info)
2567{
2568 enum nl80211_acl_policy acl_policy;
2569 struct nlattr *attr;
2570 struct cfg80211_acl_data *acl;
2571 int i = 0, n_entries, tmp;
2572
2573 if (!wiphy->max_acl_mac_addrs)
2574 return ERR_PTR(-EOPNOTSUPP);
2575
2576 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2577 return ERR_PTR(-EINVAL);
2578
2579 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2580 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2581 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2582 return ERR_PTR(-EINVAL);
2583
2584 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2585 return ERR_PTR(-EINVAL);
2586
2587 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2588 if (n_entries < 0)
2589 return ERR_PTR(n_entries);
2590
2591 if (n_entries > wiphy->max_acl_mac_addrs)
2592 return ERR_PTR(-ENOTSUPP);
2593
2594 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2595 GFP_KERNEL);
2596 if (!acl)
2597 return ERR_PTR(-ENOMEM);
2598
2599 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2600 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2601 i++;
2602 }
2603
2604 acl->n_acl_entries = n_entries;
2605 acl->acl_policy = acl_policy;
2606
2607 return acl;
2608}
2609
2610static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2611{
2612 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2613 struct net_device *dev = info->user_ptr[1];
2614 struct cfg80211_acl_data *acl;
2615 int err;
2616
2617 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2618 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2619 return -EOPNOTSUPP;
2620
2621 if (!dev->ieee80211_ptr->beacon_interval)
2622 return -EINVAL;
2623
2624 acl = parse_acl_data(&rdev->wiphy, info);
2625 if (IS_ERR(acl))
2626 return PTR_ERR(acl);
2627
2628 err = rdev_set_mac_acl(rdev, dev, acl);
2629
2630 kfree(acl);
2631
2632 return err;
2633}
2634
Johannes Berg88600202012-02-13 15:17:18 +01002635static int nl80211_parse_beacon(struct genl_info *info,
2636 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002637{
Johannes Berg88600202012-02-13 15:17:18 +01002638 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002639
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002640 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2641 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2642 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2643 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002644 return -EINVAL;
2645
Johannes Berg88600202012-02-13 15:17:18 +01002646 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002647
Johannes Berged1b6cc2007-12-19 02:03:32 +01002648 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002649 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2650 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2651 if (!bcn->head_len)
2652 return -EINVAL;
2653 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002654 }
2655
2656 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002657 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2658 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002659 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002660 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002661 }
2662
Johannes Berg4c476992010-10-04 21:36:35 +02002663 if (!haveinfo)
2664 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002665
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002666 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002667 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2668 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002669 }
2670
2671 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002672 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002673 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002674 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002675 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2676 }
2677
2678 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002679 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002680 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002681 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002682 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2683 }
2684
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002685 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002686 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002687 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002688 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002689 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2690 }
2691
Johannes Berg88600202012-02-13 15:17:18 +01002692 return 0;
2693}
2694
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002695static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2696 struct cfg80211_ap_settings *params)
2697{
2698 struct wireless_dev *wdev;
2699 bool ret = false;
2700
2701 mutex_lock(&rdev->devlist_mtx);
2702
Johannes Berg89a54e42012-06-15 14:33:17 +02002703 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002704 if (wdev->iftype != NL80211_IFTYPE_AP &&
2705 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2706 continue;
2707
Johannes Berg683b6d32012-11-08 21:25:48 +01002708 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002709 continue;
2710
Johannes Berg683b6d32012-11-08 21:25:48 +01002711 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002712 ret = true;
2713 break;
2714 }
2715
2716 mutex_unlock(&rdev->devlist_mtx);
2717
2718 return ret;
2719}
2720
Jouni Malinene39e5b52012-09-30 19:29:39 +03002721static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2722 enum nl80211_auth_type auth_type,
2723 enum nl80211_commands cmd)
2724{
2725 if (auth_type > NL80211_AUTHTYPE_MAX)
2726 return false;
2727
2728 switch (cmd) {
2729 case NL80211_CMD_AUTHENTICATE:
2730 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2731 auth_type == NL80211_AUTHTYPE_SAE)
2732 return false;
2733 return true;
2734 case NL80211_CMD_CONNECT:
2735 case NL80211_CMD_START_AP:
2736 /* SAE not supported yet */
2737 if (auth_type == NL80211_AUTHTYPE_SAE)
2738 return false;
2739 return true;
2740 default:
2741 return false;
2742 }
2743}
2744
Johannes Berg88600202012-02-13 15:17:18 +01002745static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2746{
2747 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2748 struct net_device *dev = info->user_ptr[1];
2749 struct wireless_dev *wdev = dev->ieee80211_ptr;
2750 struct cfg80211_ap_settings params;
2751 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002752 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002753
2754 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2755 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2756 return -EOPNOTSUPP;
2757
2758 if (!rdev->ops->start_ap)
2759 return -EOPNOTSUPP;
2760
2761 if (wdev->beacon_interval)
2762 return -EALREADY;
2763
2764 memset(&params, 0, sizeof(params));
2765
2766 /* these are required for START_AP */
2767 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2768 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2769 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2770 return -EINVAL;
2771
2772 err = nl80211_parse_beacon(info, &params.beacon);
2773 if (err)
2774 return err;
2775
2776 params.beacon_interval =
2777 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2778 params.dtim_period =
2779 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2780
2781 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2782 if (err)
2783 return err;
2784
2785 /*
2786 * In theory, some of these attributes should be required here
2787 * but since they were not used when the command was originally
2788 * added, keep them optional for old user space programs to let
2789 * them continue to work with drivers that do not need the
2790 * additional information -- drivers must check!
2791 */
2792 if (info->attrs[NL80211_ATTR_SSID]) {
2793 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2794 params.ssid_len =
2795 nla_len(info->attrs[NL80211_ATTR_SSID]);
2796 if (params.ssid_len == 0 ||
2797 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2798 return -EINVAL;
2799 }
2800
2801 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2802 params.hidden_ssid = nla_get_u32(
2803 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2804 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2805 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2806 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2807 return -EINVAL;
2808 }
2809
2810 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2811
2812 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2813 params.auth_type = nla_get_u32(
2814 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002815 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2816 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002817 return -EINVAL;
2818 } else
2819 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2820
2821 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2822 NL80211_MAX_NR_CIPHER_SUITES);
2823 if (err)
2824 return err;
2825
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302826 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2827 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2828 return -EOPNOTSUPP;
2829 params.inactivity_timeout = nla_get_u16(
2830 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2831 }
2832
Johannes Berg53cabad2012-11-14 15:17:28 +01002833 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2834 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2835 return -EINVAL;
2836 params.p2p_ctwindow =
2837 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2838 if (params.p2p_ctwindow > 127)
2839 return -EINVAL;
2840 if (params.p2p_ctwindow != 0 &&
2841 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2842 return -EINVAL;
2843 }
2844
2845 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2846 u8 tmp;
2847
2848 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2849 return -EINVAL;
2850 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2851 if (tmp > 1)
2852 return -EINVAL;
2853 params.p2p_opp_ps = tmp;
2854 if (params.p2p_opp_ps != 0 &&
2855 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2856 return -EINVAL;
2857 }
2858
Johannes Bergaa430da2012-05-16 23:50:18 +02002859 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002860 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2861 if (err)
2862 return err;
2863 } else if (wdev->preset_chandef.chan) {
2864 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002865 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002866 return -EINVAL;
2867
Johannes Berg683b6d32012-11-08 21:25:48 +01002868 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002869 return -EINVAL;
2870
Simon Wunderlich04f39042013-02-08 18:16:19 +01002871 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
2872 if (err < 0)
2873 return err;
2874 if (err) {
2875 radar_detect_width = BIT(params.chandef.width);
2876 params.radar_required = true;
2877 }
2878
Michal Kaziore4e32452012-06-29 12:47:08 +02002879 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01002880 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
2881 params.chandef.chan,
2882 CHAN_MODE_SHARED,
2883 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02002884 mutex_unlock(&rdev->devlist_mtx);
2885
2886 if (err)
2887 return err;
2888
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302889 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
2890 params.acl = parse_acl_data(&rdev->wiphy, info);
2891 if (IS_ERR(params.acl))
2892 return PTR_ERR(params.acl);
2893 }
2894
Hila Gonene35e4d22012-06-27 17:19:42 +03002895 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002896 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002897 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002898 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002899 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002900 wdev->ssid_len = params.ssid_len;
2901 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002902 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302903
2904 kfree(params.acl);
2905
Johannes Berg56d18932011-05-09 18:41:15 +02002906 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002907}
2908
Johannes Berg88600202012-02-13 15:17:18 +01002909static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2910{
2911 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2912 struct net_device *dev = info->user_ptr[1];
2913 struct wireless_dev *wdev = dev->ieee80211_ptr;
2914 struct cfg80211_beacon_data params;
2915 int err;
2916
2917 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2918 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2919 return -EOPNOTSUPP;
2920
2921 if (!rdev->ops->change_beacon)
2922 return -EOPNOTSUPP;
2923
2924 if (!wdev->beacon_interval)
2925 return -EINVAL;
2926
2927 err = nl80211_parse_beacon(info, &params);
2928 if (err)
2929 return err;
2930
Hila Gonene35e4d22012-06-27 17:19:42 +03002931 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002932}
2933
2934static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002935{
Johannes Berg4c476992010-10-04 21:36:35 +02002936 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2937 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002938
Michal Kazior60771782012-06-29 12:46:56 +02002939 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002940}
2941
Johannes Berg5727ef12007-12-19 02:03:34 +01002942static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2943 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2944 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2945 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002946 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002947 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002948 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002949};
2950
Johannes Bergeccb8e82009-05-11 21:57:56 +03002951static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002952 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002953 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002954{
2955 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002956 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002957 int flag;
2958
Johannes Bergeccb8e82009-05-11 21:57:56 +03002959 /*
2960 * Try parsing the new attribute first so userspace
2961 * can specify both for older kernels.
2962 */
2963 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2964 if (nla) {
2965 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002966
Johannes Bergeccb8e82009-05-11 21:57:56 +03002967 sta_flags = nla_data(nla);
2968 params->sta_flags_mask = sta_flags->mask;
2969 params->sta_flags_set = sta_flags->set;
2970 if ((params->sta_flags_mask |
2971 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2972 return -EINVAL;
2973 return 0;
2974 }
2975
2976 /* if present, parse the old attribute */
2977
2978 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002979 if (!nla)
2980 return 0;
2981
2982 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2983 nla, sta_flags_policy))
2984 return -EINVAL;
2985
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002986 /*
2987 * Only allow certain flags for interface types so that
2988 * other attributes are silently ignored. Remember that
2989 * this is backward compatibility code with old userspace
2990 * and shouldn't be hit in other cases anyway.
2991 */
2992 switch (iftype) {
2993 case NL80211_IFTYPE_AP:
2994 case NL80211_IFTYPE_AP_VLAN:
2995 case NL80211_IFTYPE_P2P_GO:
2996 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2997 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2998 BIT(NL80211_STA_FLAG_WME) |
2999 BIT(NL80211_STA_FLAG_MFP);
3000 break;
3001 case NL80211_IFTYPE_P2P_CLIENT:
3002 case NL80211_IFTYPE_STATION:
3003 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3004 BIT(NL80211_STA_FLAG_TDLS_PEER);
3005 break;
3006 case NL80211_IFTYPE_MESH_POINT:
3007 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3008 BIT(NL80211_STA_FLAG_MFP) |
3009 BIT(NL80211_STA_FLAG_AUTHORIZED);
3010 default:
3011 return -EINVAL;
3012 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003013
Johannes Berg3383b5a2012-05-10 20:14:43 +02003014 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3015 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003016 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003017
Johannes Berg3383b5a2012-05-10 20:14:43 +02003018 /* no longer support new API additions in old API */
3019 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3020 return -EINVAL;
3021 }
3022 }
3023
Johannes Berg5727ef12007-12-19 02:03:34 +01003024 return 0;
3025}
3026
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003027static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3028 int attr)
3029{
3030 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003031 u32 bitrate;
3032 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003033
3034 rate = nla_nest_start(msg, attr);
3035 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003036 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003037
3038 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3039 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003040 /* report 16-bit bitrate only if we can */
3041 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003042 if (bitrate > 0 &&
3043 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3044 return false;
3045 if (bitrate_compat > 0 &&
3046 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3047 return false;
3048
3049 if (info->flags & RATE_INFO_FLAGS_MCS) {
3050 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3051 return false;
3052 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3053 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3054 return false;
3055 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3056 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3057 return false;
3058 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3059 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3060 return false;
3061 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3062 return false;
3063 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3064 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3065 return false;
3066 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3067 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3068 return false;
3069 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3070 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3071 return false;
3072 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3073 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3074 return false;
3075 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3076 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3077 return false;
3078 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003079
3080 nla_nest_end(msg, rate);
3081 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003082}
3083
Eric W. Biederman15e47302012-09-07 20:12:54 +00003084static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003085 int flags,
3086 struct cfg80211_registered_device *rdev,
3087 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003088 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003089{
3090 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003091 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003092
Eric W. Biederman15e47302012-09-07 20:12:54 +00003093 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003094 if (!hdr)
3095 return -1;
3096
David S. Miller9360ffd2012-03-29 04:41:26 -04003097 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3098 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3099 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3100 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003101
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003102 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3103 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003104 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003105 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3106 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3107 sinfo->connected_time))
3108 goto nla_put_failure;
3109 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3110 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3111 sinfo->inactive_time))
3112 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003113 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3114 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003115 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003116 (u32)sinfo->rx_bytes))
3117 goto nla_put_failure;
3118 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3119 NL80211_STA_INFO_TX_BYTES64)) &&
3120 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3121 (u32)sinfo->tx_bytes))
3122 goto nla_put_failure;
3123 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3124 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003125 sinfo->rx_bytes))
3126 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003127 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3128 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003129 sinfo->tx_bytes))
3130 goto nla_put_failure;
3131 if ((sinfo->filled & STATION_INFO_LLID) &&
3132 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3133 goto nla_put_failure;
3134 if ((sinfo->filled & STATION_INFO_PLID) &&
3135 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3136 goto nla_put_failure;
3137 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3138 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3139 sinfo->plink_state))
3140 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003141 switch (rdev->wiphy.signal_type) {
3142 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003143 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3144 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3145 sinfo->signal))
3146 goto nla_put_failure;
3147 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3148 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3149 sinfo->signal_avg))
3150 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003151 break;
3152 default:
3153 break;
3154 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003155 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003156 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3157 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003158 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003159 }
3160 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3161 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3162 NL80211_STA_INFO_RX_BITRATE))
3163 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003164 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003165 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3166 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3167 sinfo->rx_packets))
3168 goto nla_put_failure;
3169 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3170 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3171 sinfo->tx_packets))
3172 goto nla_put_failure;
3173 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3174 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3175 sinfo->tx_retries))
3176 goto nla_put_failure;
3177 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3178 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3179 sinfo->tx_failed))
3180 goto nla_put_failure;
3181 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3182 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3183 sinfo->beacon_loss_count))
3184 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003185 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3186 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3187 sinfo->local_pm))
3188 goto nla_put_failure;
3189 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3190 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3191 sinfo->peer_pm))
3192 goto nla_put_failure;
3193 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3194 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3195 sinfo->nonpeer_pm))
3196 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003197 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3198 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3199 if (!bss_param)
3200 goto nla_put_failure;
3201
David S. Miller9360ffd2012-03-29 04:41:26 -04003202 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3203 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3204 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3205 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3206 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3207 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3208 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3209 sinfo->bss_param.dtim_period) ||
3210 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3211 sinfo->bss_param.beacon_interval))
3212 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003213
3214 nla_nest_end(msg, bss_param);
3215 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003216 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3217 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3218 sizeof(struct nl80211_sta_flag_update),
3219 &sinfo->sta_flags))
3220 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003221 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3222 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3223 sinfo->t_offset))
3224 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003225 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003226
David S. Miller9360ffd2012-03-29 04:41:26 -04003227 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3228 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3229 sinfo->assoc_req_ies))
3230 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003231
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003232 return genlmsg_end(msg, hdr);
3233
3234 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003235 genlmsg_cancel(msg, hdr);
3236 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003237}
3238
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003239static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003240 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003241{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003242 struct station_info sinfo;
3243 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003244 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003245 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003246 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003247 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003248
Johannes Berg67748892010-10-04 21:14:06 +02003249 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3250 if (err)
3251 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003252
3253 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003254 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003255 goto out_err;
3256 }
3257
Johannes Bergbba95fe2008-07-29 13:22:51 +02003258 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003259 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003260 err = rdev_dump_station(dev, netdev, sta_idx,
3261 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003262 if (err == -ENOENT)
3263 break;
3264 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003265 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003266
3267 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003268 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003269 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003270 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003271 &sinfo) < 0)
3272 goto out;
3273
3274 sta_idx++;
3275 }
3276
3277
3278 out:
3279 cb->args[1] = sta_idx;
3280 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003281 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003282 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003283
3284 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003285}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003286
Johannes Berg5727ef12007-12-19 02:03:34 +01003287static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3288{
Johannes Berg4c476992010-10-04 21:36:35 +02003289 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3290 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003291 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003292 struct sk_buff *msg;
3293 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003294 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003295
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003296 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003297
3298 if (!info->attrs[NL80211_ATTR_MAC])
3299 return -EINVAL;
3300
3301 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3302
Johannes Berg4c476992010-10-04 21:36:35 +02003303 if (!rdev->ops->get_station)
3304 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003305
Hila Gonene35e4d22012-06-27 17:19:42 +03003306 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003307 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003308 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003309
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003311 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003312 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003313
Eric W. Biederman15e47302012-09-07 20:12:54 +00003314 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003315 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003316 nlmsg_free(msg);
3317 return -ENOBUFS;
3318 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003319
Johannes Berg4c476992010-10-04 21:36:35 +02003320 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003321}
3322
3323/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003324 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003325 */
Johannes Berg80b99892011-11-18 16:23:01 +01003326static struct net_device *get_vlan(struct genl_info *info,
3327 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003328{
Johannes Berg463d0182009-07-14 00:33:35 +02003329 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003330 struct net_device *v;
3331 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003332
Johannes Berg80b99892011-11-18 16:23:01 +01003333 if (!vlanattr)
3334 return NULL;
3335
3336 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3337 if (!v)
3338 return ERR_PTR(-ENODEV);
3339
3340 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3341 ret = -EINVAL;
3342 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003343 }
Johannes Berg80b99892011-11-18 16:23:01 +01003344
3345 if (!netif_running(v)) {
3346 ret = -ENETDOWN;
3347 goto error;
3348 }
3349
3350 return v;
3351 error:
3352 dev_put(v);
3353 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003354}
3355
Jouni Malinendf881292013-02-14 21:10:54 +02003356static struct nla_policy
3357nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3358 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3359 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3360};
3361
3362static int nl80211_set_station_tdls(struct genl_info *info,
3363 struct station_parameters *params)
3364{
Jouni Malinendf881292013-02-14 21:10:54 +02003365 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3366 struct nlattr *nla;
3367 int err;
3368
Jouni Malinendf881292013-02-14 21:10:54 +02003369 /* Dummy STA entry gets updated once the peer capabilities are known */
3370 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3371 params->ht_capa =
3372 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3373 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3374 params->vht_capa =
3375 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3376
3377 /* parse WME attributes if present */
3378 if (!info->attrs[NL80211_ATTR_STA_WME])
3379 return 0;
3380
3381 nla = info->attrs[NL80211_ATTR_STA_WME];
3382 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3383 nl80211_sta_wme_policy);
3384 if (err)
3385 return err;
3386
3387 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3388 params->uapsd_queues = nla_get_u8(
3389 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3390 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3391 return -EINVAL;
3392
3393 if (tb[NL80211_STA_WME_MAX_SP])
3394 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3395
3396 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3397 return -EINVAL;
3398
3399 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3400
3401 return 0;
3402}
3403
Johannes Berg5727ef12007-12-19 02:03:34 +01003404static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3405{
Johannes Berg4c476992010-10-04 21:36:35 +02003406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003407 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003408 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003409 struct station_parameters params;
3410 u8 *mac_addr = NULL;
3411
3412 memset(&params, 0, sizeof(params));
3413
3414 params.listen_interval = -1;
3415
3416 if (info->attrs[NL80211_ATTR_STA_AID])
3417 return -EINVAL;
3418
3419 if (!info->attrs[NL80211_ATTR_MAC])
3420 return -EINVAL;
3421
3422 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3423
3424 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3425 params.supported_rates =
3426 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3427 params.supported_rates_len =
3428 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3429 }
3430
Jouni Malinen9d62a982013-02-14 21:10:13 +02003431 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3432 params.capability =
3433 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3434 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3435 }
3436
3437 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3438 params.ext_capab =
3439 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3440 params.ext_capab_len =
3441 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3442 }
3443
Jouni Malinendf881292013-02-14 21:10:54 +02003444 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003445 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003446
Johannes Bergbdd90d52011-12-14 12:20:27 +01003447 if (!rdev->ops->change_station)
3448 return -EOPNOTSUPP;
3449
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003450 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003451 return -EINVAL;
3452
Johannes Bergf8bacc22013-02-14 23:27:01 +01003453 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003454 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003455 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3456 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3457 return -EINVAL;
3458 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003459
Johannes Bergf8bacc22013-02-14 23:27:01 +01003460 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003461 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003462 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3463 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3464 return -EINVAL;
3465 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3466 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003467
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003468 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3469 enum nl80211_mesh_power_mode pm = nla_get_u32(
3470 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3471
3472 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3473 pm > NL80211_MESH_POWER_MAX)
3474 return -EINVAL;
3475
3476 params.local_pm = pm;
3477 }
3478
Johannes Berga97f4422009-06-18 17:23:43 +02003479 switch (dev->ieee80211_ptr->iftype) {
3480 case NL80211_IFTYPE_AP:
3481 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003482 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003483 /* disallow mesh-specific things */
3484 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003485 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003486 if (params.local_pm)
3487 return -EINVAL;
Johannes Bergf8bacc22013-02-14 23:27:01 +01003488 if (params.sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3489 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003490
3491 /* TDLS can't be set, ... */
3492 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3493 return -EINVAL;
3494 /*
3495 * ... but don't bother the driver with it. This works around
3496 * a hostapd/wpa_supplicant issue -- it always includes the
3497 * TLDS_PEER flag in the mask even for AP mode.
3498 */
3499 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3500
3501 /* accept only the listed bits */
3502 if (params.sta_flags_mask &
3503 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003504 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3505 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003506 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3507 BIT(NL80211_STA_FLAG_WME) |
3508 BIT(NL80211_STA_FLAG_MFP)))
3509 return -EINVAL;
3510
Johannes Bergd582cff2012-10-26 17:53:44 +02003511 /* but authenticated/associated only if driver handles it */
3512 if (!(rdev->wiphy.features &
3513 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3514 params.sta_flags_mask &
3515 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3516 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3517 return -EINVAL;
3518
Johannes Bergba23d202012-12-27 17:32:09 +01003519 /* reject other things that can't change */
3520 if (params.supported_rates)
3521 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003522 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3523 return -EINVAL;
3524 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3525 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003526 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3527 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3528 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003529
Johannes Bergbdd90d52011-12-14 12:20:27 +01003530 /* must be last in here for error handling */
3531 params.vlan = get_vlan(info, rdev);
3532 if (IS_ERR(params.vlan))
3533 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003534 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003535 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003536 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003537 /*
3538 * Don't allow userspace to change the TDLS_PEER flag,
3539 * but silently ignore attempts to change it since we
3540 * don't have state here to verify that it doesn't try
3541 * to change the flag.
3542 */
3543 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Jouni Malinendf881292013-02-14 21:10:54 +02003544 /* Include parameters for TDLS peer (driver will check) */
3545 err = nl80211_set_station_tdls(info, &params);
3546 if (err)
3547 return err;
3548 /* disallow things sta doesn't support */
3549 if (params.plink_action)
3550 return -EINVAL;
3551 if (params.local_pm)
3552 return -EINVAL;
Johannes Bergf8bacc22013-02-14 23:27:01 +01003553 if (params.sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3554 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003555 /* reject any changes other than AUTHORIZED or WME (for TDLS) */
3556 if (params.sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3557 BIT(NL80211_STA_FLAG_WME)))
3558 return -EINVAL;
3559 break;
Antonio Quartulli267335d2012-01-31 20:25:47 +01003560 case NL80211_IFTYPE_ADHOC:
3561 /* disallow things sta doesn't support */
3562 if (params.plink_action)
3563 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003564 if (params.local_pm)
3565 return -EINVAL;
Johannes Bergf8bacc22013-02-14 23:27:01 +01003566 if (params.sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3567 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003568 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3569 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3570 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003571 /* reject any changes other than AUTHORIZED */
3572 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3573 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003574 break;
3575 case NL80211_IFTYPE_MESH_POINT:
3576 /* disallow things mesh doesn't support */
3577 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003578 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003579 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003580 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003581 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3582 return -EINVAL;
3583 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3584 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003585 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3586 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3587 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003588 /*
3589 * No special handling for TDLS here -- the userspace
3590 * mesh code doesn't have this bug.
3591 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003592 if (params.sta_flags_mask &
3593 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003594 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003595 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003596 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003597 break;
3598 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003599 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003600 }
3601
Johannes Bergbdd90d52011-12-14 12:20:27 +01003602 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003603
Hila Gonene35e4d22012-06-27 17:19:42 +03003604 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003605
Johannes Berg5727ef12007-12-19 02:03:34 +01003606 if (params.vlan)
3607 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003608
Johannes Berg5727ef12007-12-19 02:03:34 +01003609 return err;
3610}
3611
3612static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3613{
Johannes Berg4c476992010-10-04 21:36:35 +02003614 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003615 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003616 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003617 struct station_parameters params;
3618 u8 *mac_addr = NULL;
3619
3620 memset(&params, 0, sizeof(params));
3621
3622 if (!info->attrs[NL80211_ATTR_MAC])
3623 return -EINVAL;
3624
Johannes Berg5727ef12007-12-19 02:03:34 +01003625 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3626 return -EINVAL;
3627
3628 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3629 return -EINVAL;
3630
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003631 if (!info->attrs[NL80211_ATTR_STA_AID])
3632 return -EINVAL;
3633
Johannes Berg5727ef12007-12-19 02:03:34 +01003634 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3635 params.supported_rates =
3636 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3637 params.supported_rates_len =
3638 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3639 params.listen_interval =
3640 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003641
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003642 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3643 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3644 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003645
Jouni Malinen9d62a982013-02-14 21:10:13 +02003646 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3647 params.capability =
3648 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3649 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3650 }
3651
3652 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3653 params.ext_capab =
3654 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3655 params.ext_capab_len =
3656 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3657 }
3658
Jouni Malinen36aedc92008-08-25 11:58:58 +03003659 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3660 params.ht_capa =
3661 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003662
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003663 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3664 params.vht_capa =
3665 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3666
Johannes Bergf8bacc22013-02-14 23:27:01 +01003667 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07003668 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003669 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3670 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3671 return -EINVAL;
3672 }
Javier Cardona96b78df2011-04-07 15:08:33 -07003673
Johannes Bergbdd90d52011-12-14 12:20:27 +01003674 if (!rdev->ops->add_station)
3675 return -EOPNOTSUPP;
3676
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003677 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003678 return -EINVAL;
3679
Johannes Bergbdd90d52011-12-14 12:20:27 +01003680 switch (dev->ieee80211_ptr->iftype) {
3681 case NL80211_IFTYPE_AP:
3682 case NL80211_IFTYPE_AP_VLAN:
3683 case NL80211_IFTYPE_P2P_GO:
3684 /* parse WME attributes if sta is WME capable */
3685 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3686 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3687 info->attrs[NL80211_ATTR_STA_WME]) {
3688 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3689 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003690
Johannes Bergbdd90d52011-12-14 12:20:27 +01003691 nla = info->attrs[NL80211_ATTR_STA_WME];
3692 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3693 nl80211_sta_wme_policy);
3694 if (err)
3695 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003696
Johannes Bergbdd90d52011-12-14 12:20:27 +01003697 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3698 params.uapsd_queues =
3699 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3700 if (params.uapsd_queues &
3701 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3702 return -EINVAL;
3703
3704 if (tb[NL80211_STA_WME_MAX_SP])
3705 params.max_sp =
3706 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3707
3708 if (params.max_sp &
3709 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3710 return -EINVAL;
3711
3712 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3713 }
3714 /* TDLS peers cannot be added */
3715 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003716 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003717 /* but don't bother the driver with it */
3718 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003719
Johannes Bergd582cff2012-10-26 17:53:44 +02003720 /* allow authenticated/associated only if driver handles it */
3721 if (!(rdev->wiphy.features &
3722 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3723 params.sta_flags_mask &
3724 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3725 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3726 return -EINVAL;
3727
Johannes Bergbdd90d52011-12-14 12:20:27 +01003728 /* must be last in here for error handling */
3729 params.vlan = get_vlan(info, rdev);
3730 if (IS_ERR(params.vlan))
3731 return PTR_ERR(params.vlan);
3732 break;
3733 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergd582cff2012-10-26 17:53:44 +02003734 /* associated is disallowed */
3735 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3736 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003737 /* TDLS peers cannot be added */
3738 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003739 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003740 break;
3741 case NL80211_IFTYPE_STATION:
Johannes Bergd582cff2012-10-26 17:53:44 +02003742 /* associated is disallowed */
3743 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3744 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003745 /* Only TDLS peers can be added */
3746 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3747 return -EINVAL;
3748 /* Can only add if TDLS ... */
3749 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3750 return -EOPNOTSUPP;
3751 /* ... with external setup is supported */
3752 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3753 return -EOPNOTSUPP;
3754 break;
3755 default:
3756 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003757 }
3758
Johannes Bergbdd90d52011-12-14 12:20:27 +01003759 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003760
Hila Gonene35e4d22012-06-27 17:19:42 +03003761 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003762
Johannes Berg5727ef12007-12-19 02:03:34 +01003763 if (params.vlan)
3764 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003765 return err;
3766}
3767
3768static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3769{
Johannes Berg4c476992010-10-04 21:36:35 +02003770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3771 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003772 u8 *mac_addr = NULL;
3773
3774 if (info->attrs[NL80211_ATTR_MAC])
3775 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3776
Johannes Berge80cf852009-05-11 14:43:13 +02003777 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003778 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003779 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003780 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3781 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003782
Johannes Berg4c476992010-10-04 21:36:35 +02003783 if (!rdev->ops->del_station)
3784 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003785
Hila Gonene35e4d22012-06-27 17:19:42 +03003786 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003787}
3788
Eric W. Biederman15e47302012-09-07 20:12:54 +00003789static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003790 int flags, struct net_device *dev,
3791 u8 *dst, u8 *next_hop,
3792 struct mpath_info *pinfo)
3793{
3794 void *hdr;
3795 struct nlattr *pinfoattr;
3796
Eric W. Biederman15e47302012-09-07 20:12:54 +00003797 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003798 if (!hdr)
3799 return -1;
3800
David S. Miller9360ffd2012-03-29 04:41:26 -04003801 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3802 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3803 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3804 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3805 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003806
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003807 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3808 if (!pinfoattr)
3809 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003810 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3811 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3812 pinfo->frame_qlen))
3813 goto nla_put_failure;
3814 if (((pinfo->filled & MPATH_INFO_SN) &&
3815 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3816 ((pinfo->filled & MPATH_INFO_METRIC) &&
3817 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3818 pinfo->metric)) ||
3819 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3820 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3821 pinfo->exptime)) ||
3822 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3823 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3824 pinfo->flags)) ||
3825 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3826 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3827 pinfo->discovery_timeout)) ||
3828 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3829 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3830 pinfo->discovery_retries)))
3831 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003832
3833 nla_nest_end(msg, pinfoattr);
3834
3835 return genlmsg_end(msg, hdr);
3836
3837 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003838 genlmsg_cancel(msg, hdr);
3839 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003840}
3841
3842static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003843 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003844{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003845 struct mpath_info pinfo;
3846 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003847 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003848 u8 dst[ETH_ALEN];
3849 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003850 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003851 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003852
Johannes Berg67748892010-10-04 21:14:06 +02003853 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3854 if (err)
3855 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003856
3857 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003858 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003859 goto out_err;
3860 }
3861
Jouni Malineneec60b02009-03-20 21:21:19 +02003862 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3863 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003864 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003865 }
3866
Johannes Bergbba95fe2008-07-29 13:22:51 +02003867 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003868 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3869 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003870 if (err == -ENOENT)
3871 break;
3872 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003873 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003874
Eric W. Biederman15e47302012-09-07 20:12:54 +00003875 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003876 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3877 netdev, dst, next_hop,
3878 &pinfo) < 0)
3879 goto out;
3880
3881 path_idx++;
3882 }
3883
3884
3885 out:
3886 cb->args[1] = path_idx;
3887 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003888 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003889 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003890 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003891}
3892
3893static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3894{
Johannes Berg4c476992010-10-04 21:36:35 +02003895 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003896 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003897 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003898 struct mpath_info pinfo;
3899 struct sk_buff *msg;
3900 u8 *dst = NULL;
3901 u8 next_hop[ETH_ALEN];
3902
3903 memset(&pinfo, 0, sizeof(pinfo));
3904
3905 if (!info->attrs[NL80211_ATTR_MAC])
3906 return -EINVAL;
3907
3908 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3909
Johannes Berg4c476992010-10-04 21:36:35 +02003910 if (!rdev->ops->get_mpath)
3911 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003912
Johannes Berg4c476992010-10-04 21:36:35 +02003913 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3914 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003915
Hila Gonene35e4d22012-06-27 17:19:42 +03003916 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003917 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003918 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003919
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003920 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003921 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003922 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003923
Eric W. Biederman15e47302012-09-07 20:12:54 +00003924 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003925 dev, dst, next_hop, &pinfo) < 0) {
3926 nlmsg_free(msg);
3927 return -ENOBUFS;
3928 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003929
Johannes Berg4c476992010-10-04 21:36:35 +02003930 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003931}
3932
3933static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3934{
Johannes Berg4c476992010-10-04 21:36:35 +02003935 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3936 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003937 u8 *dst = NULL;
3938 u8 *next_hop = NULL;
3939
3940 if (!info->attrs[NL80211_ATTR_MAC])
3941 return -EINVAL;
3942
3943 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3944 return -EINVAL;
3945
3946 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3947 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3948
Johannes Berg4c476992010-10-04 21:36:35 +02003949 if (!rdev->ops->change_mpath)
3950 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003951
Johannes Berg4c476992010-10-04 21:36:35 +02003952 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3953 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003954
Hila Gonene35e4d22012-06-27 17:19:42 +03003955 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003956}
Johannes Berg4c476992010-10-04 21:36:35 +02003957
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003958static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3959{
Johannes Berg4c476992010-10-04 21:36:35 +02003960 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3961 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003962 u8 *dst = NULL;
3963 u8 *next_hop = NULL;
3964
3965 if (!info->attrs[NL80211_ATTR_MAC])
3966 return -EINVAL;
3967
3968 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3969 return -EINVAL;
3970
3971 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3972 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3973
Johannes Berg4c476992010-10-04 21:36:35 +02003974 if (!rdev->ops->add_mpath)
3975 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003976
Johannes Berg4c476992010-10-04 21:36:35 +02003977 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3978 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003979
Hila Gonene35e4d22012-06-27 17:19:42 +03003980 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003981}
3982
3983static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3984{
Johannes Berg4c476992010-10-04 21:36:35 +02003985 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3986 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003987 u8 *dst = NULL;
3988
3989 if (info->attrs[NL80211_ATTR_MAC])
3990 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3991
Johannes Berg4c476992010-10-04 21:36:35 +02003992 if (!rdev->ops->del_mpath)
3993 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003994
Hila Gonene35e4d22012-06-27 17:19:42 +03003995 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003996}
3997
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003998static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3999{
Johannes Berg4c476992010-10-04 21:36:35 +02004000 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4001 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004002 struct bss_parameters params;
4003
4004 memset(&params, 0, sizeof(params));
4005 /* default to not changing parameters */
4006 params.use_cts_prot = -1;
4007 params.use_short_preamble = -1;
4008 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004009 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004010 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004011 params.p2p_ctwindow = -1;
4012 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004013
4014 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4015 params.use_cts_prot =
4016 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4017 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4018 params.use_short_preamble =
4019 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4020 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4021 params.use_short_slot_time =
4022 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004023 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4024 params.basic_rates =
4025 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4026 params.basic_rates_len =
4027 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4028 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004029 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4030 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004031 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4032 params.ht_opmode =
4033 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004034
Johannes Berg53cabad2012-11-14 15:17:28 +01004035 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4036 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4037 return -EINVAL;
4038 params.p2p_ctwindow =
4039 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4040 if (params.p2p_ctwindow < 0)
4041 return -EINVAL;
4042 if (params.p2p_ctwindow != 0 &&
4043 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4044 return -EINVAL;
4045 }
4046
4047 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4048 u8 tmp;
4049
4050 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4051 return -EINVAL;
4052 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4053 if (tmp > 1)
4054 return -EINVAL;
4055 params.p2p_opp_ps = tmp;
4056 if (params.p2p_opp_ps &&
4057 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4058 return -EINVAL;
4059 }
4060
Johannes Berg4c476992010-10-04 21:36:35 +02004061 if (!rdev->ops->change_bss)
4062 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004063
Johannes Berg074ac8d2010-09-16 14:58:22 +02004064 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004065 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4066 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004067
Hila Gonene35e4d22012-06-27 17:19:42 +03004068 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004069}
4070
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004071static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004072 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4073 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4074 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4075 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4076 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4077 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4078};
4079
4080static int parse_reg_rule(struct nlattr *tb[],
4081 struct ieee80211_reg_rule *reg_rule)
4082{
4083 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4084 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4085
4086 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4087 return -EINVAL;
4088 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4089 return -EINVAL;
4090 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4091 return -EINVAL;
4092 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4093 return -EINVAL;
4094 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4095 return -EINVAL;
4096
4097 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4098
4099 freq_range->start_freq_khz =
4100 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4101 freq_range->end_freq_khz =
4102 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4103 freq_range->max_bandwidth_khz =
4104 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4105
4106 power_rule->max_eirp =
4107 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4108
4109 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4110 power_rule->max_antenna_gain =
4111 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4112
4113 return 0;
4114}
4115
4116static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4117{
4118 int r;
4119 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004120 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004121
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004122 /*
4123 * You should only get this when cfg80211 hasn't yet initialized
4124 * completely when built-in to the kernel right between the time
4125 * window between nl80211_init() and regulatory_init(), if that is
4126 * even possible.
4127 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004128 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004129 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004130
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004131 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4132 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004133
4134 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4135
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004136 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4137 user_reg_hint_type =
4138 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4139 else
4140 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4141
4142 switch (user_reg_hint_type) {
4143 case NL80211_USER_REG_HINT_USER:
4144 case NL80211_USER_REG_HINT_CELL_BASE:
4145 break;
4146 default:
4147 return -EINVAL;
4148 }
4149
4150 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004151
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004152 return r;
4153}
4154
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004155static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004156 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004157{
Johannes Berg4c476992010-10-04 21:36:35 +02004158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004159 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004160 struct wireless_dev *wdev = dev->ieee80211_ptr;
4161 struct mesh_config cur_params;
4162 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004163 void *hdr;
4164 struct nlattr *pinfoattr;
4165 struct sk_buff *msg;
4166
Johannes Berg29cbe682010-12-03 09:20:44 +01004167 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4168 return -EOPNOTSUPP;
4169
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004170 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004171 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004172
Johannes Berg29cbe682010-12-03 09:20:44 +01004173 wdev_lock(wdev);
4174 /* If not connected, get default parameters */
4175 if (!wdev->mesh_id_len)
4176 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4177 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004178 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004179 wdev_unlock(wdev);
4180
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004181 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004182 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004183
4184 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004185 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004186 if (!msg)
4187 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004188 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004189 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004190 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004191 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004192 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004193 if (!pinfoattr)
4194 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004195 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4196 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4197 cur_params.dot11MeshRetryTimeout) ||
4198 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4199 cur_params.dot11MeshConfirmTimeout) ||
4200 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4201 cur_params.dot11MeshHoldingTimeout) ||
4202 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4203 cur_params.dot11MeshMaxPeerLinks) ||
4204 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4205 cur_params.dot11MeshMaxRetries) ||
4206 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4207 cur_params.dot11MeshTTL) ||
4208 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4209 cur_params.element_ttl) ||
4210 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4211 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004212 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4213 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004214 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4215 cur_params.dot11MeshHWMPmaxPREQretries) ||
4216 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4217 cur_params.path_refresh_time) ||
4218 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4219 cur_params.min_discovery_timeout) ||
4220 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4221 cur_params.dot11MeshHWMPactivePathTimeout) ||
4222 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4223 cur_params.dot11MeshHWMPpreqMinInterval) ||
4224 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4225 cur_params.dot11MeshHWMPperrMinInterval) ||
4226 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4227 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4228 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4229 cur_params.dot11MeshHWMPRootMode) ||
4230 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4231 cur_params.dot11MeshHWMPRannInterval) ||
4232 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4233 cur_params.dot11MeshGateAnnouncementProtocol) ||
4234 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4235 cur_params.dot11MeshForwarding) ||
4236 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004237 cur_params.rssi_threshold) ||
4238 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004239 cur_params.ht_opmode) ||
4240 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4241 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4242 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004243 cur_params.dot11MeshHWMProotInterval) ||
4244 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004245 cur_params.dot11MeshHWMPconfirmationInterval) ||
4246 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4247 cur_params.power_mode) ||
4248 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4249 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004250 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004251 nla_nest_end(msg, pinfoattr);
4252 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004253 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004254
Johannes Berg3b858752009-03-12 09:55:09 +01004255 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004256 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004257 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004258 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004259 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004260}
4261
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004262static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004263 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4264 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4265 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4266 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4267 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4268 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004269 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004270 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004271 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004272 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4273 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4274 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4275 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4276 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004277 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004278 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004279 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004280 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004281 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004282 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004283 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4284 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004285 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4286 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004287 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004288 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4289 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004290};
4291
Javier Cardonac80d5452010-12-16 17:37:49 -08004292static const struct nla_policy
4293 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004294 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004295 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4296 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004297 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004298 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004299 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004300 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004301};
4302
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004303static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004304 struct mesh_config *cfg,
4305 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004306{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004307 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004308 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004309
Marco Porschea54fba2013-01-07 16:04:48 +01004310#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4311do { \
4312 if (tb[attr]) { \
4313 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4314 return -EINVAL; \
4315 cfg->param = fn(tb[attr]); \
4316 mask |= (1 << (attr - 1)); \
4317 } \
4318} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004319
4320
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004321 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004322 return -EINVAL;
4323 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004324 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004325 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004326 return -EINVAL;
4327
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004328 /* This makes sure that there aren't more than 32 mesh config
4329 * parameters (otherwise our bitfield scheme would not work.) */
4330 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4331
4332 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004333 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004334 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4335 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004336 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004337 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4338 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004339 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004340 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4341 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004342 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004343 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4344 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004345 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004346 mask, NL80211_MESHCONF_MAX_RETRIES,
4347 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004348 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004349 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004350 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004351 mask, NL80211_MESHCONF_ELEMENT_TTL,
4352 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004353 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004354 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4355 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004356 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4357 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004358 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4359 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004360 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004361 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4362 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004363 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004364 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4365 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004366 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004367 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4368 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004369 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4370 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004371 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4372 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004373 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004374 1, 65535, mask,
4375 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004376 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004377 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004378 1, 65535, mask,
4379 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004380 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004381 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004382 dot11MeshHWMPnetDiameterTraversalTime,
4383 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004384 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4385 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004386 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4387 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4388 nla_get_u8);
4389 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4390 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004391 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004392 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004393 dot11MeshGateAnnouncementProtocol, 0, 1,
4394 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004395 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004396 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004397 mask, NL80211_MESHCONF_FORWARDING,
4398 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004399 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004400 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4401 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004402 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004403 mask, NL80211_MESHCONF_HT_OPMODE,
4404 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004405 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004406 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004407 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4408 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004409 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004410 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4411 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004412 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004413 dot11MeshHWMPconfirmationInterval,
4414 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004415 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4416 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004417 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4418 NL80211_MESH_POWER_ACTIVE,
4419 NL80211_MESH_POWER_MAX,
4420 mask, NL80211_MESHCONF_POWER_MODE,
4421 nla_get_u32);
4422 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4423 0, 65535, mask,
4424 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004425 if (mask_out)
4426 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004427
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004428 return 0;
4429
4430#undef FILL_IN_MESH_PARAM_IF_SET
4431}
4432
Javier Cardonac80d5452010-12-16 17:37:49 -08004433static int nl80211_parse_mesh_setup(struct genl_info *info,
4434 struct mesh_setup *setup)
4435{
4436 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4437
4438 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4439 return -EINVAL;
4440 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4441 info->attrs[NL80211_ATTR_MESH_SETUP],
4442 nl80211_mesh_setup_params_policy))
4443 return -EINVAL;
4444
Javier Cardonad299a1f2012-03-31 11:31:33 -07004445 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4446 setup->sync_method =
4447 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4448 IEEE80211_SYNC_METHOD_VENDOR :
4449 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4450
Javier Cardonac80d5452010-12-16 17:37:49 -08004451 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4452 setup->path_sel_proto =
4453 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4454 IEEE80211_PATH_PROTOCOL_VENDOR :
4455 IEEE80211_PATH_PROTOCOL_HWMP;
4456
4457 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4458 setup->path_metric =
4459 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4460 IEEE80211_PATH_METRIC_VENDOR :
4461 IEEE80211_PATH_METRIC_AIRTIME;
4462
Javier Cardona581a8b02011-04-07 15:08:27 -07004463
4464 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004465 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004466 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004467 if (!is_valid_ie_attr(ieattr))
4468 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004469 setup->ie = nla_data(ieattr);
4470 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004471 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004472 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4473 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004474
4475 return 0;
4476}
4477
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004478static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004479 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004480{
4481 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4482 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004483 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004484 struct mesh_config cfg;
4485 u32 mask;
4486 int err;
4487
Johannes Berg29cbe682010-12-03 09:20:44 +01004488 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4489 return -EOPNOTSUPP;
4490
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004491 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004492 return -EOPNOTSUPP;
4493
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004494 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004495 if (err)
4496 return err;
4497
Johannes Berg29cbe682010-12-03 09:20:44 +01004498 wdev_lock(wdev);
4499 if (!wdev->mesh_id_len)
4500 err = -ENOLINK;
4501
4502 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004503 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004504
4505 wdev_unlock(wdev);
4506
4507 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004508}
4509
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004510static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4511{
Johannes Berg458f4f92012-12-06 15:47:38 +01004512 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004513 struct sk_buff *msg;
4514 void *hdr = NULL;
4515 struct nlattr *nl_reg_rules;
4516 unsigned int i;
4517 int err = -EINVAL;
4518
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004519 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004520
4521 if (!cfg80211_regdomain)
4522 goto out;
4523
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004524 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004525 if (!msg) {
4526 err = -ENOBUFS;
4527 goto out;
4528 }
4529
Eric W. Biederman15e47302012-09-07 20:12:54 +00004530 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004531 NL80211_CMD_GET_REG);
4532 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004533 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004534
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004535 if (reg_last_request_cell_base() &&
4536 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4537 NL80211_USER_REG_HINT_CELL_BASE))
4538 goto nla_put_failure;
4539
Johannes Berg458f4f92012-12-06 15:47:38 +01004540 rcu_read_lock();
4541 regdom = rcu_dereference(cfg80211_regdomain);
4542
4543 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4544 (regdom->dfs_region &&
4545 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4546 goto nla_put_failure_rcu;
4547
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004548 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4549 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004550 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004551
Johannes Berg458f4f92012-12-06 15:47:38 +01004552 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004553 struct nlattr *nl_reg_rule;
4554 const struct ieee80211_reg_rule *reg_rule;
4555 const struct ieee80211_freq_range *freq_range;
4556 const struct ieee80211_power_rule *power_rule;
4557
Johannes Berg458f4f92012-12-06 15:47:38 +01004558 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004559 freq_range = &reg_rule->freq_range;
4560 power_rule = &reg_rule->power_rule;
4561
4562 nl_reg_rule = nla_nest_start(msg, i);
4563 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004564 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004565
David S. Miller9360ffd2012-03-29 04:41:26 -04004566 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4567 reg_rule->flags) ||
4568 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4569 freq_range->start_freq_khz) ||
4570 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4571 freq_range->end_freq_khz) ||
4572 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4573 freq_range->max_bandwidth_khz) ||
4574 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4575 power_rule->max_antenna_gain) ||
4576 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4577 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004578 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004579
4580 nla_nest_end(msg, nl_reg_rule);
4581 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004582 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004583
4584 nla_nest_end(msg, nl_reg_rules);
4585
4586 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004587 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004588 goto out;
4589
Johannes Berg458f4f92012-12-06 15:47:38 +01004590nla_put_failure_rcu:
4591 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004592nla_put_failure:
4593 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004594put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004595 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004596 err = -EMSGSIZE;
4597out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004598 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004599 return err;
4600}
4601
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004602static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4603{
4604 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4605 struct nlattr *nl_reg_rule;
4606 char *alpha2 = NULL;
4607 int rem_reg_rules = 0, r = 0;
4608 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004609 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004610 struct ieee80211_regdomain *rd = NULL;
4611
4612 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4613 return -EINVAL;
4614
4615 if (!info->attrs[NL80211_ATTR_REG_RULES])
4616 return -EINVAL;
4617
4618 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4619
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004620 if (info->attrs[NL80211_ATTR_DFS_REGION])
4621 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4622
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004623 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004624 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004625 num_rules++;
4626 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004627 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004628 }
4629
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004630 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004631 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004632
4633 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004634 if (!rd)
4635 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004636
4637 rd->n_reg_rules = num_rules;
4638 rd->alpha2[0] = alpha2[0];
4639 rd->alpha2[1] = alpha2[1];
4640
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004641 /*
4642 * Disable DFS master mode if the DFS region was
4643 * not supported or known on this kernel.
4644 */
4645 if (reg_supported_dfs_region(dfs_region))
4646 rd->dfs_region = dfs_region;
4647
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004648 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004649 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004650 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004651 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4652 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004653 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4654 if (r)
4655 goto bad_reg;
4656
4657 rule_idx++;
4658
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004659 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4660 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004661 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004662 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004663 }
4664
Johannes Berg6913b492012-12-04 00:48:59 +01004665 mutex_lock(&cfg80211_mutex);
4666
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004667 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004668 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004669 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004670 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004671
Johannes Bergd2372b32008-10-24 20:32:20 +02004672 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004673 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004674 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004675}
4676
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004677static int validate_scan_freqs(struct nlattr *freqs)
4678{
4679 struct nlattr *attr1, *attr2;
4680 int n_channels = 0, tmp1, tmp2;
4681
4682 nla_for_each_nested(attr1, freqs, tmp1) {
4683 n_channels++;
4684 /*
4685 * Some hardware has a limited channel list for
4686 * scanning, and it is pretty much nonsensical
4687 * to scan for a channel twice, so disallow that
4688 * and don't require drivers to check that the
4689 * channel list they get isn't longer than what
4690 * they can scan, as long as they can scan all
4691 * the channels they registered at once.
4692 */
4693 nla_for_each_nested(attr2, freqs, tmp2)
4694 if (attr1 != attr2 &&
4695 nla_get_u32(attr1) == nla_get_u32(attr2))
4696 return 0;
4697 }
4698
4699 return n_channels;
4700}
4701
Johannes Berg2a519312009-02-10 21:25:55 +01004702static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4703{
Johannes Berg4c476992010-10-04 21:36:35 +02004704 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004705 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004706 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004707 struct nlattr *attr;
4708 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004709 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004710 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004711
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004712 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4713 return -EINVAL;
4714
Johannes Berg79c97e92009-07-07 03:56:12 +02004715 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004716
Johannes Berg4c476992010-10-04 21:36:35 +02004717 if (!rdev->ops->scan)
4718 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004719
Johannes Berg4c476992010-10-04 21:36:35 +02004720 if (rdev->scan_req)
4721 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004722
4723 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004724 n_channels = validate_scan_freqs(
4725 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004726 if (!n_channels)
4727 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004728 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004729 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004730 n_channels = 0;
4731
Johannes Berg2a519312009-02-10 21:25:55 +01004732 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4733 if (wiphy->bands[band])
4734 n_channels += wiphy->bands[band]->n_channels;
4735 }
4736
4737 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4738 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4739 n_ssids++;
4740
Johannes Berg4c476992010-10-04 21:36:35 +02004741 if (n_ssids > wiphy->max_scan_ssids)
4742 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004743
Jouni Malinen70692ad2009-02-16 19:39:13 +02004744 if (info->attrs[NL80211_ATTR_IE])
4745 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4746 else
4747 ie_len = 0;
4748
Johannes Berg4c476992010-10-04 21:36:35 +02004749 if (ie_len > wiphy->max_scan_ie_len)
4750 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004751
Johannes Berg2a519312009-02-10 21:25:55 +01004752 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004753 + sizeof(*request->ssids) * n_ssids
4754 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004755 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004756 if (!request)
4757 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004758
Johannes Berg2a519312009-02-10 21:25:55 +01004759 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004760 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004761 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004762 if (ie_len) {
4763 if (request->ssids)
4764 request->ie = (void *)(request->ssids + n_ssids);
4765 else
4766 request->ie = (void *)(request->channels + n_channels);
4767 }
Johannes Berg2a519312009-02-10 21:25:55 +01004768
Johannes Berg584991d2009-11-02 13:32:03 +01004769 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004770 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4771 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004772 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004773 struct ieee80211_channel *chan;
4774
4775 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4776
4777 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004778 err = -EINVAL;
4779 goto out_free;
4780 }
Johannes Berg584991d2009-11-02 13:32:03 +01004781
4782 /* ignore disabled channels */
4783 if (chan->flags & IEEE80211_CHAN_DISABLED)
4784 continue;
4785
4786 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004787 i++;
4788 }
4789 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004790 enum ieee80211_band band;
4791
Johannes Berg2a519312009-02-10 21:25:55 +01004792 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004793 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4794 int j;
4795 if (!wiphy->bands[band])
4796 continue;
4797 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004798 struct ieee80211_channel *chan;
4799
4800 chan = &wiphy->bands[band]->channels[j];
4801
4802 if (chan->flags & IEEE80211_CHAN_DISABLED)
4803 continue;
4804
4805 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004806 i++;
4807 }
4808 }
4809 }
4810
Johannes Berg584991d2009-11-02 13:32:03 +01004811 if (!i) {
4812 err = -EINVAL;
4813 goto out_free;
4814 }
4815
4816 request->n_channels = i;
4817
Johannes Berg2a519312009-02-10 21:25:55 +01004818 i = 0;
4819 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4820 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004821 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004822 err = -EINVAL;
4823 goto out_free;
4824 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004825 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004826 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004827 i++;
4828 }
4829 }
4830
Jouni Malinen70692ad2009-02-16 19:39:13 +02004831 if (info->attrs[NL80211_ATTR_IE]) {
4832 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004833 memcpy((void *)request->ie,
4834 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004835 request->ie_len);
4836 }
4837
Johannes Berg34850ab2011-07-18 18:08:35 +02004838 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004839 if (wiphy->bands[i])
4840 request->rates[i] =
4841 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004842
4843 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4844 nla_for_each_nested(attr,
4845 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4846 tmp) {
4847 enum ieee80211_band band = nla_type(attr);
4848
Dan Carpenter84404622011-07-29 11:52:18 +03004849 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004850 err = -EINVAL;
4851 goto out_free;
4852 }
4853 err = ieee80211_get_ratemask(wiphy->bands[band],
4854 nla_data(attr),
4855 nla_len(attr),
4856 &request->rates[band]);
4857 if (err)
4858 goto out_free;
4859 }
4860 }
4861
Sam Leffler46856bb2012-10-11 21:03:32 -07004862 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004863 request->flags = nla_get_u32(
4864 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004865 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4866 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4867 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4868 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004869 err = -EOPNOTSUPP;
4870 goto out_free;
4871 }
4872 }
Sam Lefflered4737712012-10-11 21:03:31 -07004873
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304874 request->no_cck =
4875 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4876
Johannes Bergfd014282012-06-18 19:17:03 +02004877 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004878 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004879 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004880
Johannes Berg79c97e92009-07-07 03:56:12 +02004881 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004882 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004883
Johannes Berg463d0182009-07-14 00:33:35 +02004884 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004885 nl80211_send_scan_start(rdev, wdev);
4886 if (wdev->netdev)
4887 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004888 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004889 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004890 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004891 kfree(request);
4892 }
Johannes Berg3b858752009-03-12 09:55:09 +01004893
Johannes Berg2a519312009-02-10 21:25:55 +01004894 return err;
4895}
4896
Luciano Coelho807f8a82011-05-11 17:09:35 +03004897static int nl80211_start_sched_scan(struct sk_buff *skb,
4898 struct genl_info *info)
4899{
4900 struct cfg80211_sched_scan_request *request;
4901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4902 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004903 struct nlattr *attr;
4904 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004905 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004906 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004907 enum ieee80211_band band;
4908 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004909 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004910
4911 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4912 !rdev->ops->sched_scan_start)
4913 return -EOPNOTSUPP;
4914
4915 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4916 return -EINVAL;
4917
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004918 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4919 return -EINVAL;
4920
4921 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4922 if (interval == 0)
4923 return -EINVAL;
4924
Luciano Coelho807f8a82011-05-11 17:09:35 +03004925 wiphy = &rdev->wiphy;
4926
4927 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4928 n_channels = validate_scan_freqs(
4929 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4930 if (!n_channels)
4931 return -EINVAL;
4932 } else {
4933 n_channels = 0;
4934
4935 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4936 if (wiphy->bands[band])
4937 n_channels += wiphy->bands[band]->n_channels;
4938 }
4939
4940 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4941 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4942 tmp)
4943 n_ssids++;
4944
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004945 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004946 return -EINVAL;
4947
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004948 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4949 nla_for_each_nested(attr,
4950 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4951 tmp)
4952 n_match_sets++;
4953
4954 if (n_match_sets > wiphy->max_match_sets)
4955 return -EINVAL;
4956
Luciano Coelho807f8a82011-05-11 17:09:35 +03004957 if (info->attrs[NL80211_ATTR_IE])
4958 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4959 else
4960 ie_len = 0;
4961
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004962 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004963 return -EINVAL;
4964
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004965 mutex_lock(&rdev->sched_scan_mtx);
4966
4967 if (rdev->sched_scan_req) {
4968 err = -EINPROGRESS;
4969 goto out;
4970 }
4971
Luciano Coelho807f8a82011-05-11 17:09:35 +03004972 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004973 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004974 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004975 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004976 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004977 if (!request) {
4978 err = -ENOMEM;
4979 goto out;
4980 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004981
4982 if (n_ssids)
4983 request->ssids = (void *)&request->channels[n_channels];
4984 request->n_ssids = n_ssids;
4985 if (ie_len) {
4986 if (request->ssids)
4987 request->ie = (void *)(request->ssids + n_ssids);
4988 else
4989 request->ie = (void *)(request->channels + n_channels);
4990 }
4991
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004992 if (n_match_sets) {
4993 if (request->ie)
4994 request->match_sets = (void *)(request->ie + ie_len);
4995 else if (request->ssids)
4996 request->match_sets =
4997 (void *)(request->ssids + n_ssids);
4998 else
4999 request->match_sets =
5000 (void *)(request->channels + n_channels);
5001 }
5002 request->n_match_sets = n_match_sets;
5003
Luciano Coelho807f8a82011-05-11 17:09:35 +03005004 i = 0;
5005 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5006 /* user specified, bail out if channel not found */
5007 nla_for_each_nested(attr,
5008 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5009 tmp) {
5010 struct ieee80211_channel *chan;
5011
5012 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5013
5014 if (!chan) {
5015 err = -EINVAL;
5016 goto out_free;
5017 }
5018
5019 /* ignore disabled channels */
5020 if (chan->flags & IEEE80211_CHAN_DISABLED)
5021 continue;
5022
5023 request->channels[i] = chan;
5024 i++;
5025 }
5026 } else {
5027 /* all channels */
5028 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5029 int j;
5030 if (!wiphy->bands[band])
5031 continue;
5032 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5033 struct ieee80211_channel *chan;
5034
5035 chan = &wiphy->bands[band]->channels[j];
5036
5037 if (chan->flags & IEEE80211_CHAN_DISABLED)
5038 continue;
5039
5040 request->channels[i] = chan;
5041 i++;
5042 }
5043 }
5044 }
5045
5046 if (!i) {
5047 err = -EINVAL;
5048 goto out_free;
5049 }
5050
5051 request->n_channels = i;
5052
5053 i = 0;
5054 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5055 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5056 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005057 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005058 err = -EINVAL;
5059 goto out_free;
5060 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005061 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005062 memcpy(request->ssids[i].ssid, nla_data(attr),
5063 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005064 i++;
5065 }
5066 }
5067
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005068 i = 0;
5069 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5070 nla_for_each_nested(attr,
5071 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5072 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005073 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005074
5075 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5076 nla_data(attr), nla_len(attr),
5077 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005078 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005079 if (ssid) {
5080 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5081 err = -EINVAL;
5082 goto out_free;
5083 }
5084 memcpy(request->match_sets[i].ssid.ssid,
5085 nla_data(ssid), nla_len(ssid));
5086 request->match_sets[i].ssid.ssid_len =
5087 nla_len(ssid);
5088 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005089 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5090 if (rssi)
5091 request->rssi_thold = nla_get_u32(rssi);
5092 else
5093 request->rssi_thold =
5094 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005095 i++;
5096 }
5097 }
5098
Luciano Coelho807f8a82011-05-11 17:09:35 +03005099 if (info->attrs[NL80211_ATTR_IE]) {
5100 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5101 memcpy((void *)request->ie,
5102 nla_data(info->attrs[NL80211_ATTR_IE]),
5103 request->ie_len);
5104 }
5105
Sam Leffler46856bb2012-10-11 21:03:32 -07005106 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005107 request->flags = nla_get_u32(
5108 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005109 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5110 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5111 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5112 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005113 err = -EOPNOTSUPP;
5114 goto out_free;
5115 }
5116 }
Sam Lefflered4737712012-10-11 21:03:31 -07005117
Luciano Coelho807f8a82011-05-11 17:09:35 +03005118 request->dev = dev;
5119 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005120 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005121 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005122
Hila Gonene35e4d22012-06-27 17:19:42 +03005123 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005124 if (!err) {
5125 rdev->sched_scan_req = request;
5126 nl80211_send_sched_scan(rdev, dev,
5127 NL80211_CMD_START_SCHED_SCAN);
5128 goto out;
5129 }
5130
5131out_free:
5132 kfree(request);
5133out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005134 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005135 return err;
5136}
5137
5138static int nl80211_stop_sched_scan(struct sk_buff *skb,
5139 struct genl_info *info)
5140{
5141 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005142 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005143
5144 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5145 !rdev->ops->sched_scan_stop)
5146 return -EOPNOTSUPP;
5147
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005148 mutex_lock(&rdev->sched_scan_mtx);
5149 err = __cfg80211_stop_sched_scan(rdev, false);
5150 mutex_unlock(&rdev->sched_scan_mtx);
5151
5152 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005153}
5154
Simon Wunderlich04f39042013-02-08 18:16:19 +01005155static int nl80211_start_radar_detection(struct sk_buff *skb,
5156 struct genl_info *info)
5157{
5158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5159 struct net_device *dev = info->user_ptr[1];
5160 struct wireless_dev *wdev = dev->ieee80211_ptr;
5161 struct cfg80211_chan_def chandef;
5162 int err;
5163
5164 err = nl80211_parse_chandef(rdev, info, &chandef);
5165 if (err)
5166 return err;
5167
5168 if (wdev->cac_started)
5169 return -EBUSY;
5170
5171 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5172 if (err < 0)
5173 return err;
5174
5175 if (err == 0)
5176 return -EINVAL;
5177
5178 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5179 return -EINVAL;
5180
5181 if (!rdev->ops->start_radar_detection)
5182 return -EOPNOTSUPP;
5183
5184 mutex_lock(&rdev->devlist_mtx);
5185 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5186 chandef.chan, CHAN_MODE_SHARED,
5187 BIT(chandef.width));
5188 if (err)
5189 goto err_locked;
5190
5191 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5192 if (!err) {
5193 wdev->channel = chandef.chan;
5194 wdev->cac_started = true;
5195 wdev->cac_start_time = jiffies;
5196 }
5197err_locked:
5198 mutex_unlock(&rdev->devlist_mtx);
5199
5200 return err;
5201}
5202
Johannes Berg9720bb32011-06-21 09:45:33 +02005203static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5204 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005205 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005206 struct wireless_dev *wdev,
5207 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005208{
Johannes Berg48ab9052009-07-10 18:42:31 +02005209 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005210 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005211 void *hdr;
5212 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005213 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005214
5215 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005216
Eric W. Biederman15e47302012-09-07 20:12:54 +00005217 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005218 NL80211_CMD_NEW_SCAN_RESULTS);
5219 if (!hdr)
5220 return -1;
5221
Johannes Berg9720bb32011-06-21 09:45:33 +02005222 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5223
David S. Miller9360ffd2012-03-29 04:41:26 -04005224 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5225 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5226 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005227
5228 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5229 if (!bss)
5230 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005231 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005232 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005233 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005234
5235 rcu_read_lock();
5236 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005237 if (ies) {
5238 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5239 goto fail_unlock_rcu;
5240 tsf = true;
5241 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5242 ies->len, ies->data))
5243 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005244 }
5245 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005246 if (ies) {
5247 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5248 goto fail_unlock_rcu;
5249 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5250 ies->len, ies->data))
5251 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005252 }
5253 rcu_read_unlock();
5254
David S. Miller9360ffd2012-03-29 04:41:26 -04005255 if (res->beacon_interval &&
5256 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5257 goto nla_put_failure;
5258 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5259 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5260 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5261 jiffies_to_msecs(jiffies - intbss->ts)))
5262 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005263
Johannes Berg77965c92009-02-18 18:45:06 +01005264 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005265 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005266 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5267 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005268 break;
5269 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005270 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5271 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005272 break;
5273 default:
5274 break;
5275 }
5276
Johannes Berg48ab9052009-07-10 18:42:31 +02005277 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005278 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005279 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005280 if (intbss == wdev->current_bss &&
5281 nla_put_u32(msg, NL80211_BSS_STATUS,
5282 NL80211_BSS_STATUS_ASSOCIATED))
5283 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005284 break;
5285 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005286 if (intbss == wdev->current_bss &&
5287 nla_put_u32(msg, NL80211_BSS_STATUS,
5288 NL80211_BSS_STATUS_IBSS_JOINED))
5289 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005290 break;
5291 default:
5292 break;
5293 }
5294
Johannes Berg2a519312009-02-10 21:25:55 +01005295 nla_nest_end(msg, bss);
5296
5297 return genlmsg_end(msg, hdr);
5298
Johannes Berg8cef2c92013-02-05 16:54:31 +01005299 fail_unlock_rcu:
5300 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005301 nla_put_failure:
5302 genlmsg_cancel(msg, hdr);
5303 return -EMSGSIZE;
5304}
5305
5306static int nl80211_dump_scan(struct sk_buff *skb,
5307 struct netlink_callback *cb)
5308{
Johannes Berg48ab9052009-07-10 18:42:31 +02005309 struct cfg80211_registered_device *rdev;
5310 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005311 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005312 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005313 int start = cb->args[1], idx = 0;
5314 int err;
5315
Johannes Berg67748892010-10-04 21:14:06 +02005316 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5317 if (err)
5318 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005319
Johannes Berg48ab9052009-07-10 18:42:31 +02005320 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005321
Johannes Berg48ab9052009-07-10 18:42:31 +02005322 wdev_lock(wdev);
5323 spin_lock_bh(&rdev->bss_lock);
5324 cfg80211_bss_expire(rdev);
5325
Johannes Berg9720bb32011-06-21 09:45:33 +02005326 cb->seq = rdev->bss_generation;
5327
Johannes Berg48ab9052009-07-10 18:42:31 +02005328 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005329 if (++idx <= start)
5330 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005331 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005332 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005333 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005334 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005335 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005336 }
5337 }
5338
Johannes Berg48ab9052009-07-10 18:42:31 +02005339 spin_unlock_bh(&rdev->bss_lock);
5340 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005341
5342 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005343 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005344
Johannes Berg67748892010-10-04 21:14:06 +02005345 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005346}
5347
Eric W. Biederman15e47302012-09-07 20:12:54 +00005348static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005349 int flags, struct net_device *dev,
5350 struct survey_info *survey)
5351{
5352 void *hdr;
5353 struct nlattr *infoattr;
5354
Eric W. Biederman15e47302012-09-07 20:12:54 +00005355 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005356 NL80211_CMD_NEW_SURVEY_RESULTS);
5357 if (!hdr)
5358 return -ENOMEM;
5359
David S. Miller9360ffd2012-03-29 04:41:26 -04005360 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5361 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005362
5363 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5364 if (!infoattr)
5365 goto nla_put_failure;
5366
David S. Miller9360ffd2012-03-29 04:41:26 -04005367 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5368 survey->channel->center_freq))
5369 goto nla_put_failure;
5370
5371 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5372 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5373 goto nla_put_failure;
5374 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5375 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5376 goto nla_put_failure;
5377 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5378 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5379 survey->channel_time))
5380 goto nla_put_failure;
5381 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5382 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5383 survey->channel_time_busy))
5384 goto nla_put_failure;
5385 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5386 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5387 survey->channel_time_ext_busy))
5388 goto nla_put_failure;
5389 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5390 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5391 survey->channel_time_rx))
5392 goto nla_put_failure;
5393 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5394 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5395 survey->channel_time_tx))
5396 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005397
5398 nla_nest_end(msg, infoattr);
5399
5400 return genlmsg_end(msg, hdr);
5401
5402 nla_put_failure:
5403 genlmsg_cancel(msg, hdr);
5404 return -EMSGSIZE;
5405}
5406
5407static int nl80211_dump_survey(struct sk_buff *skb,
5408 struct netlink_callback *cb)
5409{
5410 struct survey_info survey;
5411 struct cfg80211_registered_device *dev;
5412 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005413 int survey_idx = cb->args[1];
5414 int res;
5415
Johannes Berg67748892010-10-04 21:14:06 +02005416 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5417 if (res)
5418 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005419
5420 if (!dev->ops->dump_survey) {
5421 res = -EOPNOTSUPP;
5422 goto out_err;
5423 }
5424
5425 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005426 struct ieee80211_channel *chan;
5427
Hila Gonene35e4d22012-06-27 17:19:42 +03005428 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005429 if (res == -ENOENT)
5430 break;
5431 if (res)
5432 goto out_err;
5433
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005434 /* Survey without a channel doesn't make sense */
5435 if (!survey.channel) {
5436 res = -EINVAL;
5437 goto out;
5438 }
5439
5440 chan = ieee80211_get_channel(&dev->wiphy,
5441 survey.channel->center_freq);
5442 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5443 survey_idx++;
5444 continue;
5445 }
5446
Holger Schurig61fa7132009-11-11 12:25:40 +01005447 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005448 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005449 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5450 netdev,
5451 &survey) < 0)
5452 goto out;
5453 survey_idx++;
5454 }
5455
5456 out:
5457 cb->args[1] = survey_idx;
5458 res = skb->len;
5459 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005460 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005461 return res;
5462}
5463
Samuel Ortizb23aa672009-07-01 21:26:54 +02005464static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5465{
5466 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5467 NL80211_WPA_VERSION_2));
5468}
5469
Jouni Malinen636a5d32009-03-19 13:39:22 +02005470static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5471{
Johannes Berg4c476992010-10-04 21:36:35 +02005472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5473 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005474 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005475 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5476 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005477 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005478 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005479 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005480
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005481 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5482 return -EINVAL;
5483
5484 if (!info->attrs[NL80211_ATTR_MAC])
5485 return -EINVAL;
5486
Jouni Malinen17780922009-03-27 20:52:47 +02005487 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5488 return -EINVAL;
5489
Johannes Berg19957bb2009-07-02 17:20:43 +02005490 if (!info->attrs[NL80211_ATTR_SSID])
5491 return -EINVAL;
5492
5493 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5494 return -EINVAL;
5495
Johannes Bergfffd0932009-07-08 14:22:54 +02005496 err = nl80211_parse_key(info, &key);
5497 if (err)
5498 return err;
5499
5500 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005501 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5502 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005503 if (!key.p.key || !key.p.key_len)
5504 return -EINVAL;
5505 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5506 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5507 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5508 key.p.key_len != WLAN_KEY_LEN_WEP104))
5509 return -EINVAL;
5510 if (key.idx > 4)
5511 return -EINVAL;
5512 } else {
5513 key.p.key_len = 0;
5514 key.p.key = NULL;
5515 }
5516
Johannes Bergafea0b72010-08-10 09:46:42 +02005517 if (key.idx >= 0) {
5518 int i;
5519 bool ok = false;
5520 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5521 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5522 ok = true;
5523 break;
5524 }
5525 }
Johannes Berg4c476992010-10-04 21:36:35 +02005526 if (!ok)
5527 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005528 }
5529
Johannes Berg4c476992010-10-04 21:36:35 +02005530 if (!rdev->ops->auth)
5531 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005532
Johannes Berg074ac8d2010-09-16 14:58:22 +02005533 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005534 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5535 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005536
Johannes Berg19957bb2009-07-02 17:20:43 +02005537 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005538 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005539 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005540 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5541 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005542
Johannes Berg19957bb2009-07-02 17:20:43 +02005543 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5544 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5545
5546 if (info->attrs[NL80211_ATTR_IE]) {
5547 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5548 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5549 }
5550
5551 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005552 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005553 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005554
Jouni Malinene39e5b52012-09-30 19:29:39 +03005555 if (auth_type == NL80211_AUTHTYPE_SAE &&
5556 !info->attrs[NL80211_ATTR_SAE_DATA])
5557 return -EINVAL;
5558
5559 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5560 if (auth_type != NL80211_AUTHTYPE_SAE)
5561 return -EINVAL;
5562 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5563 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5564 /* need to include at least Auth Transaction and Status Code */
5565 if (sae_data_len < 4)
5566 return -EINVAL;
5567 }
5568
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005569 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5570
Johannes Berg95de8172012-01-20 13:55:25 +01005571 /*
5572 * Since we no longer track auth state, ignore
5573 * requests to only change local state.
5574 */
5575 if (local_state_change)
5576 return 0;
5577
Johannes Berg4c476992010-10-04 21:36:35 +02005578 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5579 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005580 key.p.key, key.p.key_len, key.idx,
5581 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005582}
5583
Johannes Bergc0692b82010-08-27 14:26:53 +03005584static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5585 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005586 struct cfg80211_crypto_settings *settings,
5587 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005588{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005589 memset(settings, 0, sizeof(*settings));
5590
Samuel Ortizb23aa672009-07-01 21:26:54 +02005591 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5592
Johannes Bergc0692b82010-08-27 14:26:53 +03005593 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5594 u16 proto;
5595 proto = nla_get_u16(
5596 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5597 settings->control_port_ethertype = cpu_to_be16(proto);
5598 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5599 proto != ETH_P_PAE)
5600 return -EINVAL;
5601 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5602 settings->control_port_no_encrypt = true;
5603 } else
5604 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5605
Samuel Ortizb23aa672009-07-01 21:26:54 +02005606 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5607 void *data;
5608 int len, i;
5609
5610 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5611 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5612 settings->n_ciphers_pairwise = len / sizeof(u32);
5613
5614 if (len % sizeof(u32))
5615 return -EINVAL;
5616
Johannes Berg3dc27d22009-07-02 21:36:37 +02005617 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005618 return -EINVAL;
5619
5620 memcpy(settings->ciphers_pairwise, data, len);
5621
5622 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005623 if (!cfg80211_supported_cipher_suite(
5624 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005625 settings->ciphers_pairwise[i]))
5626 return -EINVAL;
5627 }
5628
5629 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5630 settings->cipher_group =
5631 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005632 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5633 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005634 return -EINVAL;
5635 }
5636
5637 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5638 settings->wpa_versions =
5639 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5640 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5641 return -EINVAL;
5642 }
5643
5644 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5645 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005646 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005647
5648 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5649 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5650 settings->n_akm_suites = len / sizeof(u32);
5651
5652 if (len % sizeof(u32))
5653 return -EINVAL;
5654
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005655 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5656 return -EINVAL;
5657
Samuel Ortizb23aa672009-07-01 21:26:54 +02005658 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005659 }
5660
5661 return 0;
5662}
5663
Jouni Malinen636a5d32009-03-19 13:39:22 +02005664static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5665{
Johannes Berg4c476992010-10-04 21:36:35 +02005666 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5667 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005668 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005669 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005670 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005671 int err, ssid_len, ie_len = 0;
5672 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005673 u32 flags = 0;
5674 struct ieee80211_ht_cap *ht_capa = NULL;
5675 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005676
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005677 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5678 return -EINVAL;
5679
5680 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005681 !info->attrs[NL80211_ATTR_SSID] ||
5682 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005683 return -EINVAL;
5684
Johannes Berg4c476992010-10-04 21:36:35 +02005685 if (!rdev->ops->assoc)
5686 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005687
Johannes Berg074ac8d2010-09-16 14:58:22 +02005688 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005689 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5690 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005691
Johannes Berg19957bb2009-07-02 17:20:43 +02005692 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005693
Johannes Berg19957bb2009-07-02 17:20:43 +02005694 chan = ieee80211_get_channel(&rdev->wiphy,
5695 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005696 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5697 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005698
Johannes Berg19957bb2009-07-02 17:20:43 +02005699 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5700 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005701
5702 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005703 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5704 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005705 }
5706
Jouni Malinendc6382c2009-05-06 22:09:37 +03005707 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005708 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005709 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005710 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005711 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005712 else if (mfp != NL80211_MFP_NO)
5713 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005714 }
5715
Johannes Berg3e5d7642009-07-07 14:37:26 +02005716 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5717 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5718
Ben Greear7e7c8922011-11-18 11:31:59 -08005719 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5720 flags |= ASSOC_REQ_DISABLE_HT;
5721
5722 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5723 ht_capa_mask =
5724 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5725
5726 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5727 if (!ht_capa_mask)
5728 return -EINVAL;
5729 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5730 }
5731
Johannes Bergc0692b82010-08-27 14:26:53 +03005732 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005733 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005734 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5735 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005736 &crypto, flags, ht_capa,
5737 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005738
Jouni Malinen636a5d32009-03-19 13:39:22 +02005739 return err;
5740}
5741
5742static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5743{
Johannes Berg4c476992010-10-04 21:36:35 +02005744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5745 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005746 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005747 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005748 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005749 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005750
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005751 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5752 return -EINVAL;
5753
5754 if (!info->attrs[NL80211_ATTR_MAC])
5755 return -EINVAL;
5756
5757 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5758 return -EINVAL;
5759
Johannes Berg4c476992010-10-04 21:36:35 +02005760 if (!rdev->ops->deauth)
5761 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005762
Johannes Berg074ac8d2010-09-16 14:58:22 +02005763 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005764 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5765 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005766
Johannes Berg19957bb2009-07-02 17:20:43 +02005767 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005768
Johannes Berg19957bb2009-07-02 17:20:43 +02005769 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5770 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005771 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005772 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005773 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005774
5775 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005776 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5777 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005778 }
5779
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005780 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5781
Johannes Berg4c476992010-10-04 21:36:35 +02005782 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5783 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005784}
5785
5786static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5787{
Johannes Berg4c476992010-10-04 21:36:35 +02005788 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5789 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005790 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005791 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005792 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005793 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005794
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005795 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5796 return -EINVAL;
5797
5798 if (!info->attrs[NL80211_ATTR_MAC])
5799 return -EINVAL;
5800
5801 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5802 return -EINVAL;
5803
Johannes Berg4c476992010-10-04 21:36:35 +02005804 if (!rdev->ops->disassoc)
5805 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005806
Johannes Berg074ac8d2010-09-16 14:58:22 +02005807 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005808 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5809 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005810
Johannes Berg19957bb2009-07-02 17:20:43 +02005811 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005812
Johannes Berg19957bb2009-07-02 17:20:43 +02005813 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5814 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005815 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005816 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005817 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005818
5819 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005820 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5821 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005822 }
5823
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005824 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5825
Johannes Berg4c476992010-10-04 21:36:35 +02005826 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5827 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005828}
5829
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005830static bool
5831nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5832 int mcast_rate[IEEE80211_NUM_BANDS],
5833 int rateval)
5834{
5835 struct wiphy *wiphy = &rdev->wiphy;
5836 bool found = false;
5837 int band, i;
5838
5839 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5840 struct ieee80211_supported_band *sband;
5841
5842 sband = wiphy->bands[band];
5843 if (!sband)
5844 continue;
5845
5846 for (i = 0; i < sband->n_bitrates; i++) {
5847 if (sband->bitrates[i].bitrate == rateval) {
5848 mcast_rate[band] = i + 1;
5849 found = true;
5850 break;
5851 }
5852 }
5853 }
5854
5855 return found;
5856}
5857
Johannes Berg04a773a2009-04-19 21:24:32 +02005858static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5859{
Johannes Berg4c476992010-10-04 21:36:35 +02005860 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5861 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005862 struct cfg80211_ibss_params ibss;
5863 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005864 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005865 int err;
5866
Johannes Berg8e30bc52009-04-22 17:45:38 +02005867 memset(&ibss, 0, sizeof(ibss));
5868
Johannes Berg04a773a2009-04-19 21:24:32 +02005869 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5870 return -EINVAL;
5871
Johannes Berg683b6d32012-11-08 21:25:48 +01005872 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005873 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5874 return -EINVAL;
5875
Johannes Berg8e30bc52009-04-22 17:45:38 +02005876 ibss.beacon_interval = 100;
5877
5878 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5879 ibss.beacon_interval =
5880 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5881 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5882 return -EINVAL;
5883 }
5884
Johannes Berg4c476992010-10-04 21:36:35 +02005885 if (!rdev->ops->join_ibss)
5886 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005887
Johannes Berg4c476992010-10-04 21:36:35 +02005888 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5889 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005890
Johannes Berg79c97e92009-07-07 03:56:12 +02005891 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005892
Johannes Berg39193492011-09-16 13:45:25 +02005893 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005894 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005895
5896 if (!is_valid_ether_addr(ibss.bssid))
5897 return -EINVAL;
5898 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005899 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5900 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5901
5902 if (info->attrs[NL80211_ATTR_IE]) {
5903 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5904 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5905 }
5906
Johannes Berg683b6d32012-11-08 21:25:48 +01005907 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5908 if (err)
5909 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005910
Johannes Berg683b6d32012-11-08 21:25:48 +01005911 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005912 return -EINVAL;
5913
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005914 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5915 return -EINVAL;
5916 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5917 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005918 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005919
Johannes Berg04a773a2009-04-19 21:24:32 +02005920 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005921 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005922
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005923 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5924 u8 *rates =
5925 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5926 int n_rates =
5927 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5928 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005929 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005930
Johannes Berg34850ab2011-07-18 18:08:35 +02005931 err = ieee80211_get_ratemask(sband, rates, n_rates,
5932 &ibss.basic_rates);
5933 if (err)
5934 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005935 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005936
5937 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5938 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5939 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5940 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005941
Johannes Berg4c476992010-10-04 21:36:35 +02005942 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305943 bool no_ht = false;
5944
Johannes Berg4c476992010-10-04 21:36:35 +02005945 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305946 info->attrs[NL80211_ATTR_KEYS],
5947 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005948 if (IS_ERR(connkeys))
5949 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305950
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005951 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5952 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305953 kfree(connkeys);
5954 return -EINVAL;
5955 }
Johannes Berg4c476992010-10-04 21:36:35 +02005956 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005957
Antonio Quartulli267335d2012-01-31 20:25:47 +01005958 ibss.control_port =
5959 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5960
Johannes Berg4c476992010-10-04 21:36:35 +02005961 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005962 if (err)
5963 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005964 return err;
5965}
5966
5967static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5968{
Johannes Berg4c476992010-10-04 21:36:35 +02005969 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5970 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005971
Johannes Berg4c476992010-10-04 21:36:35 +02005972 if (!rdev->ops->leave_ibss)
5973 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005974
Johannes Berg4c476992010-10-04 21:36:35 +02005975 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5976 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005977
Johannes Berg4c476992010-10-04 21:36:35 +02005978 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005979}
5980
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005981static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5982{
5983 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5984 struct net_device *dev = info->user_ptr[1];
5985 int mcast_rate[IEEE80211_NUM_BANDS];
5986 u32 nla_rate;
5987 int err;
5988
5989 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5990 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5991 return -EOPNOTSUPP;
5992
5993 if (!rdev->ops->set_mcast_rate)
5994 return -EOPNOTSUPP;
5995
5996 memset(mcast_rate, 0, sizeof(mcast_rate));
5997
5998 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5999 return -EINVAL;
6000
6001 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6002 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6003 return -EINVAL;
6004
6005 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6006
6007 return err;
6008}
6009
6010
Johannes Bergaff89a92009-07-01 21:26:51 +02006011#ifdef CONFIG_NL80211_TESTMODE
6012static struct genl_multicast_group nl80211_testmode_mcgrp = {
6013 .name = "testmode",
6014};
6015
6016static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6017{
Johannes Berg4c476992010-10-04 21:36:35 +02006018 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006019 int err;
6020
6021 if (!info->attrs[NL80211_ATTR_TESTDATA])
6022 return -EINVAL;
6023
Johannes Bergaff89a92009-07-01 21:26:51 +02006024 err = -EOPNOTSUPP;
6025 if (rdev->ops->testmode_cmd) {
6026 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006027 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006028 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6029 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6030 rdev->testmode_info = NULL;
6031 }
6032
Johannes Bergaff89a92009-07-01 21:26:51 +02006033 return err;
6034}
6035
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006036static int nl80211_testmode_dump(struct sk_buff *skb,
6037 struct netlink_callback *cb)
6038{
Johannes Berg00918d32011-12-13 17:22:05 +01006039 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006040 int err;
6041 long phy_idx;
6042 void *data = NULL;
6043 int data_len = 0;
6044
6045 if (cb->args[0]) {
6046 /*
6047 * 0 is a valid index, but not valid for args[0],
6048 * so we need to offset by 1.
6049 */
6050 phy_idx = cb->args[0] - 1;
6051 } else {
6052 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6053 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6054 nl80211_policy);
6055 if (err)
6056 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006057
Johannes Berg2bd7e352012-06-15 14:23:16 +02006058 mutex_lock(&cfg80211_mutex);
6059 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6060 nl80211_fam.attrbuf);
6061 if (IS_ERR(rdev)) {
6062 mutex_unlock(&cfg80211_mutex);
6063 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006064 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006065 phy_idx = rdev->wiphy_idx;
6066 rdev = NULL;
6067 mutex_unlock(&cfg80211_mutex);
6068
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006069 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6070 cb->args[1] =
6071 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6072 }
6073
6074 if (cb->args[1]) {
6075 data = nla_data((void *)cb->args[1]);
6076 data_len = nla_len((void *)cb->args[1]);
6077 }
6078
6079 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006080 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6081 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006082 mutex_unlock(&cfg80211_mutex);
6083 return -ENOENT;
6084 }
Johannes Berg00918d32011-12-13 17:22:05 +01006085 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006086 mutex_unlock(&cfg80211_mutex);
6087
Johannes Berg00918d32011-12-13 17:22:05 +01006088 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006089 err = -EOPNOTSUPP;
6090 goto out_err;
6091 }
6092
6093 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006094 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006095 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6096 NL80211_CMD_TESTMODE);
6097 struct nlattr *tmdata;
6098
David S. Miller9360ffd2012-03-29 04:41:26 -04006099 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006100 genlmsg_cancel(skb, hdr);
6101 break;
6102 }
6103
6104 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6105 if (!tmdata) {
6106 genlmsg_cancel(skb, hdr);
6107 break;
6108 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006109 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006110 nla_nest_end(skb, tmdata);
6111
6112 if (err == -ENOBUFS || err == -ENOENT) {
6113 genlmsg_cancel(skb, hdr);
6114 break;
6115 } else if (err) {
6116 genlmsg_cancel(skb, hdr);
6117 goto out_err;
6118 }
6119
6120 genlmsg_end(skb, hdr);
6121 }
6122
6123 err = skb->len;
6124 /* see above */
6125 cb->args[0] = phy_idx + 1;
6126 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006127 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006128 return err;
6129}
6130
Johannes Bergaff89a92009-07-01 21:26:51 +02006131static struct sk_buff *
6132__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006133 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006134{
6135 struct sk_buff *skb;
6136 void *hdr;
6137 struct nlattr *data;
6138
6139 skb = nlmsg_new(approxlen + 100, gfp);
6140 if (!skb)
6141 return NULL;
6142
Eric W. Biederman15e47302012-09-07 20:12:54 +00006143 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006144 if (!hdr) {
6145 kfree_skb(skb);
6146 return NULL;
6147 }
6148
David S. Miller9360ffd2012-03-29 04:41:26 -04006149 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6150 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006151 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6152
6153 ((void **)skb->cb)[0] = rdev;
6154 ((void **)skb->cb)[1] = hdr;
6155 ((void **)skb->cb)[2] = data;
6156
6157 return skb;
6158
6159 nla_put_failure:
6160 kfree_skb(skb);
6161 return NULL;
6162}
6163
6164struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6165 int approxlen)
6166{
6167 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6168
6169 if (WARN_ON(!rdev->testmode_info))
6170 return NULL;
6171
6172 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006173 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006174 rdev->testmode_info->snd_seq,
6175 GFP_KERNEL);
6176}
6177EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6178
6179int cfg80211_testmode_reply(struct sk_buff *skb)
6180{
6181 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6182 void *hdr = ((void **)skb->cb)[1];
6183 struct nlattr *data = ((void **)skb->cb)[2];
6184
6185 if (WARN_ON(!rdev->testmode_info)) {
6186 kfree_skb(skb);
6187 return -EINVAL;
6188 }
6189
6190 nla_nest_end(skb, data);
6191 genlmsg_end(skb, hdr);
6192 return genlmsg_reply(skb, rdev->testmode_info);
6193}
6194EXPORT_SYMBOL(cfg80211_testmode_reply);
6195
6196struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6197 int approxlen, gfp_t gfp)
6198{
6199 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6200
6201 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6202}
6203EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6204
6205void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6206{
6207 void *hdr = ((void **)skb->cb)[1];
6208 struct nlattr *data = ((void **)skb->cb)[2];
6209
6210 nla_nest_end(skb, data);
6211 genlmsg_end(skb, hdr);
6212 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6213}
6214EXPORT_SYMBOL(cfg80211_testmode_event);
6215#endif
6216
Samuel Ortizb23aa672009-07-01 21:26:54 +02006217static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6218{
Johannes Berg4c476992010-10-04 21:36:35 +02006219 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6220 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006221 struct cfg80211_connect_params connect;
6222 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006223 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006224 int err;
6225
6226 memset(&connect, 0, sizeof(connect));
6227
6228 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6229 return -EINVAL;
6230
6231 if (!info->attrs[NL80211_ATTR_SSID] ||
6232 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6233 return -EINVAL;
6234
6235 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6236 connect.auth_type =
6237 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006238 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6239 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006240 return -EINVAL;
6241 } else
6242 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6243
6244 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6245
Johannes Bergc0692b82010-08-27 14:26:53 +03006246 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006247 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006248 if (err)
6249 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006250
Johannes Berg074ac8d2010-09-16 14:58:22 +02006251 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006252 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6253 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006254
Johannes Berg79c97e92009-07-07 03:56:12 +02006255 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006256
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306257 connect.bg_scan_period = -1;
6258 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6259 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6260 connect.bg_scan_period =
6261 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6262 }
6263
Samuel Ortizb23aa672009-07-01 21:26:54 +02006264 if (info->attrs[NL80211_ATTR_MAC])
6265 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6266 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6267 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6268
6269 if (info->attrs[NL80211_ATTR_IE]) {
6270 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6271 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6272 }
6273
Jouni Malinencee00a92013-01-15 17:15:57 +02006274 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6275 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6276 if (connect.mfp != NL80211_MFP_REQUIRED &&
6277 connect.mfp != NL80211_MFP_NO)
6278 return -EINVAL;
6279 } else {
6280 connect.mfp = NL80211_MFP_NO;
6281 }
6282
Samuel Ortizb23aa672009-07-01 21:26:54 +02006283 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6284 connect.channel =
6285 ieee80211_get_channel(wiphy,
6286 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6287 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006288 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6289 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006290 }
6291
Johannes Bergfffd0932009-07-08 14:22:54 +02006292 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6293 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306294 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006295 if (IS_ERR(connkeys))
6296 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006297 }
6298
Ben Greear7e7c8922011-11-18 11:31:59 -08006299 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6300 connect.flags |= ASSOC_REQ_DISABLE_HT;
6301
6302 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6303 memcpy(&connect.ht_capa_mask,
6304 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6305 sizeof(connect.ht_capa_mask));
6306
6307 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006308 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6309 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006310 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006311 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006312 memcpy(&connect.ht_capa,
6313 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6314 sizeof(connect.ht_capa));
6315 }
6316
Johannes Bergfffd0932009-07-08 14:22:54 +02006317 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006318 if (err)
6319 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006320 return err;
6321}
6322
6323static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6324{
Johannes Berg4c476992010-10-04 21:36:35 +02006325 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6326 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006327 u16 reason;
6328
6329 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6330 reason = WLAN_REASON_DEAUTH_LEAVING;
6331 else
6332 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6333
6334 if (reason == 0)
6335 return -EINVAL;
6336
Johannes Berg074ac8d2010-09-16 14:58:22 +02006337 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006338 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6339 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006340
Johannes Berg4c476992010-10-04 21:36:35 +02006341 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006342}
6343
Johannes Berg463d0182009-07-14 00:33:35 +02006344static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6345{
Johannes Berg4c476992010-10-04 21:36:35 +02006346 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006347 struct net *net;
6348 int err;
6349 u32 pid;
6350
6351 if (!info->attrs[NL80211_ATTR_PID])
6352 return -EINVAL;
6353
6354 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6355
Johannes Berg463d0182009-07-14 00:33:35 +02006356 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006357 if (IS_ERR(net))
6358 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006359
6360 err = 0;
6361
6362 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006363 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6364 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006365
Johannes Berg463d0182009-07-14 00:33:35 +02006366 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006367 return err;
6368}
6369
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006370static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6371{
Johannes Berg4c476992010-10-04 21:36:35 +02006372 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006373 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6374 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006375 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006376 struct cfg80211_pmksa pmksa;
6377
6378 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6379
6380 if (!info->attrs[NL80211_ATTR_MAC])
6381 return -EINVAL;
6382
6383 if (!info->attrs[NL80211_ATTR_PMKID])
6384 return -EINVAL;
6385
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006386 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6387 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6388
Johannes Berg074ac8d2010-09-16 14:58:22 +02006389 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006390 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6391 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006392
6393 switch (info->genlhdr->cmd) {
6394 case NL80211_CMD_SET_PMKSA:
6395 rdev_ops = rdev->ops->set_pmksa;
6396 break;
6397 case NL80211_CMD_DEL_PMKSA:
6398 rdev_ops = rdev->ops->del_pmksa;
6399 break;
6400 default:
6401 WARN_ON(1);
6402 break;
6403 }
6404
Johannes Berg4c476992010-10-04 21:36:35 +02006405 if (!rdev_ops)
6406 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006407
Johannes Berg4c476992010-10-04 21:36:35 +02006408 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006409}
6410
6411static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6412{
Johannes Berg4c476992010-10-04 21:36:35 +02006413 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6414 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006415
Johannes Berg074ac8d2010-09-16 14:58:22 +02006416 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006417 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6418 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006419
Johannes Berg4c476992010-10-04 21:36:35 +02006420 if (!rdev->ops->flush_pmksa)
6421 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006422
Hila Gonene35e4d22012-06-27 17:19:42 +03006423 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006424}
6425
Arik Nemtsov109086c2011-09-28 14:12:50 +03006426static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6427{
6428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6429 struct net_device *dev = info->user_ptr[1];
6430 u8 action_code, dialog_token;
6431 u16 status_code;
6432 u8 *peer;
6433
6434 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6435 !rdev->ops->tdls_mgmt)
6436 return -EOPNOTSUPP;
6437
6438 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6439 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6440 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6441 !info->attrs[NL80211_ATTR_IE] ||
6442 !info->attrs[NL80211_ATTR_MAC])
6443 return -EINVAL;
6444
6445 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6446 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6447 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6448 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6449
Hila Gonene35e4d22012-06-27 17:19:42 +03006450 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6451 dialog_token, status_code,
6452 nla_data(info->attrs[NL80211_ATTR_IE]),
6453 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006454}
6455
6456static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6457{
6458 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6459 struct net_device *dev = info->user_ptr[1];
6460 enum nl80211_tdls_operation operation;
6461 u8 *peer;
6462
6463 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6464 !rdev->ops->tdls_oper)
6465 return -EOPNOTSUPP;
6466
6467 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6468 !info->attrs[NL80211_ATTR_MAC])
6469 return -EINVAL;
6470
6471 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6472 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6473
Hila Gonene35e4d22012-06-27 17:19:42 +03006474 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006475}
6476
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006477static int nl80211_remain_on_channel(struct sk_buff *skb,
6478 struct genl_info *info)
6479{
Johannes Berg4c476992010-10-04 21:36:35 +02006480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006481 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006482 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006483 struct sk_buff *msg;
6484 void *hdr;
6485 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006486 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006487 int err;
6488
6489 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6490 !info->attrs[NL80211_ATTR_DURATION])
6491 return -EINVAL;
6492
6493 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6494
Johannes Berg7c4ef712011-11-18 15:33:48 +01006495 if (!rdev->ops->remain_on_channel ||
6496 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006497 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006498
Johannes Bergebf348f2012-06-01 12:50:54 +02006499 /*
6500 * We should be on that channel for at least a minimum amount of
6501 * time (10ms) but no longer than the driver supports.
6502 */
6503 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6504 duration > rdev->wiphy.max_remain_on_channel_duration)
6505 return -EINVAL;
6506
Johannes Berg683b6d32012-11-08 21:25:48 +01006507 err = nl80211_parse_chandef(rdev, info, &chandef);
6508 if (err)
6509 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006510
6511 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006512 if (!msg)
6513 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006514
Eric W. Biederman15e47302012-09-07 20:12:54 +00006515 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006516 NL80211_CMD_REMAIN_ON_CHANNEL);
6517
6518 if (IS_ERR(hdr)) {
6519 err = PTR_ERR(hdr);
6520 goto free_msg;
6521 }
6522
Johannes Berg683b6d32012-11-08 21:25:48 +01006523 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6524 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006525
6526 if (err)
6527 goto free_msg;
6528
David S. Miller9360ffd2012-03-29 04:41:26 -04006529 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6530 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006531
6532 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006533
6534 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006535
6536 nla_put_failure:
6537 err = -ENOBUFS;
6538 free_msg:
6539 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006540 return err;
6541}
6542
6543static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6544 struct genl_info *info)
6545{
Johannes Berg4c476992010-10-04 21:36:35 +02006546 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006547 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006548 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006549
6550 if (!info->attrs[NL80211_ATTR_COOKIE])
6551 return -EINVAL;
6552
Johannes Berg4c476992010-10-04 21:36:35 +02006553 if (!rdev->ops->cancel_remain_on_channel)
6554 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006555
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006556 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6557
Hila Gonene35e4d22012-06-27 17:19:42 +03006558 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006559}
6560
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006561static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6562 u8 *rates, u8 rates_len)
6563{
6564 u8 i;
6565 u32 mask = 0;
6566
6567 for (i = 0; i < rates_len; i++) {
6568 int rate = (rates[i] & 0x7f) * 5;
6569 int ridx;
6570 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6571 struct ieee80211_rate *srate =
6572 &sband->bitrates[ridx];
6573 if (rate == srate->bitrate) {
6574 mask |= 1 << ridx;
6575 break;
6576 }
6577 }
6578 if (ridx == sband->n_bitrates)
6579 return 0; /* rate not found */
6580 }
6581
6582 return mask;
6583}
6584
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006585static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6586 u8 *rates, u8 rates_len,
6587 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6588{
6589 u8 i;
6590
6591 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6592
6593 for (i = 0; i < rates_len; i++) {
6594 int ridx, rbit;
6595
6596 ridx = rates[i] / 8;
6597 rbit = BIT(rates[i] % 8);
6598
6599 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006600 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006601 return false;
6602
6603 /* check availability */
6604 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6605 mcs[ridx] |= rbit;
6606 else
6607 return false;
6608 }
6609
6610 return true;
6611}
6612
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006613static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006614 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6615 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006616 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6617 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006618};
6619
6620static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6621 struct genl_info *info)
6622{
6623 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006624 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006625 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006626 int rem, i;
6627 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006628 struct nlattr *tx_rates;
6629 struct ieee80211_supported_band *sband;
6630
6631 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6632 return -EINVAL;
6633
Johannes Berg4c476992010-10-04 21:36:35 +02006634 if (!rdev->ops->set_bitrate_mask)
6635 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006636
6637 memset(&mask, 0, sizeof(mask));
6638 /* Default to all rates enabled */
6639 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6640 sband = rdev->wiphy.bands[i];
6641 mask.control[i].legacy =
6642 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006643 if (sband)
6644 memcpy(mask.control[i].mcs,
6645 sband->ht_cap.mcs.rx_mask,
6646 sizeof(mask.control[i].mcs));
6647 else
6648 memset(mask.control[i].mcs, 0,
6649 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006650 }
6651
6652 /*
6653 * The nested attribute uses enum nl80211_band as the index. This maps
6654 * directly to the enum ieee80211_band values used in cfg80211.
6655 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006656 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006657 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6658 {
6659 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006660 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6661 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006662 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006663 if (sband == NULL)
6664 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006665 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6666 nla_len(tx_rates), nl80211_txattr_policy);
6667 if (tb[NL80211_TXRATE_LEGACY]) {
6668 mask.control[band].legacy = rateset_to_mask(
6669 sband,
6670 nla_data(tb[NL80211_TXRATE_LEGACY]),
6671 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306672 if ((mask.control[band].legacy == 0) &&
6673 nla_len(tb[NL80211_TXRATE_LEGACY]))
6674 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006675 }
6676 if (tb[NL80211_TXRATE_MCS]) {
6677 if (!ht_rateset_to_mask(
6678 sband,
6679 nla_data(tb[NL80211_TXRATE_MCS]),
6680 nla_len(tb[NL80211_TXRATE_MCS]),
6681 mask.control[band].mcs))
6682 return -EINVAL;
6683 }
6684
6685 if (mask.control[band].legacy == 0) {
6686 /* don't allow empty legacy rates if HT
6687 * is not even supported. */
6688 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6689 return -EINVAL;
6690
6691 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6692 if (mask.control[band].mcs[i])
6693 break;
6694
6695 /* legacy and mcs rates may not be both empty */
6696 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006697 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006698 }
6699 }
6700
Hila Gonene35e4d22012-06-27 17:19:42 +03006701 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006702}
6703
Johannes Berg2e161f72010-08-12 15:38:38 +02006704static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006705{
Johannes Berg4c476992010-10-04 21:36:35 +02006706 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006707 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006708 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006709
6710 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6711 return -EINVAL;
6712
Johannes Berg2e161f72010-08-12 15:38:38 +02006713 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6714 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006715
Johannes Berg71bbc992012-06-15 15:30:18 +02006716 switch (wdev->iftype) {
6717 case NL80211_IFTYPE_STATION:
6718 case NL80211_IFTYPE_ADHOC:
6719 case NL80211_IFTYPE_P2P_CLIENT:
6720 case NL80211_IFTYPE_AP:
6721 case NL80211_IFTYPE_AP_VLAN:
6722 case NL80211_IFTYPE_MESH_POINT:
6723 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006724 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006725 break;
6726 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006727 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006728 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006729
6730 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006731 if (!rdev->ops->mgmt_tx)
6732 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006733
Eric W. Biederman15e47302012-09-07 20:12:54 +00006734 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006735 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6736 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006737}
6738
Johannes Berg2e161f72010-08-12 15:38:38 +02006739static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006740{
Johannes Berg4c476992010-10-04 21:36:35 +02006741 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006742 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006743 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006744 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006745 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006746 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006747 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006748 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006749 bool offchan, no_cck, dont_wait_for_ack;
6750
6751 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006752
Johannes Berg683b6d32012-11-08 21:25:48 +01006753 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006754 return -EINVAL;
6755
Johannes Berg4c476992010-10-04 21:36:35 +02006756 if (!rdev->ops->mgmt_tx)
6757 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006758
Johannes Berg71bbc992012-06-15 15:30:18 +02006759 switch (wdev->iftype) {
6760 case NL80211_IFTYPE_STATION:
6761 case NL80211_IFTYPE_ADHOC:
6762 case NL80211_IFTYPE_P2P_CLIENT:
6763 case NL80211_IFTYPE_AP:
6764 case NL80211_IFTYPE_AP_VLAN:
6765 case NL80211_IFTYPE_MESH_POINT:
6766 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006767 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006768 break;
6769 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006770 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006771 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006772
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006773 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006774 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006775 return -EINVAL;
6776 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006777
6778 /*
6779 * We should wait on the channel for at least a minimum amount
6780 * of time (10ms) but no longer than the driver supports.
6781 */
6782 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6783 wait > rdev->wiphy.max_remain_on_channel_duration)
6784 return -EINVAL;
6785
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006786 }
6787
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006788 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6789
Johannes Berg7c4ef712011-11-18 15:33:48 +01006790 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6791 return -EINVAL;
6792
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306793 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6794
Johannes Berg683b6d32012-11-08 21:25:48 +01006795 err = nl80211_parse_chandef(rdev, info, &chandef);
6796 if (err)
6797 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006798
Johannes Berge247bd902011-11-04 11:18:21 +01006799 if (!dont_wait_for_ack) {
6800 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6801 if (!msg)
6802 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006803
Eric W. Biederman15e47302012-09-07 20:12:54 +00006804 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006805 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006806
Johannes Berge247bd902011-11-04 11:18:21 +01006807 if (IS_ERR(hdr)) {
6808 err = PTR_ERR(hdr);
6809 goto free_msg;
6810 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006811 }
Johannes Berge247bd902011-11-04 11:18:21 +01006812
Johannes Berg683b6d32012-11-08 21:25:48 +01006813 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006814 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6815 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006816 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006817 if (err)
6818 goto free_msg;
6819
Johannes Berge247bd902011-11-04 11:18:21 +01006820 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006821 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6822 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006823
Johannes Berge247bd902011-11-04 11:18:21 +01006824 genlmsg_end(msg, hdr);
6825 return genlmsg_reply(msg, info);
6826 }
6827
6828 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006829
6830 nla_put_failure:
6831 err = -ENOBUFS;
6832 free_msg:
6833 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006834 return err;
6835}
6836
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006837static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6838{
6839 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006840 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006841 u64 cookie;
6842
6843 if (!info->attrs[NL80211_ATTR_COOKIE])
6844 return -EINVAL;
6845
6846 if (!rdev->ops->mgmt_tx_cancel_wait)
6847 return -EOPNOTSUPP;
6848
Johannes Berg71bbc992012-06-15 15:30:18 +02006849 switch (wdev->iftype) {
6850 case NL80211_IFTYPE_STATION:
6851 case NL80211_IFTYPE_ADHOC:
6852 case NL80211_IFTYPE_P2P_CLIENT:
6853 case NL80211_IFTYPE_AP:
6854 case NL80211_IFTYPE_AP_VLAN:
6855 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006856 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006857 break;
6858 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006859 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006860 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006861
6862 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6863
Hila Gonene35e4d22012-06-27 17:19:42 +03006864 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006865}
6866
Kalle Valoffb9eb32010-02-17 17:58:10 +02006867static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6868{
Johannes Berg4c476992010-10-04 21:36:35 +02006869 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006870 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006871 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006872 u8 ps_state;
6873 bool state;
6874 int err;
6875
Johannes Berg4c476992010-10-04 21:36:35 +02006876 if (!info->attrs[NL80211_ATTR_PS_STATE])
6877 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006878
6879 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6880
Johannes Berg4c476992010-10-04 21:36:35 +02006881 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6882 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006883
6884 wdev = dev->ieee80211_ptr;
6885
Johannes Berg4c476992010-10-04 21:36:35 +02006886 if (!rdev->ops->set_power_mgmt)
6887 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006888
6889 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6890
6891 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006892 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006893
Hila Gonene35e4d22012-06-27 17:19:42 +03006894 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006895 if (!err)
6896 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006897 return err;
6898}
6899
6900static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6901{
Johannes Berg4c476992010-10-04 21:36:35 +02006902 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006903 enum nl80211_ps_state ps_state;
6904 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006905 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006906 struct sk_buff *msg;
6907 void *hdr;
6908 int err;
6909
Kalle Valoffb9eb32010-02-17 17:58:10 +02006910 wdev = dev->ieee80211_ptr;
6911
Johannes Berg4c476992010-10-04 21:36:35 +02006912 if (!rdev->ops->set_power_mgmt)
6913 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006914
6915 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006916 if (!msg)
6917 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006918
Eric W. Biederman15e47302012-09-07 20:12:54 +00006919 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006920 NL80211_CMD_GET_POWER_SAVE);
6921 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006922 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006923 goto free_msg;
6924 }
6925
6926 if (wdev->ps)
6927 ps_state = NL80211_PS_ENABLED;
6928 else
6929 ps_state = NL80211_PS_DISABLED;
6930
David S. Miller9360ffd2012-03-29 04:41:26 -04006931 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6932 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006933
6934 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006935 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006936
Johannes Berg4c476992010-10-04 21:36:35 +02006937 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006938 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006939 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006940 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006941 return err;
6942}
6943
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006944static struct nla_policy
6945nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6946 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6947 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6948 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006949 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6950 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6951 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006952};
6953
Thomas Pedersen84f10702012-07-12 16:17:33 -07006954static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006955 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006956{
6957 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6958 struct wireless_dev *wdev;
6959 struct net_device *dev = info->user_ptr[1];
6960
Johannes Bergd9d8b012012-11-26 12:51:52 +01006961 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006962 return -EINVAL;
6963
6964 wdev = dev->ieee80211_ptr;
6965
6966 if (!rdev->ops->set_cqm_txe_config)
6967 return -EOPNOTSUPP;
6968
6969 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6970 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6971 return -EOPNOTSUPP;
6972
Hila Gonene35e4d22012-06-27 17:19:42 +03006973 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006974}
6975
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006976static int nl80211_set_cqm_rssi(struct genl_info *info,
6977 s32 threshold, u32 hysteresis)
6978{
Johannes Berg4c476992010-10-04 21:36:35 +02006979 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006980 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006981 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006982
6983 if (threshold > 0)
6984 return -EINVAL;
6985
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006986 wdev = dev->ieee80211_ptr;
6987
Johannes Berg4c476992010-10-04 21:36:35 +02006988 if (!rdev->ops->set_cqm_rssi_config)
6989 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006990
Johannes Berg074ac8d2010-09-16 14:58:22 +02006991 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006992 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6993 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006994
Hila Gonene35e4d22012-06-27 17:19:42 +03006995 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006996}
6997
6998static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6999{
7000 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7001 struct nlattr *cqm;
7002 int err;
7003
7004 cqm = info->attrs[NL80211_ATTR_CQM];
7005 if (!cqm) {
7006 err = -EINVAL;
7007 goto out;
7008 }
7009
7010 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7011 nl80211_attr_cqm_policy);
7012 if (err)
7013 goto out;
7014
7015 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7016 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7017 s32 threshold;
7018 u32 hysteresis;
7019 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7020 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7021 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007022 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7023 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7024 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7025 u32 rate, pkts, intvl;
7026 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7027 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7028 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7029 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007030 } else
7031 err = -EINVAL;
7032
7033out:
7034 return err;
7035}
7036
Johannes Berg29cbe682010-12-03 09:20:44 +01007037static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7038{
7039 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7040 struct net_device *dev = info->user_ptr[1];
7041 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007042 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007043 int err;
7044
7045 /* start with default */
7046 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007047 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007048
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007049 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007050 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007051 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007052 if (err)
7053 return err;
7054 }
7055
7056 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7057 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7058 return -EINVAL;
7059
Javier Cardonac80d5452010-12-16 17:37:49 -08007060 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7061 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7062
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007063 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7064 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7065 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7066 return -EINVAL;
7067
Marco Porsch9bdbf042013-01-07 16:04:51 +01007068 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7069 setup.beacon_interval =
7070 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7071 if (setup.beacon_interval < 10 ||
7072 setup.beacon_interval > 10000)
7073 return -EINVAL;
7074 }
7075
7076 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7077 setup.dtim_period =
7078 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7079 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7080 return -EINVAL;
7081 }
7082
Javier Cardonac80d5452010-12-16 17:37:49 -08007083 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7084 /* parse additional setup parameters if given */
7085 err = nl80211_parse_mesh_setup(info, &setup);
7086 if (err)
7087 return err;
7088 }
7089
Johannes Bergcc1d2802012-05-16 23:50:20 +02007090 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007091 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7092 if (err)
7093 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007094 } else {
7095 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007096 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007097 }
7098
Javier Cardonac80d5452010-12-16 17:37:49 -08007099 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007100}
7101
7102static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7103{
7104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7105 struct net_device *dev = info->user_ptr[1];
7106
7107 return cfg80211_leave_mesh(rdev, dev);
7108}
7109
Johannes Bergdfb89c52012-06-27 09:23:48 +02007110#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007111static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7112 struct cfg80211_registered_device *rdev)
7113{
7114 struct nlattr *nl_pats, *nl_pat;
7115 int i, pat_len;
7116
7117 if (!rdev->wowlan->n_patterns)
7118 return 0;
7119
7120 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7121 if (!nl_pats)
7122 return -ENOBUFS;
7123
7124 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7125 nl_pat = nla_nest_start(msg, i + 1);
7126 if (!nl_pat)
7127 return -ENOBUFS;
7128 pat_len = rdev->wowlan->patterns[i].pattern_len;
7129 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7130 DIV_ROUND_UP(pat_len, 8),
7131 rdev->wowlan->patterns[i].mask) ||
7132 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7133 pat_len, rdev->wowlan->patterns[i].pattern) ||
7134 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7135 rdev->wowlan->patterns[i].pkt_offset))
7136 return -ENOBUFS;
7137 nla_nest_end(msg, nl_pat);
7138 }
7139 nla_nest_end(msg, nl_pats);
7140
7141 return 0;
7142}
7143
Johannes Berg2a0e0472013-01-23 22:57:40 +01007144static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7145 struct cfg80211_wowlan_tcp *tcp)
7146{
7147 struct nlattr *nl_tcp;
7148
7149 if (!tcp)
7150 return 0;
7151
7152 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7153 if (!nl_tcp)
7154 return -ENOBUFS;
7155
7156 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7157 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7158 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7159 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7160 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7161 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7162 tcp->payload_len, tcp->payload) ||
7163 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7164 tcp->data_interval) ||
7165 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7166 tcp->wake_len, tcp->wake_data) ||
7167 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7168 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7169 return -ENOBUFS;
7170
7171 if (tcp->payload_seq.len &&
7172 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7173 sizeof(tcp->payload_seq), &tcp->payload_seq))
7174 return -ENOBUFS;
7175
7176 if (tcp->payload_tok.len &&
7177 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7178 sizeof(tcp->payload_tok) + tcp->tokens_size,
7179 &tcp->payload_tok))
7180 return -ENOBUFS;
7181
7182 return 0;
7183}
7184
Johannes Bergff1b6e62011-05-04 15:37:28 +02007185static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7186{
7187 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7188 struct sk_buff *msg;
7189 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007190 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007191
Johannes Berg2a0e0472013-01-23 22:57:40 +01007192 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7193 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007194 return -EOPNOTSUPP;
7195
Johannes Berg2a0e0472013-01-23 22:57:40 +01007196 if (rdev->wowlan && rdev->wowlan->tcp) {
7197 /* adjust size to have room for all the data */
7198 size += rdev->wowlan->tcp->tokens_size +
7199 rdev->wowlan->tcp->payload_len +
7200 rdev->wowlan->tcp->wake_len +
7201 rdev->wowlan->tcp->wake_len / 8;
7202 }
7203
7204 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007205 if (!msg)
7206 return -ENOMEM;
7207
Eric W. Biederman15e47302012-09-07 20:12:54 +00007208 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007209 NL80211_CMD_GET_WOWLAN);
7210 if (!hdr)
7211 goto nla_put_failure;
7212
7213 if (rdev->wowlan) {
7214 struct nlattr *nl_wowlan;
7215
7216 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7217 if (!nl_wowlan)
7218 goto nla_put_failure;
7219
David S. Miller9360ffd2012-03-29 04:41:26 -04007220 if ((rdev->wowlan->any &&
7221 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7222 (rdev->wowlan->disconnect &&
7223 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7224 (rdev->wowlan->magic_pkt &&
7225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7226 (rdev->wowlan->gtk_rekey_failure &&
7227 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7228 (rdev->wowlan->eap_identity_req &&
7229 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7230 (rdev->wowlan->four_way_handshake &&
7231 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7232 (rdev->wowlan->rfkill_release &&
7233 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7234 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007235
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007236 if (nl80211_send_wowlan_patterns(msg, rdev))
7237 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007238
7239 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7240 goto nla_put_failure;
7241
Johannes Bergff1b6e62011-05-04 15:37:28 +02007242 nla_nest_end(msg, nl_wowlan);
7243 }
7244
7245 genlmsg_end(msg, hdr);
7246 return genlmsg_reply(msg, info);
7247
7248nla_put_failure:
7249 nlmsg_free(msg);
7250 return -ENOBUFS;
7251}
7252
Johannes Berg2a0e0472013-01-23 22:57:40 +01007253static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7254 struct nlattr *attr,
7255 struct cfg80211_wowlan *trig)
7256{
7257 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7258 struct cfg80211_wowlan_tcp *cfg;
7259 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7260 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7261 u32 size;
7262 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7263 int err, port;
7264
7265 if (!rdev->wiphy.wowlan.tcp)
7266 return -EINVAL;
7267
7268 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7269 nla_data(attr), nla_len(attr),
7270 nl80211_wowlan_tcp_policy);
7271 if (err)
7272 return err;
7273
7274 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7275 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7276 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7277 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7278 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7279 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7280 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7281 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7282 return -EINVAL;
7283
7284 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7285 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7286 return -EINVAL;
7287
7288 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7289 rdev->wiphy.wowlan.tcp->data_interval_max)
7290 return -EINVAL;
7291
7292 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7293 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7294 return -EINVAL;
7295
7296 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7297 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7298 return -EINVAL;
7299
7300 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7301 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7302
7303 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7304 tokens_size = tokln - sizeof(*tok);
7305
7306 if (!tok->len || tokens_size % tok->len)
7307 return -EINVAL;
7308 if (!rdev->wiphy.wowlan.tcp->tok)
7309 return -EINVAL;
7310 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7311 return -EINVAL;
7312 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7313 return -EINVAL;
7314 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7315 return -EINVAL;
7316 if (tok->offset + tok->len > data_size)
7317 return -EINVAL;
7318 }
7319
7320 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7321 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7322 if (!rdev->wiphy.wowlan.tcp->seq)
7323 return -EINVAL;
7324 if (seq->len == 0 || seq->len > 4)
7325 return -EINVAL;
7326 if (seq->len + seq->offset > data_size)
7327 return -EINVAL;
7328 }
7329
7330 size = sizeof(*cfg);
7331 size += data_size;
7332 size += wake_size + wake_mask_size;
7333 size += tokens_size;
7334
7335 cfg = kzalloc(size, GFP_KERNEL);
7336 if (!cfg)
7337 return -ENOMEM;
7338 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7339 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7340 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7341 ETH_ALEN);
7342 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7343 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7344 else
7345 port = 0;
7346#ifdef CONFIG_INET
7347 /* allocate a socket and port for it and use it */
7348 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7349 IPPROTO_TCP, &cfg->sock, 1);
7350 if (err) {
7351 kfree(cfg);
7352 return err;
7353 }
7354 if (inet_csk_get_port(cfg->sock->sk, port)) {
7355 sock_release(cfg->sock);
7356 kfree(cfg);
7357 return -EADDRINUSE;
7358 }
7359 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7360#else
7361 if (!port) {
7362 kfree(cfg);
7363 return -EINVAL;
7364 }
7365 cfg->src_port = port;
7366#endif
7367
7368 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7369 cfg->payload_len = data_size;
7370 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7371 memcpy((void *)cfg->payload,
7372 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7373 data_size);
7374 if (seq)
7375 cfg->payload_seq = *seq;
7376 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7377 cfg->wake_len = wake_size;
7378 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7379 memcpy((void *)cfg->wake_data,
7380 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7381 wake_size);
7382 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7383 data_size + wake_size;
7384 memcpy((void *)cfg->wake_mask,
7385 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7386 wake_mask_size);
7387 if (tok) {
7388 cfg->tokens_size = tokens_size;
7389 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7390 }
7391
7392 trig->tcp = cfg;
7393
7394 return 0;
7395}
7396
Johannes Bergff1b6e62011-05-04 15:37:28 +02007397static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7398{
7399 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7400 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007401 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007402 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007403 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7404 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007405 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007406
Johannes Berg2a0e0472013-01-23 22:57:40 +01007407 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7408 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007409 return -EOPNOTSUPP;
7410
Johannes Bergae33bd82012-07-12 16:25:02 +02007411 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7412 cfg80211_rdev_free_wowlan(rdev);
7413 rdev->wowlan = NULL;
7414 goto set_wakeup;
7415 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007416
7417 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7418 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7419 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7420 nl80211_wowlan_policy);
7421 if (err)
7422 return err;
7423
7424 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7425 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7426 return -EINVAL;
7427 new_triggers.any = true;
7428 }
7429
7430 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7431 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7432 return -EINVAL;
7433 new_triggers.disconnect = true;
7434 }
7435
7436 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7437 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7438 return -EINVAL;
7439 new_triggers.magic_pkt = true;
7440 }
7441
Johannes Berg77dbbb12011-07-13 10:48:55 +02007442 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7443 return -EINVAL;
7444
7445 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7446 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7447 return -EINVAL;
7448 new_triggers.gtk_rekey_failure = true;
7449 }
7450
7451 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7452 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7453 return -EINVAL;
7454 new_triggers.eap_identity_req = true;
7455 }
7456
7457 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7458 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7459 return -EINVAL;
7460 new_triggers.four_way_handshake = true;
7461 }
7462
7463 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7464 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7465 return -EINVAL;
7466 new_triggers.rfkill_release = true;
7467 }
7468
Johannes Bergff1b6e62011-05-04 15:37:28 +02007469 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7470 struct nlattr *pat;
7471 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007472 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007473 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7474
7475 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7476 rem)
7477 n_patterns++;
7478 if (n_patterns > wowlan->n_patterns)
7479 return -EINVAL;
7480
7481 new_triggers.patterns = kcalloc(n_patterns,
7482 sizeof(new_triggers.patterns[0]),
7483 GFP_KERNEL);
7484 if (!new_triggers.patterns)
7485 return -ENOMEM;
7486
7487 new_triggers.n_patterns = n_patterns;
7488 i = 0;
7489
7490 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7491 rem) {
7492 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7493 nla_data(pat), nla_len(pat), NULL);
7494 err = -EINVAL;
7495 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7496 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7497 goto error;
7498 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7499 mask_len = DIV_ROUND_UP(pat_len, 8);
7500 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7501 mask_len)
7502 goto error;
7503 if (pat_len > wowlan->pattern_max_len ||
7504 pat_len < wowlan->pattern_min_len)
7505 goto error;
7506
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007507 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7508 pkt_offset = 0;
7509 else
7510 pkt_offset = nla_get_u32(
7511 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7512 if (pkt_offset > wowlan->max_pkt_offset)
7513 goto error;
7514 new_triggers.patterns[i].pkt_offset = pkt_offset;
7515
Johannes Bergff1b6e62011-05-04 15:37:28 +02007516 new_triggers.patterns[i].mask =
7517 kmalloc(mask_len + pat_len, GFP_KERNEL);
7518 if (!new_triggers.patterns[i].mask) {
7519 err = -ENOMEM;
7520 goto error;
7521 }
7522 new_triggers.patterns[i].pattern =
7523 new_triggers.patterns[i].mask + mask_len;
7524 memcpy(new_triggers.patterns[i].mask,
7525 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7526 mask_len);
7527 new_triggers.patterns[i].pattern_len = pat_len;
7528 memcpy(new_triggers.patterns[i].pattern,
7529 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7530 pat_len);
7531 i++;
7532 }
7533 }
7534
Johannes Berg2a0e0472013-01-23 22:57:40 +01007535 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7536 err = nl80211_parse_wowlan_tcp(
7537 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7538 &new_triggers);
7539 if (err)
7540 goto error;
7541 }
7542
Johannes Bergae33bd82012-07-12 16:25:02 +02007543 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7544 if (!ntrig) {
7545 err = -ENOMEM;
7546 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007547 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007548 cfg80211_rdev_free_wowlan(rdev);
7549 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007550
Johannes Bergae33bd82012-07-12 16:25:02 +02007551 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007552 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007553 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007554
Johannes Bergff1b6e62011-05-04 15:37:28 +02007555 return 0;
7556 error:
7557 for (i = 0; i < new_triggers.n_patterns; i++)
7558 kfree(new_triggers.patterns[i].mask);
7559 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007560 if (new_triggers.tcp && new_triggers.tcp->sock)
7561 sock_release(new_triggers.tcp->sock);
7562 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007563 return err;
7564}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007565#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007566
Johannes Berge5497d72011-07-05 16:35:40 +02007567static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7568{
7569 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7570 struct net_device *dev = info->user_ptr[1];
7571 struct wireless_dev *wdev = dev->ieee80211_ptr;
7572 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7573 struct cfg80211_gtk_rekey_data rekey_data;
7574 int err;
7575
7576 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7577 return -EINVAL;
7578
7579 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7580 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7581 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7582 nl80211_rekey_policy);
7583 if (err)
7584 return err;
7585
7586 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7587 return -ERANGE;
7588 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7589 return -ERANGE;
7590 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7591 return -ERANGE;
7592
7593 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7594 NL80211_KEK_LEN);
7595 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7596 NL80211_KCK_LEN);
7597 memcpy(rekey_data.replay_ctr,
7598 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7599 NL80211_REPLAY_CTR_LEN);
7600
7601 wdev_lock(wdev);
7602 if (!wdev->current_bss) {
7603 err = -ENOTCONN;
7604 goto out;
7605 }
7606
7607 if (!rdev->ops->set_rekey_data) {
7608 err = -EOPNOTSUPP;
7609 goto out;
7610 }
7611
Hila Gonene35e4d22012-06-27 17:19:42 +03007612 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007613 out:
7614 wdev_unlock(wdev);
7615 return err;
7616}
7617
Johannes Berg28946da2011-11-04 11:18:12 +01007618static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7619 struct genl_info *info)
7620{
7621 struct net_device *dev = info->user_ptr[1];
7622 struct wireless_dev *wdev = dev->ieee80211_ptr;
7623
7624 if (wdev->iftype != NL80211_IFTYPE_AP &&
7625 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7626 return -EINVAL;
7627
Eric W. Biederman15e47302012-09-07 20:12:54 +00007628 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007629 return -EBUSY;
7630
Eric W. Biederman15e47302012-09-07 20:12:54 +00007631 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007632 return 0;
7633}
7634
Johannes Berg7f6cf312011-11-04 11:18:15 +01007635static int nl80211_probe_client(struct sk_buff *skb,
7636 struct genl_info *info)
7637{
7638 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7639 struct net_device *dev = info->user_ptr[1];
7640 struct wireless_dev *wdev = dev->ieee80211_ptr;
7641 struct sk_buff *msg;
7642 void *hdr;
7643 const u8 *addr;
7644 u64 cookie;
7645 int err;
7646
7647 if (wdev->iftype != NL80211_IFTYPE_AP &&
7648 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7649 return -EOPNOTSUPP;
7650
7651 if (!info->attrs[NL80211_ATTR_MAC])
7652 return -EINVAL;
7653
7654 if (!rdev->ops->probe_client)
7655 return -EOPNOTSUPP;
7656
7657 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7658 if (!msg)
7659 return -ENOMEM;
7660
Eric W. Biederman15e47302012-09-07 20:12:54 +00007661 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007662 NL80211_CMD_PROBE_CLIENT);
7663
7664 if (IS_ERR(hdr)) {
7665 err = PTR_ERR(hdr);
7666 goto free_msg;
7667 }
7668
7669 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7670
Hila Gonene35e4d22012-06-27 17:19:42 +03007671 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007672 if (err)
7673 goto free_msg;
7674
David S. Miller9360ffd2012-03-29 04:41:26 -04007675 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7676 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007677
7678 genlmsg_end(msg, hdr);
7679
7680 return genlmsg_reply(msg, info);
7681
7682 nla_put_failure:
7683 err = -ENOBUFS;
7684 free_msg:
7685 nlmsg_free(msg);
7686 return err;
7687}
7688
Johannes Berg5e760232011-11-04 11:18:17 +01007689static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7690{
7691 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007692 struct cfg80211_beacon_registration *reg, *nreg;
7693 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007694
7695 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7696 return -EOPNOTSUPP;
7697
Ben Greear37c73b52012-10-26 14:49:25 -07007698 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7699 if (!nreg)
7700 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007701
Ben Greear37c73b52012-10-26 14:49:25 -07007702 /* First, check if already registered. */
7703 spin_lock_bh(&rdev->beacon_registrations_lock);
7704 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7705 if (reg->nlportid == info->snd_portid) {
7706 rv = -EALREADY;
7707 goto out_err;
7708 }
7709 }
7710 /* Add it to the list */
7711 nreg->nlportid = info->snd_portid;
7712 list_add(&nreg->list, &rdev->beacon_registrations);
7713
7714 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007715
7716 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007717out_err:
7718 spin_unlock_bh(&rdev->beacon_registrations_lock);
7719 kfree(nreg);
7720 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007721}
7722
Johannes Berg98104fde2012-06-16 00:19:54 +02007723static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7724{
7725 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7726 struct wireless_dev *wdev = info->user_ptr[1];
7727 int err;
7728
7729 if (!rdev->ops->start_p2p_device)
7730 return -EOPNOTSUPP;
7731
7732 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7733 return -EOPNOTSUPP;
7734
7735 if (wdev->p2p_started)
7736 return 0;
7737
7738 mutex_lock(&rdev->devlist_mtx);
7739 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7740 mutex_unlock(&rdev->devlist_mtx);
7741 if (err)
7742 return err;
7743
Johannes Bergeeb126e2012-10-23 15:16:50 +02007744 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007745 if (err)
7746 return err;
7747
7748 wdev->p2p_started = true;
7749 mutex_lock(&rdev->devlist_mtx);
7750 rdev->opencount++;
7751 mutex_unlock(&rdev->devlist_mtx);
7752
7753 return 0;
7754}
7755
7756static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7757{
7758 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7759 struct wireless_dev *wdev = info->user_ptr[1];
7760
7761 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7762 return -EOPNOTSUPP;
7763
7764 if (!rdev->ops->stop_p2p_device)
7765 return -EOPNOTSUPP;
7766
7767 if (!wdev->p2p_started)
7768 return 0;
7769
Johannes Bergeeb126e2012-10-23 15:16:50 +02007770 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007771 wdev->p2p_started = false;
7772
7773 mutex_lock(&rdev->devlist_mtx);
7774 rdev->opencount--;
7775 mutex_unlock(&rdev->devlist_mtx);
7776
7777 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7778 rdev->scan_req->aborted = true;
7779 ___cfg80211_scan_done(rdev, true);
7780 }
7781
7782 return 0;
7783}
7784
Johannes Berg4c476992010-10-04 21:36:35 +02007785#define NL80211_FLAG_NEED_WIPHY 0x01
7786#define NL80211_FLAG_NEED_NETDEV 0x02
7787#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007788#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7789#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7790 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007791#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007792/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007793#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7794 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007795
7796static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7797 struct genl_info *info)
7798{
7799 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007800 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007801 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007802 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7803
7804 if (rtnl)
7805 rtnl_lock();
7806
7807 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007808 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007809 if (IS_ERR(rdev)) {
7810 if (rtnl)
7811 rtnl_unlock();
7812 return PTR_ERR(rdev);
7813 }
7814 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007815 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7816 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007817 mutex_lock(&cfg80211_mutex);
7818 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7819 info->attrs);
7820 if (IS_ERR(wdev)) {
7821 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007822 if (rtnl)
7823 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007824 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007825 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007826
Johannes Berg89a54e42012-06-15 14:33:17 +02007827 dev = wdev->netdev;
7828 rdev = wiphy_to_dev(wdev->wiphy);
7829
Johannes Berg1bf614e2012-06-15 15:23:36 +02007830 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7831 if (!dev) {
7832 mutex_unlock(&cfg80211_mutex);
7833 if (rtnl)
7834 rtnl_unlock();
7835 return -EINVAL;
7836 }
7837
7838 info->user_ptr[1] = dev;
7839 } else {
7840 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007841 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007842
Johannes Berg1bf614e2012-06-15 15:23:36 +02007843 if (dev) {
7844 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7845 !netif_running(dev)) {
7846 mutex_unlock(&cfg80211_mutex);
7847 if (rtnl)
7848 rtnl_unlock();
7849 return -ENETDOWN;
7850 }
7851
7852 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007853 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7854 if (!wdev->p2p_started) {
7855 mutex_unlock(&cfg80211_mutex);
7856 if (rtnl)
7857 rtnl_unlock();
7858 return -ENETDOWN;
7859 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007860 }
7861
Johannes Berg89a54e42012-06-15 14:33:17 +02007862 cfg80211_lock_rdev(rdev);
7863
7864 mutex_unlock(&cfg80211_mutex);
7865
Johannes Berg4c476992010-10-04 21:36:35 +02007866 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007867 }
7868
7869 return 0;
7870}
7871
7872static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7873 struct genl_info *info)
7874{
7875 if (info->user_ptr[0])
7876 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007877 if (info->user_ptr[1]) {
7878 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7879 struct wireless_dev *wdev = info->user_ptr[1];
7880
7881 if (wdev->netdev)
7882 dev_put(wdev->netdev);
7883 } else {
7884 dev_put(info->user_ptr[1]);
7885 }
7886 }
Johannes Berg4c476992010-10-04 21:36:35 +02007887 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7888 rtnl_unlock();
7889}
7890
Johannes Berg55682962007-09-20 13:09:35 -04007891static struct genl_ops nl80211_ops[] = {
7892 {
7893 .cmd = NL80211_CMD_GET_WIPHY,
7894 .doit = nl80211_get_wiphy,
7895 .dumpit = nl80211_dump_wiphy,
7896 .policy = nl80211_policy,
7897 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007898 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007899 },
7900 {
7901 .cmd = NL80211_CMD_SET_WIPHY,
7902 .doit = nl80211_set_wiphy,
7903 .policy = nl80211_policy,
7904 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007905 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007906 },
7907 {
7908 .cmd = NL80211_CMD_GET_INTERFACE,
7909 .doit = nl80211_get_interface,
7910 .dumpit = nl80211_dump_interface,
7911 .policy = nl80211_policy,
7912 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007913 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007914 },
7915 {
7916 .cmd = NL80211_CMD_SET_INTERFACE,
7917 .doit = nl80211_set_interface,
7918 .policy = nl80211_policy,
7919 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007920 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7921 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007922 },
7923 {
7924 .cmd = NL80211_CMD_NEW_INTERFACE,
7925 .doit = nl80211_new_interface,
7926 .policy = nl80211_policy,
7927 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007928 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7929 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007930 },
7931 {
7932 .cmd = NL80211_CMD_DEL_INTERFACE,
7933 .doit = nl80211_del_interface,
7934 .policy = nl80211_policy,
7935 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007936 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007937 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007938 },
Johannes Berg41ade002007-12-19 02:03:29 +01007939 {
7940 .cmd = NL80211_CMD_GET_KEY,
7941 .doit = nl80211_get_key,
7942 .policy = nl80211_policy,
7943 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007944 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007945 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007946 },
7947 {
7948 .cmd = NL80211_CMD_SET_KEY,
7949 .doit = nl80211_set_key,
7950 .policy = nl80211_policy,
7951 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007952 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007953 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007954 },
7955 {
7956 .cmd = NL80211_CMD_NEW_KEY,
7957 .doit = nl80211_new_key,
7958 .policy = nl80211_policy,
7959 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007960 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007961 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007962 },
7963 {
7964 .cmd = NL80211_CMD_DEL_KEY,
7965 .doit = nl80211_del_key,
7966 .policy = nl80211_policy,
7967 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007968 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007969 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007970 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007971 {
7972 .cmd = NL80211_CMD_SET_BEACON,
7973 .policy = nl80211_policy,
7974 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007975 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007976 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007977 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007978 },
7979 {
Johannes Berg88600202012-02-13 15:17:18 +01007980 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007981 .policy = nl80211_policy,
7982 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007983 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007984 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007985 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007986 },
7987 {
Johannes Berg88600202012-02-13 15:17:18 +01007988 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007989 .policy = nl80211_policy,
7990 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007991 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007992 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007993 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007994 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007995 {
7996 .cmd = NL80211_CMD_GET_STATION,
7997 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007998 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007999 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008000 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8001 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008002 },
8003 {
8004 .cmd = NL80211_CMD_SET_STATION,
8005 .doit = nl80211_set_station,
8006 .policy = nl80211_policy,
8007 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008008 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008009 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008010 },
8011 {
8012 .cmd = NL80211_CMD_NEW_STATION,
8013 .doit = nl80211_new_station,
8014 .policy = nl80211_policy,
8015 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008016 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008017 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008018 },
8019 {
8020 .cmd = NL80211_CMD_DEL_STATION,
8021 .doit = nl80211_del_station,
8022 .policy = nl80211_policy,
8023 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008024 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008025 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008026 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008027 {
8028 .cmd = NL80211_CMD_GET_MPATH,
8029 .doit = nl80211_get_mpath,
8030 .dumpit = nl80211_dump_mpath,
8031 .policy = nl80211_policy,
8032 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008033 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008034 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008035 },
8036 {
8037 .cmd = NL80211_CMD_SET_MPATH,
8038 .doit = nl80211_set_mpath,
8039 .policy = nl80211_policy,
8040 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008041 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008042 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008043 },
8044 {
8045 .cmd = NL80211_CMD_NEW_MPATH,
8046 .doit = nl80211_new_mpath,
8047 .policy = nl80211_policy,
8048 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008049 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008050 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008051 },
8052 {
8053 .cmd = NL80211_CMD_DEL_MPATH,
8054 .doit = nl80211_del_mpath,
8055 .policy = nl80211_policy,
8056 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008057 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008058 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008059 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008060 {
8061 .cmd = NL80211_CMD_SET_BSS,
8062 .doit = nl80211_set_bss,
8063 .policy = nl80211_policy,
8064 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008065 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008066 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008067 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008068 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008069 .cmd = NL80211_CMD_GET_REG,
8070 .doit = nl80211_get_reg,
8071 .policy = nl80211_policy,
8072 /* can be retrieved by unprivileged users */
8073 },
8074 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008075 .cmd = NL80211_CMD_SET_REG,
8076 .doit = nl80211_set_reg,
8077 .policy = nl80211_policy,
8078 .flags = GENL_ADMIN_PERM,
8079 },
8080 {
8081 .cmd = NL80211_CMD_REQ_SET_REG,
8082 .doit = nl80211_req_set_reg,
8083 .policy = nl80211_policy,
8084 .flags = GENL_ADMIN_PERM,
8085 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008086 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008087 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8088 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008089 .policy = nl80211_policy,
8090 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008091 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008092 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008093 },
8094 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008095 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8096 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008097 .policy = nl80211_policy,
8098 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008099 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008100 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008101 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008102 {
Johannes Berg2a519312009-02-10 21:25:55 +01008103 .cmd = NL80211_CMD_TRIGGER_SCAN,
8104 .doit = nl80211_trigger_scan,
8105 .policy = nl80211_policy,
8106 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008107 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008108 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008109 },
8110 {
8111 .cmd = NL80211_CMD_GET_SCAN,
8112 .policy = nl80211_policy,
8113 .dumpit = nl80211_dump_scan,
8114 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008115 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008116 .cmd = NL80211_CMD_START_SCHED_SCAN,
8117 .doit = nl80211_start_sched_scan,
8118 .policy = nl80211_policy,
8119 .flags = GENL_ADMIN_PERM,
8120 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8121 NL80211_FLAG_NEED_RTNL,
8122 },
8123 {
8124 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8125 .doit = nl80211_stop_sched_scan,
8126 .policy = nl80211_policy,
8127 .flags = GENL_ADMIN_PERM,
8128 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8129 NL80211_FLAG_NEED_RTNL,
8130 },
8131 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008132 .cmd = NL80211_CMD_AUTHENTICATE,
8133 .doit = nl80211_authenticate,
8134 .policy = nl80211_policy,
8135 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008136 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008137 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008138 },
8139 {
8140 .cmd = NL80211_CMD_ASSOCIATE,
8141 .doit = nl80211_associate,
8142 .policy = nl80211_policy,
8143 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008144 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008145 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008146 },
8147 {
8148 .cmd = NL80211_CMD_DEAUTHENTICATE,
8149 .doit = nl80211_deauthenticate,
8150 .policy = nl80211_policy,
8151 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008152 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008153 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008154 },
8155 {
8156 .cmd = NL80211_CMD_DISASSOCIATE,
8157 .doit = nl80211_disassociate,
8158 .policy = nl80211_policy,
8159 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008160 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008161 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008162 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008163 {
8164 .cmd = NL80211_CMD_JOIN_IBSS,
8165 .doit = nl80211_join_ibss,
8166 .policy = nl80211_policy,
8167 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008168 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008169 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008170 },
8171 {
8172 .cmd = NL80211_CMD_LEAVE_IBSS,
8173 .doit = nl80211_leave_ibss,
8174 .policy = nl80211_policy,
8175 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008176 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008177 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008178 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008179#ifdef CONFIG_NL80211_TESTMODE
8180 {
8181 .cmd = NL80211_CMD_TESTMODE,
8182 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008183 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008184 .policy = nl80211_policy,
8185 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008186 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8187 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008188 },
8189#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008190 {
8191 .cmd = NL80211_CMD_CONNECT,
8192 .doit = nl80211_connect,
8193 .policy = nl80211_policy,
8194 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008195 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008196 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008197 },
8198 {
8199 .cmd = NL80211_CMD_DISCONNECT,
8200 .doit = nl80211_disconnect,
8201 .policy = nl80211_policy,
8202 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008203 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008204 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008205 },
Johannes Berg463d0182009-07-14 00:33:35 +02008206 {
8207 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8208 .doit = nl80211_wiphy_netns,
8209 .policy = nl80211_policy,
8210 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008211 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8212 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008213 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008214 {
8215 .cmd = NL80211_CMD_GET_SURVEY,
8216 .policy = nl80211_policy,
8217 .dumpit = nl80211_dump_survey,
8218 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008219 {
8220 .cmd = NL80211_CMD_SET_PMKSA,
8221 .doit = nl80211_setdel_pmksa,
8222 .policy = nl80211_policy,
8223 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008224 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008225 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008226 },
8227 {
8228 .cmd = NL80211_CMD_DEL_PMKSA,
8229 .doit = nl80211_setdel_pmksa,
8230 .policy = nl80211_policy,
8231 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008232 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008233 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008234 },
8235 {
8236 .cmd = NL80211_CMD_FLUSH_PMKSA,
8237 .doit = nl80211_flush_pmksa,
8238 .policy = nl80211_policy,
8239 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008240 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008241 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008242 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008243 {
8244 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8245 .doit = nl80211_remain_on_channel,
8246 .policy = nl80211_policy,
8247 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008248 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008249 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008250 },
8251 {
8252 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8253 .doit = nl80211_cancel_remain_on_channel,
8254 .policy = nl80211_policy,
8255 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008256 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008257 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008258 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008259 {
8260 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8261 .doit = nl80211_set_tx_bitrate_mask,
8262 .policy = nl80211_policy,
8263 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008264 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8265 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008266 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008267 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008268 .cmd = NL80211_CMD_REGISTER_FRAME,
8269 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008270 .policy = nl80211_policy,
8271 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008272 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008273 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008274 },
8275 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008276 .cmd = NL80211_CMD_FRAME,
8277 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008278 .policy = nl80211_policy,
8279 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008280 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008281 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008282 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008283 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008284 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8285 .doit = nl80211_tx_mgmt_cancel_wait,
8286 .policy = nl80211_policy,
8287 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008288 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008289 NL80211_FLAG_NEED_RTNL,
8290 },
8291 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008292 .cmd = NL80211_CMD_SET_POWER_SAVE,
8293 .doit = nl80211_set_power_save,
8294 .policy = nl80211_policy,
8295 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008296 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8297 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008298 },
8299 {
8300 .cmd = NL80211_CMD_GET_POWER_SAVE,
8301 .doit = nl80211_get_power_save,
8302 .policy = nl80211_policy,
8303 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008304 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8305 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008306 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008307 {
8308 .cmd = NL80211_CMD_SET_CQM,
8309 .doit = nl80211_set_cqm,
8310 .policy = nl80211_policy,
8311 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008312 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8313 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008314 },
Johannes Bergf444de02010-05-05 15:25:02 +02008315 {
8316 .cmd = NL80211_CMD_SET_CHANNEL,
8317 .doit = nl80211_set_channel,
8318 .policy = nl80211_policy,
8319 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008320 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8321 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008322 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008323 {
8324 .cmd = NL80211_CMD_SET_WDS_PEER,
8325 .doit = nl80211_set_wds_peer,
8326 .policy = nl80211_policy,
8327 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008328 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8329 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008330 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008331 {
8332 .cmd = NL80211_CMD_JOIN_MESH,
8333 .doit = nl80211_join_mesh,
8334 .policy = nl80211_policy,
8335 .flags = GENL_ADMIN_PERM,
8336 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8337 NL80211_FLAG_NEED_RTNL,
8338 },
8339 {
8340 .cmd = NL80211_CMD_LEAVE_MESH,
8341 .doit = nl80211_leave_mesh,
8342 .policy = nl80211_policy,
8343 .flags = GENL_ADMIN_PERM,
8344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8345 NL80211_FLAG_NEED_RTNL,
8346 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008347#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008348 {
8349 .cmd = NL80211_CMD_GET_WOWLAN,
8350 .doit = nl80211_get_wowlan,
8351 .policy = nl80211_policy,
8352 /* can be retrieved by unprivileged users */
8353 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8354 NL80211_FLAG_NEED_RTNL,
8355 },
8356 {
8357 .cmd = NL80211_CMD_SET_WOWLAN,
8358 .doit = nl80211_set_wowlan,
8359 .policy = nl80211_policy,
8360 .flags = GENL_ADMIN_PERM,
8361 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8362 NL80211_FLAG_NEED_RTNL,
8363 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008364#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008365 {
8366 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8367 .doit = nl80211_set_rekey_data,
8368 .policy = nl80211_policy,
8369 .flags = GENL_ADMIN_PERM,
8370 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8371 NL80211_FLAG_NEED_RTNL,
8372 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008373 {
8374 .cmd = NL80211_CMD_TDLS_MGMT,
8375 .doit = nl80211_tdls_mgmt,
8376 .policy = nl80211_policy,
8377 .flags = GENL_ADMIN_PERM,
8378 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8379 NL80211_FLAG_NEED_RTNL,
8380 },
8381 {
8382 .cmd = NL80211_CMD_TDLS_OPER,
8383 .doit = nl80211_tdls_oper,
8384 .policy = nl80211_policy,
8385 .flags = GENL_ADMIN_PERM,
8386 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8387 NL80211_FLAG_NEED_RTNL,
8388 },
Johannes Berg28946da2011-11-04 11:18:12 +01008389 {
8390 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8391 .doit = nl80211_register_unexpected_frame,
8392 .policy = nl80211_policy,
8393 .flags = GENL_ADMIN_PERM,
8394 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8395 NL80211_FLAG_NEED_RTNL,
8396 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008397 {
8398 .cmd = NL80211_CMD_PROBE_CLIENT,
8399 .doit = nl80211_probe_client,
8400 .policy = nl80211_policy,
8401 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008402 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008403 NL80211_FLAG_NEED_RTNL,
8404 },
Johannes Berg5e760232011-11-04 11:18:17 +01008405 {
8406 .cmd = NL80211_CMD_REGISTER_BEACONS,
8407 .doit = nl80211_register_beacons,
8408 .policy = nl80211_policy,
8409 .flags = GENL_ADMIN_PERM,
8410 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8411 NL80211_FLAG_NEED_RTNL,
8412 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008413 {
8414 .cmd = NL80211_CMD_SET_NOACK_MAP,
8415 .doit = nl80211_set_noack_map,
8416 .policy = nl80211_policy,
8417 .flags = GENL_ADMIN_PERM,
8418 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8419 NL80211_FLAG_NEED_RTNL,
8420 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008421 {
8422 .cmd = NL80211_CMD_START_P2P_DEVICE,
8423 .doit = nl80211_start_p2p_device,
8424 .policy = nl80211_policy,
8425 .flags = GENL_ADMIN_PERM,
8426 .internal_flags = NL80211_FLAG_NEED_WDEV |
8427 NL80211_FLAG_NEED_RTNL,
8428 },
8429 {
8430 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8431 .doit = nl80211_stop_p2p_device,
8432 .policy = nl80211_policy,
8433 .flags = GENL_ADMIN_PERM,
8434 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8435 NL80211_FLAG_NEED_RTNL,
8436 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008437 {
8438 .cmd = NL80211_CMD_SET_MCAST_RATE,
8439 .doit = nl80211_set_mcast_rate,
8440 .policy = nl80211_policy,
8441 .flags = GENL_ADMIN_PERM,
8442 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8443 NL80211_FLAG_NEED_RTNL,
8444 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308445 {
8446 .cmd = NL80211_CMD_SET_MAC_ACL,
8447 .doit = nl80211_set_mac_acl,
8448 .policy = nl80211_policy,
8449 .flags = GENL_ADMIN_PERM,
8450 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8451 NL80211_FLAG_NEED_RTNL,
8452 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008453 {
8454 .cmd = NL80211_CMD_RADAR_DETECT,
8455 .doit = nl80211_start_radar_detection,
8456 .policy = nl80211_policy,
8457 .flags = GENL_ADMIN_PERM,
8458 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8459 NL80211_FLAG_NEED_RTNL,
8460 },
Johannes Berg55682962007-09-20 13:09:35 -04008461};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008462
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008463static struct genl_multicast_group nl80211_mlme_mcgrp = {
8464 .name = "mlme",
8465};
Johannes Berg55682962007-09-20 13:09:35 -04008466
8467/* multicast groups */
8468static struct genl_multicast_group nl80211_config_mcgrp = {
8469 .name = "config",
8470};
Johannes Berg2a519312009-02-10 21:25:55 +01008471static struct genl_multicast_group nl80211_scan_mcgrp = {
8472 .name = "scan",
8473};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008474static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8475 .name = "regulatory",
8476};
Johannes Berg55682962007-09-20 13:09:35 -04008477
8478/* notification functions */
8479
8480void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8481{
8482 struct sk_buff *msg;
8483
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008484 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008485 if (!msg)
8486 return;
8487
8488 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8489 nlmsg_free(msg);
8490 return;
8491 }
8492
Johannes Berg463d0182009-07-14 00:33:35 +02008493 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8494 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008495}
8496
Johannes Berg362a4152009-05-24 16:43:15 +02008497static int nl80211_add_scan_req(struct sk_buff *msg,
8498 struct cfg80211_registered_device *rdev)
8499{
8500 struct cfg80211_scan_request *req = rdev->scan_req;
8501 struct nlattr *nest;
8502 int i;
8503
Johannes Berg667503dd2009-07-07 03:56:11 +02008504 ASSERT_RDEV_LOCK(rdev);
8505
Johannes Berg362a4152009-05-24 16:43:15 +02008506 if (WARN_ON(!req))
8507 return 0;
8508
8509 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8510 if (!nest)
8511 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008512 for (i = 0; i < req->n_ssids; i++) {
8513 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8514 goto nla_put_failure;
8515 }
Johannes Berg362a4152009-05-24 16:43:15 +02008516 nla_nest_end(msg, nest);
8517
8518 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8519 if (!nest)
8520 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008521 for (i = 0; i < req->n_channels; i++) {
8522 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8523 goto nla_put_failure;
8524 }
Johannes Berg362a4152009-05-24 16:43:15 +02008525 nla_nest_end(msg, nest);
8526
David S. Miller9360ffd2012-03-29 04:41:26 -04008527 if (req->ie &&
8528 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8529 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008530
Sam Lefflered4737712012-10-11 21:03:31 -07008531 if (req->flags)
8532 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8533
Johannes Berg362a4152009-05-24 16:43:15 +02008534 return 0;
8535 nla_put_failure:
8536 return -ENOBUFS;
8537}
8538
Johannes Berga538e2d2009-06-16 19:56:42 +02008539static int nl80211_send_scan_msg(struct sk_buff *msg,
8540 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008541 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008542 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008543 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008544{
8545 void *hdr;
8546
Eric W. Biederman15e47302012-09-07 20:12:54 +00008547 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008548 if (!hdr)
8549 return -1;
8550
David S. Miller9360ffd2012-03-29 04:41:26 -04008551 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008552 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8553 wdev->netdev->ifindex)) ||
8554 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008555 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008556
Johannes Berg362a4152009-05-24 16:43:15 +02008557 /* ignore errors and send incomplete event anyway */
8558 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008559
8560 return genlmsg_end(msg, hdr);
8561
8562 nla_put_failure:
8563 genlmsg_cancel(msg, hdr);
8564 return -EMSGSIZE;
8565}
8566
Luciano Coelho807f8a82011-05-11 17:09:35 +03008567static int
8568nl80211_send_sched_scan_msg(struct sk_buff *msg,
8569 struct cfg80211_registered_device *rdev,
8570 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008571 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008572{
8573 void *hdr;
8574
Eric W. Biederman15e47302012-09-07 20:12:54 +00008575 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008576 if (!hdr)
8577 return -1;
8578
David S. Miller9360ffd2012-03-29 04:41:26 -04008579 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8580 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8581 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008582
8583 return genlmsg_end(msg, hdr);
8584
8585 nla_put_failure:
8586 genlmsg_cancel(msg, hdr);
8587 return -EMSGSIZE;
8588}
8589
Johannes Berga538e2d2009-06-16 19:56:42 +02008590void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008591 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008592{
8593 struct sk_buff *msg;
8594
Thomas Graf58050fc2012-06-28 03:57:45 +00008595 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008596 if (!msg)
8597 return;
8598
Johannes Bergfd014282012-06-18 19:17:03 +02008599 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008600 NL80211_CMD_TRIGGER_SCAN) < 0) {
8601 nlmsg_free(msg);
8602 return;
8603 }
8604
Johannes Berg463d0182009-07-14 00:33:35 +02008605 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8606 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008607}
8608
Johannes Berg2a519312009-02-10 21:25:55 +01008609void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008610 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008611{
8612 struct sk_buff *msg;
8613
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008614 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008615 if (!msg)
8616 return;
8617
Johannes Bergfd014282012-06-18 19:17:03 +02008618 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008619 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008620 nlmsg_free(msg);
8621 return;
8622 }
8623
Johannes Berg463d0182009-07-14 00:33:35 +02008624 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8625 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008626}
8627
8628void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008629 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008630{
8631 struct sk_buff *msg;
8632
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008633 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008634 if (!msg)
8635 return;
8636
Johannes Bergfd014282012-06-18 19:17:03 +02008637 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008638 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008639 nlmsg_free(msg);
8640 return;
8641 }
8642
Johannes Berg463d0182009-07-14 00:33:35 +02008643 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8644 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008645}
8646
Luciano Coelho807f8a82011-05-11 17:09:35 +03008647void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8648 struct net_device *netdev)
8649{
8650 struct sk_buff *msg;
8651
8652 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8653 if (!msg)
8654 return;
8655
8656 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8657 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8658 nlmsg_free(msg);
8659 return;
8660 }
8661
8662 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8663 nl80211_scan_mcgrp.id, GFP_KERNEL);
8664}
8665
8666void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8667 struct net_device *netdev, u32 cmd)
8668{
8669 struct sk_buff *msg;
8670
Thomas Graf58050fc2012-06-28 03:57:45 +00008671 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008672 if (!msg)
8673 return;
8674
8675 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8676 nlmsg_free(msg);
8677 return;
8678 }
8679
8680 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8681 nl80211_scan_mcgrp.id, GFP_KERNEL);
8682}
8683
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008684/*
8685 * This can happen on global regulatory changes or device specific settings
8686 * based on custom world regulatory domains.
8687 */
8688void nl80211_send_reg_change_event(struct regulatory_request *request)
8689{
8690 struct sk_buff *msg;
8691 void *hdr;
8692
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008693 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008694 if (!msg)
8695 return;
8696
8697 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8698 if (!hdr) {
8699 nlmsg_free(msg);
8700 return;
8701 }
8702
8703 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008704 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8705 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008706
David S. Miller9360ffd2012-03-29 04:41:26 -04008707 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8708 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8709 NL80211_REGDOM_TYPE_WORLD))
8710 goto nla_put_failure;
8711 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8712 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8713 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8714 goto nla_put_failure;
8715 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8716 request->intersect) {
8717 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8718 NL80211_REGDOM_TYPE_INTERSECTION))
8719 goto nla_put_failure;
8720 } else {
8721 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8722 NL80211_REGDOM_TYPE_COUNTRY) ||
8723 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8724 request->alpha2))
8725 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008726 }
8727
Johannes Bergf4173762012-12-03 18:23:37 +01008728 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008729 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8730 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008731
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008732 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008733
Johannes Bergbc43b282009-07-25 10:54:13 +02008734 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008735 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008736 GFP_ATOMIC);
8737 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008738
8739 return;
8740
8741nla_put_failure:
8742 genlmsg_cancel(msg, hdr);
8743 nlmsg_free(msg);
8744}
8745
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008746static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8747 struct net_device *netdev,
8748 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008749 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008750{
8751 struct sk_buff *msg;
8752 void *hdr;
8753
Johannes Berge6d6e342009-07-01 21:26:47 +02008754 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008755 if (!msg)
8756 return;
8757
8758 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8759 if (!hdr) {
8760 nlmsg_free(msg);
8761 return;
8762 }
8763
David S. Miller9360ffd2012-03-29 04:41:26 -04008764 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8765 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8766 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8767 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008768
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008769 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008770
Johannes Berg463d0182009-07-14 00:33:35 +02008771 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8772 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008773 return;
8774
8775 nla_put_failure:
8776 genlmsg_cancel(msg, hdr);
8777 nlmsg_free(msg);
8778}
8779
8780void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008781 struct net_device *netdev, const u8 *buf,
8782 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008783{
8784 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008785 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008786}
8787
8788void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8789 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008790 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008791{
Johannes Berge6d6e342009-07-01 21:26:47 +02008792 nl80211_send_mlme_event(rdev, netdev, buf, len,
8793 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008794}
8795
Jouni Malinen53b46b82009-03-27 20:53:56 +02008796void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008797 struct net_device *netdev, const u8 *buf,
8798 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008799{
8800 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008801 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008802}
8803
Jouni Malinen53b46b82009-03-27 20:53:56 +02008804void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8805 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008806 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008807{
8808 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008809 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008810}
8811
Jouni Malinencf4e5942010-12-16 00:52:40 +02008812void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8813 struct net_device *netdev, const u8 *buf,
8814 size_t len, gfp_t gfp)
8815{
8816 nl80211_send_mlme_event(rdev, netdev, buf, len,
8817 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8818}
8819
8820void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8821 struct net_device *netdev, const u8 *buf,
8822 size_t len, gfp_t gfp)
8823{
8824 nl80211_send_mlme_event(rdev, netdev, buf, len,
8825 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8826}
8827
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008828static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8829 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008830 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008831{
8832 struct sk_buff *msg;
8833 void *hdr;
8834
Johannes Berge6d6e342009-07-01 21:26:47 +02008835 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008836 if (!msg)
8837 return;
8838
8839 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8840 if (!hdr) {
8841 nlmsg_free(msg);
8842 return;
8843 }
8844
David S. Miller9360ffd2012-03-29 04:41:26 -04008845 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8846 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8847 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8848 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8849 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008850
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008851 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008852
Johannes Berg463d0182009-07-14 00:33:35 +02008853 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8854 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008855 return;
8856
8857 nla_put_failure:
8858 genlmsg_cancel(msg, hdr);
8859 nlmsg_free(msg);
8860}
8861
8862void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008863 struct net_device *netdev, const u8 *addr,
8864 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008865{
8866 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008867 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008868}
8869
8870void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008871 struct net_device *netdev, const u8 *addr,
8872 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008873{
Johannes Berge6d6e342009-07-01 21:26:47 +02008874 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8875 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008876}
8877
Samuel Ortizb23aa672009-07-01 21:26:54 +02008878void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8879 struct net_device *netdev, const u8 *bssid,
8880 const u8 *req_ie, size_t req_ie_len,
8881 const u8 *resp_ie, size_t resp_ie_len,
8882 u16 status, gfp_t gfp)
8883{
8884 struct sk_buff *msg;
8885 void *hdr;
8886
Thomas Graf58050fc2012-06-28 03:57:45 +00008887 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008888 if (!msg)
8889 return;
8890
8891 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8892 if (!hdr) {
8893 nlmsg_free(msg);
8894 return;
8895 }
8896
David S. Miller9360ffd2012-03-29 04:41:26 -04008897 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8898 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8899 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8900 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8901 (req_ie &&
8902 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8903 (resp_ie &&
8904 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8905 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008906
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008907 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008908
Johannes Berg463d0182009-07-14 00:33:35 +02008909 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8910 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008911 return;
8912
8913 nla_put_failure:
8914 genlmsg_cancel(msg, hdr);
8915 nlmsg_free(msg);
8916
8917}
8918
8919void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8920 struct net_device *netdev, const u8 *bssid,
8921 const u8 *req_ie, size_t req_ie_len,
8922 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8923{
8924 struct sk_buff *msg;
8925 void *hdr;
8926
Thomas Graf58050fc2012-06-28 03:57:45 +00008927 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008928 if (!msg)
8929 return;
8930
8931 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8932 if (!hdr) {
8933 nlmsg_free(msg);
8934 return;
8935 }
8936
David S. Miller9360ffd2012-03-29 04:41:26 -04008937 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8938 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8939 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8940 (req_ie &&
8941 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8942 (resp_ie &&
8943 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8944 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008945
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008946 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008947
Johannes Berg463d0182009-07-14 00:33:35 +02008948 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8949 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008950 return;
8951
8952 nla_put_failure:
8953 genlmsg_cancel(msg, hdr);
8954 nlmsg_free(msg);
8955
8956}
8957
8958void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8959 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008960 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008961{
8962 struct sk_buff *msg;
8963 void *hdr;
8964
Thomas Graf58050fc2012-06-28 03:57:45 +00008965 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008966 if (!msg)
8967 return;
8968
8969 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8970 if (!hdr) {
8971 nlmsg_free(msg);
8972 return;
8973 }
8974
David S. Miller9360ffd2012-03-29 04:41:26 -04008975 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8976 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8977 (from_ap && reason &&
8978 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8979 (from_ap &&
8980 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8981 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8982 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008983
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008984 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008985
Johannes Berg463d0182009-07-14 00:33:35 +02008986 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8987 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008988 return;
8989
8990 nla_put_failure:
8991 genlmsg_cancel(msg, hdr);
8992 nlmsg_free(msg);
8993
8994}
8995
Johannes Berg04a773a2009-04-19 21:24:32 +02008996void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8997 struct net_device *netdev, const u8 *bssid,
8998 gfp_t gfp)
8999{
9000 struct sk_buff *msg;
9001 void *hdr;
9002
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009003 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009004 if (!msg)
9005 return;
9006
9007 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9008 if (!hdr) {
9009 nlmsg_free(msg);
9010 return;
9011 }
9012
David S. Miller9360ffd2012-03-29 04:41:26 -04009013 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9014 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9015 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9016 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009017
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009018 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009019
Johannes Berg463d0182009-07-14 00:33:35 +02009020 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9021 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009022 return;
9023
9024 nla_put_failure:
9025 genlmsg_cancel(msg, hdr);
9026 nlmsg_free(msg);
9027}
9028
Javier Cardonac93b5e72011-04-07 15:08:34 -07009029void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
9030 struct net_device *netdev,
9031 const u8 *macaddr, const u8* ie, u8 ie_len,
9032 gfp_t gfp)
9033{
9034 struct sk_buff *msg;
9035 void *hdr;
9036
9037 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9038 if (!msg)
9039 return;
9040
9041 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9042 if (!hdr) {
9043 nlmsg_free(msg);
9044 return;
9045 }
9046
David S. Miller9360ffd2012-03-29 04:41:26 -04009047 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9048 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9049 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
9050 (ie_len && ie &&
9051 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9052 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009053
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009054 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009055
9056 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9057 nl80211_mlme_mcgrp.id, gfp);
9058 return;
9059
9060 nla_put_failure:
9061 genlmsg_cancel(msg, hdr);
9062 nlmsg_free(msg);
9063}
9064
Jouni Malinena3b8b052009-03-27 21:59:49 +02009065void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9066 struct net_device *netdev, const u8 *addr,
9067 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009068 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009069{
9070 struct sk_buff *msg;
9071 void *hdr;
9072
Johannes Berge6d6e342009-07-01 21:26:47 +02009073 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009074 if (!msg)
9075 return;
9076
9077 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9078 if (!hdr) {
9079 nlmsg_free(msg);
9080 return;
9081 }
9082
David S. Miller9360ffd2012-03-29 04:41:26 -04009083 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9084 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9085 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9086 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9087 (key_id != -1 &&
9088 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9089 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9090 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009091
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009092 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009093
Johannes Berg463d0182009-07-14 00:33:35 +02009094 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9095 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009096 return;
9097
9098 nla_put_failure:
9099 genlmsg_cancel(msg, hdr);
9100 nlmsg_free(msg);
9101}
9102
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009103void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9104 struct ieee80211_channel *channel_before,
9105 struct ieee80211_channel *channel_after)
9106{
9107 struct sk_buff *msg;
9108 void *hdr;
9109 struct nlattr *nl_freq;
9110
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009111 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009112 if (!msg)
9113 return;
9114
9115 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9116 if (!hdr) {
9117 nlmsg_free(msg);
9118 return;
9119 }
9120
9121 /*
9122 * Since we are applying the beacon hint to a wiphy we know its
9123 * wiphy_idx is valid
9124 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009125 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9126 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009127
9128 /* Before */
9129 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9130 if (!nl_freq)
9131 goto nla_put_failure;
9132 if (nl80211_msg_put_channel(msg, channel_before))
9133 goto nla_put_failure;
9134 nla_nest_end(msg, nl_freq);
9135
9136 /* After */
9137 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9138 if (!nl_freq)
9139 goto nla_put_failure;
9140 if (nl80211_msg_put_channel(msg, channel_after))
9141 goto nla_put_failure;
9142 nla_nest_end(msg, nl_freq);
9143
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009144 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009145
Johannes Berg463d0182009-07-14 00:33:35 +02009146 rcu_read_lock();
9147 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9148 GFP_ATOMIC);
9149 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009150
9151 return;
9152
9153nla_put_failure:
9154 genlmsg_cancel(msg, hdr);
9155 nlmsg_free(msg);
9156}
9157
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009158static void nl80211_send_remain_on_chan_event(
9159 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009160 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009161 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009162 unsigned int duration, gfp_t gfp)
9163{
9164 struct sk_buff *msg;
9165 void *hdr;
9166
9167 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9168 if (!msg)
9169 return;
9170
9171 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9172 if (!hdr) {
9173 nlmsg_free(msg);
9174 return;
9175 }
9176
David S. Miller9360ffd2012-03-29 04:41:26 -04009177 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009178 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9179 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009180 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009181 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009182 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9183 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009184 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9185 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009186
David S. Miller9360ffd2012-03-29 04:41:26 -04009187 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9188 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9189 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009190
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009191 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009192
9193 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9194 nl80211_mlme_mcgrp.id, gfp);
9195 return;
9196
9197 nla_put_failure:
9198 genlmsg_cancel(msg, hdr);
9199 nlmsg_free(msg);
9200}
9201
9202void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009203 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009204 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009205 unsigned int duration, gfp_t gfp)
9206{
9207 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009208 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009209 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009210}
9211
9212void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009213 struct cfg80211_registered_device *rdev,
9214 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009215 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009216{
9217 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009218 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009219}
9220
Johannes Berg98b62182009-12-23 13:15:44 +01009221void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9222 struct net_device *dev, const u8 *mac_addr,
9223 struct station_info *sinfo, gfp_t gfp)
9224{
9225 struct sk_buff *msg;
9226
Thomas Graf58050fc2012-06-28 03:57:45 +00009227 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009228 if (!msg)
9229 return;
9230
John W. Linville66266b32012-03-15 13:25:41 -04009231 if (nl80211_send_station(msg, 0, 0, 0,
9232 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009233 nlmsg_free(msg);
9234 return;
9235 }
9236
9237 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9238 nl80211_mlme_mcgrp.id, gfp);
9239}
9240
Jouni Malinenec15e682011-03-23 15:29:52 +02009241void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9242 struct net_device *dev, const u8 *mac_addr,
9243 gfp_t gfp)
9244{
9245 struct sk_buff *msg;
9246 void *hdr;
9247
Thomas Graf58050fc2012-06-28 03:57:45 +00009248 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009249 if (!msg)
9250 return;
9251
9252 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9253 if (!hdr) {
9254 nlmsg_free(msg);
9255 return;
9256 }
9257
David S. Miller9360ffd2012-03-29 04:41:26 -04009258 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9259 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9260 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009261
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009262 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009263
9264 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9265 nl80211_mlme_mcgrp.id, gfp);
9266 return;
9267
9268 nla_put_failure:
9269 genlmsg_cancel(msg, hdr);
9270 nlmsg_free(msg);
9271}
9272
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309273void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9274 struct net_device *dev, const u8 *mac_addr,
9275 enum nl80211_connect_failed_reason reason,
9276 gfp_t gfp)
9277{
9278 struct sk_buff *msg;
9279 void *hdr;
9280
9281 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9282 if (!msg)
9283 return;
9284
9285 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9286 if (!hdr) {
9287 nlmsg_free(msg);
9288 return;
9289 }
9290
9291 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9292 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9293 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9294 goto nla_put_failure;
9295
9296 genlmsg_end(msg, hdr);
9297
9298 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9299 nl80211_mlme_mcgrp.id, gfp);
9300 return;
9301
9302 nla_put_failure:
9303 genlmsg_cancel(msg, hdr);
9304 nlmsg_free(msg);
9305}
9306
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009307static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9308 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009309{
9310 struct wireless_dev *wdev = dev->ieee80211_ptr;
9311 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9312 struct sk_buff *msg;
9313 void *hdr;
9314 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009315 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009316
Eric W. Biederman15e47302012-09-07 20:12:54 +00009317 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009318 return false;
9319
9320 msg = nlmsg_new(100, gfp);
9321 if (!msg)
9322 return true;
9323
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009324 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009325 if (!hdr) {
9326 nlmsg_free(msg);
9327 return true;
9328 }
9329
David S. Miller9360ffd2012-03-29 04:41:26 -04009330 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9331 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9332 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9333 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009334
9335 err = genlmsg_end(msg, hdr);
9336 if (err < 0) {
9337 nlmsg_free(msg);
9338 return true;
9339 }
9340
Eric W. Biederman15e47302012-09-07 20:12:54 +00009341 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009342 return true;
9343
9344 nla_put_failure:
9345 genlmsg_cancel(msg, hdr);
9346 nlmsg_free(msg);
9347 return true;
9348}
9349
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009350bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9351{
9352 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9353 addr, gfp);
9354}
9355
9356bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9357 const u8 *addr, gfp_t gfp)
9358{
9359 return __nl80211_unexpected_frame(dev,
9360 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9361 addr, gfp);
9362}
9363
Johannes Berg2e161f72010-08-12 15:38:38 +02009364int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009365 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009366 int freq, int sig_dbm,
9367 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009368{
Johannes Berg71bbc992012-06-15 15:30:18 +02009369 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009370 struct sk_buff *msg;
9371 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009372
9373 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9374 if (!msg)
9375 return -ENOMEM;
9376
Johannes Berg2e161f72010-08-12 15:38:38 +02009377 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009378 if (!hdr) {
9379 nlmsg_free(msg);
9380 return -ENOMEM;
9381 }
9382
David S. Miller9360ffd2012-03-29 04:41:26 -04009383 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009384 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9385 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009386 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9387 (sig_dbm &&
9388 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9389 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9390 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009391
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009392 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009393
Eric W. Biederman15e47302012-09-07 20:12:54 +00009394 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009395
9396 nla_put_failure:
9397 genlmsg_cancel(msg, hdr);
9398 nlmsg_free(msg);
9399 return -ENOBUFS;
9400}
9401
Johannes Berg2e161f72010-08-12 15:38:38 +02009402void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009403 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009404 const u8 *buf, size_t len, bool ack,
9405 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009406{
Johannes Berg71bbc992012-06-15 15:30:18 +02009407 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009408 struct sk_buff *msg;
9409 void *hdr;
9410
9411 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9412 if (!msg)
9413 return;
9414
Johannes Berg2e161f72010-08-12 15:38:38 +02009415 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009416 if (!hdr) {
9417 nlmsg_free(msg);
9418 return;
9419 }
9420
David S. Miller9360ffd2012-03-29 04:41:26 -04009421 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009422 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9423 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009424 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9425 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9426 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9427 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009428
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009429 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009430
9431 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9432 return;
9433
9434 nla_put_failure:
9435 genlmsg_cancel(msg, hdr);
9436 nlmsg_free(msg);
9437}
9438
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009439void
9440nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9441 struct net_device *netdev,
9442 enum nl80211_cqm_rssi_threshold_event rssi_event,
9443 gfp_t gfp)
9444{
9445 struct sk_buff *msg;
9446 struct nlattr *pinfoattr;
9447 void *hdr;
9448
Thomas Graf58050fc2012-06-28 03:57:45 +00009449 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009450 if (!msg)
9451 return;
9452
9453 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9454 if (!hdr) {
9455 nlmsg_free(msg);
9456 return;
9457 }
9458
David S. Miller9360ffd2012-03-29 04:41:26 -04009459 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9460 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9461 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009462
9463 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9464 if (!pinfoattr)
9465 goto nla_put_failure;
9466
David S. Miller9360ffd2012-03-29 04:41:26 -04009467 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9468 rssi_event))
9469 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009470
9471 nla_nest_end(msg, pinfoattr);
9472
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009473 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009474
9475 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9476 nl80211_mlme_mcgrp.id, gfp);
9477 return;
9478
9479 nla_put_failure:
9480 genlmsg_cancel(msg, hdr);
9481 nlmsg_free(msg);
9482}
9483
Johannes Berge5497d72011-07-05 16:35:40 +02009484void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9485 struct net_device *netdev, const u8 *bssid,
9486 const u8 *replay_ctr, gfp_t gfp)
9487{
9488 struct sk_buff *msg;
9489 struct nlattr *rekey_attr;
9490 void *hdr;
9491
Thomas Graf58050fc2012-06-28 03:57:45 +00009492 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009493 if (!msg)
9494 return;
9495
9496 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9497 if (!hdr) {
9498 nlmsg_free(msg);
9499 return;
9500 }
9501
David S. Miller9360ffd2012-03-29 04:41:26 -04009502 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9503 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9504 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9505 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009506
9507 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9508 if (!rekey_attr)
9509 goto nla_put_failure;
9510
David S. Miller9360ffd2012-03-29 04:41:26 -04009511 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9512 NL80211_REPLAY_CTR_LEN, replay_ctr))
9513 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009514
9515 nla_nest_end(msg, rekey_attr);
9516
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009517 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009518
9519 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9520 nl80211_mlme_mcgrp.id, gfp);
9521 return;
9522
9523 nla_put_failure:
9524 genlmsg_cancel(msg, hdr);
9525 nlmsg_free(msg);
9526}
9527
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009528void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9529 struct net_device *netdev, int index,
9530 const u8 *bssid, bool preauth, gfp_t gfp)
9531{
9532 struct sk_buff *msg;
9533 struct nlattr *attr;
9534 void *hdr;
9535
Thomas Graf58050fc2012-06-28 03:57:45 +00009536 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009537 if (!msg)
9538 return;
9539
9540 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9541 if (!hdr) {
9542 nlmsg_free(msg);
9543 return;
9544 }
9545
David S. Miller9360ffd2012-03-29 04:41:26 -04009546 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9547 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9548 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009549
9550 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9551 if (!attr)
9552 goto nla_put_failure;
9553
David S. Miller9360ffd2012-03-29 04:41:26 -04009554 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9555 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9556 (preauth &&
9557 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9558 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009559
9560 nla_nest_end(msg, attr);
9561
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009562 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009563
9564 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9565 nl80211_mlme_mcgrp.id, gfp);
9566 return;
9567
9568 nla_put_failure:
9569 genlmsg_cancel(msg, hdr);
9570 nlmsg_free(msg);
9571}
9572
Thomas Pedersen53145262012-04-06 13:35:47 -07009573void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009574 struct net_device *netdev,
9575 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009576{
9577 struct sk_buff *msg;
9578 void *hdr;
9579
Thomas Graf58050fc2012-06-28 03:57:45 +00009580 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009581 if (!msg)
9582 return;
9583
9584 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9585 if (!hdr) {
9586 nlmsg_free(msg);
9587 return;
9588 }
9589
Johannes Berg683b6d32012-11-08 21:25:48 +01009590 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9591 goto nla_put_failure;
9592
9593 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009594 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009595
9596 genlmsg_end(msg, hdr);
9597
9598 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9599 nl80211_mlme_mcgrp.id, gfp);
9600 return;
9601
9602 nla_put_failure:
9603 genlmsg_cancel(msg, hdr);
9604 nlmsg_free(msg);
9605}
9606
Johannes Bergc063dbf2010-11-24 08:10:05 +01009607void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009608nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9609 struct net_device *netdev, const u8 *peer,
9610 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9611{
9612 struct sk_buff *msg;
9613 struct nlattr *pinfoattr;
9614 void *hdr;
9615
9616 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9617 if (!msg)
9618 return;
9619
9620 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9621 if (!hdr) {
9622 nlmsg_free(msg);
9623 return;
9624 }
9625
9626 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9627 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9628 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9629 goto nla_put_failure;
9630
9631 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9632 if (!pinfoattr)
9633 goto nla_put_failure;
9634
9635 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9636 goto nla_put_failure;
9637
9638 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9639 goto nla_put_failure;
9640
9641 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9642 goto nla_put_failure;
9643
9644 nla_nest_end(msg, pinfoattr);
9645
9646 genlmsg_end(msg, hdr);
9647
9648 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9649 nl80211_mlme_mcgrp.id, gfp);
9650 return;
9651
9652 nla_put_failure:
9653 genlmsg_cancel(msg, hdr);
9654 nlmsg_free(msg);
9655}
9656
9657void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009658nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9659 struct cfg80211_chan_def *chandef,
9660 enum nl80211_radar_event event,
9661 struct net_device *netdev, gfp_t gfp)
9662{
9663 struct sk_buff *msg;
9664 void *hdr;
9665
9666 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9667 if (!msg)
9668 return;
9669
9670 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9671 if (!hdr) {
9672 nlmsg_free(msg);
9673 return;
9674 }
9675
9676 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9677 goto nla_put_failure;
9678
9679 /* NOP and radar events don't need a netdev parameter */
9680 if (netdev) {
9681 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9682
9683 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9684 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9685 goto nla_put_failure;
9686 }
9687
9688 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9689 goto nla_put_failure;
9690
9691 if (nl80211_send_chandef(msg, chandef))
9692 goto nla_put_failure;
9693
9694 if (genlmsg_end(msg, hdr) < 0) {
9695 nlmsg_free(msg);
9696 return;
9697 }
9698
9699 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9700 nl80211_mlme_mcgrp.id, gfp);
9701 return;
9702
9703 nla_put_failure:
9704 genlmsg_cancel(msg, hdr);
9705 nlmsg_free(msg);
9706}
9707
9708void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009709nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9710 struct net_device *netdev, const u8 *peer,
9711 u32 num_packets, gfp_t gfp)
9712{
9713 struct sk_buff *msg;
9714 struct nlattr *pinfoattr;
9715 void *hdr;
9716
Thomas Graf58050fc2012-06-28 03:57:45 +00009717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009718 if (!msg)
9719 return;
9720
9721 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9722 if (!hdr) {
9723 nlmsg_free(msg);
9724 return;
9725 }
9726
David S. Miller9360ffd2012-03-29 04:41:26 -04009727 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9728 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9729 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9730 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009731
9732 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9733 if (!pinfoattr)
9734 goto nla_put_failure;
9735
David S. Miller9360ffd2012-03-29 04:41:26 -04009736 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9737 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009738
9739 nla_nest_end(msg, pinfoattr);
9740
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009741 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009742
9743 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9744 nl80211_mlme_mcgrp.id, gfp);
9745 return;
9746
9747 nla_put_failure:
9748 genlmsg_cancel(msg, hdr);
9749 nlmsg_free(msg);
9750}
9751
Johannes Berg7f6cf312011-11-04 11:18:15 +01009752void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9753 u64 cookie, bool acked, gfp_t gfp)
9754{
9755 struct wireless_dev *wdev = dev->ieee80211_ptr;
9756 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9757 struct sk_buff *msg;
9758 void *hdr;
9759 int err;
9760
Beni Lev4ee3e062012-08-27 12:49:39 +03009761 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9762
Thomas Graf58050fc2012-06-28 03:57:45 +00009763 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009764
Johannes Berg7f6cf312011-11-04 11:18:15 +01009765 if (!msg)
9766 return;
9767
9768 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9769 if (!hdr) {
9770 nlmsg_free(msg);
9771 return;
9772 }
9773
David S. Miller9360ffd2012-03-29 04:41:26 -04009774 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9775 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9776 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9777 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9778 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9779 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009780
9781 err = genlmsg_end(msg, hdr);
9782 if (err < 0) {
9783 nlmsg_free(msg);
9784 return;
9785 }
9786
9787 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9788 nl80211_mlme_mcgrp.id, gfp);
9789 return;
9790
9791 nla_put_failure:
9792 genlmsg_cancel(msg, hdr);
9793 nlmsg_free(msg);
9794}
9795EXPORT_SYMBOL(cfg80211_probe_status);
9796
Johannes Berg5e760232011-11-04 11:18:17 +01009797void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9798 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009799 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009800{
9801 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9802 struct sk_buff *msg;
9803 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009804 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009805
Beni Lev4ee3e062012-08-27 12:49:39 +03009806 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9807
Ben Greear37c73b52012-10-26 14:49:25 -07009808 spin_lock_bh(&rdev->beacon_registrations_lock);
9809 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9810 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9811 if (!msg) {
9812 spin_unlock_bh(&rdev->beacon_registrations_lock);
9813 return;
9814 }
Johannes Berg5e760232011-11-04 11:18:17 +01009815
Ben Greear37c73b52012-10-26 14:49:25 -07009816 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9817 if (!hdr)
9818 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009819
Ben Greear37c73b52012-10-26 14:49:25 -07009820 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9821 (freq &&
9822 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9823 (sig_dbm &&
9824 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9825 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9826 goto nla_put_failure;
9827
9828 genlmsg_end(msg, hdr);
9829
9830 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009831 }
Ben Greear37c73b52012-10-26 14:49:25 -07009832 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009833 return;
9834
9835 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009836 spin_unlock_bh(&rdev->beacon_registrations_lock);
9837 if (hdr)
9838 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009839 nlmsg_free(msg);
9840}
9841EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9842
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009843#ifdef CONFIG_PM
9844void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9845 struct cfg80211_wowlan_wakeup *wakeup,
9846 gfp_t gfp)
9847{
9848 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9849 struct sk_buff *msg;
9850 void *hdr;
9851 int err, size = 200;
9852
9853 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9854
9855 if (wakeup)
9856 size += wakeup->packet_present_len;
9857
9858 msg = nlmsg_new(size, gfp);
9859 if (!msg)
9860 return;
9861
9862 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9863 if (!hdr)
9864 goto free_msg;
9865
9866 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9867 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9868 goto free_msg;
9869
9870 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9871 wdev->netdev->ifindex))
9872 goto free_msg;
9873
9874 if (wakeup) {
9875 struct nlattr *reasons;
9876
9877 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9878
9879 if (wakeup->disconnect &&
9880 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9881 goto free_msg;
9882 if (wakeup->magic_pkt &&
9883 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9884 goto free_msg;
9885 if (wakeup->gtk_rekey_failure &&
9886 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9887 goto free_msg;
9888 if (wakeup->eap_identity_req &&
9889 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9890 goto free_msg;
9891 if (wakeup->four_way_handshake &&
9892 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9893 goto free_msg;
9894 if (wakeup->rfkill_release &&
9895 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9896 goto free_msg;
9897
9898 if (wakeup->pattern_idx >= 0 &&
9899 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9900 wakeup->pattern_idx))
9901 goto free_msg;
9902
Johannes Berg2a0e0472013-01-23 22:57:40 +01009903 if (wakeup->tcp_match)
9904 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
9905
9906 if (wakeup->tcp_connlost)
9907 nla_put_flag(msg,
9908 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
9909
9910 if (wakeup->tcp_nomoretokens)
9911 nla_put_flag(msg,
9912 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
9913
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009914 if (wakeup->packet) {
9915 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9916 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9917
9918 if (!wakeup->packet_80211) {
9919 pkt_attr =
9920 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9921 len_attr =
9922 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9923 }
9924
9925 if (wakeup->packet_len &&
9926 nla_put_u32(msg, len_attr, wakeup->packet_len))
9927 goto free_msg;
9928
9929 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9930 wakeup->packet))
9931 goto free_msg;
9932 }
9933
9934 nla_nest_end(msg, reasons);
9935 }
9936
9937 err = genlmsg_end(msg, hdr);
9938 if (err < 0)
9939 goto free_msg;
9940
9941 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9942 nl80211_mlme_mcgrp.id, gfp);
9943 return;
9944
9945 free_msg:
9946 nlmsg_free(msg);
9947}
9948EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9949#endif
9950
Jouni Malinen3475b092012-11-16 22:49:57 +02009951void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9952 enum nl80211_tdls_operation oper,
9953 u16 reason_code, gfp_t gfp)
9954{
9955 struct wireless_dev *wdev = dev->ieee80211_ptr;
9956 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9957 struct sk_buff *msg;
9958 void *hdr;
9959 int err;
9960
9961 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9962 reason_code);
9963
9964 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9965 if (!msg)
9966 return;
9967
9968 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9969 if (!hdr) {
9970 nlmsg_free(msg);
9971 return;
9972 }
9973
9974 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9975 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9976 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9977 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9978 (reason_code > 0 &&
9979 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9980 goto nla_put_failure;
9981
9982 err = genlmsg_end(msg, hdr);
9983 if (err < 0) {
9984 nlmsg_free(msg);
9985 return;
9986 }
9987
9988 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9989 nl80211_mlme_mcgrp.id, gfp);
9990 return;
9991
9992 nla_put_failure:
9993 genlmsg_cancel(msg, hdr);
9994 nlmsg_free(msg);
9995}
9996EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9997
Jouni Malinen026331c2010-02-15 12:53:10 +02009998static int nl80211_netlink_notify(struct notifier_block * nb,
9999 unsigned long state,
10000 void *_notify)
10001{
10002 struct netlink_notify *notify = _notify;
10003 struct cfg80211_registered_device *rdev;
10004 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010005 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010006
10007 if (state != NETLINK_URELEASE)
10008 return NOTIFY_DONE;
10009
10010 rcu_read_lock();
10011
Johannes Berg5e760232011-11-04 11:18:17 +010010012 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010013 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010014 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010015
10016 spin_lock_bh(&rdev->beacon_registrations_lock);
10017 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10018 list) {
10019 if (reg->nlportid == notify->portid) {
10020 list_del(&reg->list);
10021 kfree(reg);
10022 break;
10023 }
10024 }
10025 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010026 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010027
10028 rcu_read_unlock();
10029
10030 return NOTIFY_DONE;
10031}
10032
10033static struct notifier_block nl80211_netlink_notifier = {
10034 .notifier_call = nl80211_netlink_notify,
10035};
10036
Johannes Berg55682962007-09-20 13:09:35 -040010037/* initialisation/exit functions */
10038
10039int nl80211_init(void)
10040{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010041 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010042
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010043 err = genl_register_family_with_ops(&nl80211_fam,
10044 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010045 if (err)
10046 return err;
10047
Johannes Berg55682962007-09-20 13:09:35 -040010048 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10049 if (err)
10050 goto err_out;
10051
Johannes Berg2a519312009-02-10 21:25:55 +010010052 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10053 if (err)
10054 goto err_out;
10055
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010056 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10057 if (err)
10058 goto err_out;
10059
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010060 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10061 if (err)
10062 goto err_out;
10063
Johannes Bergaff89a92009-07-01 21:26:51 +020010064#ifdef CONFIG_NL80211_TESTMODE
10065 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10066 if (err)
10067 goto err_out;
10068#endif
10069
Jouni Malinen026331c2010-02-15 12:53:10 +020010070 err = netlink_register_notifier(&nl80211_netlink_notifier);
10071 if (err)
10072 goto err_out;
10073
Johannes Berg55682962007-09-20 13:09:35 -040010074 return 0;
10075 err_out:
10076 genl_unregister_family(&nl80211_fam);
10077 return err;
10078}
10079
10080void nl80211_exit(void)
10081{
Jouni Malinen026331c2010-02-15 12:53:10 +020010082 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010083 genl_unregister_family(&nl80211_fam);
10084}