blob: eb0aa71a02b3a4fdaaf6546966180b7813d918ab [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 Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030025#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040026
Jouni Malinen5fb628e2011-08-10 23:54:35 +030027static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg89a54e42012-06-15 14:33:17 +020049/* returns ERR_PTR values */
50static struct wireless_dev *
51__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040052{
Johannes Berg89a54e42012-06-15 14:33:17 +020053 struct cfg80211_registered_device *rdev;
54 struct wireless_dev *result = NULL;
55 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
56 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
57 u64 wdev_id;
58 int wiphy_idx = -1;
59 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040060
Johannes Berg89a54e42012-06-15 14:33:17 +020061 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040062
Johannes Berg89a54e42012-06-15 14:33:17 +020063 if (!have_ifidx && !have_wdev_id)
64 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040065
Johannes Berg89a54e42012-06-15 14:33:17 +020066 if (have_ifidx)
67 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
68 if (have_wdev_id) {
69 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
70 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040071 }
72
Johannes Berg89a54e42012-06-15 14:33:17 +020073 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
74 struct wireless_dev *wdev;
75
76 if (wiphy_net(&rdev->wiphy) != netns)
77 continue;
78
79 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
80 continue;
81
82 mutex_lock(&rdev->devlist_mtx);
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
88 }
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
92 }
93 }
94 mutex_unlock(&rdev->devlist_mtx);
95
96 if (result)
97 break;
98 }
99
100 if (result)
101 return result;
102 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400103}
104
Johannes Berga9455402012-06-15 13:32:49 +0200105static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200106__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200107{
Johannes Berg7fee4772012-06-15 14:09:58 +0200108 struct cfg80211_registered_device *rdev = NULL, *tmp;
109 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200110
111 assert_cfg80211_lock();
112
Johannes Berg878d9ec2012-06-15 14:18:32 +0200113 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200114 !attrs[NL80211_ATTR_IFINDEX] &&
115 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200116 return ERR_PTR(-EINVAL);
117
Johannes Berg878d9ec2012-06-15 14:18:32 +0200118 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200119 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200120 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200121
Johannes Berg89a54e42012-06-15 14:33:17 +0200122 if (attrs[NL80211_ATTR_WDEV]) {
123 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
124 struct wireless_dev *wdev;
125 bool found = false;
126
127 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
128 if (tmp) {
129 /* make sure wdev exists */
130 mutex_lock(&tmp->devlist_mtx);
131 list_for_each_entry(wdev, &tmp->wdev_list, list) {
132 if (wdev->identifier != (u32)wdev_id)
133 continue;
134 found = true;
135 break;
136 }
137 mutex_unlock(&tmp->devlist_mtx);
138
139 if (!found)
140 tmp = NULL;
141
142 if (rdev && tmp != rdev)
143 return ERR_PTR(-EINVAL);
144 rdev = tmp;
145 }
146 }
147
Johannes Berg878d9ec2012-06-15 14:18:32 +0200148 if (attrs[NL80211_ATTR_IFINDEX]) {
149 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200150 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200151 if (netdev) {
152 if (netdev->ieee80211_ptr)
153 tmp = wiphy_to_dev(
154 netdev->ieee80211_ptr->wiphy);
155 else
156 tmp = NULL;
157
158 dev_put(netdev);
159
160 /* not wireless device -- return error */
161 if (!tmp)
162 return ERR_PTR(-EINVAL);
163
164 /* mismatch -- return error */
165 if (rdev && tmp != rdev)
166 return ERR_PTR(-EINVAL);
167
168 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200169 }
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
171
Johannes Berg4f7eff12012-06-15 14:14:22 +0200172 if (!rdev)
173 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200174
Johannes Berg4f7eff12012-06-15 14:14:22 +0200175 if (netns != wiphy_net(&rdev->wiphy))
176 return ERR_PTR(-ENODEV);
177
178 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200179}
180
181/*
182 * This function returns a pointer to the driver
183 * that the genl_info item that is passed refers to.
184 * If successful, it returns non-NULL and also locks
185 * the driver's mutex!
186 *
187 * This means that you need to call cfg80211_unlock_rdev()
188 * before being allowed to acquire &cfg80211_mutex!
189 *
190 * This is necessary because we need to lock the global
191 * mutex to get an item off the list safely, and then
192 * we lock the rdev mutex so it doesn't go away under us.
193 *
194 * We don't want to keep cfg80211_mutex locked
195 * for all the time in order to allow requests on
196 * other interfaces to go through at the same time.
197 *
198 * The result of this can be a PTR_ERR and hence must
199 * be checked with IS_ERR() for errors.
200 */
201static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200202cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200203{
204 struct cfg80211_registered_device *rdev;
205
206 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200207 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208
209 /* if it is not an error we grab the lock on
210 * it to assure it won't be going away while
211 * we operate on it */
212 if (!IS_ERR(rdev))
213 mutex_lock(&rdev->mtx);
214
215 mutex_unlock(&cfg80211_mutex);
216
217 return rdev;
218}
219
Johannes Berg55682962007-09-20 13:09:35 -0400220/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000221static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400222 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700224 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200225 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100226
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200227 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530228 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100229 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
230 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
232
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200233 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
234 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
236 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100237 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400238
239 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
240 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100242
Eliad Pellere007b852011-11-24 18:13:56 +0200243 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
244 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100245
Johannes Bergb9454e82009-07-08 13:29:08 +0200246 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100247 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
248 .len = WLAN_MAX_KEY_LEN },
249 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
250 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
251 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200252 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200253 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100254
255 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
256 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
257 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
258 .len = IEEE80211_MAX_DATA_LEN },
259 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
260 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100261 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
262 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
263 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
264 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
265 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100266 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100267 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200268 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100269 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800270 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100271 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300272
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700273 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
274 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
275
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300276 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
277 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200279 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
280 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100281 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300282
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800283 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700284 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700285
Johannes Berg6c739412011-11-03 09:27:01 +0100286 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200287
288 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
289 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100291 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
292 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200293
294 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
295 .len = IEEE80211_MAX_SSID_LEN },
296 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
297 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200298 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300299 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300300 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300301 [NL80211_ATTR_STA_FLAGS2] = {
302 .len = sizeof(struct nl80211_sta_flag_update),
303 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300304 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300305 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
306 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200307 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
308 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
309 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200310 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100311 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100312 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
313 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100314 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
315 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200316 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200317 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
318 .len = IEEE80211_MAX_DATA_LEN },
319 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200320 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200321 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300322 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200323 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300324 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
325 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200326 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900327 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
328 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100329 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100330 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100331 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200332 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700333 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300334 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200335 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200336 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300337 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300338 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
339 .len = IEEE80211_MAX_DATA_LEN },
340 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
341 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530342 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300343 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530344 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300345 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
346 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
349 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100350 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200351 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
352 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700353 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800354 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
355 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
356 .len = NL80211_HT_CAPABILITY_LEN
357 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100358 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530359 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530360 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200361 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700362 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300363 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000364 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700365 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400366};
367
Johannes Berge31b8212010-10-05 19:39:30 +0200368/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000369static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200370 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200371 [NL80211_KEY_IDX] = { .type = NLA_U8 },
372 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200373 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200374 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
375 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200376 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100377 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
378};
379
380/* policy for the key default flags */
381static const struct nla_policy
382nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
383 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
384 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200385};
386
Johannes Bergff1b6e62011-05-04 15:37:28 +0200387/* policy for WoWLAN attributes */
388static const struct nla_policy
389nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
390 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
391 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
392 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
393 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200394 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
395 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
396 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
397 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200398};
399
Johannes Berge5497d72011-07-05 16:35:40 +0200400/* policy for GTK rekey offload attributes */
401static const struct nla_policy
402nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
403 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
404 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
405 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
406};
407
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300408static const struct nla_policy
409nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200410 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300411 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700412 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300413};
414
Holger Schuriga0438972009-11-11 11:30:02 +0100415/* ifidx get helper */
416static int nl80211_get_ifidx(struct netlink_callback *cb)
417{
418 int res;
419
420 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
421 nl80211_fam.attrbuf, nl80211_fam.maxattr,
422 nl80211_policy);
423 if (res)
424 return res;
425
426 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
427 return -EINVAL;
428
429 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
430 if (!res)
431 return -EINVAL;
432 return res;
433}
434
Johannes Berg67748892010-10-04 21:14:06 +0200435static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
436 struct netlink_callback *cb,
437 struct cfg80211_registered_device **rdev,
438 struct net_device **dev)
439{
440 int ifidx = cb->args[0];
441 int err;
442
443 if (!ifidx)
444 ifidx = nl80211_get_ifidx(cb);
445 if (ifidx < 0)
446 return ifidx;
447
448 cb->args[0] = ifidx;
449
450 rtnl_lock();
451
452 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
453 if (!*dev) {
454 err = -ENODEV;
455 goto out_rtnl;
456 }
457
458 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100459 if (IS_ERR(*rdev)) {
460 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200461 goto out_rtnl;
462 }
463
464 return 0;
465 out_rtnl:
466 rtnl_unlock();
467 return err;
468}
469
470static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
471{
472 cfg80211_unlock_rdev(rdev);
473 rtnl_unlock();
474}
475
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100476/* IE validation */
477static bool is_valid_ie_attr(const struct nlattr *attr)
478{
479 const u8 *pos;
480 int len;
481
482 if (!attr)
483 return true;
484
485 pos = nla_data(attr);
486 len = nla_len(attr);
487
488 while (len) {
489 u8 elemlen;
490
491 if (len < 2)
492 return false;
493 len -= 2;
494
495 elemlen = pos[1];
496 if (elemlen > len)
497 return false;
498
499 len -= elemlen;
500 pos += 2 + elemlen;
501 }
502
503 return true;
504}
505
Johannes Berg55682962007-09-20 13:09:35 -0400506/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000507static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400508 int flags, u8 cmd)
509{
510 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000511 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400512}
513
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400514static int nl80211_msg_put_channel(struct sk_buff *msg,
515 struct ieee80211_channel *chan)
516{
David S. Miller9360ffd2012-03-29 04:41:26 -0400517 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
518 chan->center_freq))
519 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400520
David S. Miller9360ffd2012-03-29 04:41:26 -0400521 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
522 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
523 goto nla_put_failure;
524 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
525 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
526 goto nla_put_failure;
527 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
528 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
529 goto nla_put_failure;
530 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
531 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
532 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400533
David S. Miller9360ffd2012-03-29 04:41:26 -0400534 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
535 DBM_TO_MBM(chan->max_power)))
536 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400537
538 return 0;
539
540 nla_put_failure:
541 return -ENOBUFS;
542}
543
Johannes Berg55682962007-09-20 13:09:35 -0400544/* netlink command implementations */
545
Johannes Bergb9454e82009-07-08 13:29:08 +0200546struct key_parse {
547 struct key_params p;
548 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200549 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200550 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100551 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200552};
553
554static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
555{
556 struct nlattr *tb[NL80211_KEY_MAX + 1];
557 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
558 nl80211_key_policy);
559 if (err)
560 return err;
561
562 k->def = !!tb[NL80211_KEY_DEFAULT];
563 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
564
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100565 if (k->def) {
566 k->def_uni = true;
567 k->def_multi = true;
568 }
569 if (k->defmgmt)
570 k->def_multi = true;
571
Johannes Bergb9454e82009-07-08 13:29:08 +0200572 if (tb[NL80211_KEY_IDX])
573 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
574
575 if (tb[NL80211_KEY_DATA]) {
576 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
577 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
578 }
579
580 if (tb[NL80211_KEY_SEQ]) {
581 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
582 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
583 }
584
585 if (tb[NL80211_KEY_CIPHER])
586 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
587
Johannes Berge31b8212010-10-05 19:39:30 +0200588 if (tb[NL80211_KEY_TYPE]) {
589 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
590 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
591 return -EINVAL;
592 }
593
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100594 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
595 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100596 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
597 tb[NL80211_KEY_DEFAULT_TYPES],
598 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100599 if (err)
600 return err;
601
602 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
603 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
604 }
605
Johannes Bergb9454e82009-07-08 13:29:08 +0200606 return 0;
607}
608
609static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
610{
611 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
612 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
613 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
614 }
615
616 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
617 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
618 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
619 }
620
621 if (info->attrs[NL80211_ATTR_KEY_IDX])
622 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
623
624 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
625 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
626
627 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
628 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
629
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100630 if (k->def) {
631 k->def_uni = true;
632 k->def_multi = true;
633 }
634 if (k->defmgmt)
635 k->def_multi = true;
636
Johannes Berge31b8212010-10-05 19:39:30 +0200637 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
638 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
639 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
640 return -EINVAL;
641 }
642
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100643 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
644 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
645 int err = nla_parse_nested(
646 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
647 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
648 nl80211_key_default_policy);
649 if (err)
650 return err;
651
652 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
653 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
654 }
655
Johannes Bergb9454e82009-07-08 13:29:08 +0200656 return 0;
657}
658
659static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
660{
661 int err;
662
663 memset(k, 0, sizeof(*k));
664 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200665 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200666
667 if (info->attrs[NL80211_ATTR_KEY])
668 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
669 else
670 err = nl80211_parse_key_old(info, k);
671
672 if (err)
673 return err;
674
675 if (k->def && k->defmgmt)
676 return -EINVAL;
677
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100678 if (k->defmgmt) {
679 if (k->def_uni || !k->def_multi)
680 return -EINVAL;
681 }
682
Johannes Bergb9454e82009-07-08 13:29:08 +0200683 if (k->idx != -1) {
684 if (k->defmgmt) {
685 if (k->idx < 4 || k->idx > 5)
686 return -EINVAL;
687 } else if (k->def) {
688 if (k->idx < 0 || k->idx > 3)
689 return -EINVAL;
690 } else {
691 if (k->idx < 0 || k->idx > 5)
692 return -EINVAL;
693 }
694 }
695
696 return 0;
697}
698
Johannes Bergfffd0932009-07-08 14:22:54 +0200699static struct cfg80211_cached_keys *
700nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530701 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200702{
703 struct key_parse parse;
704 struct nlattr *key;
705 struct cfg80211_cached_keys *result;
706 int rem, err, def = 0;
707
708 result = kzalloc(sizeof(*result), GFP_KERNEL);
709 if (!result)
710 return ERR_PTR(-ENOMEM);
711
712 result->def = -1;
713 result->defmgmt = -1;
714
715 nla_for_each_nested(key, keys, rem) {
716 memset(&parse, 0, sizeof(parse));
717 parse.idx = -1;
718
719 err = nl80211_parse_key_new(key, &parse);
720 if (err)
721 goto error;
722 err = -EINVAL;
723 if (!parse.p.key)
724 goto error;
725 if (parse.idx < 0 || parse.idx > 4)
726 goto error;
727 if (parse.def) {
728 if (def)
729 goto error;
730 def = 1;
731 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100732 if (!parse.def_uni || !parse.def_multi)
733 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200734 } else if (parse.defmgmt)
735 goto error;
736 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200737 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200738 if (err)
739 goto error;
740 result->params[parse.idx].cipher = parse.p.cipher;
741 result->params[parse.idx].key_len = parse.p.key_len;
742 result->params[parse.idx].key = result->data[parse.idx];
743 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530744
745 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
746 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
747 if (no_ht)
748 *no_ht = true;
749 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200750 }
751
752 return result;
753 error:
754 kfree(result);
755 return ERR_PTR(err);
756}
757
758static int nl80211_key_allowed(struct wireless_dev *wdev)
759{
760 ASSERT_WDEV_LOCK(wdev);
761
Johannes Bergfffd0932009-07-08 14:22:54 +0200762 switch (wdev->iftype) {
763 case NL80211_IFTYPE_AP:
764 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200765 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700766 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200767 break;
768 case NL80211_IFTYPE_ADHOC:
769 if (!wdev->current_bss)
770 return -ENOLINK;
771 break;
772 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200773 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200774 if (wdev->sme_state != CFG80211_SME_CONNECTED)
775 return -ENOLINK;
776 break;
777 default:
778 return -EINVAL;
779 }
780
781 return 0;
782}
783
Johannes Berg7527a782011-05-13 10:58:57 +0200784static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
785{
786 struct nlattr *nl_modes = nla_nest_start(msg, attr);
787 int i;
788
789 if (!nl_modes)
790 goto nla_put_failure;
791
792 i = 0;
793 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400794 if ((ifmodes & 1) && nla_put_flag(msg, i))
795 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200796 ifmodes >>= 1;
797 i++;
798 }
799
800 nla_nest_end(msg, nl_modes);
801 return 0;
802
803nla_put_failure:
804 return -ENOBUFS;
805}
806
807static int nl80211_put_iface_combinations(struct wiphy *wiphy,
808 struct sk_buff *msg)
809{
810 struct nlattr *nl_combis;
811 int i, j;
812
813 nl_combis = nla_nest_start(msg,
814 NL80211_ATTR_INTERFACE_COMBINATIONS);
815 if (!nl_combis)
816 goto nla_put_failure;
817
818 for (i = 0; i < wiphy->n_iface_combinations; i++) {
819 const struct ieee80211_iface_combination *c;
820 struct nlattr *nl_combi, *nl_limits;
821
822 c = &wiphy->iface_combinations[i];
823
824 nl_combi = nla_nest_start(msg, i + 1);
825 if (!nl_combi)
826 goto nla_put_failure;
827
828 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
829 if (!nl_limits)
830 goto nla_put_failure;
831
832 for (j = 0; j < c->n_limits; j++) {
833 struct nlattr *nl_limit;
834
835 nl_limit = nla_nest_start(msg, j + 1);
836 if (!nl_limit)
837 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400838 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
839 c->limits[j].max))
840 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200841 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
842 c->limits[j].types))
843 goto nla_put_failure;
844 nla_nest_end(msg, nl_limit);
845 }
846
847 nla_nest_end(msg, nl_limits);
848
David S. Miller9360ffd2012-03-29 04:41:26 -0400849 if (c->beacon_int_infra_match &&
850 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
851 goto nla_put_failure;
852 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
853 c->num_different_channels) ||
854 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
855 c->max_interfaces))
856 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200857
858 nla_nest_end(msg, nl_combi);
859 }
860
861 nla_nest_end(msg, nl_combis);
862
863 return 0;
864nla_put_failure:
865 return -ENOBUFS;
866}
867
Eric W. Biederman15e47302012-09-07 20:12:54 +0000868static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400869 struct cfg80211_registered_device *dev)
870{
871 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100872 struct nlattr *nl_bands, *nl_band;
873 struct nlattr *nl_freqs, *nl_freq;
874 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100875 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100876 enum ieee80211_band band;
877 struct ieee80211_channel *chan;
878 struct ieee80211_rate *rate;
879 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200880 const struct ieee80211_txrx_stypes *mgmt_stypes =
881 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400882
Eric W. Biederman15e47302012-09-07 20:12:54 +0000883 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400884 if (!hdr)
885 return -1;
886
David S. Miller9360ffd2012-03-29 04:41:26 -0400887 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
888 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
889 nla_put_u32(msg, NL80211_ATTR_GENERATION,
890 cfg80211_rdev_list_generation) ||
891 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
892 dev->wiphy.retry_short) ||
893 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
894 dev->wiphy.retry_long) ||
895 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
896 dev->wiphy.frag_threshold) ||
897 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
898 dev->wiphy.rts_threshold) ||
899 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
900 dev->wiphy.coverage_class) ||
901 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
902 dev->wiphy.max_scan_ssids) ||
903 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
904 dev->wiphy.max_sched_scan_ssids) ||
905 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
906 dev->wiphy.max_scan_ie_len) ||
907 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
908 dev->wiphy.max_sched_scan_ie_len) ||
909 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
910 dev->wiphy.max_match_sets))
911 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200912
David S. Miller9360ffd2012-03-29 04:41:26 -0400913 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
914 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
915 goto nla_put_failure;
916 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
917 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
918 goto nla_put_failure;
919 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
920 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
921 goto nla_put_failure;
922 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
923 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
924 goto nla_put_failure;
925 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
926 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
927 goto nla_put_failure;
928 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
929 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
930 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200931
David S. Miller9360ffd2012-03-29 04:41:26 -0400932 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
933 sizeof(u32) * dev->wiphy.n_cipher_suites,
934 dev->wiphy.cipher_suites))
935 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100936
David S. Miller9360ffd2012-03-29 04:41:26 -0400937 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
938 dev->wiphy.max_num_pmkids))
939 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530940
David S. Miller9360ffd2012-03-29 04:41:26 -0400941 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
942 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
943 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200944
David S. Miller9360ffd2012-03-29 04:41:26 -0400945 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
946 dev->wiphy.available_antennas_tx) ||
947 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
948 dev->wiphy.available_antennas_rx))
949 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100950
David S. Miller9360ffd2012-03-29 04:41:26 -0400951 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
952 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
953 dev->wiphy.probe_resp_offload))
954 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200955
Bruno Randolf7f531e02010-12-16 11:30:22 +0900956 if ((dev->wiphy.available_antennas_tx ||
957 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900958 u32 tx_ant = 0, rx_ant = 0;
959 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300960 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900961 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400962 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
963 tx_ant) ||
964 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
965 rx_ant))
966 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900967 }
968 }
969
Johannes Berg7527a782011-05-13 10:58:57 +0200970 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
971 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700972 goto nla_put_failure;
973
Johannes Bergee688b002008-01-24 19:38:39 +0100974 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
975 if (!nl_bands)
976 goto nla_put_failure;
977
978 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
979 if (!dev->wiphy.bands[band])
980 continue;
981
982 nl_band = nla_nest_start(msg, band);
983 if (!nl_band)
984 goto nla_put_failure;
985
Johannes Bergd51626d2008-10-09 12:20:13 +0200986 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400987 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
988 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
989 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
990 &dev->wiphy.bands[band]->ht_cap.mcs) ||
991 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
992 dev->wiphy.bands[band]->ht_cap.cap) ||
993 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
994 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
995 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
996 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
997 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200998
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000999 /* add VHT info */
1000 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1001 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1002 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1003 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1004 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1005 dev->wiphy.bands[band]->vht_cap.cap)))
1006 goto nla_put_failure;
1007
Johannes Bergee688b002008-01-24 19:38:39 +01001008 /* add frequencies */
1009 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1010 if (!nl_freqs)
1011 goto nla_put_failure;
1012
1013 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1014 nl_freq = nla_nest_start(msg, i);
1015 if (!nl_freq)
1016 goto nla_put_failure;
1017
1018 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001019
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001020 if (nl80211_msg_put_channel(msg, chan))
1021 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001022
Johannes Bergee688b002008-01-24 19:38:39 +01001023 nla_nest_end(msg, nl_freq);
1024 }
1025
1026 nla_nest_end(msg, nl_freqs);
1027
1028 /* add bitrates */
1029 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1030 if (!nl_rates)
1031 goto nla_put_failure;
1032
1033 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1034 nl_rate = nla_nest_start(msg, i);
1035 if (!nl_rate)
1036 goto nla_put_failure;
1037
1038 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001039 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1040 rate->bitrate))
1041 goto nla_put_failure;
1042 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1043 nla_put_flag(msg,
1044 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1045 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001046
1047 nla_nest_end(msg, nl_rate);
1048 }
1049
1050 nla_nest_end(msg, nl_rates);
1051
1052 nla_nest_end(msg, nl_band);
1053 }
1054 nla_nest_end(msg, nl_bands);
1055
Johannes Berg8fdc6212009-03-14 09:34:01 +01001056 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1057 if (!nl_cmds)
1058 goto nla_put_failure;
1059
1060 i = 0;
1061#define CMD(op, n) \
1062 do { \
1063 if (dev->ops->op) { \
1064 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001065 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1066 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001067 } \
1068 } while (0)
1069
1070 CMD(add_virtual_intf, NEW_INTERFACE);
1071 CMD(change_virtual_intf, SET_INTERFACE);
1072 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001073 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001074 CMD(add_station, NEW_STATION);
1075 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001076 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001077 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001078 CMD(auth, AUTHENTICATE);
1079 CMD(assoc, ASSOCIATE);
1080 CMD(deauth, DEAUTHENTICATE);
1081 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001082 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001083 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001084 CMD(set_pmksa, SET_PMKSA);
1085 CMD(del_pmksa, DEL_PMKSA);
1086 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001087 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1088 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001089 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001090 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001091 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001092 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001093 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001094 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1095 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001096 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001097 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001098 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001099 i++;
1100 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1101 goto nla_put_failure;
1102 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001103 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001104 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1105 CMD(tdls_mgmt, TDLS_MGMT);
1106 CMD(tdls_oper, TDLS_OPER);
1107 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001108 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1109 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001110 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001111 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001112 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1113 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001114 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1115 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001116 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001117 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001118 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001119
Kalle Valo4745fc02011-11-17 19:06:10 +02001120#ifdef CONFIG_NL80211_TESTMODE
1121 CMD(testmode_cmd, TESTMODE);
1122#endif
1123
Johannes Berg8fdc6212009-03-14 09:34:01 +01001124#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001125
Johannes Berg6829c872009-07-02 09:13:27 +02001126 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001127 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001128 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1129 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001130 }
1131
Johannes Berg6829c872009-07-02 09:13:27 +02001132 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001133 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001134 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1135 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001136 }
1137
Johannes Berg8fdc6212009-03-14 09:34:01 +01001138 nla_nest_end(msg, nl_cmds);
1139
Johannes Berg7c4ef712011-11-18 15:33:48 +01001140 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001141 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1142 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1143 dev->wiphy.max_remain_on_channel_duration))
1144 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001145
David S. Miller9360ffd2012-03-29 04:41:26 -04001146 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1147 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1148 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001149
Johannes Berg2e161f72010-08-12 15:38:38 +02001150 if (mgmt_stypes) {
1151 u16 stypes;
1152 struct nlattr *nl_ftypes, *nl_ifs;
1153 enum nl80211_iftype ift;
1154
1155 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1156 if (!nl_ifs)
1157 goto nla_put_failure;
1158
1159 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1160 nl_ftypes = nla_nest_start(msg, ift);
1161 if (!nl_ftypes)
1162 goto nla_put_failure;
1163 i = 0;
1164 stypes = mgmt_stypes[ift].tx;
1165 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001166 if ((stypes & 1) &&
1167 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1168 (i << 4) | IEEE80211_FTYPE_MGMT))
1169 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001170 stypes >>= 1;
1171 i++;
1172 }
1173 nla_nest_end(msg, nl_ftypes);
1174 }
1175
Johannes Berg74b70a42010-08-24 12:15:53 +02001176 nla_nest_end(msg, nl_ifs);
1177
Johannes Berg2e161f72010-08-12 15:38:38 +02001178 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1179 if (!nl_ifs)
1180 goto nla_put_failure;
1181
1182 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1183 nl_ftypes = nla_nest_start(msg, ift);
1184 if (!nl_ftypes)
1185 goto nla_put_failure;
1186 i = 0;
1187 stypes = mgmt_stypes[ift].rx;
1188 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001189 if ((stypes & 1) &&
1190 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1191 (i << 4) | IEEE80211_FTYPE_MGMT))
1192 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001193 stypes >>= 1;
1194 i++;
1195 }
1196 nla_nest_end(msg, nl_ftypes);
1197 }
1198 nla_nest_end(msg, nl_ifs);
1199 }
1200
Johannes Bergdfb89c52012-06-27 09:23:48 +02001201#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001202 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1203 struct nlattr *nl_wowlan;
1204
1205 nl_wowlan = nla_nest_start(msg,
1206 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1207 if (!nl_wowlan)
1208 goto nla_put_failure;
1209
David S. Miller9360ffd2012-03-29 04:41:26 -04001210 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1211 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1212 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1213 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1214 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1215 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1216 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1217 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1218 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1219 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1220 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1221 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1222 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1223 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1224 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1226 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001227 if (dev->wiphy.wowlan.n_patterns) {
1228 struct nl80211_wowlan_pattern_support pat = {
1229 .max_patterns = dev->wiphy.wowlan.n_patterns,
1230 .min_pattern_len =
1231 dev->wiphy.wowlan.pattern_min_len,
1232 .max_pattern_len =
1233 dev->wiphy.wowlan.pattern_max_len,
1234 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001235 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1236 sizeof(pat), &pat))
1237 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001238 }
1239
1240 nla_nest_end(msg, nl_wowlan);
1241 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001242#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001243
Johannes Berg7527a782011-05-13 10:58:57 +02001244 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1245 dev->wiphy.software_iftypes))
1246 goto nla_put_failure;
1247
1248 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1249 goto nla_put_failure;
1250
David S. Miller9360ffd2012-03-29 04:41:26 -04001251 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1252 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1253 dev->wiphy.ap_sme_capa))
1254 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001255
David S. Miller9360ffd2012-03-29 04:41:26 -04001256 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1257 dev->wiphy.features))
1258 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001259
David S. Miller9360ffd2012-03-29 04:41:26 -04001260 if (dev->wiphy.ht_capa_mod_mask &&
1261 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1262 sizeof(*dev->wiphy.ht_capa_mod_mask),
1263 dev->wiphy.ht_capa_mod_mask))
1264 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001265
Johannes Berg55682962007-09-20 13:09:35 -04001266 return genlmsg_end(msg, hdr);
1267
1268 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001269 genlmsg_cancel(msg, hdr);
1270 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001271}
1272
1273static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1274{
1275 int idx = 0;
1276 int start = cb->args[0];
1277 struct cfg80211_registered_device *dev;
1278
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001279 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001280 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001281 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1282 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001283 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001284 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001285 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001286 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001287 dev) < 0) {
1288 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001289 break;
Julius Volzb4637272008-07-08 14:02:19 +02001290 }
Johannes Berg55682962007-09-20 13:09:35 -04001291 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001292 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001293
1294 cb->args[0] = idx;
1295
1296 return skb->len;
1297}
1298
1299static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1300{
1301 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001302 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001303
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001305 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001306 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001307
Eric W. Biederman15e47302012-09-07 20:12:54 +00001308 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001309 nlmsg_free(msg);
1310 return -ENOBUFS;
1311 }
Johannes Berg55682962007-09-20 13:09:35 -04001312
Johannes Berg134e6372009-07-10 09:51:34 +00001313 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001314}
1315
Jouni Malinen31888482008-10-30 16:59:24 +02001316static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1317 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1318 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1319 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1320 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1321 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1322};
1323
1324static int parse_txq_params(struct nlattr *tb[],
1325 struct ieee80211_txq_params *txq_params)
1326{
Johannes Berga3304b02012-03-28 11:04:24 +02001327 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001328 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1329 !tb[NL80211_TXQ_ATTR_AIFS])
1330 return -EINVAL;
1331
Johannes Berga3304b02012-03-28 11:04:24 +02001332 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001333 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1334 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1335 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1336 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1337
Johannes Berga3304b02012-03-28 11:04:24 +02001338 if (txq_params->ac >= NL80211_NUM_ACS)
1339 return -EINVAL;
1340
Jouni Malinen31888482008-10-30 16:59:24 +02001341 return 0;
1342}
1343
Johannes Bergf444de02010-05-05 15:25:02 +02001344static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1345{
1346 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001347 * You can only set the channel explicitly for WDS interfaces,
1348 * all others have their channel managed via their respective
1349 * "establish a connection" command (connect, join, ...)
1350 *
1351 * For AP/GO and mesh mode, the channel can be set with the
1352 * channel userspace API, but is only stored and passed to the
1353 * low-level driver when the AP starts or the mesh is joined.
1354 * This is for backward compatibility, userspace can also give
1355 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001356 *
1357 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001358 * whatever else is going on, so they have their own special
1359 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001360 */
1361 return !wdev ||
1362 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001363 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001364 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1365 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001366}
1367
Johannes Berg683b6d32012-11-08 21:25:48 +01001368static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1369 struct genl_info *info,
1370 struct cfg80211_chan_def *chandef)
1371{
1372 struct ieee80211_sta_ht_cap *ht_cap;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001373 struct ieee80211_sta_vht_cap *vht_cap;
1374 u32 control_freq, width;
Johannes Berg683b6d32012-11-08 21:25:48 +01001375
1376 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1377 return -EINVAL;
1378
1379 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1380
1381 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001382 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1383 chandef->center_freq1 = control_freq;
1384 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001385
1386 /* Primary channel not allowed */
1387 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1388 return -EINVAL;
1389
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001390 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1391 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001392
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001393 chantype = nla_get_u32(
1394 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1395
1396 switch (chantype) {
1397 case NL80211_CHAN_NO_HT:
1398 case NL80211_CHAN_HT20:
1399 case NL80211_CHAN_HT40PLUS:
1400 case NL80211_CHAN_HT40MINUS:
1401 cfg80211_chandef_create(chandef, chandef->chan,
1402 chantype);
1403 break;
1404 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001405 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001406 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001407 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1408 chandef->width =
1409 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1410 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1411 chandef->center_freq1 =
1412 nla_get_u32(
1413 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1414 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1415 chandef->center_freq2 =
1416 nla_get_u32(
1417 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1418 }
1419
1420 ht_cap = &rdev->wiphy.bands[chandef->chan->band]->ht_cap;
1421 vht_cap = &rdev->wiphy.bands[chandef->chan->band]->vht_cap;
1422
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001423 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001424 return -EINVAL;
1425
1426 switch (chandef->width) {
1427 case NL80211_CHAN_WIDTH_20:
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001428 case NL80211_CHAN_WIDTH_20_NOHT:
1429 width = 20;
1430 break;
1431 case NL80211_CHAN_WIDTH_40:
1432 width = 40;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001433 break;
1434 case NL80211_CHAN_WIDTH_80:
1435 width = 80;
Johannes Berg683b6d32012-11-08 21:25:48 +01001436 break;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001437 case NL80211_CHAN_WIDTH_80P80:
1438 width = 80;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001439 break;
1440 case NL80211_CHAN_WIDTH_160:
1441 width = 160;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001442 break;
1443 default:
1444 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001445 }
1446
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001447 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1448 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001449 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001450
Johannes Berg683b6d32012-11-08 21:25:48 +01001451 return 0;
1452}
1453
Johannes Bergf444de02010-05-05 15:25:02 +02001454static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1455 struct wireless_dev *wdev,
1456 struct genl_info *info)
1457{
Johannes Berg683b6d32012-11-08 21:25:48 +01001458 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001459 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001460 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1461
1462 if (wdev)
1463 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001464
Johannes Bergf444de02010-05-05 15:25:02 +02001465 if (!nl80211_can_set_dev_channel(wdev))
1466 return -EOPNOTSUPP;
1467
Johannes Berg683b6d32012-11-08 21:25:48 +01001468 result = nl80211_parse_chandef(rdev, info, &chandef);
1469 if (result)
1470 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001471
1472 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001473 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001474 case NL80211_IFTYPE_AP:
1475 case NL80211_IFTYPE_P2P_GO:
1476 if (wdev->beacon_interval) {
1477 result = -EBUSY;
1478 break;
1479 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001480 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001481 result = -EINVAL;
1482 break;
1483 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001484 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001485 result = 0;
1486 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001487 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001488 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001489 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001490 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001491 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001492 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001493 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001494 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001495 }
1496 mutex_unlock(&rdev->devlist_mtx);
1497
1498 return result;
1499}
1500
1501static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1502{
Johannes Berg4c476992010-10-04 21:36:35 +02001503 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1504 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001505
Johannes Berg4c476992010-10-04 21:36:35 +02001506 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001507}
1508
Bill Jordane8347eb2010-10-01 13:54:28 -04001509static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1510{
Johannes Berg43b19952010-10-07 13:10:30 +02001511 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1512 struct net_device *dev = info->user_ptr[1];
1513 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001514 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001515
1516 if (!info->attrs[NL80211_ATTR_MAC])
1517 return -EINVAL;
1518
Johannes Berg43b19952010-10-07 13:10:30 +02001519 if (netif_running(dev))
1520 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001521
Johannes Berg43b19952010-10-07 13:10:30 +02001522 if (!rdev->ops->set_wds_peer)
1523 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001524
Johannes Berg43b19952010-10-07 13:10:30 +02001525 if (wdev->iftype != NL80211_IFTYPE_WDS)
1526 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001527
1528 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001529 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001530}
1531
1532
Johannes Berg55682962007-09-20 13:09:35 -04001533static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1534{
1535 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001536 struct net_device *netdev = NULL;
1537 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001538 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001539 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001540 u32 changed;
1541 u8 retry_short = 0, retry_long = 0;
1542 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001543 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001544
Johannes Bergf444de02010-05-05 15:25:02 +02001545 /*
1546 * Try to find the wiphy and netdev. Normally this
1547 * function shouldn't need the netdev, but this is
1548 * done for backward compatibility -- previously
1549 * setting the channel was done per wiphy, but now
1550 * it is per netdev. Previous userland like hostapd
1551 * also passed a netdev to set_wiphy, so that it is
1552 * possible to let that go to the right netdev!
1553 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001554 mutex_lock(&cfg80211_mutex);
1555
Johannes Bergf444de02010-05-05 15:25:02 +02001556 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1557 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1558
1559 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1560 if (netdev && netdev->ieee80211_ptr) {
1561 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1562 mutex_lock(&rdev->mtx);
1563 } else
1564 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001565 }
1566
Johannes Bergf444de02010-05-05 15:25:02 +02001567 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001568 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1569 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001570 if (IS_ERR(rdev)) {
1571 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001572 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001573 }
1574 wdev = NULL;
1575 netdev = NULL;
1576 result = 0;
1577
1578 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001579 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001580 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001581
1582 /*
1583 * end workaround code, by now the rdev is available
1584 * and locked, and wdev may or may not be NULL.
1585 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001586
1587 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001588 result = cfg80211_dev_rename(
1589 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001590
1591 mutex_unlock(&cfg80211_mutex);
1592
1593 if (result)
1594 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001595
Jouni Malinen31888482008-10-30 16:59:24 +02001596 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1597 struct ieee80211_txq_params txq_params;
1598 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1599
1600 if (!rdev->ops->set_txq_params) {
1601 result = -EOPNOTSUPP;
1602 goto bad_res;
1603 }
1604
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001605 if (!netdev) {
1606 result = -EINVAL;
1607 goto bad_res;
1608 }
1609
Johannes Berg133a3ff2011-11-03 14:50:13 +01001610 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1611 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1612 result = -EINVAL;
1613 goto bad_res;
1614 }
1615
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001616 if (!netif_running(netdev)) {
1617 result = -ENETDOWN;
1618 goto bad_res;
1619 }
1620
Jouni Malinen31888482008-10-30 16:59:24 +02001621 nla_for_each_nested(nl_txq_params,
1622 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1623 rem_txq_params) {
1624 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1625 nla_data(nl_txq_params),
1626 nla_len(nl_txq_params),
1627 txq_params_policy);
1628 result = parse_txq_params(tb, &txq_params);
1629 if (result)
1630 goto bad_res;
1631
Hila Gonene35e4d22012-06-27 17:19:42 +03001632 result = rdev_set_txq_params(rdev, netdev,
1633 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001634 if (result)
1635 goto bad_res;
1636 }
1637 }
1638
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001639 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001640 result = __nl80211_set_channel(rdev,
1641 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1642 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001643 if (result)
1644 goto bad_res;
1645 }
1646
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001647 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001648 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001649 enum nl80211_tx_power_setting type;
1650 int idx, mbm = 0;
1651
Johannes Bergc8442112012-10-24 10:17:18 +02001652 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1653 txp_wdev = NULL;
1654
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001655 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001656 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001657 goto bad_res;
1658 }
1659
1660 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1661 type = nla_get_u32(info->attrs[idx]);
1662
1663 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1664 (type != NL80211_TX_POWER_AUTOMATIC)) {
1665 result = -EINVAL;
1666 goto bad_res;
1667 }
1668
1669 if (type != NL80211_TX_POWER_AUTOMATIC) {
1670 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1671 mbm = nla_get_u32(info->attrs[idx]);
1672 }
1673
Johannes Bergc8442112012-10-24 10:17:18 +02001674 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001675 if (result)
1676 goto bad_res;
1677 }
1678
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001679 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1680 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1681 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001682 if ((!rdev->wiphy.available_antennas_tx &&
1683 !rdev->wiphy.available_antennas_rx) ||
1684 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001685 result = -EOPNOTSUPP;
1686 goto bad_res;
1687 }
1688
1689 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1690 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1691
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001692 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001693 * available antenna masks, except for the "all" mask */
1694 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1695 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001696 result = -EINVAL;
1697 goto bad_res;
1698 }
1699
Bruno Randolf7f531e02010-12-16 11:30:22 +09001700 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1701 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001702
Hila Gonene35e4d22012-06-27 17:19:42 +03001703 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001704 if (result)
1705 goto bad_res;
1706 }
1707
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001708 changed = 0;
1709
1710 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1711 retry_short = nla_get_u8(
1712 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1713 if (retry_short == 0) {
1714 result = -EINVAL;
1715 goto bad_res;
1716 }
1717 changed |= WIPHY_PARAM_RETRY_SHORT;
1718 }
1719
1720 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1721 retry_long = nla_get_u8(
1722 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1723 if (retry_long == 0) {
1724 result = -EINVAL;
1725 goto bad_res;
1726 }
1727 changed |= WIPHY_PARAM_RETRY_LONG;
1728 }
1729
1730 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1731 frag_threshold = nla_get_u32(
1732 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1733 if (frag_threshold < 256) {
1734 result = -EINVAL;
1735 goto bad_res;
1736 }
1737 if (frag_threshold != (u32) -1) {
1738 /*
1739 * Fragments (apart from the last one) are required to
1740 * have even length. Make the fragmentation code
1741 * simpler by stripping LSB should someone try to use
1742 * odd threshold value.
1743 */
1744 frag_threshold &= ~0x1;
1745 }
1746 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1747 }
1748
1749 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1750 rts_threshold = nla_get_u32(
1751 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1752 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1753 }
1754
Lukáš Turek81077e82009-12-21 22:50:47 +01001755 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1756 coverage_class = nla_get_u8(
1757 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1758 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1759 }
1760
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001761 if (changed) {
1762 u8 old_retry_short, old_retry_long;
1763 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001764 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001765
1766 if (!rdev->ops->set_wiphy_params) {
1767 result = -EOPNOTSUPP;
1768 goto bad_res;
1769 }
1770
1771 old_retry_short = rdev->wiphy.retry_short;
1772 old_retry_long = rdev->wiphy.retry_long;
1773 old_frag_threshold = rdev->wiphy.frag_threshold;
1774 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001775 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001776
1777 if (changed & WIPHY_PARAM_RETRY_SHORT)
1778 rdev->wiphy.retry_short = retry_short;
1779 if (changed & WIPHY_PARAM_RETRY_LONG)
1780 rdev->wiphy.retry_long = retry_long;
1781 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1782 rdev->wiphy.frag_threshold = frag_threshold;
1783 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1784 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001785 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1786 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001787
Hila Gonene35e4d22012-06-27 17:19:42 +03001788 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001789 if (result) {
1790 rdev->wiphy.retry_short = old_retry_short;
1791 rdev->wiphy.retry_long = old_retry_long;
1792 rdev->wiphy.frag_threshold = old_frag_threshold;
1793 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001794 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001795 }
1796 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001797
Johannes Berg306d6112008-12-08 12:39:04 +01001798 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001799 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001800 if (netdev)
1801 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001802 return result;
1803}
1804
Johannes Berg71bbc992012-06-15 15:30:18 +02001805static inline u64 wdev_id(struct wireless_dev *wdev)
1806{
1807 return (u64)wdev->identifier |
1808 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1809}
Johannes Berg55682962007-09-20 13:09:35 -04001810
Johannes Berg683b6d32012-11-08 21:25:48 +01001811static int nl80211_send_chandef(struct sk_buff *msg,
1812 struct cfg80211_chan_def *chandef)
1813{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001814 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001815
Johannes Berg683b6d32012-11-08 21:25:48 +01001816 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1817 chandef->chan->center_freq))
1818 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001819 switch (chandef->width) {
1820 case NL80211_CHAN_WIDTH_20_NOHT:
1821 case NL80211_CHAN_WIDTH_20:
1822 case NL80211_CHAN_WIDTH_40:
1823 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1824 cfg80211_get_chandef_type(chandef)))
1825 return -ENOBUFS;
1826 break;
1827 default:
1828 break;
1829 }
1830 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1831 return -ENOBUFS;
1832 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1833 return -ENOBUFS;
1834 if (chandef->center_freq2 &&
1835 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001836 return -ENOBUFS;
1837 return 0;
1838}
1839
Eric W. Biederman15e47302012-09-07 20:12:54 +00001840static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001841 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001842 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001843{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001844 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001845 void *hdr;
1846
Eric W. Biederman15e47302012-09-07 20:12:54 +00001847 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001848 if (!hdr)
1849 return -1;
1850
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001851 if (dev &&
1852 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001853 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001854 goto nla_put_failure;
1855
1856 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1857 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001858 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001859 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001860 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1861 rdev->devlist_generation ^
1862 (cfg80211_rdev_list_generation << 2)))
1863 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001864
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001865 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001866 int ret;
1867 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001868
Johannes Berg683b6d32012-11-08 21:25:48 +01001869 ret = rdev_get_channel(rdev, wdev, &chandef);
1870 if (ret == 0) {
1871 if (nl80211_send_chandef(msg, &chandef))
1872 goto nla_put_failure;
1873 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001874 }
1875
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001876 if (wdev->ssid_len) {
1877 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1878 goto nla_put_failure;
1879 }
1880
Johannes Berg55682962007-09-20 13:09:35 -04001881 return genlmsg_end(msg, hdr);
1882
1883 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001884 genlmsg_cancel(msg, hdr);
1885 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001886}
1887
1888static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1889{
1890 int wp_idx = 0;
1891 int if_idx = 0;
1892 int wp_start = cb->args[0];
1893 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001894 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001895 struct wireless_dev *wdev;
1896
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001897 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001898 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1899 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001900 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001901 if (wp_idx < wp_start) {
1902 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001903 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001904 }
Johannes Berg55682962007-09-20 13:09:35 -04001905 if_idx = 0;
1906
Johannes Bergf5ea9122009-08-07 16:17:38 +02001907 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001908 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001909 if (if_idx < if_start) {
1910 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001911 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001912 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001913 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001914 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001915 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001916 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001917 goto out;
1918 }
1919 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001920 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001921 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001922
1923 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001924 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001925 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001926 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001927
1928 cb->args[0] = wp_idx;
1929 cb->args[1] = if_idx;
1930
1931 return skb->len;
1932}
1933
1934static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1935{
1936 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001937 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001938 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001939
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001940 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001941 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001942 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001943
Eric W. Biederman15e47302012-09-07 20:12:54 +00001944 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001945 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001946 nlmsg_free(msg);
1947 return -ENOBUFS;
1948 }
Johannes Berg55682962007-09-20 13:09:35 -04001949
Johannes Berg134e6372009-07-10 09:51:34 +00001950 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001951}
1952
Michael Wu66f7ac52008-01-31 19:48:22 +01001953static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1954 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1955 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1956 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1957 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1958 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1959};
1960
1961static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1962{
1963 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1964 int flag;
1965
1966 *mntrflags = 0;
1967
1968 if (!nla)
1969 return -EINVAL;
1970
1971 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1972 nla, mntr_flags_policy))
1973 return -EINVAL;
1974
1975 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1976 if (flags[flag])
1977 *mntrflags |= (1<<flag);
1978
1979 return 0;
1980}
1981
Johannes Berg9bc383d2009-11-19 11:55:19 +01001982static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001983 struct net_device *netdev, u8 use_4addr,
1984 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001985{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001986 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001987 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001988 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001989 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001990 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001991
1992 switch (iftype) {
1993 case NL80211_IFTYPE_AP_VLAN:
1994 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1995 return 0;
1996 break;
1997 case NL80211_IFTYPE_STATION:
1998 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1999 return 0;
2000 break;
2001 default:
2002 break;
2003 }
2004
2005 return -EOPNOTSUPP;
2006}
2007
Johannes Berg55682962007-09-20 13:09:35 -04002008static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2009{
Johannes Berg4c476992010-10-04 21:36:35 +02002010 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002011 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002012 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002013 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002014 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002015 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002016 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002017
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002018 memset(&params, 0, sizeof(params));
2019
Johannes Berg04a773a2009-04-19 21:24:32 +02002020 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002021
Johannes Berg723b0382008-09-16 20:22:09 +02002022 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002023 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002024 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002025 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002026 if (ntype > NL80211_IFTYPE_MAX)
2027 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002028 }
2029
Johannes Berg92ffe052008-09-16 20:39:36 +02002030 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002031 struct wireless_dev *wdev = dev->ieee80211_ptr;
2032
Johannes Berg4c476992010-10-04 21:36:35 +02002033 if (ntype != NL80211_IFTYPE_MESH_POINT)
2034 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002035 if (netif_running(dev))
2036 return -EBUSY;
2037
2038 wdev_lock(wdev);
2039 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2040 IEEE80211_MAX_MESH_ID_LEN);
2041 wdev->mesh_id_up_len =
2042 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2043 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2044 wdev->mesh_id_up_len);
2045 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002046 }
2047
Felix Fietkau8b787642009-11-10 18:53:10 +01002048 if (info->attrs[NL80211_ATTR_4ADDR]) {
2049 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2050 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002051 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002052 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002053 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002054 } else {
2055 params.use_4addr = -1;
2056 }
2057
Johannes Berg92ffe052008-09-16 20:39:36 +02002058 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002059 if (ntype != NL80211_IFTYPE_MONITOR)
2060 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002061 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2062 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002063 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002064 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002065
2066 flags = &_flags;
2067 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002068 }
Johannes Berg3b858752009-03-12 09:55:09 +01002069
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002070 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002071 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002072 else
2073 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002074
Johannes Berg9bc383d2009-11-19 11:55:19 +01002075 if (!err && params.use_4addr != -1)
2076 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2077
Johannes Berg55682962007-09-20 13:09:35 -04002078 return err;
2079}
2080
2081static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2082{
Johannes Berg4c476992010-10-04 21:36:35 +02002083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002084 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002085 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002086 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002087 int err;
2088 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002089 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002090
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002091 memset(&params, 0, sizeof(params));
2092
Johannes Berg55682962007-09-20 13:09:35 -04002093 if (!info->attrs[NL80211_ATTR_IFNAME])
2094 return -EINVAL;
2095
2096 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2097 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2098 if (type > NL80211_IFTYPE_MAX)
2099 return -EINVAL;
2100 }
2101
Johannes Berg79c97e92009-07-07 03:56:12 +02002102 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002103 !(rdev->wiphy.interface_modes & (1 << type)))
2104 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002105
Johannes Berg9bc383d2009-11-19 11:55:19 +01002106 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002107 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002108 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002109 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002110 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002111 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002112
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002113 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2114 if (!msg)
2115 return -ENOMEM;
2116
Michael Wu66f7ac52008-01-31 19:48:22 +01002117 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2118 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2119 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002120 wdev = rdev_add_virtual_intf(rdev,
2121 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2122 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002123 if (IS_ERR(wdev)) {
2124 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002125 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002126 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002127
Johannes Berg98104fde2012-06-16 00:19:54 +02002128 switch (type) {
2129 case NL80211_IFTYPE_MESH_POINT:
2130 if (!info->attrs[NL80211_ATTR_MESH_ID])
2131 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002132 wdev_lock(wdev);
2133 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2134 IEEE80211_MAX_MESH_ID_LEN);
2135 wdev->mesh_id_up_len =
2136 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2137 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2138 wdev->mesh_id_up_len);
2139 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002140 break;
2141 case NL80211_IFTYPE_P2P_DEVICE:
2142 /*
2143 * P2P Device doesn't have a netdev, so doesn't go
2144 * through the netdev notifier and must be added here
2145 */
2146 mutex_init(&wdev->mtx);
2147 INIT_LIST_HEAD(&wdev->event_list);
2148 spin_lock_init(&wdev->event_lock);
2149 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2150 spin_lock_init(&wdev->mgmt_registrations_lock);
2151
2152 mutex_lock(&rdev->devlist_mtx);
2153 wdev->identifier = ++rdev->wdev_id;
2154 list_add_rcu(&wdev->list, &rdev->wdev_list);
2155 rdev->devlist_generation++;
2156 mutex_unlock(&rdev->devlist_mtx);
2157 break;
2158 default:
2159 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002160 }
2161
Eric W. Biederman15e47302012-09-07 20:12:54 +00002162 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002163 rdev, wdev) < 0) {
2164 nlmsg_free(msg);
2165 return -ENOBUFS;
2166 }
2167
2168 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002169}
2170
2171static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2172{
Johannes Berg4c476992010-10-04 21:36:35 +02002173 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002174 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002175
Johannes Berg4c476992010-10-04 21:36:35 +02002176 if (!rdev->ops->del_virtual_intf)
2177 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002178
Johannes Berg84efbb82012-06-16 00:00:26 +02002179 /*
2180 * If we remove a wireless device without a netdev then clear
2181 * user_ptr[1] so that nl80211_post_doit won't dereference it
2182 * to check if it needs to do dev_put(). Otherwise it crashes
2183 * since the wdev has been freed, unlike with a netdev where
2184 * we need the dev_put() for the netdev to really be freed.
2185 */
2186 if (!wdev->netdev)
2187 info->user_ptr[1] = NULL;
2188
Hila Gonene35e4d22012-06-27 17:19:42 +03002189 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002190}
2191
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002192static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2193{
2194 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2195 struct net_device *dev = info->user_ptr[1];
2196 u16 noack_map;
2197
2198 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2199 return -EINVAL;
2200
2201 if (!rdev->ops->set_noack_map)
2202 return -EOPNOTSUPP;
2203
2204 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2205
Hila Gonene35e4d22012-06-27 17:19:42 +03002206 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002207}
2208
Johannes Berg41ade002007-12-19 02:03:29 +01002209struct get_key_cookie {
2210 struct sk_buff *msg;
2211 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002212 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002213};
2214
2215static void get_key_callback(void *c, struct key_params *params)
2216{
Johannes Bergb9454e82009-07-08 13:29:08 +02002217 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002218 struct get_key_cookie *cookie = c;
2219
David S. Miller9360ffd2012-03-29 04:41:26 -04002220 if ((params->key &&
2221 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2222 params->key_len, params->key)) ||
2223 (params->seq &&
2224 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2225 params->seq_len, params->seq)) ||
2226 (params->cipher &&
2227 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2228 params->cipher)))
2229 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002230
Johannes Bergb9454e82009-07-08 13:29:08 +02002231 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2232 if (!key)
2233 goto nla_put_failure;
2234
David S. Miller9360ffd2012-03-29 04:41:26 -04002235 if ((params->key &&
2236 nla_put(cookie->msg, NL80211_KEY_DATA,
2237 params->key_len, params->key)) ||
2238 (params->seq &&
2239 nla_put(cookie->msg, NL80211_KEY_SEQ,
2240 params->seq_len, params->seq)) ||
2241 (params->cipher &&
2242 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2243 params->cipher)))
2244 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002245
David S. Miller9360ffd2012-03-29 04:41:26 -04002246 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2247 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002248
2249 nla_nest_end(cookie->msg, key);
2250
Johannes Berg41ade002007-12-19 02:03:29 +01002251 return;
2252 nla_put_failure:
2253 cookie->error = 1;
2254}
2255
2256static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2257{
Johannes Berg4c476992010-10-04 21:36:35 +02002258 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002259 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002260 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002261 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002262 const u8 *mac_addr = NULL;
2263 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002264 struct get_key_cookie cookie = {
2265 .error = 0,
2266 };
2267 void *hdr;
2268 struct sk_buff *msg;
2269
2270 if (info->attrs[NL80211_ATTR_KEY_IDX])
2271 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2272
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002273 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002274 return -EINVAL;
2275
2276 if (info->attrs[NL80211_ATTR_MAC])
2277 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2278
Johannes Berge31b8212010-10-05 19:39:30 +02002279 pairwise = !!mac_addr;
2280 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2281 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2282 if (kt >= NUM_NL80211_KEYTYPES)
2283 return -EINVAL;
2284 if (kt != NL80211_KEYTYPE_GROUP &&
2285 kt != NL80211_KEYTYPE_PAIRWISE)
2286 return -EINVAL;
2287 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2288 }
2289
Johannes Berg4c476992010-10-04 21:36:35 +02002290 if (!rdev->ops->get_key)
2291 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002292
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002294 if (!msg)
2295 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002296
Eric W. Biederman15e47302012-09-07 20:12:54 +00002297 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002298 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002299 if (IS_ERR(hdr))
2300 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002301
2302 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002303 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002304
David S. Miller9360ffd2012-03-29 04:41:26 -04002305 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2306 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2307 goto nla_put_failure;
2308 if (mac_addr &&
2309 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2310 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002311
Johannes Berge31b8212010-10-05 19:39:30 +02002312 if (pairwise && mac_addr &&
2313 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2314 return -ENOENT;
2315
Hila Gonene35e4d22012-06-27 17:19:42 +03002316 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2317 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002318
2319 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002320 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002321
2322 if (cookie.error)
2323 goto nla_put_failure;
2324
2325 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002326 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002327
2328 nla_put_failure:
2329 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002330 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002331 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002332 return err;
2333}
2334
2335static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2336{
Johannes Berg4c476992010-10-04 21:36:35 +02002337 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002338 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002339 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002340 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002341
Johannes Bergb9454e82009-07-08 13:29:08 +02002342 err = nl80211_parse_key(info, &key);
2343 if (err)
2344 return err;
2345
2346 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002347 return -EINVAL;
2348
Johannes Bergb9454e82009-07-08 13:29:08 +02002349 /* only support setting default key */
2350 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002351 return -EINVAL;
2352
Johannes Bergfffd0932009-07-08 14:22:54 +02002353 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002354
2355 if (key.def) {
2356 if (!rdev->ops->set_default_key) {
2357 err = -EOPNOTSUPP;
2358 goto out;
2359 }
2360
2361 err = nl80211_key_allowed(dev->ieee80211_ptr);
2362 if (err)
2363 goto out;
2364
Hila Gonene35e4d22012-06-27 17:19:42 +03002365 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002366 key.def_uni, key.def_multi);
2367
2368 if (err)
2369 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002370
Johannes Berg3d23e342009-09-29 23:27:28 +02002371#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002372 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002373#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002374 } else {
2375 if (key.def_uni || !key.def_multi) {
2376 err = -EINVAL;
2377 goto out;
2378 }
2379
2380 if (!rdev->ops->set_default_mgmt_key) {
2381 err = -EOPNOTSUPP;
2382 goto out;
2383 }
2384
2385 err = nl80211_key_allowed(dev->ieee80211_ptr);
2386 if (err)
2387 goto out;
2388
Hila Gonene35e4d22012-06-27 17:19:42 +03002389 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002390 if (err)
2391 goto out;
2392
2393#ifdef CONFIG_CFG80211_WEXT
2394 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2395#endif
2396 }
2397
2398 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002399 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002400
Johannes Berg41ade002007-12-19 02:03:29 +01002401 return err;
2402}
2403
2404static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2405{
Johannes Berg4c476992010-10-04 21:36:35 +02002406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002407 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002408 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002409 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002410 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002411
Johannes Bergb9454e82009-07-08 13:29:08 +02002412 err = nl80211_parse_key(info, &key);
2413 if (err)
2414 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002415
Johannes Bergb9454e82009-07-08 13:29:08 +02002416 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002417 return -EINVAL;
2418
Johannes Berg41ade002007-12-19 02:03:29 +01002419 if (info->attrs[NL80211_ATTR_MAC])
2420 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2421
Johannes Berge31b8212010-10-05 19:39:30 +02002422 if (key.type == -1) {
2423 if (mac_addr)
2424 key.type = NL80211_KEYTYPE_PAIRWISE;
2425 else
2426 key.type = NL80211_KEYTYPE_GROUP;
2427 }
2428
2429 /* for now */
2430 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2431 key.type != NL80211_KEYTYPE_GROUP)
2432 return -EINVAL;
2433
Johannes Berg4c476992010-10-04 21:36:35 +02002434 if (!rdev->ops->add_key)
2435 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002436
Johannes Berge31b8212010-10-05 19:39:30 +02002437 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2438 key.type == NL80211_KEYTYPE_PAIRWISE,
2439 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002440 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002441
2442 wdev_lock(dev->ieee80211_ptr);
2443 err = nl80211_key_allowed(dev->ieee80211_ptr);
2444 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002445 err = rdev_add_key(rdev, dev, key.idx,
2446 key.type == NL80211_KEYTYPE_PAIRWISE,
2447 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002448 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002449
Johannes Berg41ade002007-12-19 02:03:29 +01002450 return err;
2451}
2452
2453static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2454{
Johannes Berg4c476992010-10-04 21:36:35 +02002455 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002456 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002457 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002458 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002459 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002460
Johannes Bergb9454e82009-07-08 13:29:08 +02002461 err = nl80211_parse_key(info, &key);
2462 if (err)
2463 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002464
2465 if (info->attrs[NL80211_ATTR_MAC])
2466 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2467
Johannes Berge31b8212010-10-05 19:39:30 +02002468 if (key.type == -1) {
2469 if (mac_addr)
2470 key.type = NL80211_KEYTYPE_PAIRWISE;
2471 else
2472 key.type = NL80211_KEYTYPE_GROUP;
2473 }
2474
2475 /* for now */
2476 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2477 key.type != NL80211_KEYTYPE_GROUP)
2478 return -EINVAL;
2479
Johannes Berg4c476992010-10-04 21:36:35 +02002480 if (!rdev->ops->del_key)
2481 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002482
Johannes Bergfffd0932009-07-08 14:22:54 +02002483 wdev_lock(dev->ieee80211_ptr);
2484 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002485
2486 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2487 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2488 err = -ENOENT;
2489
Johannes Bergfffd0932009-07-08 14:22:54 +02002490 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002491 err = rdev_del_key(rdev, dev, key.idx,
2492 key.type == NL80211_KEYTYPE_PAIRWISE,
2493 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002494
Johannes Berg3d23e342009-09-29 23:27:28 +02002495#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002496 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002497 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002498 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002499 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002500 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2501 }
2502#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002503 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002504
Johannes Berg41ade002007-12-19 02:03:29 +01002505 return err;
2506}
2507
Johannes Berg88600202012-02-13 15:17:18 +01002508static int nl80211_parse_beacon(struct genl_info *info,
2509 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002510{
Johannes Berg88600202012-02-13 15:17:18 +01002511 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002512
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002513 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2514 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2515 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2516 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002517 return -EINVAL;
2518
Johannes Berg88600202012-02-13 15:17:18 +01002519 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002520
Johannes Berged1b6cc2007-12-19 02:03:32 +01002521 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002522 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2523 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2524 if (!bcn->head_len)
2525 return -EINVAL;
2526 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002527 }
2528
2529 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002530 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2531 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002532 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002533 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002534 }
2535
Johannes Berg4c476992010-10-04 21:36:35 +02002536 if (!haveinfo)
2537 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002538
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002539 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002540 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2541 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002542 }
2543
2544 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002545 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002546 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002547 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002548 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2549 }
2550
2551 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002552 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002553 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002554 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002555 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2556 }
2557
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002558 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002559 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002560 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002561 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002562 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2563 }
2564
Johannes Berg88600202012-02-13 15:17:18 +01002565 return 0;
2566}
2567
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002568static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2569 struct cfg80211_ap_settings *params)
2570{
2571 struct wireless_dev *wdev;
2572 bool ret = false;
2573
2574 mutex_lock(&rdev->devlist_mtx);
2575
Johannes Berg89a54e42012-06-15 14:33:17 +02002576 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002577 if (wdev->iftype != NL80211_IFTYPE_AP &&
2578 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2579 continue;
2580
Johannes Berg683b6d32012-11-08 21:25:48 +01002581 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002582 continue;
2583
Johannes Berg683b6d32012-11-08 21:25:48 +01002584 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002585 ret = true;
2586 break;
2587 }
2588
2589 mutex_unlock(&rdev->devlist_mtx);
2590
2591 return ret;
2592}
2593
Jouni Malinene39e5b52012-09-30 19:29:39 +03002594static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2595 enum nl80211_auth_type auth_type,
2596 enum nl80211_commands cmd)
2597{
2598 if (auth_type > NL80211_AUTHTYPE_MAX)
2599 return false;
2600
2601 switch (cmd) {
2602 case NL80211_CMD_AUTHENTICATE:
2603 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2604 auth_type == NL80211_AUTHTYPE_SAE)
2605 return false;
2606 return true;
2607 case NL80211_CMD_CONNECT:
2608 case NL80211_CMD_START_AP:
2609 /* SAE not supported yet */
2610 if (auth_type == NL80211_AUTHTYPE_SAE)
2611 return false;
2612 return true;
2613 default:
2614 return false;
2615 }
2616}
2617
Johannes Berg88600202012-02-13 15:17:18 +01002618static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2619{
2620 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2621 struct net_device *dev = info->user_ptr[1];
2622 struct wireless_dev *wdev = dev->ieee80211_ptr;
2623 struct cfg80211_ap_settings params;
2624 int err;
2625
2626 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2627 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2628 return -EOPNOTSUPP;
2629
2630 if (!rdev->ops->start_ap)
2631 return -EOPNOTSUPP;
2632
2633 if (wdev->beacon_interval)
2634 return -EALREADY;
2635
2636 memset(&params, 0, sizeof(params));
2637
2638 /* these are required for START_AP */
2639 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2640 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2641 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2642 return -EINVAL;
2643
2644 err = nl80211_parse_beacon(info, &params.beacon);
2645 if (err)
2646 return err;
2647
2648 params.beacon_interval =
2649 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2650 params.dtim_period =
2651 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2652
2653 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2654 if (err)
2655 return err;
2656
2657 /*
2658 * In theory, some of these attributes should be required here
2659 * but since they were not used when the command was originally
2660 * added, keep them optional for old user space programs to let
2661 * them continue to work with drivers that do not need the
2662 * additional information -- drivers must check!
2663 */
2664 if (info->attrs[NL80211_ATTR_SSID]) {
2665 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2666 params.ssid_len =
2667 nla_len(info->attrs[NL80211_ATTR_SSID]);
2668 if (params.ssid_len == 0 ||
2669 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2670 return -EINVAL;
2671 }
2672
2673 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2674 params.hidden_ssid = nla_get_u32(
2675 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2676 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2677 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2678 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2679 return -EINVAL;
2680 }
2681
2682 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2683
2684 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2685 params.auth_type = nla_get_u32(
2686 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002687 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2688 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002689 return -EINVAL;
2690 } else
2691 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2692
2693 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2694 NL80211_MAX_NR_CIPHER_SUITES);
2695 if (err)
2696 return err;
2697
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302698 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2699 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2700 return -EOPNOTSUPP;
2701 params.inactivity_timeout = nla_get_u16(
2702 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2703 }
2704
Johannes Bergaa430da2012-05-16 23:50:18 +02002705 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002706 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2707 if (err)
2708 return err;
2709 } else if (wdev->preset_chandef.chan) {
2710 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002711 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002712 return -EINVAL;
2713
Johannes Berg683b6d32012-11-08 21:25:48 +01002714 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002715 return -EINVAL;
2716
Michal Kaziore4e32452012-06-29 12:47:08 +02002717 mutex_lock(&rdev->devlist_mtx);
Johannes Berg683b6d32012-11-08 21:25:48 +01002718 err = cfg80211_can_use_chan(rdev, wdev, params.chandef.chan,
Michal Kaziore4e32452012-06-29 12:47:08 +02002719 CHAN_MODE_SHARED);
2720 mutex_unlock(&rdev->devlist_mtx);
2721
2722 if (err)
2723 return err;
2724
Hila Gonene35e4d22012-06-27 17:19:42 +03002725 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002726 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002727 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002728 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002729 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002730 wdev->ssid_len = params.ssid_len;
2731 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002732 }
Johannes Berg56d18932011-05-09 18:41:15 +02002733 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002734}
2735
Johannes Berg88600202012-02-13 15:17:18 +01002736static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2737{
2738 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2739 struct net_device *dev = info->user_ptr[1];
2740 struct wireless_dev *wdev = dev->ieee80211_ptr;
2741 struct cfg80211_beacon_data params;
2742 int err;
2743
2744 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2745 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2746 return -EOPNOTSUPP;
2747
2748 if (!rdev->ops->change_beacon)
2749 return -EOPNOTSUPP;
2750
2751 if (!wdev->beacon_interval)
2752 return -EINVAL;
2753
2754 err = nl80211_parse_beacon(info, &params);
2755 if (err)
2756 return err;
2757
Hila Gonene35e4d22012-06-27 17:19:42 +03002758 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002759}
2760
2761static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002762{
Johannes Berg4c476992010-10-04 21:36:35 +02002763 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2764 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002765
Michal Kazior60771782012-06-29 12:46:56 +02002766 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002767}
2768
Johannes Berg5727ef12007-12-19 02:03:34 +01002769static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2770 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2771 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2772 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002773 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002774 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002775 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002776};
2777
Johannes Bergeccb8e82009-05-11 21:57:56 +03002778static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002779 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002780 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002781{
2782 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002783 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002784 int flag;
2785
Johannes Bergeccb8e82009-05-11 21:57:56 +03002786 /*
2787 * Try parsing the new attribute first so userspace
2788 * can specify both for older kernels.
2789 */
2790 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2791 if (nla) {
2792 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002793
Johannes Bergeccb8e82009-05-11 21:57:56 +03002794 sta_flags = nla_data(nla);
2795 params->sta_flags_mask = sta_flags->mask;
2796 params->sta_flags_set = sta_flags->set;
2797 if ((params->sta_flags_mask |
2798 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2799 return -EINVAL;
2800 return 0;
2801 }
2802
2803 /* if present, parse the old attribute */
2804
2805 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002806 if (!nla)
2807 return 0;
2808
2809 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2810 nla, sta_flags_policy))
2811 return -EINVAL;
2812
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002813 /*
2814 * Only allow certain flags for interface types so that
2815 * other attributes are silently ignored. Remember that
2816 * this is backward compatibility code with old userspace
2817 * and shouldn't be hit in other cases anyway.
2818 */
2819 switch (iftype) {
2820 case NL80211_IFTYPE_AP:
2821 case NL80211_IFTYPE_AP_VLAN:
2822 case NL80211_IFTYPE_P2P_GO:
2823 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2824 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2825 BIT(NL80211_STA_FLAG_WME) |
2826 BIT(NL80211_STA_FLAG_MFP);
2827 break;
2828 case NL80211_IFTYPE_P2P_CLIENT:
2829 case NL80211_IFTYPE_STATION:
2830 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2831 BIT(NL80211_STA_FLAG_TDLS_PEER);
2832 break;
2833 case NL80211_IFTYPE_MESH_POINT:
2834 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2835 BIT(NL80211_STA_FLAG_MFP) |
2836 BIT(NL80211_STA_FLAG_AUTHORIZED);
2837 default:
2838 return -EINVAL;
2839 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002840
Johannes Berg3383b5a2012-05-10 20:14:43 +02002841 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2842 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002843 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002844
Johannes Berg3383b5a2012-05-10 20:14:43 +02002845 /* no longer support new API additions in old API */
2846 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2847 return -EINVAL;
2848 }
2849 }
2850
Johannes Berg5727ef12007-12-19 02:03:34 +01002851 return 0;
2852}
2853
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002854static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2855 int attr)
2856{
2857 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002858 u32 bitrate;
2859 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002860
2861 rate = nla_nest_start(msg, attr);
2862 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002863 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002864
2865 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2866 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002867 /* report 16-bit bitrate only if we can */
2868 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002869 if (bitrate > 0 &&
2870 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
2871 return false;
2872 if (bitrate_compat > 0 &&
2873 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
2874 return false;
2875
2876 if (info->flags & RATE_INFO_FLAGS_MCS) {
2877 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
2878 return false;
2879 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2880 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2881 return false;
2882 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2883 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2884 return false;
2885 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
2886 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
2887 return false;
2888 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
2889 return false;
2890 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2891 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2892 return false;
2893 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
2894 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
2895 return false;
2896 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
2897 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
2898 return false;
2899 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
2900 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
2901 return false;
2902 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2903 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2904 return false;
2905 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002906
2907 nla_nest_end(msg, rate);
2908 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002909}
2910
Eric W. Biederman15e47302012-09-07 20:12:54 +00002911static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002912 int flags,
2913 struct cfg80211_registered_device *rdev,
2914 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002915 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002916{
2917 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002918 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002919
Eric W. Biederman15e47302012-09-07 20:12:54 +00002920 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002921 if (!hdr)
2922 return -1;
2923
David S. Miller9360ffd2012-03-29 04:41:26 -04002924 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2925 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2926 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2927 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002928
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002929 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2930 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002931 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002932 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2933 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2934 sinfo->connected_time))
2935 goto nla_put_failure;
2936 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2937 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2938 sinfo->inactive_time))
2939 goto nla_put_failure;
2940 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2941 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2942 sinfo->rx_bytes))
2943 goto nla_put_failure;
2944 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2945 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2946 sinfo->tx_bytes))
2947 goto nla_put_failure;
2948 if ((sinfo->filled & STATION_INFO_LLID) &&
2949 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2950 goto nla_put_failure;
2951 if ((sinfo->filled & STATION_INFO_PLID) &&
2952 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2953 goto nla_put_failure;
2954 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2955 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2956 sinfo->plink_state))
2957 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002958 switch (rdev->wiphy.signal_type) {
2959 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002960 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2961 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2962 sinfo->signal))
2963 goto nla_put_failure;
2964 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2965 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2966 sinfo->signal_avg))
2967 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002968 break;
2969 default:
2970 break;
2971 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002972 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002973 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2974 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002975 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002976 }
2977 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2978 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2979 NL80211_STA_INFO_RX_BITRATE))
2980 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002981 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002982 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2983 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2984 sinfo->rx_packets))
2985 goto nla_put_failure;
2986 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2987 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2988 sinfo->tx_packets))
2989 goto nla_put_failure;
2990 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2991 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2992 sinfo->tx_retries))
2993 goto nla_put_failure;
2994 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2995 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2996 sinfo->tx_failed))
2997 goto nla_put_failure;
2998 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2999 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3000 sinfo->beacon_loss_count))
3001 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003002 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3003 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3004 if (!bss_param)
3005 goto nla_put_failure;
3006
David S. Miller9360ffd2012-03-29 04:41:26 -04003007 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3008 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3009 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3010 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3011 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3012 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3013 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3014 sinfo->bss_param.dtim_period) ||
3015 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3016 sinfo->bss_param.beacon_interval))
3017 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003018
3019 nla_nest_end(msg, bss_param);
3020 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003021 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3022 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3023 sizeof(struct nl80211_sta_flag_update),
3024 &sinfo->sta_flags))
3025 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003026 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3027 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3028 sinfo->t_offset))
3029 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003030 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003031
David S. Miller9360ffd2012-03-29 04:41:26 -04003032 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3033 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3034 sinfo->assoc_req_ies))
3035 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003036
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003037 return genlmsg_end(msg, hdr);
3038
3039 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003040 genlmsg_cancel(msg, hdr);
3041 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003042}
3043
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003044static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003045 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003046{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003047 struct station_info sinfo;
3048 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003049 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003050 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003051 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003052 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003053
Johannes Berg67748892010-10-04 21:14:06 +02003054 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3055 if (err)
3056 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003057
3058 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003059 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003060 goto out_err;
3061 }
3062
Johannes Bergbba95fe2008-07-29 13:22:51 +02003063 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003064 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003065 err = rdev_dump_station(dev, netdev, sta_idx,
3066 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003067 if (err == -ENOENT)
3068 break;
3069 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003070 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003071
3072 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003073 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003074 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003075 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003076 &sinfo) < 0)
3077 goto out;
3078
3079 sta_idx++;
3080 }
3081
3082
3083 out:
3084 cb->args[1] = sta_idx;
3085 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003086 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003087 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003088
3089 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003090}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003091
Johannes Berg5727ef12007-12-19 02:03:34 +01003092static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3093{
Johannes Berg4c476992010-10-04 21:36:35 +02003094 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3095 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003096 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003097 struct sk_buff *msg;
3098 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003099 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003100
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003101 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003102
3103 if (!info->attrs[NL80211_ATTR_MAC])
3104 return -EINVAL;
3105
3106 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3107
Johannes Berg4c476992010-10-04 21:36:35 +02003108 if (!rdev->ops->get_station)
3109 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003110
Hila Gonene35e4d22012-06-27 17:19:42 +03003111 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003112 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003113 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003114
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003115 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003116 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003117 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003118
Eric W. Biederman15e47302012-09-07 20:12:54 +00003119 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003120 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003121 nlmsg_free(msg);
3122 return -ENOBUFS;
3123 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003124
Johannes Berg4c476992010-10-04 21:36:35 +02003125 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003126}
3127
3128/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003129 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003130 */
Johannes Berg80b99892011-11-18 16:23:01 +01003131static struct net_device *get_vlan(struct genl_info *info,
3132 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003133{
Johannes Berg463d0182009-07-14 00:33:35 +02003134 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003135 struct net_device *v;
3136 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003137
Johannes Berg80b99892011-11-18 16:23:01 +01003138 if (!vlanattr)
3139 return NULL;
3140
3141 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3142 if (!v)
3143 return ERR_PTR(-ENODEV);
3144
3145 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3146 ret = -EINVAL;
3147 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003148 }
Johannes Berg80b99892011-11-18 16:23:01 +01003149
3150 if (!netif_running(v)) {
3151 ret = -ENETDOWN;
3152 goto error;
3153 }
3154
3155 return v;
3156 error:
3157 dev_put(v);
3158 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003159}
3160
3161static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3162{
Johannes Berg4c476992010-10-04 21:36:35 +02003163 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003164 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003165 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003166 struct station_parameters params;
3167 u8 *mac_addr = NULL;
3168
3169 memset(&params, 0, sizeof(params));
3170
3171 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003172 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003173
3174 if (info->attrs[NL80211_ATTR_STA_AID])
3175 return -EINVAL;
3176
3177 if (!info->attrs[NL80211_ATTR_MAC])
3178 return -EINVAL;
3179
3180 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3181
3182 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3183 params.supported_rates =
3184 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3185 params.supported_rates_len =
3186 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3187 }
3188
3189 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3190 params.listen_interval =
3191 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3192
Jouni Malinen36aedc92008-08-25 11:58:58 +03003193 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3194 params.ht_capa =
3195 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3196
Johannes Bergbdd90d52011-12-14 12:20:27 +01003197 if (!rdev->ops->change_station)
3198 return -EOPNOTSUPP;
3199
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003200 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003201 return -EINVAL;
3202
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003203 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3204 params.plink_action =
3205 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3206
Javier Cardona9c3990a2011-05-03 16:57:11 -07003207 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3208 params.plink_state =
3209 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3210
Johannes Berga97f4422009-06-18 17:23:43 +02003211 switch (dev->ieee80211_ptr->iftype) {
3212 case NL80211_IFTYPE_AP:
3213 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003214 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003215 /* disallow mesh-specific things */
3216 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003217 return -EINVAL;
3218
3219 /* TDLS can't be set, ... */
3220 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3221 return -EINVAL;
3222 /*
3223 * ... but don't bother the driver with it. This works around
3224 * a hostapd/wpa_supplicant issue -- it always includes the
3225 * TLDS_PEER flag in the mask even for AP mode.
3226 */
3227 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3228
3229 /* accept only the listed bits */
3230 if (params.sta_flags_mask &
3231 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3232 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3233 BIT(NL80211_STA_FLAG_WME) |
3234 BIT(NL80211_STA_FLAG_MFP)))
3235 return -EINVAL;
3236
3237 /* must be last in here for error handling */
3238 params.vlan = get_vlan(info, rdev);
3239 if (IS_ERR(params.vlan))
3240 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003241 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003242 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003243 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003244 /*
3245 * Don't allow userspace to change the TDLS_PEER flag,
3246 * but silently ignore attempts to change it since we
3247 * don't have state here to verify that it doesn't try
3248 * to change the flag.
3249 */
3250 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003251 /* fall through */
3252 case NL80211_IFTYPE_ADHOC:
3253 /* disallow things sta doesn't support */
3254 if (params.plink_action)
3255 return -EINVAL;
3256 if (params.ht_capa)
3257 return -EINVAL;
3258 if (params.listen_interval >= 0)
3259 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003260 /* reject any changes other than AUTHORIZED */
3261 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3262 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003263 break;
3264 case NL80211_IFTYPE_MESH_POINT:
3265 /* disallow things mesh doesn't support */
3266 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003267 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003268 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003269 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003270 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003271 return -EINVAL;
3272 /*
3273 * No special handling for TDLS here -- the userspace
3274 * mesh code doesn't have this bug.
3275 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003276 if (params.sta_flags_mask &
3277 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003278 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003279 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003280 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003281 break;
3282 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003283 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003284 }
3285
Johannes Bergbdd90d52011-12-14 12:20:27 +01003286 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003287
Hila Gonene35e4d22012-06-27 17:19:42 +03003288 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003289
Johannes Berg5727ef12007-12-19 02:03:34 +01003290 if (params.vlan)
3291 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003292
Johannes Berg5727ef12007-12-19 02:03:34 +01003293 return err;
3294}
3295
Eliad Pellerc75786c2011-08-23 14:37:46 +03003296static struct nla_policy
3297nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3298 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3299 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3300};
3301
Johannes Berg5727ef12007-12-19 02:03:34 +01003302static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3303{
Johannes Berg4c476992010-10-04 21:36:35 +02003304 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003305 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003306 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003307 struct station_parameters params;
3308 u8 *mac_addr = NULL;
3309
3310 memset(&params, 0, sizeof(params));
3311
3312 if (!info->attrs[NL80211_ATTR_MAC])
3313 return -EINVAL;
3314
Johannes Berg5727ef12007-12-19 02:03:34 +01003315 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3316 return -EINVAL;
3317
3318 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3319 return -EINVAL;
3320
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003321 if (!info->attrs[NL80211_ATTR_STA_AID])
3322 return -EINVAL;
3323
Johannes Berg5727ef12007-12-19 02:03:34 +01003324 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3325 params.supported_rates =
3326 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3327 params.supported_rates_len =
3328 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3329 params.listen_interval =
3330 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003331
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003332 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3333 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3334 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003335
Jouni Malinen36aedc92008-08-25 11:58:58 +03003336 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3337 params.ht_capa =
3338 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003339
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003340 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3341 params.vht_capa =
3342 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3343
Javier Cardona96b78df2011-04-07 15:08:33 -07003344 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3345 params.plink_action =
3346 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3347
Johannes Bergbdd90d52011-12-14 12:20:27 +01003348 if (!rdev->ops->add_station)
3349 return -EOPNOTSUPP;
3350
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003351 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003352 return -EINVAL;
3353
Johannes Bergbdd90d52011-12-14 12:20:27 +01003354 switch (dev->ieee80211_ptr->iftype) {
3355 case NL80211_IFTYPE_AP:
3356 case NL80211_IFTYPE_AP_VLAN:
3357 case NL80211_IFTYPE_P2P_GO:
3358 /* parse WME attributes if sta is WME capable */
3359 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3360 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3361 info->attrs[NL80211_ATTR_STA_WME]) {
3362 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3363 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003364
Johannes Bergbdd90d52011-12-14 12:20:27 +01003365 nla = info->attrs[NL80211_ATTR_STA_WME];
3366 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3367 nl80211_sta_wme_policy);
3368 if (err)
3369 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003370
Johannes Bergbdd90d52011-12-14 12:20:27 +01003371 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3372 params.uapsd_queues =
3373 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3374 if (params.uapsd_queues &
3375 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3376 return -EINVAL;
3377
3378 if (tb[NL80211_STA_WME_MAX_SP])
3379 params.max_sp =
3380 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3381
3382 if (params.max_sp &
3383 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3384 return -EINVAL;
3385
3386 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3387 }
3388 /* TDLS peers cannot be added */
3389 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003390 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003391 /* but don't bother the driver with it */
3392 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003393
Johannes Bergbdd90d52011-12-14 12:20:27 +01003394 /* must be last in here for error handling */
3395 params.vlan = get_vlan(info, rdev);
3396 if (IS_ERR(params.vlan))
3397 return PTR_ERR(params.vlan);
3398 break;
3399 case NL80211_IFTYPE_MESH_POINT:
3400 /* TDLS peers cannot be added */
3401 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003402 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003403 break;
3404 case NL80211_IFTYPE_STATION:
3405 /* Only TDLS peers can be added */
3406 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3407 return -EINVAL;
3408 /* Can only add if TDLS ... */
3409 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3410 return -EOPNOTSUPP;
3411 /* ... with external setup is supported */
3412 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3413 return -EOPNOTSUPP;
3414 break;
3415 default:
3416 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003417 }
3418
Johannes Bergbdd90d52011-12-14 12:20:27 +01003419 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003420
Hila Gonene35e4d22012-06-27 17:19:42 +03003421 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003422
Johannes Berg5727ef12007-12-19 02:03:34 +01003423 if (params.vlan)
3424 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003425 return err;
3426}
3427
3428static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3429{
Johannes Berg4c476992010-10-04 21:36:35 +02003430 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3431 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003432 u8 *mac_addr = NULL;
3433
3434 if (info->attrs[NL80211_ATTR_MAC])
3435 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3436
Johannes Berge80cf852009-05-11 14:43:13 +02003437 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003438 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003439 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003440 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3441 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003442
Johannes Berg4c476992010-10-04 21:36:35 +02003443 if (!rdev->ops->del_station)
3444 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003445
Hila Gonene35e4d22012-06-27 17:19:42 +03003446 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003447}
3448
Eric W. Biederman15e47302012-09-07 20:12:54 +00003449static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003450 int flags, struct net_device *dev,
3451 u8 *dst, u8 *next_hop,
3452 struct mpath_info *pinfo)
3453{
3454 void *hdr;
3455 struct nlattr *pinfoattr;
3456
Eric W. Biederman15e47302012-09-07 20:12:54 +00003457 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003458 if (!hdr)
3459 return -1;
3460
David S. Miller9360ffd2012-03-29 04:41:26 -04003461 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3462 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3463 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3464 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3465 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003466
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003467 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3468 if (!pinfoattr)
3469 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003470 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3471 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3472 pinfo->frame_qlen))
3473 goto nla_put_failure;
3474 if (((pinfo->filled & MPATH_INFO_SN) &&
3475 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3476 ((pinfo->filled & MPATH_INFO_METRIC) &&
3477 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3478 pinfo->metric)) ||
3479 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3480 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3481 pinfo->exptime)) ||
3482 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3483 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3484 pinfo->flags)) ||
3485 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3486 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3487 pinfo->discovery_timeout)) ||
3488 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3489 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3490 pinfo->discovery_retries)))
3491 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003492
3493 nla_nest_end(msg, pinfoattr);
3494
3495 return genlmsg_end(msg, hdr);
3496
3497 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003498 genlmsg_cancel(msg, hdr);
3499 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003500}
3501
3502static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003503 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003504{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003505 struct mpath_info pinfo;
3506 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003507 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003508 u8 dst[ETH_ALEN];
3509 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003510 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003511 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003512
Johannes Berg67748892010-10-04 21:14:06 +02003513 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3514 if (err)
3515 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003516
3517 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003518 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003519 goto out_err;
3520 }
3521
Jouni Malineneec60b02009-03-20 21:21:19 +02003522 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3523 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003524 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003525 }
3526
Johannes Bergbba95fe2008-07-29 13:22:51 +02003527 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003528 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3529 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003530 if (err == -ENOENT)
3531 break;
3532 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003533 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003534
Eric W. Biederman15e47302012-09-07 20:12:54 +00003535 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003536 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3537 netdev, dst, next_hop,
3538 &pinfo) < 0)
3539 goto out;
3540
3541 path_idx++;
3542 }
3543
3544
3545 out:
3546 cb->args[1] = path_idx;
3547 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003548 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003549 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003550 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003551}
3552
3553static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3554{
Johannes Berg4c476992010-10-04 21:36:35 +02003555 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003556 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003557 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003558 struct mpath_info pinfo;
3559 struct sk_buff *msg;
3560 u8 *dst = NULL;
3561 u8 next_hop[ETH_ALEN];
3562
3563 memset(&pinfo, 0, sizeof(pinfo));
3564
3565 if (!info->attrs[NL80211_ATTR_MAC])
3566 return -EINVAL;
3567
3568 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3569
Johannes Berg4c476992010-10-04 21:36:35 +02003570 if (!rdev->ops->get_mpath)
3571 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003572
Johannes Berg4c476992010-10-04 21:36:35 +02003573 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3574 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003575
Hila Gonene35e4d22012-06-27 17:19:42 +03003576 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003577 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003578 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003579
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003580 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003581 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003582 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003583
Eric W. Biederman15e47302012-09-07 20:12:54 +00003584 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003585 dev, dst, next_hop, &pinfo) < 0) {
3586 nlmsg_free(msg);
3587 return -ENOBUFS;
3588 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003589
Johannes Berg4c476992010-10-04 21:36:35 +02003590 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003591}
3592
3593static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3594{
Johannes Berg4c476992010-10-04 21:36:35 +02003595 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3596 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003597 u8 *dst = NULL;
3598 u8 *next_hop = NULL;
3599
3600 if (!info->attrs[NL80211_ATTR_MAC])
3601 return -EINVAL;
3602
3603 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3604 return -EINVAL;
3605
3606 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3607 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3608
Johannes Berg4c476992010-10-04 21:36:35 +02003609 if (!rdev->ops->change_mpath)
3610 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003611
Johannes Berg4c476992010-10-04 21:36:35 +02003612 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3613 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003614
Hila Gonene35e4d22012-06-27 17:19:42 +03003615 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003616}
Johannes Berg4c476992010-10-04 21:36:35 +02003617
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003618static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3619{
Johannes Berg4c476992010-10-04 21:36:35 +02003620 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3621 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003622 u8 *dst = NULL;
3623 u8 *next_hop = NULL;
3624
3625 if (!info->attrs[NL80211_ATTR_MAC])
3626 return -EINVAL;
3627
3628 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3629 return -EINVAL;
3630
3631 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3632 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3633
Johannes Berg4c476992010-10-04 21:36:35 +02003634 if (!rdev->ops->add_mpath)
3635 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003636
Johannes Berg4c476992010-10-04 21:36:35 +02003637 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3638 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003639
Hila Gonene35e4d22012-06-27 17:19:42 +03003640 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003641}
3642
3643static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3644{
Johannes Berg4c476992010-10-04 21:36:35 +02003645 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3646 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003647 u8 *dst = NULL;
3648
3649 if (info->attrs[NL80211_ATTR_MAC])
3650 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3651
Johannes Berg4c476992010-10-04 21:36:35 +02003652 if (!rdev->ops->del_mpath)
3653 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003654
Hila Gonene35e4d22012-06-27 17:19:42 +03003655 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003656}
3657
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003658static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3659{
Johannes Berg4c476992010-10-04 21:36:35 +02003660 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3661 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003662 struct bss_parameters params;
3663
3664 memset(&params, 0, sizeof(params));
3665 /* default to not changing parameters */
3666 params.use_cts_prot = -1;
3667 params.use_short_preamble = -1;
3668 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003669 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003670 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003671
3672 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3673 params.use_cts_prot =
3674 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3675 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3676 params.use_short_preamble =
3677 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3678 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3679 params.use_short_slot_time =
3680 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003681 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3682 params.basic_rates =
3683 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3684 params.basic_rates_len =
3685 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3686 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003687 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3688 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003689 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3690 params.ht_opmode =
3691 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003692
Johannes Berg4c476992010-10-04 21:36:35 +02003693 if (!rdev->ops->change_bss)
3694 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003695
Johannes Berg074ac8d2010-09-16 14:58:22 +02003696 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003697 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3698 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003699
Hila Gonene35e4d22012-06-27 17:19:42 +03003700 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003701}
3702
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003703static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003704 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3705 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3706 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3707 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3708 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3709 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3710};
3711
3712static int parse_reg_rule(struct nlattr *tb[],
3713 struct ieee80211_reg_rule *reg_rule)
3714{
3715 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3716 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3717
3718 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3719 return -EINVAL;
3720 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3721 return -EINVAL;
3722 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3723 return -EINVAL;
3724 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3725 return -EINVAL;
3726 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3727 return -EINVAL;
3728
3729 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3730
3731 freq_range->start_freq_khz =
3732 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3733 freq_range->end_freq_khz =
3734 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3735 freq_range->max_bandwidth_khz =
3736 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3737
3738 power_rule->max_eirp =
3739 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3740
3741 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3742 power_rule->max_antenna_gain =
3743 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3744
3745 return 0;
3746}
3747
3748static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3749{
3750 int r;
3751 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003752 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003753
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003754 /*
3755 * You should only get this when cfg80211 hasn't yet initialized
3756 * completely when built-in to the kernel right between the time
3757 * window between nl80211_init() and regulatory_init(), if that is
3758 * even possible.
3759 */
3760 mutex_lock(&cfg80211_mutex);
3761 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003762 mutex_unlock(&cfg80211_mutex);
3763 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003764 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003765 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003766
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003767 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3768 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003769
3770 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3771
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003772 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3773 user_reg_hint_type =
3774 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3775 else
3776 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3777
3778 switch (user_reg_hint_type) {
3779 case NL80211_USER_REG_HINT_USER:
3780 case NL80211_USER_REG_HINT_CELL_BASE:
3781 break;
3782 default:
3783 return -EINVAL;
3784 }
3785
3786 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003787
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003788 return r;
3789}
3790
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003791static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003792 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003793{
Johannes Berg4c476992010-10-04 21:36:35 +02003794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003795 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003796 struct wireless_dev *wdev = dev->ieee80211_ptr;
3797 struct mesh_config cur_params;
3798 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003799 void *hdr;
3800 struct nlattr *pinfoattr;
3801 struct sk_buff *msg;
3802
Johannes Berg29cbe682010-12-03 09:20:44 +01003803 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3804 return -EOPNOTSUPP;
3805
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003806 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003807 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003808
Johannes Berg29cbe682010-12-03 09:20:44 +01003809 wdev_lock(wdev);
3810 /* If not connected, get default parameters */
3811 if (!wdev->mesh_id_len)
3812 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3813 else
Hila Gonene35e4d22012-06-27 17:19:42 +03003814 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01003815 wdev_unlock(wdev);
3816
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003817 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003818 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003819
3820 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003821 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003822 if (!msg)
3823 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003824 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003825 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003826 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003827 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003828 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003829 if (!pinfoattr)
3830 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003831 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3832 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3833 cur_params.dot11MeshRetryTimeout) ||
3834 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3835 cur_params.dot11MeshConfirmTimeout) ||
3836 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3837 cur_params.dot11MeshHoldingTimeout) ||
3838 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3839 cur_params.dot11MeshMaxPeerLinks) ||
3840 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3841 cur_params.dot11MeshMaxRetries) ||
3842 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3843 cur_params.dot11MeshTTL) ||
3844 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3845 cur_params.element_ttl) ||
3846 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3847 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003848 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3849 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003850 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3851 cur_params.dot11MeshHWMPmaxPREQretries) ||
3852 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3853 cur_params.path_refresh_time) ||
3854 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3855 cur_params.min_discovery_timeout) ||
3856 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3857 cur_params.dot11MeshHWMPactivePathTimeout) ||
3858 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3859 cur_params.dot11MeshHWMPpreqMinInterval) ||
3860 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3861 cur_params.dot11MeshHWMPperrMinInterval) ||
3862 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3863 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3864 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3865 cur_params.dot11MeshHWMPRootMode) ||
3866 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3867 cur_params.dot11MeshHWMPRannInterval) ||
3868 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3869 cur_params.dot11MeshGateAnnouncementProtocol) ||
3870 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3871 cur_params.dot11MeshForwarding) ||
3872 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003873 cur_params.rssi_threshold) ||
3874 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003875 cur_params.ht_opmode) ||
3876 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3877 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3878 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003879 cur_params.dot11MeshHWMProotInterval) ||
3880 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3881 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003882 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003883 nla_nest_end(msg, pinfoattr);
3884 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003885 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003886
Johannes Berg3b858752009-03-12 09:55:09 +01003887 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003888 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003889 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003890 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003891 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003892}
3893
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003894static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003895 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3896 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3897 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3898 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3899 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3900 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003901 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003902 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003903 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003904 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3905 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3906 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3907 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3908 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003909 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003910 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003911 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003912 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003913 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003914 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003915 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3916 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003917 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3918 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003919 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003920};
3921
Javier Cardonac80d5452010-12-16 17:37:49 -08003922static const struct nla_policy
3923 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003924 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003925 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3926 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003927 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003928 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003929 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003930 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003931};
3932
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003933static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003934 struct mesh_config *cfg,
3935 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003936{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003937 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003938 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003939
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003940#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3941do {\
3942 if (table[attr_num]) {\
3943 cfg->param = nla_fn(table[attr_num]); \
3944 mask |= (1 << (attr_num - 1)); \
3945 } \
3946} while (0);\
3947
3948
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003949 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003950 return -EINVAL;
3951 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003952 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003953 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003954 return -EINVAL;
3955
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003956 /* This makes sure that there aren't more than 32 mesh config
3957 * parameters (otherwise our bitfield scheme would not work.) */
3958 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3959
3960 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003961 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003962 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3963 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003964 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003965 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3966 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003967 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003968 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3969 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003970 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003971 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3972 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003973 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003974 mask, NL80211_MESHCONF_MAX_RETRIES,
3975 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003976 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003977 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003978 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003979 mask, NL80211_MESHCONF_ELEMENT_TTL,
3980 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003981 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003982 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3983 nla_get_u8);
3984 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3985 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3986 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003987 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003988 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3989 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003990 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003991 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3992 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003993 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003994 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3995 nla_get_u16);
3996 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3997 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3998 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003999 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004000 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4001 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004002 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004003 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4004 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004005 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004006 dot11MeshHWMPnetDiameterTraversalTime, mask,
4007 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4008 nla_get_u16);
4009 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
4010 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
4011 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
4012 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4013 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004014 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004015 dot11MeshGateAnnouncementProtocol, mask,
4016 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4017 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004018 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004019 mask, NL80211_MESHCONF_FORWARDING,
4020 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08004021 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004022 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4023 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004024 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004025 mask, NL80211_MESHCONF_HT_OPMODE,
4026 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004027 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
4028 mask,
4029 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4030 nla_get_u32);
4031 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
4032 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4033 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004034 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
4035 dot11MeshHWMPconfirmationInterval, mask,
4036 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4037 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004038 if (mask_out)
4039 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004040
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004041 return 0;
4042
4043#undef FILL_IN_MESH_PARAM_IF_SET
4044}
4045
Javier Cardonac80d5452010-12-16 17:37:49 -08004046static int nl80211_parse_mesh_setup(struct genl_info *info,
4047 struct mesh_setup *setup)
4048{
4049 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4050
4051 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4052 return -EINVAL;
4053 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4054 info->attrs[NL80211_ATTR_MESH_SETUP],
4055 nl80211_mesh_setup_params_policy))
4056 return -EINVAL;
4057
Javier Cardonad299a1f2012-03-31 11:31:33 -07004058 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4059 setup->sync_method =
4060 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4061 IEEE80211_SYNC_METHOD_VENDOR :
4062 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4063
Javier Cardonac80d5452010-12-16 17:37:49 -08004064 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4065 setup->path_sel_proto =
4066 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4067 IEEE80211_PATH_PROTOCOL_VENDOR :
4068 IEEE80211_PATH_PROTOCOL_HWMP;
4069
4070 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4071 setup->path_metric =
4072 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4073 IEEE80211_PATH_METRIC_VENDOR :
4074 IEEE80211_PATH_METRIC_AIRTIME;
4075
Javier Cardona581a8b02011-04-07 15:08:27 -07004076
4077 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004078 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004079 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004080 if (!is_valid_ie_attr(ieattr))
4081 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004082 setup->ie = nla_data(ieattr);
4083 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004084 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004085 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4086 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004087
4088 return 0;
4089}
4090
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004091static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004092 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004093{
4094 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4095 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004096 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004097 struct mesh_config cfg;
4098 u32 mask;
4099 int err;
4100
Johannes Berg29cbe682010-12-03 09:20:44 +01004101 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4102 return -EOPNOTSUPP;
4103
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004104 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004105 return -EOPNOTSUPP;
4106
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004107 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004108 if (err)
4109 return err;
4110
Johannes Berg29cbe682010-12-03 09:20:44 +01004111 wdev_lock(wdev);
4112 if (!wdev->mesh_id_len)
4113 err = -ENOLINK;
4114
4115 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004116 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004117
4118 wdev_unlock(wdev);
4119
4120 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004121}
4122
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004123static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4124{
4125 struct sk_buff *msg;
4126 void *hdr = NULL;
4127 struct nlattr *nl_reg_rules;
4128 unsigned int i;
4129 int err = -EINVAL;
4130
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004131 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004132
4133 if (!cfg80211_regdomain)
4134 goto out;
4135
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004136 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004137 if (!msg) {
4138 err = -ENOBUFS;
4139 goto out;
4140 }
4141
Eric W. Biederman15e47302012-09-07 20:12:54 +00004142 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004143 NL80211_CMD_GET_REG);
4144 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004145 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004146
David S. Miller9360ffd2012-03-29 04:41:26 -04004147 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4148 cfg80211_regdomain->alpha2) ||
4149 (cfg80211_regdomain->dfs_region &&
4150 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4151 cfg80211_regdomain->dfs_region)))
4152 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004153
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004154 if (reg_last_request_cell_base() &&
4155 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4156 NL80211_USER_REG_HINT_CELL_BASE))
4157 goto nla_put_failure;
4158
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004159 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4160 if (!nl_reg_rules)
4161 goto nla_put_failure;
4162
4163 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4164 struct nlattr *nl_reg_rule;
4165 const struct ieee80211_reg_rule *reg_rule;
4166 const struct ieee80211_freq_range *freq_range;
4167 const struct ieee80211_power_rule *power_rule;
4168
4169 reg_rule = &cfg80211_regdomain->reg_rules[i];
4170 freq_range = &reg_rule->freq_range;
4171 power_rule = &reg_rule->power_rule;
4172
4173 nl_reg_rule = nla_nest_start(msg, i);
4174 if (!nl_reg_rule)
4175 goto nla_put_failure;
4176
David S. Miller9360ffd2012-03-29 04:41:26 -04004177 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4178 reg_rule->flags) ||
4179 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4180 freq_range->start_freq_khz) ||
4181 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4182 freq_range->end_freq_khz) ||
4183 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4184 freq_range->max_bandwidth_khz) ||
4185 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4186 power_rule->max_antenna_gain) ||
4187 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4188 power_rule->max_eirp))
4189 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004190
4191 nla_nest_end(msg, nl_reg_rule);
4192 }
4193
4194 nla_nest_end(msg, nl_reg_rules);
4195
4196 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004197 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004198 goto out;
4199
4200nla_put_failure:
4201 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004202put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004203 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004204 err = -EMSGSIZE;
4205out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004206 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004207 return err;
4208}
4209
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004210static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4211{
4212 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4213 struct nlattr *nl_reg_rule;
4214 char *alpha2 = NULL;
4215 int rem_reg_rules = 0, r = 0;
4216 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004217 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004218 struct ieee80211_regdomain *rd = NULL;
4219
4220 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4221 return -EINVAL;
4222
4223 if (!info->attrs[NL80211_ATTR_REG_RULES])
4224 return -EINVAL;
4225
4226 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4227
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004228 if (info->attrs[NL80211_ATTR_DFS_REGION])
4229 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4230
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004231 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4232 rem_reg_rules) {
4233 num_rules++;
4234 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004235 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004236 }
4237
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004238 mutex_lock(&cfg80211_mutex);
4239
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004240 if (!reg_is_valid_request(alpha2)) {
4241 r = -EINVAL;
4242 goto bad_reg;
4243 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004244
4245 size_of_regd = sizeof(struct ieee80211_regdomain) +
4246 (num_rules * sizeof(struct ieee80211_reg_rule));
4247
4248 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004249 if (!rd) {
4250 r = -ENOMEM;
4251 goto bad_reg;
4252 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004253
4254 rd->n_reg_rules = num_rules;
4255 rd->alpha2[0] = alpha2[0];
4256 rd->alpha2[1] = alpha2[1];
4257
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004258 /*
4259 * Disable DFS master mode if the DFS region was
4260 * not supported or known on this kernel.
4261 */
4262 if (reg_supported_dfs_region(dfs_region))
4263 rd->dfs_region = dfs_region;
4264
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004265 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4266 rem_reg_rules) {
4267 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4268 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4269 reg_rule_policy);
4270 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4271 if (r)
4272 goto bad_reg;
4273
4274 rule_idx++;
4275
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004276 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4277 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004278 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004279 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004280 }
4281
4282 BUG_ON(rule_idx != num_rules);
4283
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004284 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004285
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004286 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004287
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004288 return r;
4289
Johannes Bergd2372b32008-10-24 20:32:20 +02004290 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004291 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004292 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004293 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004294}
4295
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004296static int validate_scan_freqs(struct nlattr *freqs)
4297{
4298 struct nlattr *attr1, *attr2;
4299 int n_channels = 0, tmp1, tmp2;
4300
4301 nla_for_each_nested(attr1, freqs, tmp1) {
4302 n_channels++;
4303 /*
4304 * Some hardware has a limited channel list for
4305 * scanning, and it is pretty much nonsensical
4306 * to scan for a channel twice, so disallow that
4307 * and don't require drivers to check that the
4308 * channel list they get isn't longer than what
4309 * they can scan, as long as they can scan all
4310 * the channels they registered at once.
4311 */
4312 nla_for_each_nested(attr2, freqs, tmp2)
4313 if (attr1 != attr2 &&
4314 nla_get_u32(attr1) == nla_get_u32(attr2))
4315 return 0;
4316 }
4317
4318 return n_channels;
4319}
4320
Johannes Berg2a519312009-02-10 21:25:55 +01004321static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4322{
Johannes Berg4c476992010-10-04 21:36:35 +02004323 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004324 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004325 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004326 struct nlattr *attr;
4327 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004328 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004329 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004330
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004331 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4332 return -EINVAL;
4333
Johannes Berg79c97e92009-07-07 03:56:12 +02004334 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004335
Johannes Berg4c476992010-10-04 21:36:35 +02004336 if (!rdev->ops->scan)
4337 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004338
Johannes Berg4c476992010-10-04 21:36:35 +02004339 if (rdev->scan_req)
4340 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004341
4342 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004343 n_channels = validate_scan_freqs(
4344 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004345 if (!n_channels)
4346 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004347 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004348 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004349 n_channels = 0;
4350
Johannes Berg2a519312009-02-10 21:25:55 +01004351 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4352 if (wiphy->bands[band])
4353 n_channels += wiphy->bands[band]->n_channels;
4354 }
4355
4356 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4357 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4358 n_ssids++;
4359
Johannes Berg4c476992010-10-04 21:36:35 +02004360 if (n_ssids > wiphy->max_scan_ssids)
4361 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004362
Jouni Malinen70692ad2009-02-16 19:39:13 +02004363 if (info->attrs[NL80211_ATTR_IE])
4364 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4365 else
4366 ie_len = 0;
4367
Johannes Berg4c476992010-10-04 21:36:35 +02004368 if (ie_len > wiphy->max_scan_ie_len)
4369 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004370
Johannes Berg2a519312009-02-10 21:25:55 +01004371 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004372 + sizeof(*request->ssids) * n_ssids
4373 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004374 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004375 if (!request)
4376 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004377
Johannes Berg2a519312009-02-10 21:25:55 +01004378 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004379 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004380 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004381 if (ie_len) {
4382 if (request->ssids)
4383 request->ie = (void *)(request->ssids + n_ssids);
4384 else
4385 request->ie = (void *)(request->channels + n_channels);
4386 }
Johannes Berg2a519312009-02-10 21:25:55 +01004387
Johannes Berg584991d2009-11-02 13:32:03 +01004388 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004389 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4390 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004391 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004392 struct ieee80211_channel *chan;
4393
4394 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4395
4396 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004397 err = -EINVAL;
4398 goto out_free;
4399 }
Johannes Berg584991d2009-11-02 13:32:03 +01004400
4401 /* ignore disabled channels */
4402 if (chan->flags & IEEE80211_CHAN_DISABLED)
4403 continue;
4404
4405 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004406 i++;
4407 }
4408 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004409 enum ieee80211_band band;
4410
Johannes Berg2a519312009-02-10 21:25:55 +01004411 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004412 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4413 int j;
4414 if (!wiphy->bands[band])
4415 continue;
4416 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004417 struct ieee80211_channel *chan;
4418
4419 chan = &wiphy->bands[band]->channels[j];
4420
4421 if (chan->flags & IEEE80211_CHAN_DISABLED)
4422 continue;
4423
4424 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004425 i++;
4426 }
4427 }
4428 }
4429
Johannes Berg584991d2009-11-02 13:32:03 +01004430 if (!i) {
4431 err = -EINVAL;
4432 goto out_free;
4433 }
4434
4435 request->n_channels = i;
4436
Johannes Berg2a519312009-02-10 21:25:55 +01004437 i = 0;
4438 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4439 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004440 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004441 err = -EINVAL;
4442 goto out_free;
4443 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004444 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004445 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004446 i++;
4447 }
4448 }
4449
Jouni Malinen70692ad2009-02-16 19:39:13 +02004450 if (info->attrs[NL80211_ATTR_IE]) {
4451 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004452 memcpy((void *)request->ie,
4453 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004454 request->ie_len);
4455 }
4456
Johannes Berg34850ab2011-07-18 18:08:35 +02004457 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004458 if (wiphy->bands[i])
4459 request->rates[i] =
4460 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004461
4462 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4463 nla_for_each_nested(attr,
4464 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4465 tmp) {
4466 enum ieee80211_band band = nla_type(attr);
4467
Dan Carpenter84404622011-07-29 11:52:18 +03004468 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004469 err = -EINVAL;
4470 goto out_free;
4471 }
4472 err = ieee80211_get_ratemask(wiphy->bands[band],
4473 nla_data(attr),
4474 nla_len(attr),
4475 &request->rates[band]);
4476 if (err)
4477 goto out_free;
4478 }
4479 }
4480
Sam Leffler46856bb2012-10-11 21:03:32 -07004481 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004482 request->flags = nla_get_u32(
4483 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004484 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4485 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4486 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4487 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004488 err = -EOPNOTSUPP;
4489 goto out_free;
4490 }
4491 }
Sam Lefflered4737712012-10-11 21:03:31 -07004492
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304493 request->no_cck =
4494 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4495
Johannes Bergfd014282012-06-18 19:17:03 +02004496 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004497 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004498 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004499
Johannes Berg79c97e92009-07-07 03:56:12 +02004500 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004501 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004502
Johannes Berg463d0182009-07-14 00:33:35 +02004503 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004504 nl80211_send_scan_start(rdev, wdev);
4505 if (wdev->netdev)
4506 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004507 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004508 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004509 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004510 kfree(request);
4511 }
Johannes Berg3b858752009-03-12 09:55:09 +01004512
Johannes Berg2a519312009-02-10 21:25:55 +01004513 return err;
4514}
4515
Luciano Coelho807f8a82011-05-11 17:09:35 +03004516static int nl80211_start_sched_scan(struct sk_buff *skb,
4517 struct genl_info *info)
4518{
4519 struct cfg80211_sched_scan_request *request;
4520 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4521 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004522 struct nlattr *attr;
4523 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004524 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004525 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004526 enum ieee80211_band band;
4527 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004528 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004529
4530 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4531 !rdev->ops->sched_scan_start)
4532 return -EOPNOTSUPP;
4533
4534 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4535 return -EINVAL;
4536
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004537 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4538 return -EINVAL;
4539
4540 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4541 if (interval == 0)
4542 return -EINVAL;
4543
Luciano Coelho807f8a82011-05-11 17:09:35 +03004544 wiphy = &rdev->wiphy;
4545
4546 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4547 n_channels = validate_scan_freqs(
4548 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4549 if (!n_channels)
4550 return -EINVAL;
4551 } else {
4552 n_channels = 0;
4553
4554 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4555 if (wiphy->bands[band])
4556 n_channels += wiphy->bands[band]->n_channels;
4557 }
4558
4559 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4560 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4561 tmp)
4562 n_ssids++;
4563
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004564 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004565 return -EINVAL;
4566
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004567 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4568 nla_for_each_nested(attr,
4569 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4570 tmp)
4571 n_match_sets++;
4572
4573 if (n_match_sets > wiphy->max_match_sets)
4574 return -EINVAL;
4575
Luciano Coelho807f8a82011-05-11 17:09:35 +03004576 if (info->attrs[NL80211_ATTR_IE])
4577 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4578 else
4579 ie_len = 0;
4580
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004581 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004582 return -EINVAL;
4583
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004584 mutex_lock(&rdev->sched_scan_mtx);
4585
4586 if (rdev->sched_scan_req) {
4587 err = -EINPROGRESS;
4588 goto out;
4589 }
4590
Luciano Coelho807f8a82011-05-11 17:09:35 +03004591 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004592 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004593 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004594 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004595 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004596 if (!request) {
4597 err = -ENOMEM;
4598 goto out;
4599 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004600
4601 if (n_ssids)
4602 request->ssids = (void *)&request->channels[n_channels];
4603 request->n_ssids = n_ssids;
4604 if (ie_len) {
4605 if (request->ssids)
4606 request->ie = (void *)(request->ssids + n_ssids);
4607 else
4608 request->ie = (void *)(request->channels + n_channels);
4609 }
4610
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004611 if (n_match_sets) {
4612 if (request->ie)
4613 request->match_sets = (void *)(request->ie + ie_len);
4614 else if (request->ssids)
4615 request->match_sets =
4616 (void *)(request->ssids + n_ssids);
4617 else
4618 request->match_sets =
4619 (void *)(request->channels + n_channels);
4620 }
4621 request->n_match_sets = n_match_sets;
4622
Luciano Coelho807f8a82011-05-11 17:09:35 +03004623 i = 0;
4624 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4625 /* user specified, bail out if channel not found */
4626 nla_for_each_nested(attr,
4627 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4628 tmp) {
4629 struct ieee80211_channel *chan;
4630
4631 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4632
4633 if (!chan) {
4634 err = -EINVAL;
4635 goto out_free;
4636 }
4637
4638 /* ignore disabled channels */
4639 if (chan->flags & IEEE80211_CHAN_DISABLED)
4640 continue;
4641
4642 request->channels[i] = chan;
4643 i++;
4644 }
4645 } else {
4646 /* all channels */
4647 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4648 int j;
4649 if (!wiphy->bands[band])
4650 continue;
4651 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4652 struct ieee80211_channel *chan;
4653
4654 chan = &wiphy->bands[band]->channels[j];
4655
4656 if (chan->flags & IEEE80211_CHAN_DISABLED)
4657 continue;
4658
4659 request->channels[i] = chan;
4660 i++;
4661 }
4662 }
4663 }
4664
4665 if (!i) {
4666 err = -EINVAL;
4667 goto out_free;
4668 }
4669
4670 request->n_channels = i;
4671
4672 i = 0;
4673 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4674 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4675 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004676 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004677 err = -EINVAL;
4678 goto out_free;
4679 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004680 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004681 memcpy(request->ssids[i].ssid, nla_data(attr),
4682 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004683 i++;
4684 }
4685 }
4686
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004687 i = 0;
4688 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4689 nla_for_each_nested(attr,
4690 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4691 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004692 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004693
4694 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4695 nla_data(attr), nla_len(attr),
4696 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004697 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004698 if (ssid) {
4699 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4700 err = -EINVAL;
4701 goto out_free;
4702 }
4703 memcpy(request->match_sets[i].ssid.ssid,
4704 nla_data(ssid), nla_len(ssid));
4705 request->match_sets[i].ssid.ssid_len =
4706 nla_len(ssid);
4707 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004708 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4709 if (rssi)
4710 request->rssi_thold = nla_get_u32(rssi);
4711 else
4712 request->rssi_thold =
4713 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004714 i++;
4715 }
4716 }
4717
Luciano Coelho807f8a82011-05-11 17:09:35 +03004718 if (info->attrs[NL80211_ATTR_IE]) {
4719 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4720 memcpy((void *)request->ie,
4721 nla_data(info->attrs[NL80211_ATTR_IE]),
4722 request->ie_len);
4723 }
4724
Sam Leffler46856bb2012-10-11 21:03:32 -07004725 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004726 request->flags = nla_get_u32(
4727 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004728 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4729 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4730 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4731 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004732 err = -EOPNOTSUPP;
4733 goto out_free;
4734 }
4735 }
Sam Lefflered4737712012-10-11 21:03:31 -07004736
Luciano Coelho807f8a82011-05-11 17:09:35 +03004737 request->dev = dev;
4738 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004739 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004740 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004741
Hila Gonene35e4d22012-06-27 17:19:42 +03004742 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004743 if (!err) {
4744 rdev->sched_scan_req = request;
4745 nl80211_send_sched_scan(rdev, dev,
4746 NL80211_CMD_START_SCHED_SCAN);
4747 goto out;
4748 }
4749
4750out_free:
4751 kfree(request);
4752out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004753 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004754 return err;
4755}
4756
4757static int nl80211_stop_sched_scan(struct sk_buff *skb,
4758 struct genl_info *info)
4759{
4760 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004761 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004762
4763 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4764 !rdev->ops->sched_scan_stop)
4765 return -EOPNOTSUPP;
4766
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004767 mutex_lock(&rdev->sched_scan_mtx);
4768 err = __cfg80211_stop_sched_scan(rdev, false);
4769 mutex_unlock(&rdev->sched_scan_mtx);
4770
4771 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004772}
4773
Johannes Berg9720bb32011-06-21 09:45:33 +02004774static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4775 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004776 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004777 struct wireless_dev *wdev,
4778 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004779{
Johannes Berg48ab9052009-07-10 18:42:31 +02004780 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004781 void *hdr;
4782 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004783
4784 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004785
Eric W. Biederman15e47302012-09-07 20:12:54 +00004786 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004787 NL80211_CMD_NEW_SCAN_RESULTS);
4788 if (!hdr)
4789 return -1;
4790
Johannes Berg9720bb32011-06-21 09:45:33 +02004791 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4792
David S. Miller9360ffd2012-03-29 04:41:26 -04004793 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4794 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4795 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004796
4797 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4798 if (!bss)
4799 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004800 if ((!is_zero_ether_addr(res->bssid) &&
4801 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4802 (res->information_elements && res->len_information_elements &&
4803 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4804 res->len_information_elements,
4805 res->information_elements)) ||
4806 (res->beacon_ies && res->len_beacon_ies &&
4807 res->beacon_ies != res->information_elements &&
4808 nla_put(msg, NL80211_BSS_BEACON_IES,
4809 res->len_beacon_ies, res->beacon_ies)))
4810 goto nla_put_failure;
4811 if (res->tsf &&
4812 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4813 goto nla_put_failure;
4814 if (res->beacon_interval &&
4815 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4816 goto nla_put_failure;
4817 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4818 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4819 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4820 jiffies_to_msecs(jiffies - intbss->ts)))
4821 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004822
Johannes Berg77965c92009-02-18 18:45:06 +01004823 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004824 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004825 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4826 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004827 break;
4828 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004829 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4830 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004831 break;
4832 default:
4833 break;
4834 }
4835
Johannes Berg48ab9052009-07-10 18:42:31 +02004836 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004837 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004838 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004839 if (intbss == wdev->current_bss &&
4840 nla_put_u32(msg, NL80211_BSS_STATUS,
4841 NL80211_BSS_STATUS_ASSOCIATED))
4842 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004843 break;
4844 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004845 if (intbss == wdev->current_bss &&
4846 nla_put_u32(msg, NL80211_BSS_STATUS,
4847 NL80211_BSS_STATUS_IBSS_JOINED))
4848 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004849 break;
4850 default:
4851 break;
4852 }
4853
Johannes Berg2a519312009-02-10 21:25:55 +01004854 nla_nest_end(msg, bss);
4855
4856 return genlmsg_end(msg, hdr);
4857
4858 nla_put_failure:
4859 genlmsg_cancel(msg, hdr);
4860 return -EMSGSIZE;
4861}
4862
4863static int nl80211_dump_scan(struct sk_buff *skb,
4864 struct netlink_callback *cb)
4865{
Johannes Berg48ab9052009-07-10 18:42:31 +02004866 struct cfg80211_registered_device *rdev;
4867 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004868 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004869 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004870 int start = cb->args[1], idx = 0;
4871 int err;
4872
Johannes Berg67748892010-10-04 21:14:06 +02004873 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4874 if (err)
4875 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004876
Johannes Berg48ab9052009-07-10 18:42:31 +02004877 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004878
Johannes Berg48ab9052009-07-10 18:42:31 +02004879 wdev_lock(wdev);
4880 spin_lock_bh(&rdev->bss_lock);
4881 cfg80211_bss_expire(rdev);
4882
Johannes Berg9720bb32011-06-21 09:45:33 +02004883 cb->seq = rdev->bss_generation;
4884
Johannes Berg48ab9052009-07-10 18:42:31 +02004885 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004886 if (++idx <= start)
4887 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004888 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004889 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004890 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004891 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004892 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004893 }
4894 }
4895
Johannes Berg48ab9052009-07-10 18:42:31 +02004896 spin_unlock_bh(&rdev->bss_lock);
4897 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004898
4899 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004900 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004901
Johannes Berg67748892010-10-04 21:14:06 +02004902 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004903}
4904
Eric W. Biederman15e47302012-09-07 20:12:54 +00004905static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004906 int flags, struct net_device *dev,
4907 struct survey_info *survey)
4908{
4909 void *hdr;
4910 struct nlattr *infoattr;
4911
Eric W. Biederman15e47302012-09-07 20:12:54 +00004912 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004913 NL80211_CMD_NEW_SURVEY_RESULTS);
4914 if (!hdr)
4915 return -ENOMEM;
4916
David S. Miller9360ffd2012-03-29 04:41:26 -04004917 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4918 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004919
4920 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4921 if (!infoattr)
4922 goto nla_put_failure;
4923
David S. Miller9360ffd2012-03-29 04:41:26 -04004924 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4925 survey->channel->center_freq))
4926 goto nla_put_failure;
4927
4928 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4929 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4930 goto nla_put_failure;
4931 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4932 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4933 goto nla_put_failure;
4934 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4935 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4936 survey->channel_time))
4937 goto nla_put_failure;
4938 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4939 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4940 survey->channel_time_busy))
4941 goto nla_put_failure;
4942 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4943 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4944 survey->channel_time_ext_busy))
4945 goto nla_put_failure;
4946 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4947 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4948 survey->channel_time_rx))
4949 goto nla_put_failure;
4950 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4951 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4952 survey->channel_time_tx))
4953 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004954
4955 nla_nest_end(msg, infoattr);
4956
4957 return genlmsg_end(msg, hdr);
4958
4959 nla_put_failure:
4960 genlmsg_cancel(msg, hdr);
4961 return -EMSGSIZE;
4962}
4963
4964static int nl80211_dump_survey(struct sk_buff *skb,
4965 struct netlink_callback *cb)
4966{
4967 struct survey_info survey;
4968 struct cfg80211_registered_device *dev;
4969 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004970 int survey_idx = cb->args[1];
4971 int res;
4972
Johannes Berg67748892010-10-04 21:14:06 +02004973 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4974 if (res)
4975 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004976
4977 if (!dev->ops->dump_survey) {
4978 res = -EOPNOTSUPP;
4979 goto out_err;
4980 }
4981
4982 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004983 struct ieee80211_channel *chan;
4984
Hila Gonene35e4d22012-06-27 17:19:42 +03004985 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01004986 if (res == -ENOENT)
4987 break;
4988 if (res)
4989 goto out_err;
4990
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004991 /* Survey without a channel doesn't make sense */
4992 if (!survey.channel) {
4993 res = -EINVAL;
4994 goto out;
4995 }
4996
4997 chan = ieee80211_get_channel(&dev->wiphy,
4998 survey.channel->center_freq);
4999 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5000 survey_idx++;
5001 continue;
5002 }
5003
Holger Schurig61fa7132009-11-11 12:25:40 +01005004 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005005 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005006 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5007 netdev,
5008 &survey) < 0)
5009 goto out;
5010 survey_idx++;
5011 }
5012
5013 out:
5014 cb->args[1] = survey_idx;
5015 res = skb->len;
5016 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005017 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005018 return res;
5019}
5020
Samuel Ortizb23aa672009-07-01 21:26:54 +02005021static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5022{
5023 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5024 NL80211_WPA_VERSION_2));
5025}
5026
Jouni Malinen636a5d32009-03-19 13:39:22 +02005027static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5028{
Johannes Berg4c476992010-10-04 21:36:35 +02005029 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5030 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005031 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005032 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5033 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005034 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005035 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005036 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005037
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005038 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5039 return -EINVAL;
5040
5041 if (!info->attrs[NL80211_ATTR_MAC])
5042 return -EINVAL;
5043
Jouni Malinen17780922009-03-27 20:52:47 +02005044 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5045 return -EINVAL;
5046
Johannes Berg19957bb2009-07-02 17:20:43 +02005047 if (!info->attrs[NL80211_ATTR_SSID])
5048 return -EINVAL;
5049
5050 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5051 return -EINVAL;
5052
Johannes Bergfffd0932009-07-08 14:22:54 +02005053 err = nl80211_parse_key(info, &key);
5054 if (err)
5055 return err;
5056
5057 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005058 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5059 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005060 if (!key.p.key || !key.p.key_len)
5061 return -EINVAL;
5062 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5063 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5064 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5065 key.p.key_len != WLAN_KEY_LEN_WEP104))
5066 return -EINVAL;
5067 if (key.idx > 4)
5068 return -EINVAL;
5069 } else {
5070 key.p.key_len = 0;
5071 key.p.key = NULL;
5072 }
5073
Johannes Bergafea0b72010-08-10 09:46:42 +02005074 if (key.idx >= 0) {
5075 int i;
5076 bool ok = false;
5077 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5078 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5079 ok = true;
5080 break;
5081 }
5082 }
Johannes Berg4c476992010-10-04 21:36:35 +02005083 if (!ok)
5084 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005085 }
5086
Johannes Berg4c476992010-10-04 21:36:35 +02005087 if (!rdev->ops->auth)
5088 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005089
Johannes Berg074ac8d2010-09-16 14:58:22 +02005090 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005091 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5092 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005093
Johannes Berg19957bb2009-07-02 17:20:43 +02005094 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005095 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005096 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005097 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5098 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005099
Johannes Berg19957bb2009-07-02 17:20:43 +02005100 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5101 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5102
5103 if (info->attrs[NL80211_ATTR_IE]) {
5104 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5105 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5106 }
5107
5108 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005109 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005110 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005111
Jouni Malinene39e5b52012-09-30 19:29:39 +03005112 if (auth_type == NL80211_AUTHTYPE_SAE &&
5113 !info->attrs[NL80211_ATTR_SAE_DATA])
5114 return -EINVAL;
5115
5116 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5117 if (auth_type != NL80211_AUTHTYPE_SAE)
5118 return -EINVAL;
5119 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5120 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5121 /* need to include at least Auth Transaction and Status Code */
5122 if (sae_data_len < 4)
5123 return -EINVAL;
5124 }
5125
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005126 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5127
Johannes Berg95de8172012-01-20 13:55:25 +01005128 /*
5129 * Since we no longer track auth state, ignore
5130 * requests to only change local state.
5131 */
5132 if (local_state_change)
5133 return 0;
5134
Johannes Berg4c476992010-10-04 21:36:35 +02005135 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5136 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005137 key.p.key, key.p.key_len, key.idx,
5138 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005139}
5140
Johannes Bergc0692b82010-08-27 14:26:53 +03005141static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5142 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005143 struct cfg80211_crypto_settings *settings,
5144 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005145{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005146 memset(settings, 0, sizeof(*settings));
5147
Samuel Ortizb23aa672009-07-01 21:26:54 +02005148 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5149
Johannes Bergc0692b82010-08-27 14:26:53 +03005150 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5151 u16 proto;
5152 proto = nla_get_u16(
5153 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5154 settings->control_port_ethertype = cpu_to_be16(proto);
5155 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5156 proto != ETH_P_PAE)
5157 return -EINVAL;
5158 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5159 settings->control_port_no_encrypt = true;
5160 } else
5161 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5162
Samuel Ortizb23aa672009-07-01 21:26:54 +02005163 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5164 void *data;
5165 int len, i;
5166
5167 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5168 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5169 settings->n_ciphers_pairwise = len / sizeof(u32);
5170
5171 if (len % sizeof(u32))
5172 return -EINVAL;
5173
Johannes Berg3dc27d22009-07-02 21:36:37 +02005174 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005175 return -EINVAL;
5176
5177 memcpy(settings->ciphers_pairwise, data, len);
5178
5179 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005180 if (!cfg80211_supported_cipher_suite(
5181 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005182 settings->ciphers_pairwise[i]))
5183 return -EINVAL;
5184 }
5185
5186 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5187 settings->cipher_group =
5188 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005189 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5190 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005191 return -EINVAL;
5192 }
5193
5194 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5195 settings->wpa_versions =
5196 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5197 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5198 return -EINVAL;
5199 }
5200
5201 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5202 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005203 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005204
5205 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5206 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5207 settings->n_akm_suites = len / sizeof(u32);
5208
5209 if (len % sizeof(u32))
5210 return -EINVAL;
5211
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005212 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5213 return -EINVAL;
5214
Samuel Ortizb23aa672009-07-01 21:26:54 +02005215 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005216 }
5217
5218 return 0;
5219}
5220
Jouni Malinen636a5d32009-03-19 13:39:22 +02005221static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5222{
Johannes Berg4c476992010-10-04 21:36:35 +02005223 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5224 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005225 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005226 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005227 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005228 int err, ssid_len, ie_len = 0;
5229 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005230 u32 flags = 0;
5231 struct ieee80211_ht_cap *ht_capa = NULL;
5232 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005233
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005234 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5235 return -EINVAL;
5236
5237 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005238 !info->attrs[NL80211_ATTR_SSID] ||
5239 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005240 return -EINVAL;
5241
Johannes Berg4c476992010-10-04 21:36:35 +02005242 if (!rdev->ops->assoc)
5243 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005244
Johannes Berg074ac8d2010-09-16 14:58:22 +02005245 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005246 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5247 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005248
Johannes Berg19957bb2009-07-02 17:20:43 +02005249 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005250
Johannes Berg19957bb2009-07-02 17:20:43 +02005251 chan = ieee80211_get_channel(&rdev->wiphy,
5252 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005253 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5254 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005255
Johannes Berg19957bb2009-07-02 17:20:43 +02005256 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5257 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005258
5259 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005260 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5261 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005262 }
5263
Jouni Malinendc6382c2009-05-06 22:09:37 +03005264 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005265 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005266 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005267 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005268 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005269 else if (mfp != NL80211_MFP_NO)
5270 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005271 }
5272
Johannes Berg3e5d7642009-07-07 14:37:26 +02005273 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5274 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5275
Ben Greear7e7c8922011-11-18 11:31:59 -08005276 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5277 flags |= ASSOC_REQ_DISABLE_HT;
5278
5279 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5280 ht_capa_mask =
5281 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5282
5283 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5284 if (!ht_capa_mask)
5285 return -EINVAL;
5286 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5287 }
5288
Johannes Bergc0692b82010-08-27 14:26:53 +03005289 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005290 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005291 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5292 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005293 &crypto, flags, ht_capa,
5294 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005295
Jouni Malinen636a5d32009-03-19 13:39:22 +02005296 return err;
5297}
5298
5299static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5300{
Johannes Berg4c476992010-10-04 21:36:35 +02005301 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5302 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005303 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005304 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005305 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005306 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005307
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005308 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5309 return -EINVAL;
5310
5311 if (!info->attrs[NL80211_ATTR_MAC])
5312 return -EINVAL;
5313
5314 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5315 return -EINVAL;
5316
Johannes Berg4c476992010-10-04 21:36:35 +02005317 if (!rdev->ops->deauth)
5318 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005319
Johannes Berg074ac8d2010-09-16 14:58:22 +02005320 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005321 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5322 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005323
Johannes Berg19957bb2009-07-02 17:20:43 +02005324 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005325
Johannes Berg19957bb2009-07-02 17:20:43 +02005326 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5327 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005328 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005329 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005330 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005331
5332 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005333 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5334 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005335 }
5336
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005337 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5338
Johannes Berg4c476992010-10-04 21:36:35 +02005339 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5340 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005341}
5342
5343static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5344{
Johannes Berg4c476992010-10-04 21:36:35 +02005345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5346 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005347 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005348 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005349 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005350 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005351
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005352 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5353 return -EINVAL;
5354
5355 if (!info->attrs[NL80211_ATTR_MAC])
5356 return -EINVAL;
5357
5358 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5359 return -EINVAL;
5360
Johannes Berg4c476992010-10-04 21:36:35 +02005361 if (!rdev->ops->disassoc)
5362 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005363
Johannes Berg074ac8d2010-09-16 14:58:22 +02005364 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005365 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5366 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005367
Johannes Berg19957bb2009-07-02 17:20:43 +02005368 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005369
Johannes Berg19957bb2009-07-02 17:20:43 +02005370 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5371 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005372 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005373 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005374 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005375
5376 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005377 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5378 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005379 }
5380
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005381 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5382
Johannes Berg4c476992010-10-04 21:36:35 +02005383 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5384 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005385}
5386
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005387static bool
5388nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5389 int mcast_rate[IEEE80211_NUM_BANDS],
5390 int rateval)
5391{
5392 struct wiphy *wiphy = &rdev->wiphy;
5393 bool found = false;
5394 int band, i;
5395
5396 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5397 struct ieee80211_supported_band *sband;
5398
5399 sband = wiphy->bands[band];
5400 if (!sband)
5401 continue;
5402
5403 for (i = 0; i < sband->n_bitrates; i++) {
5404 if (sband->bitrates[i].bitrate == rateval) {
5405 mcast_rate[band] = i + 1;
5406 found = true;
5407 break;
5408 }
5409 }
5410 }
5411
5412 return found;
5413}
5414
Johannes Berg04a773a2009-04-19 21:24:32 +02005415static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5416{
Johannes Berg4c476992010-10-04 21:36:35 +02005417 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5418 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005419 struct cfg80211_ibss_params ibss;
5420 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005421 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005422 int err;
5423
Johannes Berg8e30bc52009-04-22 17:45:38 +02005424 memset(&ibss, 0, sizeof(ibss));
5425
Johannes Berg04a773a2009-04-19 21:24:32 +02005426 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5427 return -EINVAL;
5428
Johannes Berg683b6d32012-11-08 21:25:48 +01005429 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005430 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5431 return -EINVAL;
5432
Johannes Berg8e30bc52009-04-22 17:45:38 +02005433 ibss.beacon_interval = 100;
5434
5435 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5436 ibss.beacon_interval =
5437 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5438 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5439 return -EINVAL;
5440 }
5441
Johannes Berg4c476992010-10-04 21:36:35 +02005442 if (!rdev->ops->join_ibss)
5443 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005444
Johannes Berg4c476992010-10-04 21:36:35 +02005445 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5446 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005447
Johannes Berg79c97e92009-07-07 03:56:12 +02005448 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005449
Johannes Berg39193492011-09-16 13:45:25 +02005450 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005451 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005452
5453 if (!is_valid_ether_addr(ibss.bssid))
5454 return -EINVAL;
5455 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005456 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5457 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5458
5459 if (info->attrs[NL80211_ATTR_IE]) {
5460 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5461 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5462 }
5463
Johannes Berg683b6d32012-11-08 21:25:48 +01005464 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5465 if (err)
5466 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005467
Johannes Berg683b6d32012-11-08 21:25:48 +01005468 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005469 return -EINVAL;
5470
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005471 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5472 return -EINVAL;
5473 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5474 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
5475
Johannes Berg04a773a2009-04-19 21:24:32 +02005476 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005477 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005478
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005479 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5480 u8 *rates =
5481 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5482 int n_rates =
5483 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5484 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005485 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005486
Johannes Berg34850ab2011-07-18 18:08:35 +02005487 err = ieee80211_get_ratemask(sband, rates, n_rates,
5488 &ibss.basic_rates);
5489 if (err)
5490 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005491 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005492
5493 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5494 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5495 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5496 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005497
Johannes Berg4c476992010-10-04 21:36:35 +02005498 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305499 bool no_ht = false;
5500
Johannes Berg4c476992010-10-04 21:36:35 +02005501 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305502 info->attrs[NL80211_ATTR_KEYS],
5503 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005504 if (IS_ERR(connkeys))
5505 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305506
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005507 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5508 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305509 kfree(connkeys);
5510 return -EINVAL;
5511 }
Johannes Berg4c476992010-10-04 21:36:35 +02005512 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005513
Antonio Quartulli267335d2012-01-31 20:25:47 +01005514 ibss.control_port =
5515 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5516
Johannes Berg4c476992010-10-04 21:36:35 +02005517 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005518 if (err)
5519 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005520 return err;
5521}
5522
5523static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5524{
Johannes Berg4c476992010-10-04 21:36:35 +02005525 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5526 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005527
Johannes Berg4c476992010-10-04 21:36:35 +02005528 if (!rdev->ops->leave_ibss)
5529 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005530
Johannes Berg4c476992010-10-04 21:36:35 +02005531 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5532 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005533
Johannes Berg4c476992010-10-04 21:36:35 +02005534 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005535}
5536
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005537static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5538{
5539 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5540 struct net_device *dev = info->user_ptr[1];
5541 int mcast_rate[IEEE80211_NUM_BANDS];
5542 u32 nla_rate;
5543 int err;
5544
5545 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5546 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5547 return -EOPNOTSUPP;
5548
5549 if (!rdev->ops->set_mcast_rate)
5550 return -EOPNOTSUPP;
5551
5552 memset(mcast_rate, 0, sizeof(mcast_rate));
5553
5554 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5555 return -EINVAL;
5556
5557 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5558 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5559 return -EINVAL;
5560
5561 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5562
5563 return err;
5564}
5565
5566
Johannes Bergaff89a92009-07-01 21:26:51 +02005567#ifdef CONFIG_NL80211_TESTMODE
5568static struct genl_multicast_group nl80211_testmode_mcgrp = {
5569 .name = "testmode",
5570};
5571
5572static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5573{
Johannes Berg4c476992010-10-04 21:36:35 +02005574 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005575 int err;
5576
5577 if (!info->attrs[NL80211_ATTR_TESTDATA])
5578 return -EINVAL;
5579
Johannes Bergaff89a92009-07-01 21:26:51 +02005580 err = -EOPNOTSUPP;
5581 if (rdev->ops->testmode_cmd) {
5582 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005583 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005584 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5585 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5586 rdev->testmode_info = NULL;
5587 }
5588
Johannes Bergaff89a92009-07-01 21:26:51 +02005589 return err;
5590}
5591
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005592static int nl80211_testmode_dump(struct sk_buff *skb,
5593 struct netlink_callback *cb)
5594{
Johannes Berg00918d32011-12-13 17:22:05 +01005595 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005596 int err;
5597 long phy_idx;
5598 void *data = NULL;
5599 int data_len = 0;
5600
5601 if (cb->args[0]) {
5602 /*
5603 * 0 is a valid index, but not valid for args[0],
5604 * so we need to offset by 1.
5605 */
5606 phy_idx = cb->args[0] - 1;
5607 } else {
5608 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5609 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5610 nl80211_policy);
5611 if (err)
5612 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005613
Johannes Berg2bd7e352012-06-15 14:23:16 +02005614 mutex_lock(&cfg80211_mutex);
5615 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5616 nl80211_fam.attrbuf);
5617 if (IS_ERR(rdev)) {
5618 mutex_unlock(&cfg80211_mutex);
5619 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005620 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005621 phy_idx = rdev->wiphy_idx;
5622 rdev = NULL;
5623 mutex_unlock(&cfg80211_mutex);
5624
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005625 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5626 cb->args[1] =
5627 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5628 }
5629
5630 if (cb->args[1]) {
5631 data = nla_data((void *)cb->args[1]);
5632 data_len = nla_len((void *)cb->args[1]);
5633 }
5634
5635 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005636 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5637 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005638 mutex_unlock(&cfg80211_mutex);
5639 return -ENOENT;
5640 }
Johannes Berg00918d32011-12-13 17:22:05 +01005641 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005642 mutex_unlock(&cfg80211_mutex);
5643
Johannes Berg00918d32011-12-13 17:22:05 +01005644 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005645 err = -EOPNOTSUPP;
5646 goto out_err;
5647 }
5648
5649 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005650 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005651 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5652 NL80211_CMD_TESTMODE);
5653 struct nlattr *tmdata;
5654
David S. Miller9360ffd2012-03-29 04:41:26 -04005655 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005656 genlmsg_cancel(skb, hdr);
5657 break;
5658 }
5659
5660 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5661 if (!tmdata) {
5662 genlmsg_cancel(skb, hdr);
5663 break;
5664 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005665 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005666 nla_nest_end(skb, tmdata);
5667
5668 if (err == -ENOBUFS || err == -ENOENT) {
5669 genlmsg_cancel(skb, hdr);
5670 break;
5671 } else if (err) {
5672 genlmsg_cancel(skb, hdr);
5673 goto out_err;
5674 }
5675
5676 genlmsg_end(skb, hdr);
5677 }
5678
5679 err = skb->len;
5680 /* see above */
5681 cb->args[0] = phy_idx + 1;
5682 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005683 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005684 return err;
5685}
5686
Johannes Bergaff89a92009-07-01 21:26:51 +02005687static struct sk_buff *
5688__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005689 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005690{
5691 struct sk_buff *skb;
5692 void *hdr;
5693 struct nlattr *data;
5694
5695 skb = nlmsg_new(approxlen + 100, gfp);
5696 if (!skb)
5697 return NULL;
5698
Eric W. Biederman15e47302012-09-07 20:12:54 +00005699 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005700 if (!hdr) {
5701 kfree_skb(skb);
5702 return NULL;
5703 }
5704
David S. Miller9360ffd2012-03-29 04:41:26 -04005705 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5706 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005707 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5708
5709 ((void **)skb->cb)[0] = rdev;
5710 ((void **)skb->cb)[1] = hdr;
5711 ((void **)skb->cb)[2] = data;
5712
5713 return skb;
5714
5715 nla_put_failure:
5716 kfree_skb(skb);
5717 return NULL;
5718}
5719
5720struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5721 int approxlen)
5722{
5723 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5724
5725 if (WARN_ON(!rdev->testmode_info))
5726 return NULL;
5727
5728 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005729 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005730 rdev->testmode_info->snd_seq,
5731 GFP_KERNEL);
5732}
5733EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5734
5735int cfg80211_testmode_reply(struct sk_buff *skb)
5736{
5737 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5738 void *hdr = ((void **)skb->cb)[1];
5739 struct nlattr *data = ((void **)skb->cb)[2];
5740
5741 if (WARN_ON(!rdev->testmode_info)) {
5742 kfree_skb(skb);
5743 return -EINVAL;
5744 }
5745
5746 nla_nest_end(skb, data);
5747 genlmsg_end(skb, hdr);
5748 return genlmsg_reply(skb, rdev->testmode_info);
5749}
5750EXPORT_SYMBOL(cfg80211_testmode_reply);
5751
5752struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5753 int approxlen, gfp_t gfp)
5754{
5755 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5756
5757 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5758}
5759EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5760
5761void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5762{
5763 void *hdr = ((void **)skb->cb)[1];
5764 struct nlattr *data = ((void **)skb->cb)[2];
5765
5766 nla_nest_end(skb, data);
5767 genlmsg_end(skb, hdr);
5768 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5769}
5770EXPORT_SYMBOL(cfg80211_testmode_event);
5771#endif
5772
Samuel Ortizb23aa672009-07-01 21:26:54 +02005773static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5774{
Johannes Berg4c476992010-10-04 21:36:35 +02005775 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5776 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005777 struct cfg80211_connect_params connect;
5778 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005779 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005780 int err;
5781
5782 memset(&connect, 0, sizeof(connect));
5783
5784 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5785 return -EINVAL;
5786
5787 if (!info->attrs[NL80211_ATTR_SSID] ||
5788 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5789 return -EINVAL;
5790
5791 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5792 connect.auth_type =
5793 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005794 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5795 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005796 return -EINVAL;
5797 } else
5798 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5799
5800 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5801
Johannes Bergc0692b82010-08-27 14:26:53 +03005802 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005803 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005804 if (err)
5805 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +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;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005810
Johannes Berg79c97e92009-07-07 03:56:12 +02005811 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005812
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305813 connect.bg_scan_period = -1;
5814 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5815 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5816 connect.bg_scan_period =
5817 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5818 }
5819
Samuel Ortizb23aa672009-07-01 21:26:54 +02005820 if (info->attrs[NL80211_ATTR_MAC])
5821 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5822 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5823 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5824
5825 if (info->attrs[NL80211_ATTR_IE]) {
5826 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5827 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5828 }
5829
5830 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5831 connect.channel =
5832 ieee80211_get_channel(wiphy,
5833 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5834 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005835 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5836 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005837 }
5838
Johannes Bergfffd0932009-07-08 14:22:54 +02005839 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5840 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305841 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005842 if (IS_ERR(connkeys))
5843 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005844 }
5845
Ben Greear7e7c8922011-11-18 11:31:59 -08005846 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5847 connect.flags |= ASSOC_REQ_DISABLE_HT;
5848
5849 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5850 memcpy(&connect.ht_capa_mask,
5851 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5852 sizeof(connect.ht_capa_mask));
5853
5854 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005855 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5856 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005857 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005858 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005859 memcpy(&connect.ht_capa,
5860 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5861 sizeof(connect.ht_capa));
5862 }
5863
Johannes Bergfffd0932009-07-08 14:22:54 +02005864 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005865 if (err)
5866 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005867 return err;
5868}
5869
5870static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5871{
Johannes Berg4c476992010-10-04 21:36:35 +02005872 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5873 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005874 u16 reason;
5875
5876 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5877 reason = WLAN_REASON_DEAUTH_LEAVING;
5878 else
5879 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5880
5881 if (reason == 0)
5882 return -EINVAL;
5883
Johannes Berg074ac8d2010-09-16 14:58:22 +02005884 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005885 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5886 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005887
Johannes Berg4c476992010-10-04 21:36:35 +02005888 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005889}
5890
Johannes Berg463d0182009-07-14 00:33:35 +02005891static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5892{
Johannes Berg4c476992010-10-04 21:36:35 +02005893 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005894 struct net *net;
5895 int err;
5896 u32 pid;
5897
5898 if (!info->attrs[NL80211_ATTR_PID])
5899 return -EINVAL;
5900
5901 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5902
Johannes Berg463d0182009-07-14 00:33:35 +02005903 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005904 if (IS_ERR(net))
5905 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005906
5907 err = 0;
5908
5909 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005910 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5911 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005912
Johannes Berg463d0182009-07-14 00:33:35 +02005913 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005914 return err;
5915}
5916
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005917static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5918{
Johannes Berg4c476992010-10-04 21:36:35 +02005919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005920 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5921 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005922 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005923 struct cfg80211_pmksa pmksa;
5924
5925 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5926
5927 if (!info->attrs[NL80211_ATTR_MAC])
5928 return -EINVAL;
5929
5930 if (!info->attrs[NL80211_ATTR_PMKID])
5931 return -EINVAL;
5932
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005933 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5934 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5935
Johannes Berg074ac8d2010-09-16 14:58:22 +02005936 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005937 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5938 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005939
5940 switch (info->genlhdr->cmd) {
5941 case NL80211_CMD_SET_PMKSA:
5942 rdev_ops = rdev->ops->set_pmksa;
5943 break;
5944 case NL80211_CMD_DEL_PMKSA:
5945 rdev_ops = rdev->ops->del_pmksa;
5946 break;
5947 default:
5948 WARN_ON(1);
5949 break;
5950 }
5951
Johannes Berg4c476992010-10-04 21:36:35 +02005952 if (!rdev_ops)
5953 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005954
Johannes Berg4c476992010-10-04 21:36:35 +02005955 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005956}
5957
5958static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5959{
Johannes Berg4c476992010-10-04 21:36:35 +02005960 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5961 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005962
Johannes Berg074ac8d2010-09-16 14:58:22 +02005963 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005964 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5965 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005966
Johannes Berg4c476992010-10-04 21:36:35 +02005967 if (!rdev->ops->flush_pmksa)
5968 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005969
Hila Gonene35e4d22012-06-27 17:19:42 +03005970 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005971}
5972
Arik Nemtsov109086c2011-09-28 14:12:50 +03005973static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5974{
5975 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5976 struct net_device *dev = info->user_ptr[1];
5977 u8 action_code, dialog_token;
5978 u16 status_code;
5979 u8 *peer;
5980
5981 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5982 !rdev->ops->tdls_mgmt)
5983 return -EOPNOTSUPP;
5984
5985 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5986 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5987 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5988 !info->attrs[NL80211_ATTR_IE] ||
5989 !info->attrs[NL80211_ATTR_MAC])
5990 return -EINVAL;
5991
5992 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5993 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5994 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5995 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5996
Hila Gonene35e4d22012-06-27 17:19:42 +03005997 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
5998 dialog_token, status_code,
5999 nla_data(info->attrs[NL80211_ATTR_IE]),
6000 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006001}
6002
6003static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6004{
6005 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6006 struct net_device *dev = info->user_ptr[1];
6007 enum nl80211_tdls_operation operation;
6008 u8 *peer;
6009
6010 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6011 !rdev->ops->tdls_oper)
6012 return -EOPNOTSUPP;
6013
6014 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6015 !info->attrs[NL80211_ATTR_MAC])
6016 return -EINVAL;
6017
6018 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6019 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6020
Hila Gonene35e4d22012-06-27 17:19:42 +03006021 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006022}
6023
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006024static int nl80211_remain_on_channel(struct sk_buff *skb,
6025 struct genl_info *info)
6026{
Johannes Berg4c476992010-10-04 21:36:35 +02006027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006028 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006029 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006030 struct sk_buff *msg;
6031 void *hdr;
6032 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006033 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006034 int err;
6035
6036 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6037 !info->attrs[NL80211_ATTR_DURATION])
6038 return -EINVAL;
6039
6040 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6041
Johannes Berg7c4ef712011-11-18 15:33:48 +01006042 if (!rdev->ops->remain_on_channel ||
6043 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006044 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006045
Johannes Bergebf348f2012-06-01 12:50:54 +02006046 /*
6047 * We should be on that channel for at least a minimum amount of
6048 * time (10ms) but no longer than the driver supports.
6049 */
6050 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6051 duration > rdev->wiphy.max_remain_on_channel_duration)
6052 return -EINVAL;
6053
Johannes Berg683b6d32012-11-08 21:25:48 +01006054 err = nl80211_parse_chandef(rdev, info, &chandef);
6055 if (err)
6056 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006057
6058 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006059 if (!msg)
6060 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006061
Eric W. Biederman15e47302012-09-07 20:12:54 +00006062 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006063 NL80211_CMD_REMAIN_ON_CHANNEL);
6064
6065 if (IS_ERR(hdr)) {
6066 err = PTR_ERR(hdr);
6067 goto free_msg;
6068 }
6069
Johannes Berg683b6d32012-11-08 21:25:48 +01006070 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6071 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006072
6073 if (err)
6074 goto free_msg;
6075
David S. Miller9360ffd2012-03-29 04:41:26 -04006076 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6077 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006078
6079 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006080
6081 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006082
6083 nla_put_failure:
6084 err = -ENOBUFS;
6085 free_msg:
6086 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006087 return err;
6088}
6089
6090static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6091 struct genl_info *info)
6092{
Johannes Berg4c476992010-10-04 21:36:35 +02006093 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006094 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006095 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006096
6097 if (!info->attrs[NL80211_ATTR_COOKIE])
6098 return -EINVAL;
6099
Johannes Berg4c476992010-10-04 21:36:35 +02006100 if (!rdev->ops->cancel_remain_on_channel)
6101 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006102
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006103 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6104
Hila Gonene35e4d22012-06-27 17:19:42 +03006105 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006106}
6107
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006108static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6109 u8 *rates, u8 rates_len)
6110{
6111 u8 i;
6112 u32 mask = 0;
6113
6114 for (i = 0; i < rates_len; i++) {
6115 int rate = (rates[i] & 0x7f) * 5;
6116 int ridx;
6117 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6118 struct ieee80211_rate *srate =
6119 &sband->bitrates[ridx];
6120 if (rate == srate->bitrate) {
6121 mask |= 1 << ridx;
6122 break;
6123 }
6124 }
6125 if (ridx == sband->n_bitrates)
6126 return 0; /* rate not found */
6127 }
6128
6129 return mask;
6130}
6131
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006132static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6133 u8 *rates, u8 rates_len,
6134 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6135{
6136 u8 i;
6137
6138 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6139
6140 for (i = 0; i < rates_len; i++) {
6141 int ridx, rbit;
6142
6143 ridx = rates[i] / 8;
6144 rbit = BIT(rates[i] % 8);
6145
6146 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006147 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006148 return false;
6149
6150 /* check availability */
6151 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6152 mcs[ridx] |= rbit;
6153 else
6154 return false;
6155 }
6156
6157 return true;
6158}
6159
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006160static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006161 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6162 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006163 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6164 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006165};
6166
6167static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6168 struct genl_info *info)
6169{
6170 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006171 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006172 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006173 int rem, i;
6174 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006175 struct nlattr *tx_rates;
6176 struct ieee80211_supported_band *sband;
6177
6178 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6179 return -EINVAL;
6180
Johannes Berg4c476992010-10-04 21:36:35 +02006181 if (!rdev->ops->set_bitrate_mask)
6182 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006183
6184 memset(&mask, 0, sizeof(mask));
6185 /* Default to all rates enabled */
6186 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6187 sband = rdev->wiphy.bands[i];
6188 mask.control[i].legacy =
6189 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006190 if (sband)
6191 memcpy(mask.control[i].mcs,
6192 sband->ht_cap.mcs.rx_mask,
6193 sizeof(mask.control[i].mcs));
6194 else
6195 memset(mask.control[i].mcs, 0,
6196 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006197 }
6198
6199 /*
6200 * The nested attribute uses enum nl80211_band as the index. This maps
6201 * directly to the enum ieee80211_band values used in cfg80211.
6202 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006203 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006204 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6205 {
6206 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006207 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6208 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006209 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006210 if (sband == NULL)
6211 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006212 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6213 nla_len(tx_rates), nl80211_txattr_policy);
6214 if (tb[NL80211_TXRATE_LEGACY]) {
6215 mask.control[band].legacy = rateset_to_mask(
6216 sband,
6217 nla_data(tb[NL80211_TXRATE_LEGACY]),
6218 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306219 if ((mask.control[band].legacy == 0) &&
6220 nla_len(tb[NL80211_TXRATE_LEGACY]))
6221 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006222 }
6223 if (tb[NL80211_TXRATE_MCS]) {
6224 if (!ht_rateset_to_mask(
6225 sband,
6226 nla_data(tb[NL80211_TXRATE_MCS]),
6227 nla_len(tb[NL80211_TXRATE_MCS]),
6228 mask.control[band].mcs))
6229 return -EINVAL;
6230 }
6231
6232 if (mask.control[band].legacy == 0) {
6233 /* don't allow empty legacy rates if HT
6234 * is not even supported. */
6235 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6236 return -EINVAL;
6237
6238 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6239 if (mask.control[band].mcs[i])
6240 break;
6241
6242 /* legacy and mcs rates may not be both empty */
6243 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006244 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006245 }
6246 }
6247
Hila Gonene35e4d22012-06-27 17:19:42 +03006248 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006249}
6250
Johannes Berg2e161f72010-08-12 15:38:38 +02006251static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006252{
Johannes Berg4c476992010-10-04 21:36:35 +02006253 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006254 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006255 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006256
6257 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6258 return -EINVAL;
6259
Johannes Berg2e161f72010-08-12 15:38:38 +02006260 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6261 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006262
Johannes Berg71bbc992012-06-15 15:30:18 +02006263 switch (wdev->iftype) {
6264 case NL80211_IFTYPE_STATION:
6265 case NL80211_IFTYPE_ADHOC:
6266 case NL80211_IFTYPE_P2P_CLIENT:
6267 case NL80211_IFTYPE_AP:
6268 case NL80211_IFTYPE_AP_VLAN:
6269 case NL80211_IFTYPE_MESH_POINT:
6270 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006271 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006272 break;
6273 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006274 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006275 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006276
6277 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006278 if (!rdev->ops->mgmt_tx)
6279 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006280
Eric W. Biederman15e47302012-09-07 20:12:54 +00006281 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006282 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6283 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006284}
6285
Johannes Berg2e161f72010-08-12 15:38:38 +02006286static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006287{
Johannes Berg4c476992010-10-04 21:36:35 +02006288 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006289 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006290 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006291 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006292 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006293 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006294 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006295 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006296 bool offchan, no_cck, dont_wait_for_ack;
6297
6298 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006299
Johannes Berg683b6d32012-11-08 21:25:48 +01006300 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006301 return -EINVAL;
6302
Johannes Berg4c476992010-10-04 21:36:35 +02006303 if (!rdev->ops->mgmt_tx)
6304 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006305
Johannes Berg71bbc992012-06-15 15:30:18 +02006306 switch (wdev->iftype) {
6307 case NL80211_IFTYPE_STATION:
6308 case NL80211_IFTYPE_ADHOC:
6309 case NL80211_IFTYPE_P2P_CLIENT:
6310 case NL80211_IFTYPE_AP:
6311 case NL80211_IFTYPE_AP_VLAN:
6312 case NL80211_IFTYPE_MESH_POINT:
6313 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006314 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006315 break;
6316 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006317 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006318 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006319
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006320 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006321 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006322 return -EINVAL;
6323 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006324
6325 /*
6326 * We should wait on the channel for at least a minimum amount
6327 * of time (10ms) but no longer than the driver supports.
6328 */
6329 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6330 wait > rdev->wiphy.max_remain_on_channel_duration)
6331 return -EINVAL;
6332
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006333 }
6334
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006335 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6336
Johannes Berg7c4ef712011-11-18 15:33:48 +01006337 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6338 return -EINVAL;
6339
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306340 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6341
Johannes Berg683b6d32012-11-08 21:25:48 +01006342 err = nl80211_parse_chandef(rdev, info, &chandef);
6343 if (err)
6344 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006345
Johannes Berge247bd902011-11-04 11:18:21 +01006346 if (!dont_wait_for_ack) {
6347 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6348 if (!msg)
6349 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006350
Eric W. Biederman15e47302012-09-07 20:12:54 +00006351 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006352 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006353
Johannes Berge247bd902011-11-04 11:18:21 +01006354 if (IS_ERR(hdr)) {
6355 err = PTR_ERR(hdr);
6356 goto free_msg;
6357 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006358 }
Johannes Berge247bd902011-11-04 11:18:21 +01006359
Johannes Berg683b6d32012-11-08 21:25:48 +01006360 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006361 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6362 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006363 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006364 if (err)
6365 goto free_msg;
6366
Johannes Berge247bd902011-11-04 11:18:21 +01006367 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006368 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6369 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006370
Johannes Berge247bd902011-11-04 11:18:21 +01006371 genlmsg_end(msg, hdr);
6372 return genlmsg_reply(msg, info);
6373 }
6374
6375 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006376
6377 nla_put_failure:
6378 err = -ENOBUFS;
6379 free_msg:
6380 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006381 return err;
6382}
6383
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006384static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6385{
6386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006387 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006388 u64 cookie;
6389
6390 if (!info->attrs[NL80211_ATTR_COOKIE])
6391 return -EINVAL;
6392
6393 if (!rdev->ops->mgmt_tx_cancel_wait)
6394 return -EOPNOTSUPP;
6395
Johannes Berg71bbc992012-06-15 15:30:18 +02006396 switch (wdev->iftype) {
6397 case NL80211_IFTYPE_STATION:
6398 case NL80211_IFTYPE_ADHOC:
6399 case NL80211_IFTYPE_P2P_CLIENT:
6400 case NL80211_IFTYPE_AP:
6401 case NL80211_IFTYPE_AP_VLAN:
6402 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006403 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006404 break;
6405 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006406 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006407 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006408
6409 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6410
Hila Gonene35e4d22012-06-27 17:19:42 +03006411 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006412}
6413
Kalle Valoffb9eb32010-02-17 17:58:10 +02006414static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6415{
Johannes Berg4c476992010-10-04 21:36:35 +02006416 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006417 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006418 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006419 u8 ps_state;
6420 bool state;
6421 int err;
6422
Johannes Berg4c476992010-10-04 21:36:35 +02006423 if (!info->attrs[NL80211_ATTR_PS_STATE])
6424 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006425
6426 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6427
Johannes Berg4c476992010-10-04 21:36:35 +02006428 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6429 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006430
6431 wdev = dev->ieee80211_ptr;
6432
Johannes Berg4c476992010-10-04 21:36:35 +02006433 if (!rdev->ops->set_power_mgmt)
6434 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006435
6436 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6437
6438 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006439 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006440
Hila Gonene35e4d22012-06-27 17:19:42 +03006441 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006442 if (!err)
6443 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006444 return err;
6445}
6446
6447static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6448{
Johannes Berg4c476992010-10-04 21:36:35 +02006449 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006450 enum nl80211_ps_state ps_state;
6451 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006452 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006453 struct sk_buff *msg;
6454 void *hdr;
6455 int err;
6456
Kalle Valoffb9eb32010-02-17 17:58:10 +02006457 wdev = dev->ieee80211_ptr;
6458
Johannes Berg4c476992010-10-04 21:36:35 +02006459 if (!rdev->ops->set_power_mgmt)
6460 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006461
6462 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006463 if (!msg)
6464 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006465
Eric W. Biederman15e47302012-09-07 20:12:54 +00006466 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006467 NL80211_CMD_GET_POWER_SAVE);
6468 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006469 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006470 goto free_msg;
6471 }
6472
6473 if (wdev->ps)
6474 ps_state = NL80211_PS_ENABLED;
6475 else
6476 ps_state = NL80211_PS_DISABLED;
6477
David S. Miller9360ffd2012-03-29 04:41:26 -04006478 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6479 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006480
6481 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006482 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006483
Johannes Berg4c476992010-10-04 21:36:35 +02006484 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006485 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006486 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006487 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006488 return err;
6489}
6490
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006491static struct nla_policy
6492nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6493 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6494 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6495 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006496 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6497 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6498 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006499};
6500
Thomas Pedersen84f10702012-07-12 16:17:33 -07006501static int nl80211_set_cqm_txe(struct genl_info *info,
6502 u32 rate, u32 pkts, u32 intvl)
6503{
6504 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6505 struct wireless_dev *wdev;
6506 struct net_device *dev = info->user_ptr[1];
6507
6508 if ((rate < 0 || rate > 100) ||
6509 (intvl < 0 || intvl > NL80211_CQM_TXE_MAX_INTVL))
6510 return -EINVAL;
6511
6512 wdev = dev->ieee80211_ptr;
6513
6514 if (!rdev->ops->set_cqm_txe_config)
6515 return -EOPNOTSUPP;
6516
6517 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6518 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6519 return -EOPNOTSUPP;
6520
Hila Gonene35e4d22012-06-27 17:19:42 +03006521 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006522}
6523
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006524static int nl80211_set_cqm_rssi(struct genl_info *info,
6525 s32 threshold, u32 hysteresis)
6526{
Johannes Berg4c476992010-10-04 21:36:35 +02006527 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006528 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006529 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006530
6531 if (threshold > 0)
6532 return -EINVAL;
6533
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006534 wdev = dev->ieee80211_ptr;
6535
Johannes Berg4c476992010-10-04 21:36:35 +02006536 if (!rdev->ops->set_cqm_rssi_config)
6537 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006538
Johannes Berg074ac8d2010-09-16 14:58:22 +02006539 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006540 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6541 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006542
Hila Gonene35e4d22012-06-27 17:19:42 +03006543 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006544}
6545
6546static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6547{
6548 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6549 struct nlattr *cqm;
6550 int err;
6551
6552 cqm = info->attrs[NL80211_ATTR_CQM];
6553 if (!cqm) {
6554 err = -EINVAL;
6555 goto out;
6556 }
6557
6558 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6559 nl80211_attr_cqm_policy);
6560 if (err)
6561 goto out;
6562
6563 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6564 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6565 s32 threshold;
6566 u32 hysteresis;
6567 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6568 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6569 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006570 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6571 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6572 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6573 u32 rate, pkts, intvl;
6574 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6575 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6576 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6577 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006578 } else
6579 err = -EINVAL;
6580
6581out:
6582 return err;
6583}
6584
Johannes Berg29cbe682010-12-03 09:20:44 +01006585static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6586{
6587 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6588 struct net_device *dev = info->user_ptr[1];
6589 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006590 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006591 int err;
6592
6593 /* start with default */
6594 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006595 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006596
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006597 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006598 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006599 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006600 if (err)
6601 return err;
6602 }
6603
6604 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6605 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6606 return -EINVAL;
6607
Javier Cardonac80d5452010-12-16 17:37:49 -08006608 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6609 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6610
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006611 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6612 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6613 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6614 return -EINVAL;
6615
Javier Cardonac80d5452010-12-16 17:37:49 -08006616 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6617 /* parse additional setup parameters if given */
6618 err = nl80211_parse_mesh_setup(info, &setup);
6619 if (err)
6620 return err;
6621 }
6622
Johannes Bergcc1d2802012-05-16 23:50:20 +02006623 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01006624 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
6625 if (err)
6626 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006627 } else {
6628 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01006629 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006630 }
6631
Javier Cardonac80d5452010-12-16 17:37:49 -08006632 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006633}
6634
6635static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6636{
6637 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6638 struct net_device *dev = info->user_ptr[1];
6639
6640 return cfg80211_leave_mesh(rdev, dev);
6641}
6642
Johannes Bergdfb89c52012-06-27 09:23:48 +02006643#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006644static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6645{
6646 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6647 struct sk_buff *msg;
6648 void *hdr;
6649
6650 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6651 return -EOPNOTSUPP;
6652
6653 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6654 if (!msg)
6655 return -ENOMEM;
6656
Eric W. Biederman15e47302012-09-07 20:12:54 +00006657 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006658 NL80211_CMD_GET_WOWLAN);
6659 if (!hdr)
6660 goto nla_put_failure;
6661
6662 if (rdev->wowlan) {
6663 struct nlattr *nl_wowlan;
6664
6665 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6666 if (!nl_wowlan)
6667 goto nla_put_failure;
6668
David S. Miller9360ffd2012-03-29 04:41:26 -04006669 if ((rdev->wowlan->any &&
6670 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6671 (rdev->wowlan->disconnect &&
6672 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6673 (rdev->wowlan->magic_pkt &&
6674 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6675 (rdev->wowlan->gtk_rekey_failure &&
6676 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6677 (rdev->wowlan->eap_identity_req &&
6678 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6679 (rdev->wowlan->four_way_handshake &&
6680 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6681 (rdev->wowlan->rfkill_release &&
6682 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6683 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006684 if (rdev->wowlan->n_patterns) {
6685 struct nlattr *nl_pats, *nl_pat;
6686 int i, pat_len;
6687
6688 nl_pats = nla_nest_start(msg,
6689 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6690 if (!nl_pats)
6691 goto nla_put_failure;
6692
6693 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6694 nl_pat = nla_nest_start(msg, i + 1);
6695 if (!nl_pat)
6696 goto nla_put_failure;
6697 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006698 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6699 DIV_ROUND_UP(pat_len, 8),
6700 rdev->wowlan->patterns[i].mask) ||
6701 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6702 pat_len,
6703 rdev->wowlan->patterns[i].pattern))
6704 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006705 nla_nest_end(msg, nl_pat);
6706 }
6707 nla_nest_end(msg, nl_pats);
6708 }
6709
6710 nla_nest_end(msg, nl_wowlan);
6711 }
6712
6713 genlmsg_end(msg, hdr);
6714 return genlmsg_reply(msg, info);
6715
6716nla_put_failure:
6717 nlmsg_free(msg);
6718 return -ENOBUFS;
6719}
6720
6721static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6722{
6723 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6724 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006725 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006726 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006727 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6728 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006729 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006730
6731 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6732 return -EOPNOTSUPP;
6733
Johannes Bergae33bd82012-07-12 16:25:02 +02006734 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6735 cfg80211_rdev_free_wowlan(rdev);
6736 rdev->wowlan = NULL;
6737 goto set_wakeup;
6738 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006739
6740 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6741 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6742 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6743 nl80211_wowlan_policy);
6744 if (err)
6745 return err;
6746
6747 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6748 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6749 return -EINVAL;
6750 new_triggers.any = true;
6751 }
6752
6753 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6754 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6755 return -EINVAL;
6756 new_triggers.disconnect = true;
6757 }
6758
6759 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6760 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6761 return -EINVAL;
6762 new_triggers.magic_pkt = true;
6763 }
6764
Johannes Berg77dbbb12011-07-13 10:48:55 +02006765 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6766 return -EINVAL;
6767
6768 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6769 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6770 return -EINVAL;
6771 new_triggers.gtk_rekey_failure = true;
6772 }
6773
6774 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6775 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6776 return -EINVAL;
6777 new_triggers.eap_identity_req = true;
6778 }
6779
6780 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6781 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6782 return -EINVAL;
6783 new_triggers.four_way_handshake = true;
6784 }
6785
6786 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6787 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6788 return -EINVAL;
6789 new_triggers.rfkill_release = true;
6790 }
6791
Johannes Bergff1b6e62011-05-04 15:37:28 +02006792 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6793 struct nlattr *pat;
6794 int n_patterns = 0;
6795 int rem, pat_len, mask_len;
6796 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6797
6798 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6799 rem)
6800 n_patterns++;
6801 if (n_patterns > wowlan->n_patterns)
6802 return -EINVAL;
6803
6804 new_triggers.patterns = kcalloc(n_patterns,
6805 sizeof(new_triggers.patterns[0]),
6806 GFP_KERNEL);
6807 if (!new_triggers.patterns)
6808 return -ENOMEM;
6809
6810 new_triggers.n_patterns = n_patterns;
6811 i = 0;
6812
6813 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6814 rem) {
6815 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6816 nla_data(pat), nla_len(pat), NULL);
6817 err = -EINVAL;
6818 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6819 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6820 goto error;
6821 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6822 mask_len = DIV_ROUND_UP(pat_len, 8);
6823 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6824 mask_len)
6825 goto error;
6826 if (pat_len > wowlan->pattern_max_len ||
6827 pat_len < wowlan->pattern_min_len)
6828 goto error;
6829
6830 new_triggers.patterns[i].mask =
6831 kmalloc(mask_len + pat_len, GFP_KERNEL);
6832 if (!new_triggers.patterns[i].mask) {
6833 err = -ENOMEM;
6834 goto error;
6835 }
6836 new_triggers.patterns[i].pattern =
6837 new_triggers.patterns[i].mask + mask_len;
6838 memcpy(new_triggers.patterns[i].mask,
6839 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6840 mask_len);
6841 new_triggers.patterns[i].pattern_len = pat_len;
6842 memcpy(new_triggers.patterns[i].pattern,
6843 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6844 pat_len);
6845 i++;
6846 }
6847 }
6848
Johannes Bergae33bd82012-07-12 16:25:02 +02006849 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6850 if (!ntrig) {
6851 err = -ENOMEM;
6852 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006853 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006854 cfg80211_rdev_free_wowlan(rdev);
6855 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006856
Johannes Bergae33bd82012-07-12 16:25:02 +02006857 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006858 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03006859 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02006860
Johannes Bergff1b6e62011-05-04 15:37:28 +02006861 return 0;
6862 error:
6863 for (i = 0; i < new_triggers.n_patterns; i++)
6864 kfree(new_triggers.patterns[i].mask);
6865 kfree(new_triggers.patterns);
6866 return err;
6867}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006868#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006869
Johannes Berge5497d72011-07-05 16:35:40 +02006870static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6871{
6872 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6873 struct net_device *dev = info->user_ptr[1];
6874 struct wireless_dev *wdev = dev->ieee80211_ptr;
6875 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6876 struct cfg80211_gtk_rekey_data rekey_data;
6877 int err;
6878
6879 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6880 return -EINVAL;
6881
6882 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6883 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6884 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6885 nl80211_rekey_policy);
6886 if (err)
6887 return err;
6888
6889 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6890 return -ERANGE;
6891 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6892 return -ERANGE;
6893 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6894 return -ERANGE;
6895
6896 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6897 NL80211_KEK_LEN);
6898 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6899 NL80211_KCK_LEN);
6900 memcpy(rekey_data.replay_ctr,
6901 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6902 NL80211_REPLAY_CTR_LEN);
6903
6904 wdev_lock(wdev);
6905 if (!wdev->current_bss) {
6906 err = -ENOTCONN;
6907 goto out;
6908 }
6909
6910 if (!rdev->ops->set_rekey_data) {
6911 err = -EOPNOTSUPP;
6912 goto out;
6913 }
6914
Hila Gonene35e4d22012-06-27 17:19:42 +03006915 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02006916 out:
6917 wdev_unlock(wdev);
6918 return err;
6919}
6920
Johannes Berg28946da2011-11-04 11:18:12 +01006921static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6922 struct genl_info *info)
6923{
6924 struct net_device *dev = info->user_ptr[1];
6925 struct wireless_dev *wdev = dev->ieee80211_ptr;
6926
6927 if (wdev->iftype != NL80211_IFTYPE_AP &&
6928 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6929 return -EINVAL;
6930
Eric W. Biederman15e47302012-09-07 20:12:54 +00006931 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006932 return -EBUSY;
6933
Eric W. Biederman15e47302012-09-07 20:12:54 +00006934 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006935 return 0;
6936}
6937
Johannes Berg7f6cf312011-11-04 11:18:15 +01006938static int nl80211_probe_client(struct sk_buff *skb,
6939 struct genl_info *info)
6940{
6941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6942 struct net_device *dev = info->user_ptr[1];
6943 struct wireless_dev *wdev = dev->ieee80211_ptr;
6944 struct sk_buff *msg;
6945 void *hdr;
6946 const u8 *addr;
6947 u64 cookie;
6948 int err;
6949
6950 if (wdev->iftype != NL80211_IFTYPE_AP &&
6951 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6952 return -EOPNOTSUPP;
6953
6954 if (!info->attrs[NL80211_ATTR_MAC])
6955 return -EINVAL;
6956
6957 if (!rdev->ops->probe_client)
6958 return -EOPNOTSUPP;
6959
6960 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6961 if (!msg)
6962 return -ENOMEM;
6963
Eric W. Biederman15e47302012-09-07 20:12:54 +00006964 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01006965 NL80211_CMD_PROBE_CLIENT);
6966
6967 if (IS_ERR(hdr)) {
6968 err = PTR_ERR(hdr);
6969 goto free_msg;
6970 }
6971
6972 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6973
Hila Gonene35e4d22012-06-27 17:19:42 +03006974 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01006975 if (err)
6976 goto free_msg;
6977
David S. Miller9360ffd2012-03-29 04:41:26 -04006978 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6979 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006980
6981 genlmsg_end(msg, hdr);
6982
6983 return genlmsg_reply(msg, info);
6984
6985 nla_put_failure:
6986 err = -ENOBUFS;
6987 free_msg:
6988 nlmsg_free(msg);
6989 return err;
6990}
6991
Johannes Berg5e760232011-11-04 11:18:17 +01006992static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6993{
6994 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07006995 struct cfg80211_beacon_registration *reg, *nreg;
6996 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01006997
6998 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6999 return -EOPNOTSUPP;
7000
Ben Greear37c73b52012-10-26 14:49:25 -07007001 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7002 if (!nreg)
7003 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007004
Ben Greear37c73b52012-10-26 14:49:25 -07007005 /* First, check if already registered. */
7006 spin_lock_bh(&rdev->beacon_registrations_lock);
7007 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7008 if (reg->nlportid == info->snd_portid) {
7009 rv = -EALREADY;
7010 goto out_err;
7011 }
7012 }
7013 /* Add it to the list */
7014 nreg->nlportid = info->snd_portid;
7015 list_add(&nreg->list, &rdev->beacon_registrations);
7016
7017 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007018
7019 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007020out_err:
7021 spin_unlock_bh(&rdev->beacon_registrations_lock);
7022 kfree(nreg);
7023 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007024}
7025
Johannes Berg98104fde2012-06-16 00:19:54 +02007026static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7027{
7028 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7029 struct wireless_dev *wdev = info->user_ptr[1];
7030 int err;
7031
7032 if (!rdev->ops->start_p2p_device)
7033 return -EOPNOTSUPP;
7034
7035 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7036 return -EOPNOTSUPP;
7037
7038 if (wdev->p2p_started)
7039 return 0;
7040
7041 mutex_lock(&rdev->devlist_mtx);
7042 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7043 mutex_unlock(&rdev->devlist_mtx);
7044 if (err)
7045 return err;
7046
Johannes Bergeeb126e2012-10-23 15:16:50 +02007047 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007048 if (err)
7049 return err;
7050
7051 wdev->p2p_started = true;
7052 mutex_lock(&rdev->devlist_mtx);
7053 rdev->opencount++;
7054 mutex_unlock(&rdev->devlist_mtx);
7055
7056 return 0;
7057}
7058
7059static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7060{
7061 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7062 struct wireless_dev *wdev = info->user_ptr[1];
7063
7064 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7065 return -EOPNOTSUPP;
7066
7067 if (!rdev->ops->stop_p2p_device)
7068 return -EOPNOTSUPP;
7069
7070 if (!wdev->p2p_started)
7071 return 0;
7072
Johannes Bergeeb126e2012-10-23 15:16:50 +02007073 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007074 wdev->p2p_started = false;
7075
7076 mutex_lock(&rdev->devlist_mtx);
7077 rdev->opencount--;
7078 mutex_unlock(&rdev->devlist_mtx);
7079
7080 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7081 rdev->scan_req->aborted = true;
7082 ___cfg80211_scan_done(rdev, true);
7083 }
7084
7085 return 0;
7086}
7087
Johannes Berg4c476992010-10-04 21:36:35 +02007088#define NL80211_FLAG_NEED_WIPHY 0x01
7089#define NL80211_FLAG_NEED_NETDEV 0x02
7090#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007091#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7092#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7093 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007094#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007095/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007096#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7097 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007098
7099static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7100 struct genl_info *info)
7101{
7102 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007103 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007104 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007105 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7106
7107 if (rtnl)
7108 rtnl_lock();
7109
7110 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007111 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007112 if (IS_ERR(rdev)) {
7113 if (rtnl)
7114 rtnl_unlock();
7115 return PTR_ERR(rdev);
7116 }
7117 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007118 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7119 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007120 mutex_lock(&cfg80211_mutex);
7121 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7122 info->attrs);
7123 if (IS_ERR(wdev)) {
7124 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007125 if (rtnl)
7126 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007127 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007128 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007129
Johannes Berg89a54e42012-06-15 14:33:17 +02007130 dev = wdev->netdev;
7131 rdev = wiphy_to_dev(wdev->wiphy);
7132
Johannes Berg1bf614e2012-06-15 15:23:36 +02007133 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7134 if (!dev) {
7135 mutex_unlock(&cfg80211_mutex);
7136 if (rtnl)
7137 rtnl_unlock();
7138 return -EINVAL;
7139 }
7140
7141 info->user_ptr[1] = dev;
7142 } else {
7143 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007144 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007145
Johannes Berg1bf614e2012-06-15 15:23:36 +02007146 if (dev) {
7147 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7148 !netif_running(dev)) {
7149 mutex_unlock(&cfg80211_mutex);
7150 if (rtnl)
7151 rtnl_unlock();
7152 return -ENETDOWN;
7153 }
7154
7155 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007156 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7157 if (!wdev->p2p_started) {
7158 mutex_unlock(&cfg80211_mutex);
7159 if (rtnl)
7160 rtnl_unlock();
7161 return -ENETDOWN;
7162 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007163 }
7164
Johannes Berg89a54e42012-06-15 14:33:17 +02007165 cfg80211_lock_rdev(rdev);
7166
7167 mutex_unlock(&cfg80211_mutex);
7168
Johannes Berg4c476992010-10-04 21:36:35 +02007169 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007170 }
7171
7172 return 0;
7173}
7174
7175static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7176 struct genl_info *info)
7177{
7178 if (info->user_ptr[0])
7179 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007180 if (info->user_ptr[1]) {
7181 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7182 struct wireless_dev *wdev = info->user_ptr[1];
7183
7184 if (wdev->netdev)
7185 dev_put(wdev->netdev);
7186 } else {
7187 dev_put(info->user_ptr[1]);
7188 }
7189 }
Johannes Berg4c476992010-10-04 21:36:35 +02007190 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7191 rtnl_unlock();
7192}
7193
Johannes Berg55682962007-09-20 13:09:35 -04007194static struct genl_ops nl80211_ops[] = {
7195 {
7196 .cmd = NL80211_CMD_GET_WIPHY,
7197 .doit = nl80211_get_wiphy,
7198 .dumpit = nl80211_dump_wiphy,
7199 .policy = nl80211_policy,
7200 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007201 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007202 },
7203 {
7204 .cmd = NL80211_CMD_SET_WIPHY,
7205 .doit = nl80211_set_wiphy,
7206 .policy = nl80211_policy,
7207 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007208 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007209 },
7210 {
7211 .cmd = NL80211_CMD_GET_INTERFACE,
7212 .doit = nl80211_get_interface,
7213 .dumpit = nl80211_dump_interface,
7214 .policy = nl80211_policy,
7215 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007216 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007217 },
7218 {
7219 .cmd = NL80211_CMD_SET_INTERFACE,
7220 .doit = nl80211_set_interface,
7221 .policy = nl80211_policy,
7222 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007223 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7224 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007225 },
7226 {
7227 .cmd = NL80211_CMD_NEW_INTERFACE,
7228 .doit = nl80211_new_interface,
7229 .policy = nl80211_policy,
7230 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007231 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7232 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007233 },
7234 {
7235 .cmd = NL80211_CMD_DEL_INTERFACE,
7236 .doit = nl80211_del_interface,
7237 .policy = nl80211_policy,
7238 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007239 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007240 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007241 },
Johannes Berg41ade002007-12-19 02:03:29 +01007242 {
7243 .cmd = NL80211_CMD_GET_KEY,
7244 .doit = nl80211_get_key,
7245 .policy = nl80211_policy,
7246 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007247 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007248 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007249 },
7250 {
7251 .cmd = NL80211_CMD_SET_KEY,
7252 .doit = nl80211_set_key,
7253 .policy = nl80211_policy,
7254 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007255 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007256 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007257 },
7258 {
7259 .cmd = NL80211_CMD_NEW_KEY,
7260 .doit = nl80211_new_key,
7261 .policy = nl80211_policy,
7262 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007263 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007264 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007265 },
7266 {
7267 .cmd = NL80211_CMD_DEL_KEY,
7268 .doit = nl80211_del_key,
7269 .policy = nl80211_policy,
7270 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007271 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007272 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007273 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007274 {
7275 .cmd = NL80211_CMD_SET_BEACON,
7276 .policy = nl80211_policy,
7277 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007278 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007279 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007280 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007281 },
7282 {
Johannes Berg88600202012-02-13 15:17:18 +01007283 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007284 .policy = nl80211_policy,
7285 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007286 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007287 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007288 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007289 },
7290 {
Johannes Berg88600202012-02-13 15:17:18 +01007291 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007292 .policy = nl80211_policy,
7293 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007294 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007295 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007296 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007297 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007298 {
7299 .cmd = NL80211_CMD_GET_STATION,
7300 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007301 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007302 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007303 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7304 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007305 },
7306 {
7307 .cmd = NL80211_CMD_SET_STATION,
7308 .doit = nl80211_set_station,
7309 .policy = nl80211_policy,
7310 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007311 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007312 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007313 },
7314 {
7315 .cmd = NL80211_CMD_NEW_STATION,
7316 .doit = nl80211_new_station,
7317 .policy = nl80211_policy,
7318 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007319 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007320 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007321 },
7322 {
7323 .cmd = NL80211_CMD_DEL_STATION,
7324 .doit = nl80211_del_station,
7325 .policy = nl80211_policy,
7326 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007327 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007328 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007329 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007330 {
7331 .cmd = NL80211_CMD_GET_MPATH,
7332 .doit = nl80211_get_mpath,
7333 .dumpit = nl80211_dump_mpath,
7334 .policy = nl80211_policy,
7335 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007336 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007337 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007338 },
7339 {
7340 .cmd = NL80211_CMD_SET_MPATH,
7341 .doit = nl80211_set_mpath,
7342 .policy = nl80211_policy,
7343 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007345 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007346 },
7347 {
7348 .cmd = NL80211_CMD_NEW_MPATH,
7349 .doit = nl80211_new_mpath,
7350 .policy = nl80211_policy,
7351 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007352 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007353 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007354 },
7355 {
7356 .cmd = NL80211_CMD_DEL_MPATH,
7357 .doit = nl80211_del_mpath,
7358 .policy = nl80211_policy,
7359 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007360 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007361 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007362 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007363 {
7364 .cmd = NL80211_CMD_SET_BSS,
7365 .doit = nl80211_set_bss,
7366 .policy = nl80211_policy,
7367 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007368 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007369 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007370 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007371 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007372 .cmd = NL80211_CMD_GET_REG,
7373 .doit = nl80211_get_reg,
7374 .policy = nl80211_policy,
7375 /* can be retrieved by unprivileged users */
7376 },
7377 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007378 .cmd = NL80211_CMD_SET_REG,
7379 .doit = nl80211_set_reg,
7380 .policy = nl80211_policy,
7381 .flags = GENL_ADMIN_PERM,
7382 },
7383 {
7384 .cmd = NL80211_CMD_REQ_SET_REG,
7385 .doit = nl80211_req_set_reg,
7386 .policy = nl80211_policy,
7387 .flags = GENL_ADMIN_PERM,
7388 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007389 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007390 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7391 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007392 .policy = nl80211_policy,
7393 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007394 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007395 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007396 },
7397 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007398 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7399 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007400 .policy = nl80211_policy,
7401 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007402 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007403 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007404 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007405 {
Johannes Berg2a519312009-02-10 21:25:55 +01007406 .cmd = NL80211_CMD_TRIGGER_SCAN,
7407 .doit = nl80211_trigger_scan,
7408 .policy = nl80211_policy,
7409 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007410 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007411 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007412 },
7413 {
7414 .cmd = NL80211_CMD_GET_SCAN,
7415 .policy = nl80211_policy,
7416 .dumpit = nl80211_dump_scan,
7417 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007418 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007419 .cmd = NL80211_CMD_START_SCHED_SCAN,
7420 .doit = nl80211_start_sched_scan,
7421 .policy = nl80211_policy,
7422 .flags = GENL_ADMIN_PERM,
7423 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7424 NL80211_FLAG_NEED_RTNL,
7425 },
7426 {
7427 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7428 .doit = nl80211_stop_sched_scan,
7429 .policy = nl80211_policy,
7430 .flags = GENL_ADMIN_PERM,
7431 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7432 NL80211_FLAG_NEED_RTNL,
7433 },
7434 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007435 .cmd = NL80211_CMD_AUTHENTICATE,
7436 .doit = nl80211_authenticate,
7437 .policy = nl80211_policy,
7438 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007439 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007440 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007441 },
7442 {
7443 .cmd = NL80211_CMD_ASSOCIATE,
7444 .doit = nl80211_associate,
7445 .policy = nl80211_policy,
7446 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007447 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007448 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007449 },
7450 {
7451 .cmd = NL80211_CMD_DEAUTHENTICATE,
7452 .doit = nl80211_deauthenticate,
7453 .policy = nl80211_policy,
7454 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007455 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007456 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007457 },
7458 {
7459 .cmd = NL80211_CMD_DISASSOCIATE,
7460 .doit = nl80211_disassociate,
7461 .policy = nl80211_policy,
7462 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007463 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007464 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007465 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007466 {
7467 .cmd = NL80211_CMD_JOIN_IBSS,
7468 .doit = nl80211_join_ibss,
7469 .policy = nl80211_policy,
7470 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007471 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007472 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007473 },
7474 {
7475 .cmd = NL80211_CMD_LEAVE_IBSS,
7476 .doit = nl80211_leave_ibss,
7477 .policy = nl80211_policy,
7478 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007479 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007480 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007481 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007482#ifdef CONFIG_NL80211_TESTMODE
7483 {
7484 .cmd = NL80211_CMD_TESTMODE,
7485 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007486 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007487 .policy = nl80211_policy,
7488 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007489 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7490 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007491 },
7492#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007493 {
7494 .cmd = NL80211_CMD_CONNECT,
7495 .doit = nl80211_connect,
7496 .policy = nl80211_policy,
7497 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007498 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007499 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007500 },
7501 {
7502 .cmd = NL80211_CMD_DISCONNECT,
7503 .doit = nl80211_disconnect,
7504 .policy = nl80211_policy,
7505 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007506 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007507 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007508 },
Johannes Berg463d0182009-07-14 00:33:35 +02007509 {
7510 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7511 .doit = nl80211_wiphy_netns,
7512 .policy = nl80211_policy,
7513 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007514 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7515 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007516 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007517 {
7518 .cmd = NL80211_CMD_GET_SURVEY,
7519 .policy = nl80211_policy,
7520 .dumpit = nl80211_dump_survey,
7521 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007522 {
7523 .cmd = NL80211_CMD_SET_PMKSA,
7524 .doit = nl80211_setdel_pmksa,
7525 .policy = nl80211_policy,
7526 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007527 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007528 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007529 },
7530 {
7531 .cmd = NL80211_CMD_DEL_PMKSA,
7532 .doit = nl80211_setdel_pmksa,
7533 .policy = nl80211_policy,
7534 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007535 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007536 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007537 },
7538 {
7539 .cmd = NL80211_CMD_FLUSH_PMKSA,
7540 .doit = nl80211_flush_pmksa,
7541 .policy = nl80211_policy,
7542 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007543 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007544 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007545 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007546 {
7547 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7548 .doit = nl80211_remain_on_channel,
7549 .policy = nl80211_policy,
7550 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007551 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007552 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007553 },
7554 {
7555 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7556 .doit = nl80211_cancel_remain_on_channel,
7557 .policy = nl80211_policy,
7558 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007559 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007560 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007561 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007562 {
7563 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7564 .doit = nl80211_set_tx_bitrate_mask,
7565 .policy = nl80211_policy,
7566 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007567 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7568 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007569 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007570 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007571 .cmd = NL80211_CMD_REGISTER_FRAME,
7572 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007573 .policy = nl80211_policy,
7574 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007575 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007576 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007577 },
7578 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007579 .cmd = NL80211_CMD_FRAME,
7580 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007581 .policy = nl80211_policy,
7582 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007583 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007584 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007585 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007586 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007587 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7588 .doit = nl80211_tx_mgmt_cancel_wait,
7589 .policy = nl80211_policy,
7590 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007591 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007592 NL80211_FLAG_NEED_RTNL,
7593 },
7594 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007595 .cmd = NL80211_CMD_SET_POWER_SAVE,
7596 .doit = nl80211_set_power_save,
7597 .policy = nl80211_policy,
7598 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007599 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7600 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007601 },
7602 {
7603 .cmd = NL80211_CMD_GET_POWER_SAVE,
7604 .doit = nl80211_get_power_save,
7605 .policy = nl80211_policy,
7606 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007607 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7608 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007609 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007610 {
7611 .cmd = NL80211_CMD_SET_CQM,
7612 .doit = nl80211_set_cqm,
7613 .policy = nl80211_policy,
7614 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007615 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7616 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007617 },
Johannes Bergf444de02010-05-05 15:25:02 +02007618 {
7619 .cmd = NL80211_CMD_SET_CHANNEL,
7620 .doit = nl80211_set_channel,
7621 .policy = nl80211_policy,
7622 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007623 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7624 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007625 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007626 {
7627 .cmd = NL80211_CMD_SET_WDS_PEER,
7628 .doit = nl80211_set_wds_peer,
7629 .policy = nl80211_policy,
7630 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007631 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7632 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007633 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007634 {
7635 .cmd = NL80211_CMD_JOIN_MESH,
7636 .doit = nl80211_join_mesh,
7637 .policy = nl80211_policy,
7638 .flags = GENL_ADMIN_PERM,
7639 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7640 NL80211_FLAG_NEED_RTNL,
7641 },
7642 {
7643 .cmd = NL80211_CMD_LEAVE_MESH,
7644 .doit = nl80211_leave_mesh,
7645 .policy = nl80211_policy,
7646 .flags = GENL_ADMIN_PERM,
7647 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7648 NL80211_FLAG_NEED_RTNL,
7649 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007650#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007651 {
7652 .cmd = NL80211_CMD_GET_WOWLAN,
7653 .doit = nl80211_get_wowlan,
7654 .policy = nl80211_policy,
7655 /* can be retrieved by unprivileged users */
7656 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7657 NL80211_FLAG_NEED_RTNL,
7658 },
7659 {
7660 .cmd = NL80211_CMD_SET_WOWLAN,
7661 .doit = nl80211_set_wowlan,
7662 .policy = nl80211_policy,
7663 .flags = GENL_ADMIN_PERM,
7664 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7665 NL80211_FLAG_NEED_RTNL,
7666 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007667#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007668 {
7669 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7670 .doit = nl80211_set_rekey_data,
7671 .policy = nl80211_policy,
7672 .flags = GENL_ADMIN_PERM,
7673 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7674 NL80211_FLAG_NEED_RTNL,
7675 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007676 {
7677 .cmd = NL80211_CMD_TDLS_MGMT,
7678 .doit = nl80211_tdls_mgmt,
7679 .policy = nl80211_policy,
7680 .flags = GENL_ADMIN_PERM,
7681 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7682 NL80211_FLAG_NEED_RTNL,
7683 },
7684 {
7685 .cmd = NL80211_CMD_TDLS_OPER,
7686 .doit = nl80211_tdls_oper,
7687 .policy = nl80211_policy,
7688 .flags = GENL_ADMIN_PERM,
7689 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7690 NL80211_FLAG_NEED_RTNL,
7691 },
Johannes Berg28946da2011-11-04 11:18:12 +01007692 {
7693 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7694 .doit = nl80211_register_unexpected_frame,
7695 .policy = nl80211_policy,
7696 .flags = GENL_ADMIN_PERM,
7697 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7698 NL80211_FLAG_NEED_RTNL,
7699 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007700 {
7701 .cmd = NL80211_CMD_PROBE_CLIENT,
7702 .doit = nl80211_probe_client,
7703 .policy = nl80211_policy,
7704 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007705 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007706 NL80211_FLAG_NEED_RTNL,
7707 },
Johannes Berg5e760232011-11-04 11:18:17 +01007708 {
7709 .cmd = NL80211_CMD_REGISTER_BEACONS,
7710 .doit = nl80211_register_beacons,
7711 .policy = nl80211_policy,
7712 .flags = GENL_ADMIN_PERM,
7713 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7714 NL80211_FLAG_NEED_RTNL,
7715 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007716 {
7717 .cmd = NL80211_CMD_SET_NOACK_MAP,
7718 .doit = nl80211_set_noack_map,
7719 .policy = nl80211_policy,
7720 .flags = GENL_ADMIN_PERM,
7721 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7722 NL80211_FLAG_NEED_RTNL,
7723 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007724 {
7725 .cmd = NL80211_CMD_START_P2P_DEVICE,
7726 .doit = nl80211_start_p2p_device,
7727 .policy = nl80211_policy,
7728 .flags = GENL_ADMIN_PERM,
7729 .internal_flags = NL80211_FLAG_NEED_WDEV |
7730 NL80211_FLAG_NEED_RTNL,
7731 },
7732 {
7733 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7734 .doit = nl80211_stop_p2p_device,
7735 .policy = nl80211_policy,
7736 .flags = GENL_ADMIN_PERM,
7737 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7738 NL80211_FLAG_NEED_RTNL,
7739 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007740 {
7741 .cmd = NL80211_CMD_SET_MCAST_RATE,
7742 .doit = nl80211_set_mcast_rate,
7743 .policy = nl80211_policy,
7744 .flags = GENL_ADMIN_PERM,
7745 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7746 NL80211_FLAG_NEED_RTNL,
7747 },
Johannes Berg55682962007-09-20 13:09:35 -04007748};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007749
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007750static struct genl_multicast_group nl80211_mlme_mcgrp = {
7751 .name = "mlme",
7752};
Johannes Berg55682962007-09-20 13:09:35 -04007753
7754/* multicast groups */
7755static struct genl_multicast_group nl80211_config_mcgrp = {
7756 .name = "config",
7757};
Johannes Berg2a519312009-02-10 21:25:55 +01007758static struct genl_multicast_group nl80211_scan_mcgrp = {
7759 .name = "scan",
7760};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007761static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7762 .name = "regulatory",
7763};
Johannes Berg55682962007-09-20 13:09:35 -04007764
7765/* notification functions */
7766
7767void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7768{
7769 struct sk_buff *msg;
7770
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007771 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007772 if (!msg)
7773 return;
7774
7775 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7776 nlmsg_free(msg);
7777 return;
7778 }
7779
Johannes Berg463d0182009-07-14 00:33:35 +02007780 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7781 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007782}
7783
Johannes Berg362a4152009-05-24 16:43:15 +02007784static int nl80211_add_scan_req(struct sk_buff *msg,
7785 struct cfg80211_registered_device *rdev)
7786{
7787 struct cfg80211_scan_request *req = rdev->scan_req;
7788 struct nlattr *nest;
7789 int i;
7790
Johannes Berg667503dd2009-07-07 03:56:11 +02007791 ASSERT_RDEV_LOCK(rdev);
7792
Johannes Berg362a4152009-05-24 16:43:15 +02007793 if (WARN_ON(!req))
7794 return 0;
7795
7796 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7797 if (!nest)
7798 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007799 for (i = 0; i < req->n_ssids; i++) {
7800 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7801 goto nla_put_failure;
7802 }
Johannes Berg362a4152009-05-24 16:43:15 +02007803 nla_nest_end(msg, nest);
7804
7805 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7806 if (!nest)
7807 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007808 for (i = 0; i < req->n_channels; i++) {
7809 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7810 goto nla_put_failure;
7811 }
Johannes Berg362a4152009-05-24 16:43:15 +02007812 nla_nest_end(msg, nest);
7813
David S. Miller9360ffd2012-03-29 04:41:26 -04007814 if (req->ie &&
7815 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7816 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007817
Sam Lefflered4737712012-10-11 21:03:31 -07007818 if (req->flags)
7819 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7820
Johannes Berg362a4152009-05-24 16:43:15 +02007821 return 0;
7822 nla_put_failure:
7823 return -ENOBUFS;
7824}
7825
Johannes Berga538e2d2009-06-16 19:56:42 +02007826static int nl80211_send_scan_msg(struct sk_buff *msg,
7827 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007828 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007829 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007830 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007831{
7832 void *hdr;
7833
Eric W. Biederman15e47302012-09-07 20:12:54 +00007834 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007835 if (!hdr)
7836 return -1;
7837
David S. Miller9360ffd2012-03-29 04:41:26 -04007838 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007839 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7840 wdev->netdev->ifindex)) ||
7841 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007842 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007843
Johannes Berg362a4152009-05-24 16:43:15 +02007844 /* ignore errors and send incomplete event anyway */
7845 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007846
7847 return genlmsg_end(msg, hdr);
7848
7849 nla_put_failure:
7850 genlmsg_cancel(msg, hdr);
7851 return -EMSGSIZE;
7852}
7853
Luciano Coelho807f8a82011-05-11 17:09:35 +03007854static int
7855nl80211_send_sched_scan_msg(struct sk_buff *msg,
7856 struct cfg80211_registered_device *rdev,
7857 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007858 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007859{
7860 void *hdr;
7861
Eric W. Biederman15e47302012-09-07 20:12:54 +00007862 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007863 if (!hdr)
7864 return -1;
7865
David S. Miller9360ffd2012-03-29 04:41:26 -04007866 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7867 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7868 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007869
7870 return genlmsg_end(msg, hdr);
7871
7872 nla_put_failure:
7873 genlmsg_cancel(msg, hdr);
7874 return -EMSGSIZE;
7875}
7876
Johannes Berga538e2d2009-06-16 19:56:42 +02007877void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007878 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007879{
7880 struct sk_buff *msg;
7881
Thomas Graf58050fc2012-06-28 03:57:45 +00007882 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007883 if (!msg)
7884 return;
7885
Johannes Bergfd014282012-06-18 19:17:03 +02007886 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007887 NL80211_CMD_TRIGGER_SCAN) < 0) {
7888 nlmsg_free(msg);
7889 return;
7890 }
7891
Johannes Berg463d0182009-07-14 00:33:35 +02007892 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7893 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007894}
7895
Johannes Berg2a519312009-02-10 21:25:55 +01007896void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007897 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007898{
7899 struct sk_buff *msg;
7900
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007901 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007902 if (!msg)
7903 return;
7904
Johannes Bergfd014282012-06-18 19:17:03 +02007905 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007906 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007907 nlmsg_free(msg);
7908 return;
7909 }
7910
Johannes Berg463d0182009-07-14 00:33:35 +02007911 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7912 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007913}
7914
7915void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007916 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007917{
7918 struct sk_buff *msg;
7919
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007920 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007921 if (!msg)
7922 return;
7923
Johannes Bergfd014282012-06-18 19:17:03 +02007924 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007925 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007926 nlmsg_free(msg);
7927 return;
7928 }
7929
Johannes Berg463d0182009-07-14 00:33:35 +02007930 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7931 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007932}
7933
Luciano Coelho807f8a82011-05-11 17:09:35 +03007934void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7935 struct net_device *netdev)
7936{
7937 struct sk_buff *msg;
7938
7939 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7940 if (!msg)
7941 return;
7942
7943 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7944 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7945 nlmsg_free(msg);
7946 return;
7947 }
7948
7949 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7950 nl80211_scan_mcgrp.id, GFP_KERNEL);
7951}
7952
7953void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7954 struct net_device *netdev, u32 cmd)
7955{
7956 struct sk_buff *msg;
7957
Thomas Graf58050fc2012-06-28 03:57:45 +00007958 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007959 if (!msg)
7960 return;
7961
7962 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7963 nlmsg_free(msg);
7964 return;
7965 }
7966
7967 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7968 nl80211_scan_mcgrp.id, GFP_KERNEL);
7969}
7970
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007971/*
7972 * This can happen on global regulatory changes or device specific settings
7973 * based on custom world regulatory domains.
7974 */
7975void nl80211_send_reg_change_event(struct regulatory_request *request)
7976{
7977 struct sk_buff *msg;
7978 void *hdr;
7979
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007980 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007981 if (!msg)
7982 return;
7983
7984 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7985 if (!hdr) {
7986 nlmsg_free(msg);
7987 return;
7988 }
7989
7990 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007991 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7992 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007993
David S. Miller9360ffd2012-03-29 04:41:26 -04007994 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7995 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7996 NL80211_REGDOM_TYPE_WORLD))
7997 goto nla_put_failure;
7998 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7999 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8000 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8001 goto nla_put_failure;
8002 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8003 request->intersect) {
8004 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8005 NL80211_REGDOM_TYPE_INTERSECTION))
8006 goto nla_put_failure;
8007 } else {
8008 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8009 NL80211_REGDOM_TYPE_COUNTRY) ||
8010 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8011 request->alpha2))
8012 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008013 }
8014
David S. Miller9360ffd2012-03-29 04:41:26 -04008015 if (wiphy_idx_valid(request->wiphy_idx) &&
8016 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8017 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008018
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008019 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008020
Johannes Bergbc43b282009-07-25 10:54:13 +02008021 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008022 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008023 GFP_ATOMIC);
8024 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008025
8026 return;
8027
8028nla_put_failure:
8029 genlmsg_cancel(msg, hdr);
8030 nlmsg_free(msg);
8031}
8032
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008033static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8034 struct net_device *netdev,
8035 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008036 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008037{
8038 struct sk_buff *msg;
8039 void *hdr;
8040
Johannes Berge6d6e342009-07-01 21:26:47 +02008041 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008042 if (!msg)
8043 return;
8044
8045 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8046 if (!hdr) {
8047 nlmsg_free(msg);
8048 return;
8049 }
8050
David S. Miller9360ffd2012-03-29 04:41:26 -04008051 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8052 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8053 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8054 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008055
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008056 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008057
Johannes Berg463d0182009-07-14 00:33:35 +02008058 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8059 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008060 return;
8061
8062 nla_put_failure:
8063 genlmsg_cancel(msg, hdr);
8064 nlmsg_free(msg);
8065}
8066
8067void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008068 struct net_device *netdev, const u8 *buf,
8069 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008070{
8071 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008072 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008073}
8074
8075void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8076 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008077 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008078{
Johannes Berge6d6e342009-07-01 21:26:47 +02008079 nl80211_send_mlme_event(rdev, netdev, buf, len,
8080 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008081}
8082
Jouni Malinen53b46b82009-03-27 20:53:56 +02008083void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008084 struct net_device *netdev, const u8 *buf,
8085 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008086{
8087 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008088 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008089}
8090
Jouni Malinen53b46b82009-03-27 20:53:56 +02008091void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8092 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008093 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008094{
8095 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008096 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008097}
8098
Jouni Malinencf4e5942010-12-16 00:52:40 +02008099void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8100 struct net_device *netdev, const u8 *buf,
8101 size_t len, gfp_t gfp)
8102{
8103 nl80211_send_mlme_event(rdev, netdev, buf, len,
8104 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8105}
8106
8107void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8108 struct net_device *netdev, const u8 *buf,
8109 size_t len, gfp_t gfp)
8110{
8111 nl80211_send_mlme_event(rdev, netdev, buf, len,
8112 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8113}
8114
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008115static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8116 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008117 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008118{
8119 struct sk_buff *msg;
8120 void *hdr;
8121
Johannes Berge6d6e342009-07-01 21:26:47 +02008122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008123 if (!msg)
8124 return;
8125
8126 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8127 if (!hdr) {
8128 nlmsg_free(msg);
8129 return;
8130 }
8131
David S. Miller9360ffd2012-03-29 04:41:26 -04008132 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8133 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8134 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8135 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8136 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008137
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008138 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008139
Johannes Berg463d0182009-07-14 00:33:35 +02008140 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8141 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008142 return;
8143
8144 nla_put_failure:
8145 genlmsg_cancel(msg, hdr);
8146 nlmsg_free(msg);
8147}
8148
8149void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008150 struct net_device *netdev, const u8 *addr,
8151 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008152{
8153 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008154 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008155}
8156
8157void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008158 struct net_device *netdev, const u8 *addr,
8159 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008160{
Johannes Berge6d6e342009-07-01 21:26:47 +02008161 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8162 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008163}
8164
Samuel Ortizb23aa672009-07-01 21:26:54 +02008165void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8166 struct net_device *netdev, const u8 *bssid,
8167 const u8 *req_ie, size_t req_ie_len,
8168 const u8 *resp_ie, size_t resp_ie_len,
8169 u16 status, gfp_t gfp)
8170{
8171 struct sk_buff *msg;
8172 void *hdr;
8173
Thomas Graf58050fc2012-06-28 03:57:45 +00008174 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008175 if (!msg)
8176 return;
8177
8178 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8179 if (!hdr) {
8180 nlmsg_free(msg);
8181 return;
8182 }
8183
David S. Miller9360ffd2012-03-29 04:41:26 -04008184 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8185 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8186 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8187 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8188 (req_ie &&
8189 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8190 (resp_ie &&
8191 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8192 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008193
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008194 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008195
Johannes Berg463d0182009-07-14 00:33:35 +02008196 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8197 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008198 return;
8199
8200 nla_put_failure:
8201 genlmsg_cancel(msg, hdr);
8202 nlmsg_free(msg);
8203
8204}
8205
8206void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8207 struct net_device *netdev, const u8 *bssid,
8208 const u8 *req_ie, size_t req_ie_len,
8209 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8210{
8211 struct sk_buff *msg;
8212 void *hdr;
8213
Thomas Graf58050fc2012-06-28 03:57:45 +00008214 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008215 if (!msg)
8216 return;
8217
8218 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8219 if (!hdr) {
8220 nlmsg_free(msg);
8221 return;
8222 }
8223
David S. Miller9360ffd2012-03-29 04:41:26 -04008224 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8225 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8226 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8227 (req_ie &&
8228 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8229 (resp_ie &&
8230 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8231 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008232
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008233 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008234
Johannes Berg463d0182009-07-14 00:33:35 +02008235 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8236 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008237 return;
8238
8239 nla_put_failure:
8240 genlmsg_cancel(msg, hdr);
8241 nlmsg_free(msg);
8242
8243}
8244
8245void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8246 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008247 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008248{
8249 struct sk_buff *msg;
8250 void *hdr;
8251
Thomas Graf58050fc2012-06-28 03:57:45 +00008252 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008253 if (!msg)
8254 return;
8255
8256 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8257 if (!hdr) {
8258 nlmsg_free(msg);
8259 return;
8260 }
8261
David S. Miller9360ffd2012-03-29 04:41:26 -04008262 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8263 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8264 (from_ap && reason &&
8265 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8266 (from_ap &&
8267 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8268 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8269 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008270
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008271 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008272
Johannes Berg463d0182009-07-14 00:33:35 +02008273 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8274 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008275 return;
8276
8277 nla_put_failure:
8278 genlmsg_cancel(msg, hdr);
8279 nlmsg_free(msg);
8280
8281}
8282
Johannes Berg04a773a2009-04-19 21:24:32 +02008283void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8284 struct net_device *netdev, const u8 *bssid,
8285 gfp_t gfp)
8286{
8287 struct sk_buff *msg;
8288 void *hdr;
8289
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008290 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008291 if (!msg)
8292 return;
8293
8294 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8295 if (!hdr) {
8296 nlmsg_free(msg);
8297 return;
8298 }
8299
David S. Miller9360ffd2012-03-29 04:41:26 -04008300 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8301 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8302 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8303 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008304
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008305 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008306
Johannes Berg463d0182009-07-14 00:33:35 +02008307 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8308 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008309 return;
8310
8311 nla_put_failure:
8312 genlmsg_cancel(msg, hdr);
8313 nlmsg_free(msg);
8314}
8315
Javier Cardonac93b5e72011-04-07 15:08:34 -07008316void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8317 struct net_device *netdev,
8318 const u8 *macaddr, const u8* ie, u8 ie_len,
8319 gfp_t gfp)
8320{
8321 struct sk_buff *msg;
8322 void *hdr;
8323
8324 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8325 if (!msg)
8326 return;
8327
8328 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8329 if (!hdr) {
8330 nlmsg_free(msg);
8331 return;
8332 }
8333
David S. Miller9360ffd2012-03-29 04:41:26 -04008334 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8335 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8336 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8337 (ie_len && ie &&
8338 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8339 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008340
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008341 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008342
8343 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8344 nl80211_mlme_mcgrp.id, gfp);
8345 return;
8346
8347 nla_put_failure:
8348 genlmsg_cancel(msg, hdr);
8349 nlmsg_free(msg);
8350}
8351
Jouni Malinena3b8b052009-03-27 21:59:49 +02008352void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8353 struct net_device *netdev, const u8 *addr,
8354 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008355 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008356{
8357 struct sk_buff *msg;
8358 void *hdr;
8359
Johannes Berge6d6e342009-07-01 21:26:47 +02008360 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008361 if (!msg)
8362 return;
8363
8364 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8365 if (!hdr) {
8366 nlmsg_free(msg);
8367 return;
8368 }
8369
David S. Miller9360ffd2012-03-29 04:41:26 -04008370 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8371 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8372 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8373 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8374 (key_id != -1 &&
8375 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8376 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8377 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008378
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008379 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008380
Johannes Berg463d0182009-07-14 00:33:35 +02008381 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8382 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008383 return;
8384
8385 nla_put_failure:
8386 genlmsg_cancel(msg, hdr);
8387 nlmsg_free(msg);
8388}
8389
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008390void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8391 struct ieee80211_channel *channel_before,
8392 struct ieee80211_channel *channel_after)
8393{
8394 struct sk_buff *msg;
8395 void *hdr;
8396 struct nlattr *nl_freq;
8397
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008398 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008399 if (!msg)
8400 return;
8401
8402 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8403 if (!hdr) {
8404 nlmsg_free(msg);
8405 return;
8406 }
8407
8408 /*
8409 * Since we are applying the beacon hint to a wiphy we know its
8410 * wiphy_idx is valid
8411 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008412 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8413 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008414
8415 /* Before */
8416 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8417 if (!nl_freq)
8418 goto nla_put_failure;
8419 if (nl80211_msg_put_channel(msg, channel_before))
8420 goto nla_put_failure;
8421 nla_nest_end(msg, nl_freq);
8422
8423 /* After */
8424 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8425 if (!nl_freq)
8426 goto nla_put_failure;
8427 if (nl80211_msg_put_channel(msg, channel_after))
8428 goto nla_put_failure;
8429 nla_nest_end(msg, nl_freq);
8430
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008431 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008432
Johannes Berg463d0182009-07-14 00:33:35 +02008433 rcu_read_lock();
8434 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8435 GFP_ATOMIC);
8436 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008437
8438 return;
8439
8440nla_put_failure:
8441 genlmsg_cancel(msg, hdr);
8442 nlmsg_free(msg);
8443}
8444
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008445static void nl80211_send_remain_on_chan_event(
8446 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008447 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008448 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008449 unsigned int duration, gfp_t gfp)
8450{
8451 struct sk_buff *msg;
8452 void *hdr;
8453
8454 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8455 if (!msg)
8456 return;
8457
8458 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8459 if (!hdr) {
8460 nlmsg_free(msg);
8461 return;
8462 }
8463
David S. Miller9360ffd2012-03-29 04:41:26 -04008464 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008465 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8466 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008467 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008468 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01008469 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
8470 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008471 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8472 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008473
David S. Miller9360ffd2012-03-29 04:41:26 -04008474 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8475 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8476 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008477
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008478 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008479
8480 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8481 nl80211_mlme_mcgrp.id, gfp);
8482 return;
8483
8484 nla_put_failure:
8485 genlmsg_cancel(msg, hdr);
8486 nlmsg_free(msg);
8487}
8488
8489void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008490 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008491 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008492 unsigned int duration, gfp_t gfp)
8493{
8494 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008495 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01008496 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008497}
8498
8499void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008500 struct cfg80211_registered_device *rdev,
8501 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01008502 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008503{
8504 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01008505 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008506}
8507
Johannes Berg98b62182009-12-23 13:15:44 +01008508void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8509 struct net_device *dev, const u8 *mac_addr,
8510 struct station_info *sinfo, gfp_t gfp)
8511{
8512 struct sk_buff *msg;
8513
Thomas Graf58050fc2012-06-28 03:57:45 +00008514 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008515 if (!msg)
8516 return;
8517
John W. Linville66266b32012-03-15 13:25:41 -04008518 if (nl80211_send_station(msg, 0, 0, 0,
8519 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008520 nlmsg_free(msg);
8521 return;
8522 }
8523
8524 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8525 nl80211_mlme_mcgrp.id, gfp);
8526}
8527
Jouni Malinenec15e682011-03-23 15:29:52 +02008528void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8529 struct net_device *dev, const u8 *mac_addr,
8530 gfp_t gfp)
8531{
8532 struct sk_buff *msg;
8533 void *hdr;
8534
Thomas Graf58050fc2012-06-28 03:57:45 +00008535 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008536 if (!msg)
8537 return;
8538
8539 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8540 if (!hdr) {
8541 nlmsg_free(msg);
8542 return;
8543 }
8544
David S. Miller9360ffd2012-03-29 04:41:26 -04008545 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8546 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8547 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008548
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008549 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008550
8551 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8552 nl80211_mlme_mcgrp.id, gfp);
8553 return;
8554
8555 nla_put_failure:
8556 genlmsg_cancel(msg, hdr);
8557 nlmsg_free(msg);
8558}
8559
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308560void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8561 struct net_device *dev, const u8 *mac_addr,
8562 enum nl80211_connect_failed_reason reason,
8563 gfp_t gfp)
8564{
8565 struct sk_buff *msg;
8566 void *hdr;
8567
8568 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8569 if (!msg)
8570 return;
8571
8572 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8573 if (!hdr) {
8574 nlmsg_free(msg);
8575 return;
8576 }
8577
8578 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8579 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8580 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8581 goto nla_put_failure;
8582
8583 genlmsg_end(msg, hdr);
8584
8585 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8586 nl80211_mlme_mcgrp.id, gfp);
8587 return;
8588
8589 nla_put_failure:
8590 genlmsg_cancel(msg, hdr);
8591 nlmsg_free(msg);
8592}
8593
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008594static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8595 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008596{
8597 struct wireless_dev *wdev = dev->ieee80211_ptr;
8598 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8599 struct sk_buff *msg;
8600 void *hdr;
8601 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008602 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008603
Eric W. Biederman15e47302012-09-07 20:12:54 +00008604 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008605 return false;
8606
8607 msg = nlmsg_new(100, gfp);
8608 if (!msg)
8609 return true;
8610
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008611 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008612 if (!hdr) {
8613 nlmsg_free(msg);
8614 return true;
8615 }
8616
David S. Miller9360ffd2012-03-29 04:41:26 -04008617 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8618 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8619 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8620 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008621
8622 err = genlmsg_end(msg, hdr);
8623 if (err < 0) {
8624 nlmsg_free(msg);
8625 return true;
8626 }
8627
Eric W. Biederman15e47302012-09-07 20:12:54 +00008628 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008629 return true;
8630
8631 nla_put_failure:
8632 genlmsg_cancel(msg, hdr);
8633 nlmsg_free(msg);
8634 return true;
8635}
8636
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008637bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8638{
8639 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8640 addr, gfp);
8641}
8642
8643bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8644 const u8 *addr, gfp_t gfp)
8645{
8646 return __nl80211_unexpected_frame(dev,
8647 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8648 addr, gfp);
8649}
8650
Johannes Berg2e161f72010-08-12 15:38:38 +02008651int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008652 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008653 int freq, int sig_dbm,
8654 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008655{
Johannes Berg71bbc992012-06-15 15:30:18 +02008656 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008657 struct sk_buff *msg;
8658 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008659
8660 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8661 if (!msg)
8662 return -ENOMEM;
8663
Johannes Berg2e161f72010-08-12 15:38:38 +02008664 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008665 if (!hdr) {
8666 nlmsg_free(msg);
8667 return -ENOMEM;
8668 }
8669
David S. Miller9360ffd2012-03-29 04:41:26 -04008670 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008671 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8672 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008673 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8674 (sig_dbm &&
8675 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8676 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8677 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008678
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008679 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008680
Eric W. Biederman15e47302012-09-07 20:12:54 +00008681 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008682
8683 nla_put_failure:
8684 genlmsg_cancel(msg, hdr);
8685 nlmsg_free(msg);
8686 return -ENOBUFS;
8687}
8688
Johannes Berg2e161f72010-08-12 15:38:38 +02008689void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008690 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008691 const u8 *buf, size_t len, bool ack,
8692 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008693{
Johannes Berg71bbc992012-06-15 15:30:18 +02008694 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008695 struct sk_buff *msg;
8696 void *hdr;
8697
8698 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8699 if (!msg)
8700 return;
8701
Johannes Berg2e161f72010-08-12 15:38:38 +02008702 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008703 if (!hdr) {
8704 nlmsg_free(msg);
8705 return;
8706 }
8707
David S. Miller9360ffd2012-03-29 04:41:26 -04008708 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008709 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8710 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008711 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8712 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8713 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8714 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008715
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008716 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008717
8718 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8719 return;
8720
8721 nla_put_failure:
8722 genlmsg_cancel(msg, hdr);
8723 nlmsg_free(msg);
8724}
8725
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008726void
8727nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8728 struct net_device *netdev,
8729 enum nl80211_cqm_rssi_threshold_event rssi_event,
8730 gfp_t gfp)
8731{
8732 struct sk_buff *msg;
8733 struct nlattr *pinfoattr;
8734 void *hdr;
8735
Thomas Graf58050fc2012-06-28 03:57:45 +00008736 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008737 if (!msg)
8738 return;
8739
8740 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8741 if (!hdr) {
8742 nlmsg_free(msg);
8743 return;
8744 }
8745
David S. Miller9360ffd2012-03-29 04:41:26 -04008746 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8747 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8748 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008749
8750 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8751 if (!pinfoattr)
8752 goto nla_put_failure;
8753
David S. Miller9360ffd2012-03-29 04:41:26 -04008754 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8755 rssi_event))
8756 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008757
8758 nla_nest_end(msg, pinfoattr);
8759
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008760 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008761
8762 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8763 nl80211_mlme_mcgrp.id, gfp);
8764 return;
8765
8766 nla_put_failure:
8767 genlmsg_cancel(msg, hdr);
8768 nlmsg_free(msg);
8769}
8770
Johannes Berge5497d72011-07-05 16:35:40 +02008771void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8772 struct net_device *netdev, const u8 *bssid,
8773 const u8 *replay_ctr, gfp_t gfp)
8774{
8775 struct sk_buff *msg;
8776 struct nlattr *rekey_attr;
8777 void *hdr;
8778
Thomas Graf58050fc2012-06-28 03:57:45 +00008779 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008780 if (!msg)
8781 return;
8782
8783 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8784 if (!hdr) {
8785 nlmsg_free(msg);
8786 return;
8787 }
8788
David S. Miller9360ffd2012-03-29 04:41:26 -04008789 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8790 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8791 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8792 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008793
8794 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8795 if (!rekey_attr)
8796 goto nla_put_failure;
8797
David S. Miller9360ffd2012-03-29 04:41:26 -04008798 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8799 NL80211_REPLAY_CTR_LEN, replay_ctr))
8800 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008801
8802 nla_nest_end(msg, rekey_attr);
8803
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008804 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008805
8806 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8807 nl80211_mlme_mcgrp.id, gfp);
8808 return;
8809
8810 nla_put_failure:
8811 genlmsg_cancel(msg, hdr);
8812 nlmsg_free(msg);
8813}
8814
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008815void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8816 struct net_device *netdev, int index,
8817 const u8 *bssid, bool preauth, gfp_t gfp)
8818{
8819 struct sk_buff *msg;
8820 struct nlattr *attr;
8821 void *hdr;
8822
Thomas Graf58050fc2012-06-28 03:57:45 +00008823 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008824 if (!msg)
8825 return;
8826
8827 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8828 if (!hdr) {
8829 nlmsg_free(msg);
8830 return;
8831 }
8832
David S. Miller9360ffd2012-03-29 04:41:26 -04008833 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8834 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8835 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008836
8837 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8838 if (!attr)
8839 goto nla_put_failure;
8840
David S. Miller9360ffd2012-03-29 04:41:26 -04008841 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8842 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8843 (preauth &&
8844 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8845 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008846
8847 nla_nest_end(msg, attr);
8848
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008849 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008850
8851 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8852 nl80211_mlme_mcgrp.id, gfp);
8853 return;
8854
8855 nla_put_failure:
8856 genlmsg_cancel(msg, hdr);
8857 nlmsg_free(msg);
8858}
8859
Thomas Pedersen53145262012-04-06 13:35:47 -07008860void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01008861 struct net_device *netdev,
8862 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07008863{
8864 struct sk_buff *msg;
8865 void *hdr;
8866
Thomas Graf58050fc2012-06-28 03:57:45 +00008867 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008868 if (!msg)
8869 return;
8870
8871 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8872 if (!hdr) {
8873 nlmsg_free(msg);
8874 return;
8875 }
8876
Johannes Berg683b6d32012-11-08 21:25:48 +01008877 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8878 goto nla_put_failure;
8879
8880 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04008881 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008882
8883 genlmsg_end(msg, hdr);
8884
8885 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8886 nl80211_mlme_mcgrp.id, gfp);
8887 return;
8888
8889 nla_put_failure:
8890 genlmsg_cancel(msg, hdr);
8891 nlmsg_free(msg);
8892}
8893
Johannes Bergc063dbf2010-11-24 08:10:05 +01008894void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008895nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8896 struct net_device *netdev, const u8 *peer,
8897 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8898{
8899 struct sk_buff *msg;
8900 struct nlattr *pinfoattr;
8901 void *hdr;
8902
8903 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8904 if (!msg)
8905 return;
8906
8907 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8908 if (!hdr) {
8909 nlmsg_free(msg);
8910 return;
8911 }
8912
8913 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8914 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8915 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8916 goto nla_put_failure;
8917
8918 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8919 if (!pinfoattr)
8920 goto nla_put_failure;
8921
8922 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8923 goto nla_put_failure;
8924
8925 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8926 goto nla_put_failure;
8927
8928 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8929 goto nla_put_failure;
8930
8931 nla_nest_end(msg, pinfoattr);
8932
8933 genlmsg_end(msg, hdr);
8934
8935 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8936 nl80211_mlme_mcgrp.id, gfp);
8937 return;
8938
8939 nla_put_failure:
8940 genlmsg_cancel(msg, hdr);
8941 nlmsg_free(msg);
8942}
8943
8944void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008945nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8946 struct net_device *netdev, const u8 *peer,
8947 u32 num_packets, gfp_t gfp)
8948{
8949 struct sk_buff *msg;
8950 struct nlattr *pinfoattr;
8951 void *hdr;
8952
Thomas Graf58050fc2012-06-28 03:57:45 +00008953 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008954 if (!msg)
8955 return;
8956
8957 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8958 if (!hdr) {
8959 nlmsg_free(msg);
8960 return;
8961 }
8962
David S. Miller9360ffd2012-03-29 04:41:26 -04008963 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8964 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8965 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8966 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008967
8968 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8969 if (!pinfoattr)
8970 goto nla_put_failure;
8971
David S. Miller9360ffd2012-03-29 04:41:26 -04008972 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8973 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008974
8975 nla_nest_end(msg, pinfoattr);
8976
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008977 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008978
8979 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8980 nl80211_mlme_mcgrp.id, gfp);
8981 return;
8982
8983 nla_put_failure:
8984 genlmsg_cancel(msg, hdr);
8985 nlmsg_free(msg);
8986}
8987
Johannes Berg7f6cf312011-11-04 11:18:15 +01008988void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8989 u64 cookie, bool acked, gfp_t gfp)
8990{
8991 struct wireless_dev *wdev = dev->ieee80211_ptr;
8992 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8993 struct sk_buff *msg;
8994 void *hdr;
8995 int err;
8996
Beni Lev4ee3e062012-08-27 12:49:39 +03008997 trace_cfg80211_probe_status(dev, addr, cookie, acked);
8998
Thomas Graf58050fc2012-06-28 03:57:45 +00008999 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009000
Johannes Berg7f6cf312011-11-04 11:18:15 +01009001 if (!msg)
9002 return;
9003
9004 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9005 if (!hdr) {
9006 nlmsg_free(msg);
9007 return;
9008 }
9009
David S. Miller9360ffd2012-03-29 04:41:26 -04009010 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9011 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9012 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9013 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9014 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9015 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009016
9017 err = genlmsg_end(msg, hdr);
9018 if (err < 0) {
9019 nlmsg_free(msg);
9020 return;
9021 }
9022
9023 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9024 nl80211_mlme_mcgrp.id, gfp);
9025 return;
9026
9027 nla_put_failure:
9028 genlmsg_cancel(msg, hdr);
9029 nlmsg_free(msg);
9030}
9031EXPORT_SYMBOL(cfg80211_probe_status);
9032
Johannes Berg5e760232011-11-04 11:18:17 +01009033void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9034 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009035 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009036{
9037 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9038 struct sk_buff *msg;
9039 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009040 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009041
Beni Lev4ee3e062012-08-27 12:49:39 +03009042 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9043
Ben Greear37c73b52012-10-26 14:49:25 -07009044 spin_lock_bh(&rdev->beacon_registrations_lock);
9045 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9046 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9047 if (!msg) {
9048 spin_unlock_bh(&rdev->beacon_registrations_lock);
9049 return;
9050 }
Johannes Berg5e760232011-11-04 11:18:17 +01009051
Ben Greear37c73b52012-10-26 14:49:25 -07009052 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9053 if (!hdr)
9054 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009055
Ben Greear37c73b52012-10-26 14:49:25 -07009056 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9057 (freq &&
9058 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9059 (sig_dbm &&
9060 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9061 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9062 goto nla_put_failure;
9063
9064 genlmsg_end(msg, hdr);
9065
9066 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009067 }
Ben Greear37c73b52012-10-26 14:49:25 -07009068 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009069 return;
9070
9071 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009072 spin_unlock_bh(&rdev->beacon_registrations_lock);
9073 if (hdr)
9074 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009075 nlmsg_free(msg);
9076}
9077EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9078
Jouni Malinen3475b092012-11-16 22:49:57 +02009079void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9080 enum nl80211_tdls_operation oper,
9081 u16 reason_code, gfp_t gfp)
9082{
9083 struct wireless_dev *wdev = dev->ieee80211_ptr;
9084 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9085 struct sk_buff *msg;
9086 void *hdr;
9087 int err;
9088
9089 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9090 reason_code);
9091
9092 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9093 if (!msg)
9094 return;
9095
9096 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9097 if (!hdr) {
9098 nlmsg_free(msg);
9099 return;
9100 }
9101
9102 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9103 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9104 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9105 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9106 (reason_code > 0 &&
9107 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9108 goto nla_put_failure;
9109
9110 err = genlmsg_end(msg, hdr);
9111 if (err < 0) {
9112 nlmsg_free(msg);
9113 return;
9114 }
9115
9116 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9117 nl80211_mlme_mcgrp.id, gfp);
9118 return;
9119
9120 nla_put_failure:
9121 genlmsg_cancel(msg, hdr);
9122 nlmsg_free(msg);
9123}
9124EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9125
Jouni Malinen026331c2010-02-15 12:53:10 +02009126static int nl80211_netlink_notify(struct notifier_block * nb,
9127 unsigned long state,
9128 void *_notify)
9129{
9130 struct netlink_notify *notify = _notify;
9131 struct cfg80211_registered_device *rdev;
9132 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009133 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009134
9135 if (state != NETLINK_URELEASE)
9136 return NOTIFY_DONE;
9137
9138 rcu_read_lock();
9139
Johannes Berg5e760232011-11-04 11:18:17 +01009140 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009141 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009142 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009143
9144 spin_lock_bh(&rdev->beacon_registrations_lock);
9145 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9146 list) {
9147 if (reg->nlportid == notify->portid) {
9148 list_del(&reg->list);
9149 kfree(reg);
9150 break;
9151 }
9152 }
9153 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009154 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009155
9156 rcu_read_unlock();
9157
9158 return NOTIFY_DONE;
9159}
9160
9161static struct notifier_block nl80211_netlink_notifier = {
9162 .notifier_call = nl80211_netlink_notify,
9163};
9164
Johannes Berg55682962007-09-20 13:09:35 -04009165/* initialisation/exit functions */
9166
9167int nl80211_init(void)
9168{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009169 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009170
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009171 err = genl_register_family_with_ops(&nl80211_fam,
9172 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009173 if (err)
9174 return err;
9175
Johannes Berg55682962007-09-20 13:09:35 -04009176 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9177 if (err)
9178 goto err_out;
9179
Johannes Berg2a519312009-02-10 21:25:55 +01009180 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9181 if (err)
9182 goto err_out;
9183
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009184 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9185 if (err)
9186 goto err_out;
9187
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009188 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9189 if (err)
9190 goto err_out;
9191
Johannes Bergaff89a92009-07-01 21:26:51 +02009192#ifdef CONFIG_NL80211_TESTMODE
9193 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9194 if (err)
9195 goto err_out;
9196#endif
9197
Jouni Malinen026331c2010-02-15 12:53:10 +02009198 err = netlink_register_notifier(&nl80211_netlink_notifier);
9199 if (err)
9200 goto err_out;
9201
Johannes Berg55682962007-09-20 13:09:35 -04009202 return 0;
9203 err_out:
9204 genl_unregister_family(&nl80211_fam);
9205 return err;
9206}
9207
9208void nl80211_exit(void)
9209{
Jouni Malinen026331c2010-02-15 12:53:10 +02009210 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009211 genl_unregister_family(&nl80211_fam);
9212}