blob: 7cce43bc948949d273b904bf2997a98b3a5c4bd3 [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 Malinen36aedc902008-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 Berg53cabad2012-11-14 15:17:28 +0100366 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
367 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400368};
369
Johannes Berge31b8212010-10-05 19:39:30 +0200370/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000371static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200372 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200373 [NL80211_KEY_IDX] = { .type = NLA_U8 },
374 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200375 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200376 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
377 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200378 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100379 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
380};
381
382/* policy for the key default flags */
383static const struct nla_policy
384nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
385 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
386 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200387};
388
Johannes Bergff1b6e62011-05-04 15:37:28 +0200389/* policy for WoWLAN attributes */
390static const struct nla_policy
391nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
392 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
393 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
394 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
395 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200396 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
397 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
398 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200400};
401
Johannes Berge5497d72011-07-05 16:35:40 +0200402/* policy for GTK rekey offload attributes */
403static const struct nla_policy
404nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
405 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
406 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
407 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
408};
409
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300410static const struct nla_policy
411nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200412 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300413 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700414 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300415};
416
Holger Schuriga0438972009-11-11 11:30:02 +0100417/* ifidx get helper */
418static int nl80211_get_ifidx(struct netlink_callback *cb)
419{
420 int res;
421
422 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
423 nl80211_fam.attrbuf, nl80211_fam.maxattr,
424 nl80211_policy);
425 if (res)
426 return res;
427
428 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
429 return -EINVAL;
430
431 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
432 if (!res)
433 return -EINVAL;
434 return res;
435}
436
Johannes Berg67748892010-10-04 21:14:06 +0200437static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
438 struct netlink_callback *cb,
439 struct cfg80211_registered_device **rdev,
440 struct net_device **dev)
441{
442 int ifidx = cb->args[0];
443 int err;
444
445 if (!ifidx)
446 ifidx = nl80211_get_ifidx(cb);
447 if (ifidx < 0)
448 return ifidx;
449
450 cb->args[0] = ifidx;
451
452 rtnl_lock();
453
454 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
455 if (!*dev) {
456 err = -ENODEV;
457 goto out_rtnl;
458 }
459
460 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100461 if (IS_ERR(*rdev)) {
462 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200463 goto out_rtnl;
464 }
465
466 return 0;
467 out_rtnl:
468 rtnl_unlock();
469 return err;
470}
471
472static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
473{
474 cfg80211_unlock_rdev(rdev);
475 rtnl_unlock();
476}
477
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100478/* IE validation */
479static bool is_valid_ie_attr(const struct nlattr *attr)
480{
481 const u8 *pos;
482 int len;
483
484 if (!attr)
485 return true;
486
487 pos = nla_data(attr);
488 len = nla_len(attr);
489
490 while (len) {
491 u8 elemlen;
492
493 if (len < 2)
494 return false;
495 len -= 2;
496
497 elemlen = pos[1];
498 if (elemlen > len)
499 return false;
500
501 len -= elemlen;
502 pos += 2 + elemlen;
503 }
504
505 return true;
506}
507
Johannes Berg55682962007-09-20 13:09:35 -0400508/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000509static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400510 int flags, u8 cmd)
511{
512 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000513 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400514}
515
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400516static int nl80211_msg_put_channel(struct sk_buff *msg,
517 struct ieee80211_channel *chan)
518{
David S. Miller9360ffd2012-03-29 04:41:26 -0400519 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
520 chan->center_freq))
521 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400522
David S. Miller9360ffd2012-03-29 04:41:26 -0400523 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
524 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
525 goto nla_put_failure;
526 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
527 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
528 goto nla_put_failure;
529 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
530 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
531 goto nla_put_failure;
532 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
533 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
534 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400535
David S. Miller9360ffd2012-03-29 04:41:26 -0400536 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
537 DBM_TO_MBM(chan->max_power)))
538 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400539
540 return 0;
541
542 nla_put_failure:
543 return -ENOBUFS;
544}
545
Johannes Berg55682962007-09-20 13:09:35 -0400546/* netlink command implementations */
547
Johannes Bergb9454e82009-07-08 13:29:08 +0200548struct key_parse {
549 struct key_params p;
550 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200551 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200552 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100553 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200554};
555
556static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
557{
558 struct nlattr *tb[NL80211_KEY_MAX + 1];
559 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
560 nl80211_key_policy);
561 if (err)
562 return err;
563
564 k->def = !!tb[NL80211_KEY_DEFAULT];
565 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
566
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100567 if (k->def) {
568 k->def_uni = true;
569 k->def_multi = true;
570 }
571 if (k->defmgmt)
572 k->def_multi = true;
573
Johannes Bergb9454e82009-07-08 13:29:08 +0200574 if (tb[NL80211_KEY_IDX])
575 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
576
577 if (tb[NL80211_KEY_DATA]) {
578 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
579 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
580 }
581
582 if (tb[NL80211_KEY_SEQ]) {
583 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
584 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
585 }
586
587 if (tb[NL80211_KEY_CIPHER])
588 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
589
Johannes Berge31b8212010-10-05 19:39:30 +0200590 if (tb[NL80211_KEY_TYPE]) {
591 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
592 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
593 return -EINVAL;
594 }
595
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100596 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
597 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100598 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
599 tb[NL80211_KEY_DEFAULT_TYPES],
600 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100601 if (err)
602 return err;
603
604 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
605 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
606 }
607
Johannes Bergb9454e82009-07-08 13:29:08 +0200608 return 0;
609}
610
611static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
612{
613 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
614 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
615 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
616 }
617
618 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
619 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
620 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
621 }
622
623 if (info->attrs[NL80211_ATTR_KEY_IDX])
624 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
625
626 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
627 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
628
629 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
630 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
631
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100632 if (k->def) {
633 k->def_uni = true;
634 k->def_multi = true;
635 }
636 if (k->defmgmt)
637 k->def_multi = true;
638
Johannes Berge31b8212010-10-05 19:39:30 +0200639 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
640 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
641 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
642 return -EINVAL;
643 }
644
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100645 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
646 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
647 int err = nla_parse_nested(
648 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
649 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
650 nl80211_key_default_policy);
651 if (err)
652 return err;
653
654 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
655 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
656 }
657
Johannes Bergb9454e82009-07-08 13:29:08 +0200658 return 0;
659}
660
661static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
662{
663 int err;
664
665 memset(k, 0, sizeof(*k));
666 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200667 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200668
669 if (info->attrs[NL80211_ATTR_KEY])
670 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
671 else
672 err = nl80211_parse_key_old(info, k);
673
674 if (err)
675 return err;
676
677 if (k->def && k->defmgmt)
678 return -EINVAL;
679
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100680 if (k->defmgmt) {
681 if (k->def_uni || !k->def_multi)
682 return -EINVAL;
683 }
684
Johannes Bergb9454e82009-07-08 13:29:08 +0200685 if (k->idx != -1) {
686 if (k->defmgmt) {
687 if (k->idx < 4 || k->idx > 5)
688 return -EINVAL;
689 } else if (k->def) {
690 if (k->idx < 0 || k->idx > 3)
691 return -EINVAL;
692 } else {
693 if (k->idx < 0 || k->idx > 5)
694 return -EINVAL;
695 }
696 }
697
698 return 0;
699}
700
Johannes Bergfffd0932009-07-08 14:22:54 +0200701static struct cfg80211_cached_keys *
702nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530703 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200704{
705 struct key_parse parse;
706 struct nlattr *key;
707 struct cfg80211_cached_keys *result;
708 int rem, err, def = 0;
709
710 result = kzalloc(sizeof(*result), GFP_KERNEL);
711 if (!result)
712 return ERR_PTR(-ENOMEM);
713
714 result->def = -1;
715 result->defmgmt = -1;
716
717 nla_for_each_nested(key, keys, rem) {
718 memset(&parse, 0, sizeof(parse));
719 parse.idx = -1;
720
721 err = nl80211_parse_key_new(key, &parse);
722 if (err)
723 goto error;
724 err = -EINVAL;
725 if (!parse.p.key)
726 goto error;
727 if (parse.idx < 0 || parse.idx > 4)
728 goto error;
729 if (parse.def) {
730 if (def)
731 goto error;
732 def = 1;
733 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100734 if (!parse.def_uni || !parse.def_multi)
735 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200736 } else if (parse.defmgmt)
737 goto error;
738 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200739 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200740 if (err)
741 goto error;
742 result->params[parse.idx].cipher = parse.p.cipher;
743 result->params[parse.idx].key_len = parse.p.key_len;
744 result->params[parse.idx].key = result->data[parse.idx];
745 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530746
747 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
748 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
749 if (no_ht)
750 *no_ht = true;
751 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 }
753
754 return result;
755 error:
756 kfree(result);
757 return ERR_PTR(err);
758}
759
760static int nl80211_key_allowed(struct wireless_dev *wdev)
761{
762 ASSERT_WDEV_LOCK(wdev);
763
Johannes Bergfffd0932009-07-08 14:22:54 +0200764 switch (wdev->iftype) {
765 case NL80211_IFTYPE_AP:
766 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200767 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700768 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200769 break;
770 case NL80211_IFTYPE_ADHOC:
771 if (!wdev->current_bss)
772 return -ENOLINK;
773 break;
774 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200775 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200776 if (wdev->sme_state != CFG80211_SME_CONNECTED)
777 return -ENOLINK;
778 break;
779 default:
780 return -EINVAL;
781 }
782
783 return 0;
784}
785
Johannes Berg7527a782011-05-13 10:58:57 +0200786static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
787{
788 struct nlattr *nl_modes = nla_nest_start(msg, attr);
789 int i;
790
791 if (!nl_modes)
792 goto nla_put_failure;
793
794 i = 0;
795 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400796 if ((ifmodes & 1) && nla_put_flag(msg, i))
797 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200798 ifmodes >>= 1;
799 i++;
800 }
801
802 nla_nest_end(msg, nl_modes);
803 return 0;
804
805nla_put_failure:
806 return -ENOBUFS;
807}
808
809static int nl80211_put_iface_combinations(struct wiphy *wiphy,
810 struct sk_buff *msg)
811{
812 struct nlattr *nl_combis;
813 int i, j;
814
815 nl_combis = nla_nest_start(msg,
816 NL80211_ATTR_INTERFACE_COMBINATIONS);
817 if (!nl_combis)
818 goto nla_put_failure;
819
820 for (i = 0; i < wiphy->n_iface_combinations; i++) {
821 const struct ieee80211_iface_combination *c;
822 struct nlattr *nl_combi, *nl_limits;
823
824 c = &wiphy->iface_combinations[i];
825
826 nl_combi = nla_nest_start(msg, i + 1);
827 if (!nl_combi)
828 goto nla_put_failure;
829
830 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
831 if (!nl_limits)
832 goto nla_put_failure;
833
834 for (j = 0; j < c->n_limits; j++) {
835 struct nlattr *nl_limit;
836
837 nl_limit = nla_nest_start(msg, j + 1);
838 if (!nl_limit)
839 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400840 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
841 c->limits[j].max))
842 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200843 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
844 c->limits[j].types))
845 goto nla_put_failure;
846 nla_nest_end(msg, nl_limit);
847 }
848
849 nla_nest_end(msg, nl_limits);
850
David S. Miller9360ffd2012-03-29 04:41:26 -0400851 if (c->beacon_int_infra_match &&
852 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
853 goto nla_put_failure;
854 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
855 c->num_different_channels) ||
856 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
857 c->max_interfaces))
858 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200859
860 nla_nest_end(msg, nl_combi);
861 }
862
863 nla_nest_end(msg, nl_combis);
864
865 return 0;
866nla_put_failure:
867 return -ENOBUFS;
868}
869
Eric W. Biederman15e47302012-09-07 20:12:54 +0000870static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400871 struct cfg80211_registered_device *dev)
872{
873 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100874 struct nlattr *nl_bands, *nl_band;
875 struct nlattr *nl_freqs, *nl_freq;
876 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100877 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100878 enum ieee80211_band band;
879 struct ieee80211_channel *chan;
880 struct ieee80211_rate *rate;
881 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200882 const struct ieee80211_txrx_stypes *mgmt_stypes =
883 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400884
Eric W. Biederman15e47302012-09-07 20:12:54 +0000885 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400886 if (!hdr)
887 return -1;
888
David S. Miller9360ffd2012-03-29 04:41:26 -0400889 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
890 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
891 nla_put_u32(msg, NL80211_ATTR_GENERATION,
892 cfg80211_rdev_list_generation) ||
893 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
894 dev->wiphy.retry_short) ||
895 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
896 dev->wiphy.retry_long) ||
897 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
898 dev->wiphy.frag_threshold) ||
899 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
900 dev->wiphy.rts_threshold) ||
901 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
902 dev->wiphy.coverage_class) ||
903 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
904 dev->wiphy.max_scan_ssids) ||
905 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
906 dev->wiphy.max_sched_scan_ssids) ||
907 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
908 dev->wiphy.max_scan_ie_len) ||
909 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
910 dev->wiphy.max_sched_scan_ie_len) ||
911 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
912 dev->wiphy.max_match_sets))
913 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200914
David S. Miller9360ffd2012-03-29 04:41:26 -0400915 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
916 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
917 goto nla_put_failure;
918 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
919 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
920 goto nla_put_failure;
921 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
922 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
923 goto nla_put_failure;
924 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
925 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
926 goto nla_put_failure;
927 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
928 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
929 goto nla_put_failure;
930 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
931 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
932 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200933
David S. Miller9360ffd2012-03-29 04:41:26 -0400934 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
935 sizeof(u32) * dev->wiphy.n_cipher_suites,
936 dev->wiphy.cipher_suites))
937 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100938
David S. Miller9360ffd2012-03-29 04:41:26 -0400939 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
940 dev->wiphy.max_num_pmkids))
941 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530942
David S. Miller9360ffd2012-03-29 04:41:26 -0400943 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
944 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
945 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200946
David S. Miller9360ffd2012-03-29 04:41:26 -0400947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
948 dev->wiphy.available_antennas_tx) ||
949 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
950 dev->wiphy.available_antennas_rx))
951 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100952
David S. Miller9360ffd2012-03-29 04:41:26 -0400953 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
954 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
955 dev->wiphy.probe_resp_offload))
956 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200957
Bruno Randolf7f531e02010-12-16 11:30:22 +0900958 if ((dev->wiphy.available_antennas_tx ||
959 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900960 u32 tx_ant = 0, rx_ant = 0;
961 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300962 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900963 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400964 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
965 tx_ant) ||
966 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
967 rx_ant))
968 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900969 }
970 }
971
Johannes Berg7527a782011-05-13 10:58:57 +0200972 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
973 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700974 goto nla_put_failure;
975
Johannes Bergee688b002008-01-24 19:38:39 +0100976 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
977 if (!nl_bands)
978 goto nla_put_failure;
979
980 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
981 if (!dev->wiphy.bands[band])
982 continue;
983
984 nl_band = nla_nest_start(msg, band);
985 if (!nl_band)
986 goto nla_put_failure;
987
Johannes Bergd51626d2008-10-09 12:20:13 +0200988 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400989 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
990 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
991 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
992 &dev->wiphy.bands[band]->ht_cap.mcs) ||
993 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
994 dev->wiphy.bands[band]->ht_cap.cap) ||
995 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
996 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
997 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
998 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
999 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001000
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001001 /* add VHT info */
1002 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1003 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1004 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1005 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1006 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1007 dev->wiphy.bands[band]->vht_cap.cap)))
1008 goto nla_put_failure;
1009
Johannes Bergee688b002008-01-24 19:38:39 +01001010 /* add frequencies */
1011 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1012 if (!nl_freqs)
1013 goto nla_put_failure;
1014
1015 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1016 nl_freq = nla_nest_start(msg, i);
1017 if (!nl_freq)
1018 goto nla_put_failure;
1019
1020 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001021
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001022 if (nl80211_msg_put_channel(msg, chan))
1023 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001024
Johannes Bergee688b002008-01-24 19:38:39 +01001025 nla_nest_end(msg, nl_freq);
1026 }
1027
1028 nla_nest_end(msg, nl_freqs);
1029
1030 /* add bitrates */
1031 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1032 if (!nl_rates)
1033 goto nla_put_failure;
1034
1035 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1036 nl_rate = nla_nest_start(msg, i);
1037 if (!nl_rate)
1038 goto nla_put_failure;
1039
1040 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001041 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1042 rate->bitrate))
1043 goto nla_put_failure;
1044 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1045 nla_put_flag(msg,
1046 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1047 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001048
1049 nla_nest_end(msg, nl_rate);
1050 }
1051
1052 nla_nest_end(msg, nl_rates);
1053
1054 nla_nest_end(msg, nl_band);
1055 }
1056 nla_nest_end(msg, nl_bands);
1057
Johannes Berg8fdc6212009-03-14 09:34:01 +01001058 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1059 if (!nl_cmds)
1060 goto nla_put_failure;
1061
1062 i = 0;
1063#define CMD(op, n) \
1064 do { \
1065 if (dev->ops->op) { \
1066 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001067 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1068 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001069 } \
1070 } while (0)
1071
1072 CMD(add_virtual_intf, NEW_INTERFACE);
1073 CMD(change_virtual_intf, SET_INTERFACE);
1074 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001075 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001076 CMD(add_station, NEW_STATION);
1077 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001078 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001079 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001080 CMD(auth, AUTHENTICATE);
1081 CMD(assoc, ASSOCIATE);
1082 CMD(deauth, DEAUTHENTICATE);
1083 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001084 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001085 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001086 CMD(set_pmksa, SET_PMKSA);
1087 CMD(del_pmksa, DEL_PMKSA);
1088 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001089 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1090 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001091 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001092 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001093 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001094 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001095 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001096 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1097 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001098 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001099 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001100 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001101 i++;
1102 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1103 goto nla_put_failure;
1104 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001105 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001106 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1107 CMD(tdls_mgmt, TDLS_MGMT);
1108 CMD(tdls_oper, TDLS_OPER);
1109 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001110 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1111 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001112 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001113 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e7602302011-11-04 11:18:17 +01001114 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1115 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001116 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1117 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01001118 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001119 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001120 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001121
Kalle Valo4745fc02011-11-17 19:06:10 +02001122#ifdef CONFIG_NL80211_TESTMODE
1123 CMD(testmode_cmd, TESTMODE);
1124#endif
1125
Johannes Berg8fdc6212009-03-14 09:34:01 +01001126#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001127
Johannes Berg6829c872009-07-02 09:13:27 +02001128 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001129 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001130 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1131 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001132 }
1133
Johannes Berg6829c872009-07-02 09:13:27 +02001134 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001135 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001136 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1137 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001138 }
1139
Johannes Berg8fdc6212009-03-14 09:34:01 +01001140 nla_nest_end(msg, nl_cmds);
1141
Johannes Berg7c4ef712011-11-18 15:33:48 +01001142 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001143 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1144 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1145 dev->wiphy.max_remain_on_channel_duration))
1146 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001147
David S. Miller9360ffd2012-03-29 04:41:26 -04001148 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1149 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1150 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001151
Johannes Berg2e161f72010-08-12 15:38:38 +02001152 if (mgmt_stypes) {
1153 u16 stypes;
1154 struct nlattr *nl_ftypes, *nl_ifs;
1155 enum nl80211_iftype ift;
1156
1157 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1158 if (!nl_ifs)
1159 goto nla_put_failure;
1160
1161 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1162 nl_ftypes = nla_nest_start(msg, ift);
1163 if (!nl_ftypes)
1164 goto nla_put_failure;
1165 i = 0;
1166 stypes = mgmt_stypes[ift].tx;
1167 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001168 if ((stypes & 1) &&
1169 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1170 (i << 4) | IEEE80211_FTYPE_MGMT))
1171 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001172 stypes >>= 1;
1173 i++;
1174 }
1175 nla_nest_end(msg, nl_ftypes);
1176 }
1177
Johannes Berg74b70a42010-08-24 12:15:53 +02001178 nla_nest_end(msg, nl_ifs);
1179
Johannes Berg2e161f72010-08-12 15:38:38 +02001180 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1181 if (!nl_ifs)
1182 goto nla_put_failure;
1183
1184 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1185 nl_ftypes = nla_nest_start(msg, ift);
1186 if (!nl_ftypes)
1187 goto nla_put_failure;
1188 i = 0;
1189 stypes = mgmt_stypes[ift].rx;
1190 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001191 if ((stypes & 1) &&
1192 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1193 (i << 4) | IEEE80211_FTYPE_MGMT))
1194 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001195 stypes >>= 1;
1196 i++;
1197 }
1198 nla_nest_end(msg, nl_ftypes);
1199 }
1200 nla_nest_end(msg, nl_ifs);
1201 }
1202
Johannes Bergdfb89c52012-06-27 09:23:48 +02001203#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001204 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1205 struct nlattr *nl_wowlan;
1206
1207 nl_wowlan = nla_nest_start(msg,
1208 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1209 if (!nl_wowlan)
1210 goto nla_put_failure;
1211
David S. Miller9360ffd2012-03-29 04:41:26 -04001212 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1213 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1214 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1215 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1216 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1217 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1218 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1219 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1220 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1221 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1222 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1223 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1224 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1226 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1227 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1228 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001229 if (dev->wiphy.wowlan.n_patterns) {
1230 struct nl80211_wowlan_pattern_support pat = {
1231 .max_patterns = dev->wiphy.wowlan.n_patterns,
1232 .min_pattern_len =
1233 dev->wiphy.wowlan.pattern_min_len,
1234 .max_pattern_len =
1235 dev->wiphy.wowlan.pattern_max_len,
1236 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1238 sizeof(pat), &pat))
1239 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001240 }
1241
1242 nla_nest_end(msg, nl_wowlan);
1243 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001244#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001245
Johannes Berg7527a782011-05-13 10:58:57 +02001246 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1247 dev->wiphy.software_iftypes))
1248 goto nla_put_failure;
1249
1250 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1251 goto nla_put_failure;
1252
David S. Miller9360ffd2012-03-29 04:41:26 -04001253 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1254 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1255 dev->wiphy.ap_sme_capa))
1256 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001257
David S. Miller9360ffd2012-03-29 04:41:26 -04001258 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1259 dev->wiphy.features))
1260 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001261
David S. Miller9360ffd2012-03-29 04:41:26 -04001262 if (dev->wiphy.ht_capa_mod_mask &&
1263 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1264 sizeof(*dev->wiphy.ht_capa_mod_mask),
1265 dev->wiphy.ht_capa_mod_mask))
1266 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001267
Johannes Berg55682962007-09-20 13:09:35 -04001268 return genlmsg_end(msg, hdr);
1269
1270 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001271 genlmsg_cancel(msg, hdr);
1272 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001273}
1274
1275static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1276{
1277 int idx = 0;
1278 int start = cb->args[0];
1279 struct cfg80211_registered_device *dev;
1280
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001281 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001282 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001283 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1284 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001285 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001286 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001287 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001288 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001289 dev) < 0) {
1290 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001291 break;
Julius Volzb4637272008-07-08 14:02:19 +02001292 }
Johannes Berg55682962007-09-20 13:09:35 -04001293 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001294 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001295
1296 cb->args[0] = idx;
1297
1298 return skb->len;
1299}
1300
1301static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1302{
1303 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001304 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001305
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001306 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001307 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001308 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001309
Eric W. Biederman15e47302012-09-07 20:12:54 +00001310 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001311 nlmsg_free(msg);
1312 return -ENOBUFS;
1313 }
Johannes Berg55682962007-09-20 13:09:35 -04001314
Johannes Berg134e6372009-07-10 09:51:34 +00001315 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001316}
1317
Jouni Malinen31888482008-10-30 16:59:24 +02001318static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1319 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1320 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1321 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1322 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1323 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1324};
1325
1326static int parse_txq_params(struct nlattr *tb[],
1327 struct ieee80211_txq_params *txq_params)
1328{
Johannes Berga3304b02012-03-28 11:04:24 +02001329 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001330 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1331 !tb[NL80211_TXQ_ATTR_AIFS])
1332 return -EINVAL;
1333
Johannes Berga3304b02012-03-28 11:04:24 +02001334 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001335 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1336 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1337 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1338 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1339
Johannes Berga3304b02012-03-28 11:04:24 +02001340 if (txq_params->ac >= NL80211_NUM_ACS)
1341 return -EINVAL;
1342
Jouni Malinen31888482008-10-30 16:59:24 +02001343 return 0;
1344}
1345
Johannes Bergf444de02010-05-05 15:25:02 +02001346static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1347{
1348 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001349 * You can only set the channel explicitly for WDS interfaces,
1350 * all others have their channel managed via their respective
1351 * "establish a connection" command (connect, join, ...)
1352 *
1353 * For AP/GO and mesh mode, the channel can be set with the
1354 * channel userspace API, but is only stored and passed to the
1355 * low-level driver when the AP starts or the mesh is joined.
1356 * This is for backward compatibility, userspace can also give
1357 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001358 *
1359 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001360 * whatever else is going on, so they have their own special
1361 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001362 */
1363 return !wdev ||
1364 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001365 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001366 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1367 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001368}
1369
Johannes Berg683b6d32012-11-08 21:25:48 +01001370static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1371 struct genl_info *info,
1372 struct cfg80211_chan_def *chandef)
1373{
1374 struct ieee80211_sta_ht_cap *ht_cap;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001375 struct ieee80211_sta_vht_cap *vht_cap;
1376 u32 control_freq, width;
Johannes Berg683b6d32012-11-08 21:25:48 +01001377
1378 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1379 return -EINVAL;
1380
1381 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1382
1383 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001384 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1385 chandef->center_freq1 = control_freq;
1386 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001387
1388 /* Primary channel not allowed */
1389 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1390 return -EINVAL;
1391
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001392 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1393 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001394
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001395 chantype = nla_get_u32(
1396 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1397
1398 switch (chantype) {
1399 case NL80211_CHAN_NO_HT:
1400 case NL80211_CHAN_HT20:
1401 case NL80211_CHAN_HT40PLUS:
1402 case NL80211_CHAN_HT40MINUS:
1403 cfg80211_chandef_create(chandef, chandef->chan,
1404 chantype);
1405 break;
1406 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001407 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001408 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001409 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1410 chandef->width =
1411 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1412 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1413 chandef->center_freq1 =
1414 nla_get_u32(
1415 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1416 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1417 chandef->center_freq2 =
1418 nla_get_u32(
1419 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1420 }
1421
1422 ht_cap = &rdev->wiphy.bands[chandef->chan->band]->ht_cap;
1423 vht_cap = &rdev->wiphy.bands[chandef->chan->band]->vht_cap;
1424
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001425 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001426 return -EINVAL;
1427
1428 switch (chandef->width) {
1429 case NL80211_CHAN_WIDTH_20:
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001430 case NL80211_CHAN_WIDTH_20_NOHT:
1431 width = 20;
1432 break;
1433 case NL80211_CHAN_WIDTH_40:
1434 width = 40;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001435 break;
1436 case NL80211_CHAN_WIDTH_80:
1437 width = 80;
Johannes Berg683b6d32012-11-08 21:25:48 +01001438 break;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001439 case NL80211_CHAN_WIDTH_80P80:
1440 width = 80;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001441 break;
1442 case NL80211_CHAN_WIDTH_160:
1443 width = 160;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001444 break;
1445 default:
1446 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001447 }
1448
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001449 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1450 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001451 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001452
Johannes Berg683b6d32012-11-08 21:25:48 +01001453 return 0;
1454}
1455
Johannes Bergf444de02010-05-05 15:25:02 +02001456static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1457 struct wireless_dev *wdev,
1458 struct genl_info *info)
1459{
Johannes Berg683b6d32012-11-08 21:25:48 +01001460 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001461 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001462 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1463
1464 if (wdev)
1465 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001466
Johannes Bergf444de02010-05-05 15:25:02 +02001467 if (!nl80211_can_set_dev_channel(wdev))
1468 return -EOPNOTSUPP;
1469
Johannes Berg683b6d32012-11-08 21:25:48 +01001470 result = nl80211_parse_chandef(rdev, info, &chandef);
1471 if (result)
1472 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001473
1474 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001475 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001476 case NL80211_IFTYPE_AP:
1477 case NL80211_IFTYPE_P2P_GO:
1478 if (wdev->beacon_interval) {
1479 result = -EBUSY;
1480 break;
1481 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001482 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001483 result = -EINVAL;
1484 break;
1485 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001486 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001487 result = 0;
1488 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001489 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001490 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001491 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001492 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001493 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001494 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001495 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001496 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001497 }
1498 mutex_unlock(&rdev->devlist_mtx);
1499
1500 return result;
1501}
1502
1503static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1504{
Johannes Berg4c476992010-10-04 21:36:35 +02001505 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1506 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001507
Johannes Berg4c476992010-10-04 21:36:35 +02001508 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001509}
1510
Bill Jordane8347eb2010-10-01 13:54:28 -04001511static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1512{
Johannes Berg43b19952010-10-07 13:10:30 +02001513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1514 struct net_device *dev = info->user_ptr[1];
1515 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001516 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001517
1518 if (!info->attrs[NL80211_ATTR_MAC])
1519 return -EINVAL;
1520
Johannes Berg43b19952010-10-07 13:10:30 +02001521 if (netif_running(dev))
1522 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001523
Johannes Berg43b19952010-10-07 13:10:30 +02001524 if (!rdev->ops->set_wds_peer)
1525 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001526
Johannes Berg43b19952010-10-07 13:10:30 +02001527 if (wdev->iftype != NL80211_IFTYPE_WDS)
1528 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001529
1530 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001531 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001532}
1533
1534
Johannes Berg55682962007-09-20 13:09:35 -04001535static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1536{
1537 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001538 struct net_device *netdev = NULL;
1539 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001540 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001541 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001542 u32 changed;
1543 u8 retry_short = 0, retry_long = 0;
1544 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001545 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001546
Johannes Bergf444de02010-05-05 15:25:02 +02001547 /*
1548 * Try to find the wiphy and netdev. Normally this
1549 * function shouldn't need the netdev, but this is
1550 * done for backward compatibility -- previously
1551 * setting the channel was done per wiphy, but now
1552 * it is per netdev. Previous userland like hostapd
1553 * also passed a netdev to set_wiphy, so that it is
1554 * possible to let that go to the right netdev!
1555 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001556 mutex_lock(&cfg80211_mutex);
1557
Johannes Bergf444de02010-05-05 15:25:02 +02001558 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1559 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1560
1561 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1562 if (netdev && netdev->ieee80211_ptr) {
1563 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1564 mutex_lock(&rdev->mtx);
1565 } else
1566 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001567 }
1568
Johannes Bergf444de02010-05-05 15:25:02 +02001569 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001570 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1571 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001572 if (IS_ERR(rdev)) {
1573 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001574 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001575 }
1576 wdev = NULL;
1577 netdev = NULL;
1578 result = 0;
1579
1580 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001581 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001582 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001583
1584 /*
1585 * end workaround code, by now the rdev is available
1586 * and locked, and wdev may or may not be NULL.
1587 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001588
1589 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001590 result = cfg80211_dev_rename(
1591 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001592
1593 mutex_unlock(&cfg80211_mutex);
1594
1595 if (result)
1596 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001597
Jouni Malinen31888482008-10-30 16:59:24 +02001598 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1599 struct ieee80211_txq_params txq_params;
1600 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1601
1602 if (!rdev->ops->set_txq_params) {
1603 result = -EOPNOTSUPP;
1604 goto bad_res;
1605 }
1606
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001607 if (!netdev) {
1608 result = -EINVAL;
1609 goto bad_res;
1610 }
1611
Johannes Berg133a3ff2011-11-03 14:50:13 +01001612 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1613 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1614 result = -EINVAL;
1615 goto bad_res;
1616 }
1617
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001618 if (!netif_running(netdev)) {
1619 result = -ENETDOWN;
1620 goto bad_res;
1621 }
1622
Jouni Malinen31888482008-10-30 16:59:24 +02001623 nla_for_each_nested(nl_txq_params,
1624 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1625 rem_txq_params) {
1626 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1627 nla_data(nl_txq_params),
1628 nla_len(nl_txq_params),
1629 txq_params_policy);
1630 result = parse_txq_params(tb, &txq_params);
1631 if (result)
1632 goto bad_res;
1633
Hila Gonene35e4d22012-06-27 17:19:42 +03001634 result = rdev_set_txq_params(rdev, netdev,
1635 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001636 if (result)
1637 goto bad_res;
1638 }
1639 }
1640
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001641 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001642 result = __nl80211_set_channel(rdev,
1643 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1644 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001645 if (result)
1646 goto bad_res;
1647 }
1648
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001649 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001650 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001651 enum nl80211_tx_power_setting type;
1652 int idx, mbm = 0;
1653
Johannes Bergc8442112012-10-24 10:17:18 +02001654 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1655 txp_wdev = NULL;
1656
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001657 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001658 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001659 goto bad_res;
1660 }
1661
1662 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1663 type = nla_get_u32(info->attrs[idx]);
1664
1665 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1666 (type != NL80211_TX_POWER_AUTOMATIC)) {
1667 result = -EINVAL;
1668 goto bad_res;
1669 }
1670
1671 if (type != NL80211_TX_POWER_AUTOMATIC) {
1672 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1673 mbm = nla_get_u32(info->attrs[idx]);
1674 }
1675
Johannes Bergc8442112012-10-24 10:17:18 +02001676 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001677 if (result)
1678 goto bad_res;
1679 }
1680
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001681 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1682 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1683 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001684 if ((!rdev->wiphy.available_antennas_tx &&
1685 !rdev->wiphy.available_antennas_rx) ||
1686 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001687 result = -EOPNOTSUPP;
1688 goto bad_res;
1689 }
1690
1691 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1692 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1693
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001694 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001695 * available antenna masks, except for the "all" mask */
1696 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1697 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001698 result = -EINVAL;
1699 goto bad_res;
1700 }
1701
Bruno Randolf7f531e02010-12-16 11:30:22 +09001702 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1703 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001704
Hila Gonene35e4d22012-06-27 17:19:42 +03001705 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001706 if (result)
1707 goto bad_res;
1708 }
1709
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001710 changed = 0;
1711
1712 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1713 retry_short = nla_get_u8(
1714 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1715 if (retry_short == 0) {
1716 result = -EINVAL;
1717 goto bad_res;
1718 }
1719 changed |= WIPHY_PARAM_RETRY_SHORT;
1720 }
1721
1722 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1723 retry_long = nla_get_u8(
1724 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1725 if (retry_long == 0) {
1726 result = -EINVAL;
1727 goto bad_res;
1728 }
1729 changed |= WIPHY_PARAM_RETRY_LONG;
1730 }
1731
1732 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1733 frag_threshold = nla_get_u32(
1734 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1735 if (frag_threshold < 256) {
1736 result = -EINVAL;
1737 goto bad_res;
1738 }
1739 if (frag_threshold != (u32) -1) {
1740 /*
1741 * Fragments (apart from the last one) are required to
1742 * have even length. Make the fragmentation code
1743 * simpler by stripping LSB should someone try to use
1744 * odd threshold value.
1745 */
1746 frag_threshold &= ~0x1;
1747 }
1748 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1749 }
1750
1751 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1752 rts_threshold = nla_get_u32(
1753 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1754 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1755 }
1756
Lukáš Turek81077e82009-12-21 22:50:47 +01001757 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1758 coverage_class = nla_get_u8(
1759 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1760 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1761 }
1762
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001763 if (changed) {
1764 u8 old_retry_short, old_retry_long;
1765 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001766 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001767
1768 if (!rdev->ops->set_wiphy_params) {
1769 result = -EOPNOTSUPP;
1770 goto bad_res;
1771 }
1772
1773 old_retry_short = rdev->wiphy.retry_short;
1774 old_retry_long = rdev->wiphy.retry_long;
1775 old_frag_threshold = rdev->wiphy.frag_threshold;
1776 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001777 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001778
1779 if (changed & WIPHY_PARAM_RETRY_SHORT)
1780 rdev->wiphy.retry_short = retry_short;
1781 if (changed & WIPHY_PARAM_RETRY_LONG)
1782 rdev->wiphy.retry_long = retry_long;
1783 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1784 rdev->wiphy.frag_threshold = frag_threshold;
1785 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1786 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001787 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1788 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001789
Hila Gonene35e4d22012-06-27 17:19:42 +03001790 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001791 if (result) {
1792 rdev->wiphy.retry_short = old_retry_short;
1793 rdev->wiphy.retry_long = old_retry_long;
1794 rdev->wiphy.frag_threshold = old_frag_threshold;
1795 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001796 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001797 }
1798 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001799
Johannes Berg306d6112008-12-08 12:39:04 +01001800 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001801 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001802 if (netdev)
1803 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001804 return result;
1805}
1806
Johannes Berg71bbc992012-06-15 15:30:18 +02001807static inline u64 wdev_id(struct wireless_dev *wdev)
1808{
1809 return (u64)wdev->identifier |
1810 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1811}
Johannes Berg55682962007-09-20 13:09:35 -04001812
Johannes Berg683b6d32012-11-08 21:25:48 +01001813static int nl80211_send_chandef(struct sk_buff *msg,
1814 struct cfg80211_chan_def *chandef)
1815{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001816 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001817
Johannes Berg683b6d32012-11-08 21:25:48 +01001818 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1819 chandef->chan->center_freq))
1820 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001821 switch (chandef->width) {
1822 case NL80211_CHAN_WIDTH_20_NOHT:
1823 case NL80211_CHAN_WIDTH_20:
1824 case NL80211_CHAN_WIDTH_40:
1825 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1826 cfg80211_get_chandef_type(chandef)))
1827 return -ENOBUFS;
1828 break;
1829 default:
1830 break;
1831 }
1832 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1833 return -ENOBUFS;
1834 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1835 return -ENOBUFS;
1836 if (chandef->center_freq2 &&
1837 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001838 return -ENOBUFS;
1839 return 0;
1840}
1841
Eric W. Biederman15e47302012-09-07 20:12:54 +00001842static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001843 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001844 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001845{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001846 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001847 void *hdr;
1848
Eric W. Biederman15e47302012-09-07 20:12:54 +00001849 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001850 if (!hdr)
1851 return -1;
1852
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001853 if (dev &&
1854 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001855 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001856 goto nla_put_failure;
1857
1858 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1859 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001860 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001861 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001862 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1863 rdev->devlist_generation ^
1864 (cfg80211_rdev_list_generation << 2)))
1865 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001866
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001867 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001868 int ret;
1869 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001870
Johannes Berg683b6d32012-11-08 21:25:48 +01001871 ret = rdev_get_channel(rdev, wdev, &chandef);
1872 if (ret == 0) {
1873 if (nl80211_send_chandef(msg, &chandef))
1874 goto nla_put_failure;
1875 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001876 }
1877
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001878 if (wdev->ssid_len) {
1879 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1880 goto nla_put_failure;
1881 }
1882
Johannes Berg55682962007-09-20 13:09:35 -04001883 return genlmsg_end(msg, hdr);
1884
1885 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001886 genlmsg_cancel(msg, hdr);
1887 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001888}
1889
1890static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1891{
1892 int wp_idx = 0;
1893 int if_idx = 0;
1894 int wp_start = cb->args[0];
1895 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001896 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001897 struct wireless_dev *wdev;
1898
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001899 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001900 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1901 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001902 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001903 if (wp_idx < wp_start) {
1904 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001905 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001906 }
Johannes Berg55682962007-09-20 13:09:35 -04001907 if_idx = 0;
1908
Johannes Bergf5ea9122009-08-07 16:17:38 +02001909 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001910 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001911 if (if_idx < if_start) {
1912 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001913 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001914 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001915 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001916 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001917 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001918 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001919 goto out;
1920 }
1921 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001922 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001923 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001924
1925 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001926 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001927 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001928 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001929
1930 cb->args[0] = wp_idx;
1931 cb->args[1] = if_idx;
1932
1933 return skb->len;
1934}
1935
1936static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1937{
1938 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001939 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001940 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001941
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001942 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001943 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001944 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001945
Eric W. Biederman15e47302012-09-07 20:12:54 +00001946 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001947 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001948 nlmsg_free(msg);
1949 return -ENOBUFS;
1950 }
Johannes Berg55682962007-09-20 13:09:35 -04001951
Johannes Berg134e6372009-07-10 09:51:34 +00001952 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001953}
1954
Michael Wu66f7ac52008-01-31 19:48:22 +01001955static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1956 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1957 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1958 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1959 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1960 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1961};
1962
1963static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1964{
1965 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1966 int flag;
1967
1968 *mntrflags = 0;
1969
1970 if (!nla)
1971 return -EINVAL;
1972
1973 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1974 nla, mntr_flags_policy))
1975 return -EINVAL;
1976
1977 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1978 if (flags[flag])
1979 *mntrflags |= (1<<flag);
1980
1981 return 0;
1982}
1983
Johannes Berg9bc383d2009-11-19 11:55:19 +01001984static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001985 struct net_device *netdev, u8 use_4addr,
1986 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001987{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001988 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001989 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001990 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001991 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001992 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001993
1994 switch (iftype) {
1995 case NL80211_IFTYPE_AP_VLAN:
1996 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1997 return 0;
1998 break;
1999 case NL80211_IFTYPE_STATION:
2000 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2001 return 0;
2002 break;
2003 default:
2004 break;
2005 }
2006
2007 return -EOPNOTSUPP;
2008}
2009
Johannes Berg55682962007-09-20 13:09:35 -04002010static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2011{
Johannes Berg4c476992010-10-04 21:36:35 +02002012 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002013 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002014 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002015 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002016 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002017 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002018 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002019
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002020 memset(&params, 0, sizeof(params));
2021
Johannes Berg04a773a2009-04-19 21:24:32 +02002022 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002023
Johannes Berg723b0382008-09-16 20:22:09 +02002024 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002025 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002026 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002027 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002028 if (ntype > NL80211_IFTYPE_MAX)
2029 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002030 }
2031
Johannes Berg92ffe052008-09-16 20:39:36 +02002032 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002033 struct wireless_dev *wdev = dev->ieee80211_ptr;
2034
Johannes Berg4c476992010-10-04 21:36:35 +02002035 if (ntype != NL80211_IFTYPE_MESH_POINT)
2036 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002037 if (netif_running(dev))
2038 return -EBUSY;
2039
2040 wdev_lock(wdev);
2041 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2042 IEEE80211_MAX_MESH_ID_LEN);
2043 wdev->mesh_id_up_len =
2044 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2045 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2046 wdev->mesh_id_up_len);
2047 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002048 }
2049
Felix Fietkau8b787642009-11-10 18:53:10 +01002050 if (info->attrs[NL80211_ATTR_4ADDR]) {
2051 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2052 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002053 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002054 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002055 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002056 } else {
2057 params.use_4addr = -1;
2058 }
2059
Johannes Berg92ffe052008-09-16 20:39:36 +02002060 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002061 if (ntype != NL80211_IFTYPE_MONITOR)
2062 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002063 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2064 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002065 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002066 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002067
2068 flags = &_flags;
2069 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002070 }
Johannes Berg3b858752009-03-12 09:55:09 +01002071
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002072 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002073 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002074 else
2075 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002076
Johannes Berg9bc383d2009-11-19 11:55:19 +01002077 if (!err && params.use_4addr != -1)
2078 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2079
Johannes Berg55682962007-09-20 13:09:35 -04002080 return err;
2081}
2082
2083static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2084{
Johannes Berg4c476992010-10-04 21:36:35 +02002085 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002086 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002087 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002088 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002089 int err;
2090 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002091 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002092
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002093 memset(&params, 0, sizeof(params));
2094
Johannes Berg55682962007-09-20 13:09:35 -04002095 if (!info->attrs[NL80211_ATTR_IFNAME])
2096 return -EINVAL;
2097
2098 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2099 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2100 if (type > NL80211_IFTYPE_MAX)
2101 return -EINVAL;
2102 }
2103
Johannes Berg79c97e92009-07-07 03:56:12 +02002104 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002105 !(rdev->wiphy.interface_modes & (1 << type)))
2106 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002107
Johannes Berg9bc383d2009-11-19 11:55:19 +01002108 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002109 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002110 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002111 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002112 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002113 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002114
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002115 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2116 if (!msg)
2117 return -ENOMEM;
2118
Michael Wu66f7ac52008-01-31 19:48:22 +01002119 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2120 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2121 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002122 wdev = rdev_add_virtual_intf(rdev,
2123 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2124 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002125 if (IS_ERR(wdev)) {
2126 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002127 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002128 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002129
Johannes Berg98104fde2012-06-16 00:19:54 +02002130 switch (type) {
2131 case NL80211_IFTYPE_MESH_POINT:
2132 if (!info->attrs[NL80211_ATTR_MESH_ID])
2133 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002134 wdev_lock(wdev);
2135 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2136 IEEE80211_MAX_MESH_ID_LEN);
2137 wdev->mesh_id_up_len =
2138 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2139 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2140 wdev->mesh_id_up_len);
2141 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002142 break;
2143 case NL80211_IFTYPE_P2P_DEVICE:
2144 /*
2145 * P2P Device doesn't have a netdev, so doesn't go
2146 * through the netdev notifier and must be added here
2147 */
2148 mutex_init(&wdev->mtx);
2149 INIT_LIST_HEAD(&wdev->event_list);
2150 spin_lock_init(&wdev->event_lock);
2151 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2152 spin_lock_init(&wdev->mgmt_registrations_lock);
2153
2154 mutex_lock(&rdev->devlist_mtx);
2155 wdev->identifier = ++rdev->wdev_id;
2156 list_add_rcu(&wdev->list, &rdev->wdev_list);
2157 rdev->devlist_generation++;
2158 mutex_unlock(&rdev->devlist_mtx);
2159 break;
2160 default:
2161 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002162 }
2163
Eric W. Biederman15e47302012-09-07 20:12:54 +00002164 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002165 rdev, wdev) < 0) {
2166 nlmsg_free(msg);
2167 return -ENOBUFS;
2168 }
2169
2170 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002171}
2172
2173static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2174{
Johannes Berg4c476992010-10-04 21:36:35 +02002175 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002176 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002177
Johannes Berg4c476992010-10-04 21:36:35 +02002178 if (!rdev->ops->del_virtual_intf)
2179 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002180
Johannes Berg84efbb82012-06-16 00:00:26 +02002181 /*
2182 * If we remove a wireless device without a netdev then clear
2183 * user_ptr[1] so that nl80211_post_doit won't dereference it
2184 * to check if it needs to do dev_put(). Otherwise it crashes
2185 * since the wdev has been freed, unlike with a netdev where
2186 * we need the dev_put() for the netdev to really be freed.
2187 */
2188 if (!wdev->netdev)
2189 info->user_ptr[1] = NULL;
2190
Hila Gonene35e4d22012-06-27 17:19:42 +03002191 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002192}
2193
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002194static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2195{
2196 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2197 struct net_device *dev = info->user_ptr[1];
2198 u16 noack_map;
2199
2200 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2201 return -EINVAL;
2202
2203 if (!rdev->ops->set_noack_map)
2204 return -EOPNOTSUPP;
2205
2206 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2207
Hila Gonene35e4d22012-06-27 17:19:42 +03002208 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002209}
2210
Johannes Berg41ade002007-12-19 02:03:29 +01002211struct get_key_cookie {
2212 struct sk_buff *msg;
2213 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002214 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002215};
2216
2217static void get_key_callback(void *c, struct key_params *params)
2218{
Johannes Bergb9454e82009-07-08 13:29:08 +02002219 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002220 struct get_key_cookie *cookie = c;
2221
David S. Miller9360ffd2012-03-29 04:41:26 -04002222 if ((params->key &&
2223 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2224 params->key_len, params->key)) ||
2225 (params->seq &&
2226 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2227 params->seq_len, params->seq)) ||
2228 (params->cipher &&
2229 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2230 params->cipher)))
2231 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002232
Johannes Bergb9454e82009-07-08 13:29:08 +02002233 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2234 if (!key)
2235 goto nla_put_failure;
2236
David S. Miller9360ffd2012-03-29 04:41:26 -04002237 if ((params->key &&
2238 nla_put(cookie->msg, NL80211_KEY_DATA,
2239 params->key_len, params->key)) ||
2240 (params->seq &&
2241 nla_put(cookie->msg, NL80211_KEY_SEQ,
2242 params->seq_len, params->seq)) ||
2243 (params->cipher &&
2244 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2245 params->cipher)))
2246 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002247
David S. Miller9360ffd2012-03-29 04:41:26 -04002248 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2249 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002250
2251 nla_nest_end(cookie->msg, key);
2252
Johannes Berg41ade002007-12-19 02:03:29 +01002253 return;
2254 nla_put_failure:
2255 cookie->error = 1;
2256}
2257
2258static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2259{
Johannes Berg4c476992010-10-04 21:36:35 +02002260 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002261 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002262 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002263 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002264 const u8 *mac_addr = NULL;
2265 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002266 struct get_key_cookie cookie = {
2267 .error = 0,
2268 };
2269 void *hdr;
2270 struct sk_buff *msg;
2271
2272 if (info->attrs[NL80211_ATTR_KEY_IDX])
2273 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2274
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002275 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002276 return -EINVAL;
2277
2278 if (info->attrs[NL80211_ATTR_MAC])
2279 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2280
Johannes Berge31b8212010-10-05 19:39:30 +02002281 pairwise = !!mac_addr;
2282 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2283 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2284 if (kt >= NUM_NL80211_KEYTYPES)
2285 return -EINVAL;
2286 if (kt != NL80211_KEYTYPE_GROUP &&
2287 kt != NL80211_KEYTYPE_PAIRWISE)
2288 return -EINVAL;
2289 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2290 }
2291
Johannes Berg4c476992010-10-04 21:36:35 +02002292 if (!rdev->ops->get_key)
2293 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002294
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002295 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002296 if (!msg)
2297 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002298
Eric W. Biederman15e47302012-09-07 20:12:54 +00002299 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002300 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002301 if (IS_ERR(hdr))
2302 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002303
2304 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002305 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002306
David S. Miller9360ffd2012-03-29 04:41:26 -04002307 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2308 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2309 goto nla_put_failure;
2310 if (mac_addr &&
2311 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2312 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002313
Johannes Berge31b8212010-10-05 19:39:30 +02002314 if (pairwise && mac_addr &&
2315 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2316 return -ENOENT;
2317
Hila Gonene35e4d22012-06-27 17:19:42 +03002318 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2319 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002320
2321 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002322 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002323
2324 if (cookie.error)
2325 goto nla_put_failure;
2326
2327 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002328 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002329
2330 nla_put_failure:
2331 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002332 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002333 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002334 return err;
2335}
2336
2337static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2338{
Johannes Berg4c476992010-10-04 21:36:35 +02002339 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002340 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002341 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002342 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002343
Johannes Bergb9454e82009-07-08 13:29:08 +02002344 err = nl80211_parse_key(info, &key);
2345 if (err)
2346 return err;
2347
2348 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002349 return -EINVAL;
2350
Johannes Bergb9454e82009-07-08 13:29:08 +02002351 /* only support setting default key */
2352 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002353 return -EINVAL;
2354
Johannes Bergfffd0932009-07-08 14:22:54 +02002355 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002356
2357 if (key.def) {
2358 if (!rdev->ops->set_default_key) {
2359 err = -EOPNOTSUPP;
2360 goto out;
2361 }
2362
2363 err = nl80211_key_allowed(dev->ieee80211_ptr);
2364 if (err)
2365 goto out;
2366
Hila Gonene35e4d22012-06-27 17:19:42 +03002367 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002368 key.def_uni, key.def_multi);
2369
2370 if (err)
2371 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002372
Johannes Berg3d23e342009-09-29 23:27:28 +02002373#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002374 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002375#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002376 } else {
2377 if (key.def_uni || !key.def_multi) {
2378 err = -EINVAL;
2379 goto out;
2380 }
2381
2382 if (!rdev->ops->set_default_mgmt_key) {
2383 err = -EOPNOTSUPP;
2384 goto out;
2385 }
2386
2387 err = nl80211_key_allowed(dev->ieee80211_ptr);
2388 if (err)
2389 goto out;
2390
Hila Gonene35e4d22012-06-27 17:19:42 +03002391 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002392 if (err)
2393 goto out;
2394
2395#ifdef CONFIG_CFG80211_WEXT
2396 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2397#endif
2398 }
2399
2400 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002401 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002402
Johannes Berg41ade002007-12-19 02:03:29 +01002403 return err;
2404}
2405
2406static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2407{
Johannes Berg4c476992010-10-04 21:36:35 +02002408 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002409 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002410 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002411 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002412 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002413
Johannes Bergb9454e82009-07-08 13:29:08 +02002414 err = nl80211_parse_key(info, &key);
2415 if (err)
2416 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002417
Johannes Bergb9454e82009-07-08 13:29:08 +02002418 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002419 return -EINVAL;
2420
Johannes Berg41ade002007-12-19 02:03:29 +01002421 if (info->attrs[NL80211_ATTR_MAC])
2422 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2423
Johannes Berge31b8212010-10-05 19:39:30 +02002424 if (key.type == -1) {
2425 if (mac_addr)
2426 key.type = NL80211_KEYTYPE_PAIRWISE;
2427 else
2428 key.type = NL80211_KEYTYPE_GROUP;
2429 }
2430
2431 /* for now */
2432 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2433 key.type != NL80211_KEYTYPE_GROUP)
2434 return -EINVAL;
2435
Johannes Berg4c476992010-10-04 21:36:35 +02002436 if (!rdev->ops->add_key)
2437 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002438
Johannes Berge31b8212010-10-05 19:39:30 +02002439 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2440 key.type == NL80211_KEYTYPE_PAIRWISE,
2441 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002442 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002443
2444 wdev_lock(dev->ieee80211_ptr);
2445 err = nl80211_key_allowed(dev->ieee80211_ptr);
2446 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002447 err = rdev_add_key(rdev, dev, key.idx,
2448 key.type == NL80211_KEYTYPE_PAIRWISE,
2449 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002450 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002451
Johannes Berg41ade002007-12-19 02:03:29 +01002452 return err;
2453}
2454
2455static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2456{
Johannes Berg4c476992010-10-04 21:36:35 +02002457 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002458 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002459 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002460 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002461 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002462
Johannes Bergb9454e82009-07-08 13:29:08 +02002463 err = nl80211_parse_key(info, &key);
2464 if (err)
2465 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002466
2467 if (info->attrs[NL80211_ATTR_MAC])
2468 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2469
Johannes Berge31b8212010-10-05 19:39:30 +02002470 if (key.type == -1) {
2471 if (mac_addr)
2472 key.type = NL80211_KEYTYPE_PAIRWISE;
2473 else
2474 key.type = NL80211_KEYTYPE_GROUP;
2475 }
2476
2477 /* for now */
2478 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2479 key.type != NL80211_KEYTYPE_GROUP)
2480 return -EINVAL;
2481
Johannes Berg4c476992010-10-04 21:36:35 +02002482 if (!rdev->ops->del_key)
2483 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002484
Johannes Bergfffd0932009-07-08 14:22:54 +02002485 wdev_lock(dev->ieee80211_ptr);
2486 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002487
2488 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2489 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2490 err = -ENOENT;
2491
Johannes Bergfffd0932009-07-08 14:22:54 +02002492 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002493 err = rdev_del_key(rdev, dev, key.idx,
2494 key.type == NL80211_KEYTYPE_PAIRWISE,
2495 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002496
Johannes Berg3d23e342009-09-29 23:27:28 +02002497#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002498 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002499 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002500 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002501 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002502 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2503 }
2504#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002505 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002506
Johannes Berg41ade002007-12-19 02:03:29 +01002507 return err;
2508}
2509
Johannes Berg88600202012-02-13 15:17:18 +01002510static int nl80211_parse_beacon(struct genl_info *info,
2511 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002512{
Johannes Berg88600202012-02-13 15:17:18 +01002513 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002514
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002515 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2516 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2517 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2518 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002519 return -EINVAL;
2520
Johannes Berg88600202012-02-13 15:17:18 +01002521 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002522
Johannes Berged1b6cc2007-12-19 02:03:32 +01002523 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002524 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2525 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2526 if (!bcn->head_len)
2527 return -EINVAL;
2528 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002529 }
2530
2531 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002532 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2533 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002534 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002535 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002536 }
2537
Johannes Berg4c476992010-10-04 21:36:35 +02002538 if (!haveinfo)
2539 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002540
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002541 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002542 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2543 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002544 }
2545
2546 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002547 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002548 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002549 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002550 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2551 }
2552
2553 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002554 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002555 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002556 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002557 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2558 }
2559
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002560 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002561 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002562 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002563 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002564 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2565 }
2566
Johannes Berg88600202012-02-13 15:17:18 +01002567 return 0;
2568}
2569
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002570static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2571 struct cfg80211_ap_settings *params)
2572{
2573 struct wireless_dev *wdev;
2574 bool ret = false;
2575
2576 mutex_lock(&rdev->devlist_mtx);
2577
Johannes Berg89a54e42012-06-15 14:33:17 +02002578 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002579 if (wdev->iftype != NL80211_IFTYPE_AP &&
2580 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2581 continue;
2582
Johannes Berg683b6d32012-11-08 21:25:48 +01002583 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002584 continue;
2585
Johannes Berg683b6d32012-11-08 21:25:48 +01002586 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002587 ret = true;
2588 break;
2589 }
2590
2591 mutex_unlock(&rdev->devlist_mtx);
2592
2593 return ret;
2594}
2595
Jouni Malinene39e5b52012-09-30 19:29:39 +03002596static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2597 enum nl80211_auth_type auth_type,
2598 enum nl80211_commands cmd)
2599{
2600 if (auth_type > NL80211_AUTHTYPE_MAX)
2601 return false;
2602
2603 switch (cmd) {
2604 case NL80211_CMD_AUTHENTICATE:
2605 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2606 auth_type == NL80211_AUTHTYPE_SAE)
2607 return false;
2608 return true;
2609 case NL80211_CMD_CONNECT:
2610 case NL80211_CMD_START_AP:
2611 /* SAE not supported yet */
2612 if (auth_type == NL80211_AUTHTYPE_SAE)
2613 return false;
2614 return true;
2615 default:
2616 return false;
2617 }
2618}
2619
Johannes Berg88600202012-02-13 15:17:18 +01002620static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2621{
2622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2623 struct net_device *dev = info->user_ptr[1];
2624 struct wireless_dev *wdev = dev->ieee80211_ptr;
2625 struct cfg80211_ap_settings params;
2626 int err;
2627
2628 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2629 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2630 return -EOPNOTSUPP;
2631
2632 if (!rdev->ops->start_ap)
2633 return -EOPNOTSUPP;
2634
2635 if (wdev->beacon_interval)
2636 return -EALREADY;
2637
2638 memset(&params, 0, sizeof(params));
2639
2640 /* these are required for START_AP */
2641 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2642 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2643 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2644 return -EINVAL;
2645
2646 err = nl80211_parse_beacon(info, &params.beacon);
2647 if (err)
2648 return err;
2649
2650 params.beacon_interval =
2651 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2652 params.dtim_period =
2653 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2654
2655 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2656 if (err)
2657 return err;
2658
2659 /*
2660 * In theory, some of these attributes should be required here
2661 * but since they were not used when the command was originally
2662 * added, keep them optional for old user space programs to let
2663 * them continue to work with drivers that do not need the
2664 * additional information -- drivers must check!
2665 */
2666 if (info->attrs[NL80211_ATTR_SSID]) {
2667 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2668 params.ssid_len =
2669 nla_len(info->attrs[NL80211_ATTR_SSID]);
2670 if (params.ssid_len == 0 ||
2671 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2672 return -EINVAL;
2673 }
2674
2675 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2676 params.hidden_ssid = nla_get_u32(
2677 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2678 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2679 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2680 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2681 return -EINVAL;
2682 }
2683
2684 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2685
2686 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2687 params.auth_type = nla_get_u32(
2688 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002689 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2690 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002691 return -EINVAL;
2692 } else
2693 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2694
2695 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2696 NL80211_MAX_NR_CIPHER_SUITES);
2697 if (err)
2698 return err;
2699
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302700 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2701 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2702 return -EOPNOTSUPP;
2703 params.inactivity_timeout = nla_get_u16(
2704 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2705 }
2706
Johannes Berg53cabad2012-11-14 15:17:28 +01002707 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2708 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2709 return -EINVAL;
2710 params.p2p_ctwindow =
2711 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2712 if (params.p2p_ctwindow > 127)
2713 return -EINVAL;
2714 if (params.p2p_ctwindow != 0 &&
2715 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2716 return -EINVAL;
2717 }
2718
2719 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2720 u8 tmp;
2721
2722 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2723 return -EINVAL;
2724 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2725 if (tmp > 1)
2726 return -EINVAL;
2727 params.p2p_opp_ps = tmp;
2728 if (params.p2p_opp_ps != 0 &&
2729 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2730 return -EINVAL;
2731 }
2732
Johannes Bergaa430da2012-05-16 23:50:18 +02002733 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002734 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2735 if (err)
2736 return err;
2737 } else if (wdev->preset_chandef.chan) {
2738 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002739 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002740 return -EINVAL;
2741
Johannes Berg683b6d32012-11-08 21:25:48 +01002742 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002743 return -EINVAL;
2744
Michal Kaziore4e32452012-06-29 12:47:08 +02002745 mutex_lock(&rdev->devlist_mtx);
Johannes Berg683b6d32012-11-08 21:25:48 +01002746 err = cfg80211_can_use_chan(rdev, wdev, params.chandef.chan,
Michal Kaziore4e32452012-06-29 12:47:08 +02002747 CHAN_MODE_SHARED);
2748 mutex_unlock(&rdev->devlist_mtx);
2749
2750 if (err)
2751 return err;
2752
Hila Gonene35e4d22012-06-27 17:19:42 +03002753 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002754 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002755 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002756 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002757 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002758 wdev->ssid_len = params.ssid_len;
2759 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002760 }
Johannes Berg56d18932011-05-09 18:41:15 +02002761 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002762}
2763
Johannes Berg88600202012-02-13 15:17:18 +01002764static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2765{
2766 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2767 struct net_device *dev = info->user_ptr[1];
2768 struct wireless_dev *wdev = dev->ieee80211_ptr;
2769 struct cfg80211_beacon_data params;
2770 int err;
2771
2772 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2773 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2774 return -EOPNOTSUPP;
2775
2776 if (!rdev->ops->change_beacon)
2777 return -EOPNOTSUPP;
2778
2779 if (!wdev->beacon_interval)
2780 return -EINVAL;
2781
2782 err = nl80211_parse_beacon(info, &params);
2783 if (err)
2784 return err;
2785
Hila Gonene35e4d22012-06-27 17:19:42 +03002786 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002787}
2788
2789static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002790{
Johannes Berg4c476992010-10-04 21:36:35 +02002791 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2792 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002793
Michal Kazior60771782012-06-29 12:46:56 +02002794 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002795}
2796
Johannes Berg5727ef12007-12-19 02:03:34 +01002797static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2798 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2799 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2800 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002801 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002802 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002803 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002804};
2805
Johannes Bergeccb8e82009-05-11 21:57:56 +03002806static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002807 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002808 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002809{
2810 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002811 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002812 int flag;
2813
Johannes Bergeccb8e82009-05-11 21:57:56 +03002814 /*
2815 * Try parsing the new attribute first so userspace
2816 * can specify both for older kernels.
2817 */
2818 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2819 if (nla) {
2820 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002821
Johannes Bergeccb8e82009-05-11 21:57:56 +03002822 sta_flags = nla_data(nla);
2823 params->sta_flags_mask = sta_flags->mask;
2824 params->sta_flags_set = sta_flags->set;
2825 if ((params->sta_flags_mask |
2826 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2827 return -EINVAL;
2828 return 0;
2829 }
2830
2831 /* if present, parse the old attribute */
2832
2833 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002834 if (!nla)
2835 return 0;
2836
2837 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2838 nla, sta_flags_policy))
2839 return -EINVAL;
2840
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002841 /*
2842 * Only allow certain flags for interface types so that
2843 * other attributes are silently ignored. Remember that
2844 * this is backward compatibility code with old userspace
2845 * and shouldn't be hit in other cases anyway.
2846 */
2847 switch (iftype) {
2848 case NL80211_IFTYPE_AP:
2849 case NL80211_IFTYPE_AP_VLAN:
2850 case NL80211_IFTYPE_P2P_GO:
2851 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2852 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2853 BIT(NL80211_STA_FLAG_WME) |
2854 BIT(NL80211_STA_FLAG_MFP);
2855 break;
2856 case NL80211_IFTYPE_P2P_CLIENT:
2857 case NL80211_IFTYPE_STATION:
2858 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2859 BIT(NL80211_STA_FLAG_TDLS_PEER);
2860 break;
2861 case NL80211_IFTYPE_MESH_POINT:
2862 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2863 BIT(NL80211_STA_FLAG_MFP) |
2864 BIT(NL80211_STA_FLAG_AUTHORIZED);
2865 default:
2866 return -EINVAL;
2867 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002868
Johannes Berg3383b5a2012-05-10 20:14:43 +02002869 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2870 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002871 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002872
Johannes Berg3383b5a2012-05-10 20:14:43 +02002873 /* no longer support new API additions in old API */
2874 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2875 return -EINVAL;
2876 }
2877 }
2878
Johannes Berg5727ef12007-12-19 02:03:34 +01002879 return 0;
2880}
2881
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002882static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2883 int attr)
2884{
2885 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002886 u32 bitrate;
2887 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002888
2889 rate = nla_nest_start(msg, attr);
2890 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002891 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002892
2893 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2894 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002895 /* report 16-bit bitrate only if we can */
2896 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002897 if (bitrate > 0 &&
2898 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
2899 return false;
2900 if (bitrate_compat > 0 &&
2901 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
2902 return false;
2903
2904 if (info->flags & RATE_INFO_FLAGS_MCS) {
2905 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
2906 return false;
2907 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2908 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2909 return false;
2910 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2911 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2912 return false;
2913 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
2914 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
2915 return false;
2916 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
2917 return false;
2918 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2919 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2920 return false;
2921 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
2922 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
2923 return false;
2924 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
2925 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
2926 return false;
2927 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
2928 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
2929 return false;
2930 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2931 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2932 return false;
2933 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002934
2935 nla_nest_end(msg, rate);
2936 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002937}
2938
Eric W. Biederman15e47302012-09-07 20:12:54 +00002939static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002940 int flags,
2941 struct cfg80211_registered_device *rdev,
2942 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002943 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002944{
2945 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002946 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002947
Eric W. Biederman15e47302012-09-07 20:12:54 +00002948 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002949 if (!hdr)
2950 return -1;
2951
David S. Miller9360ffd2012-03-29 04:41:26 -04002952 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2953 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2954 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2955 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002956
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002957 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2958 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002959 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002960 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2961 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2962 sinfo->connected_time))
2963 goto nla_put_failure;
2964 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2965 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2966 sinfo->inactive_time))
2967 goto nla_put_failure;
2968 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2969 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2970 sinfo->rx_bytes))
2971 goto nla_put_failure;
2972 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2973 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2974 sinfo->tx_bytes))
2975 goto nla_put_failure;
2976 if ((sinfo->filled & STATION_INFO_LLID) &&
2977 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2978 goto nla_put_failure;
2979 if ((sinfo->filled & STATION_INFO_PLID) &&
2980 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2981 goto nla_put_failure;
2982 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2983 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2984 sinfo->plink_state))
2985 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002986 switch (rdev->wiphy.signal_type) {
2987 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002988 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2989 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2990 sinfo->signal))
2991 goto nla_put_failure;
2992 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2993 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2994 sinfo->signal_avg))
2995 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002996 break;
2997 default:
2998 break;
2999 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003000 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003001 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3002 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003003 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003004 }
3005 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3006 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3007 NL80211_STA_INFO_RX_BITRATE))
3008 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003009 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003010 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3011 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3012 sinfo->rx_packets))
3013 goto nla_put_failure;
3014 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3015 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3016 sinfo->tx_packets))
3017 goto nla_put_failure;
3018 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3019 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3020 sinfo->tx_retries))
3021 goto nla_put_failure;
3022 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3023 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3024 sinfo->tx_failed))
3025 goto nla_put_failure;
3026 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3027 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3028 sinfo->beacon_loss_count))
3029 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003030 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3031 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3032 if (!bss_param)
3033 goto nla_put_failure;
3034
David S. Miller9360ffd2012-03-29 04:41:26 -04003035 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3036 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3037 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3038 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3039 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3040 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3041 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3042 sinfo->bss_param.dtim_period) ||
3043 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3044 sinfo->bss_param.beacon_interval))
3045 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003046
3047 nla_nest_end(msg, bss_param);
3048 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003049 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3050 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3051 sizeof(struct nl80211_sta_flag_update),
3052 &sinfo->sta_flags))
3053 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003054 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3055 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3056 sinfo->t_offset))
3057 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003058 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003059
David S. Miller9360ffd2012-03-29 04:41:26 -04003060 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3061 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3062 sinfo->assoc_req_ies))
3063 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003064
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003065 return genlmsg_end(msg, hdr);
3066
3067 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003068 genlmsg_cancel(msg, hdr);
3069 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003070}
3071
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003072static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003073 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003074{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003075 struct station_info sinfo;
3076 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003077 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003078 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003079 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003080 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003081
Johannes Berg67748892010-10-04 21:14:06 +02003082 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3083 if (err)
3084 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003085
3086 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003087 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003088 goto out_err;
3089 }
3090
Johannes Bergbba95fe2008-07-29 13:22:51 +02003091 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003092 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003093 err = rdev_dump_station(dev, netdev, sta_idx,
3094 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003095 if (err == -ENOENT)
3096 break;
3097 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003098 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003099
3100 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003101 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003102 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003103 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003104 &sinfo) < 0)
3105 goto out;
3106
3107 sta_idx++;
3108 }
3109
3110
3111 out:
3112 cb->args[1] = sta_idx;
3113 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003114 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003115 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003116
3117 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003118}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003119
Johannes Berg5727ef12007-12-19 02:03:34 +01003120static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3121{
Johannes Berg4c476992010-10-04 21:36:35 +02003122 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3123 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003124 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003125 struct sk_buff *msg;
3126 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003127 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003128
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003129 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003130
3131 if (!info->attrs[NL80211_ATTR_MAC])
3132 return -EINVAL;
3133
3134 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3135
Johannes Berg4c476992010-10-04 21:36:35 +02003136 if (!rdev->ops->get_station)
3137 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003138
Hila Gonene35e4d22012-06-27 17:19:42 +03003139 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003140 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003141 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003142
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003143 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003144 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003145 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003146
Eric W. Biederman15e47302012-09-07 20:12:54 +00003147 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003148 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003149 nlmsg_free(msg);
3150 return -ENOBUFS;
3151 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003152
Johannes Berg4c476992010-10-04 21:36:35 +02003153 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003154}
3155
3156/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003157 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003158 */
Johannes Berg80b99892011-11-18 16:23:01 +01003159static struct net_device *get_vlan(struct genl_info *info,
3160 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003161{
Johannes Berg463d0182009-07-14 00:33:35 +02003162 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003163 struct net_device *v;
3164 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003165
Johannes Berg80b99892011-11-18 16:23:01 +01003166 if (!vlanattr)
3167 return NULL;
3168
3169 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3170 if (!v)
3171 return ERR_PTR(-ENODEV);
3172
3173 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3174 ret = -EINVAL;
3175 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003176 }
Johannes Berg80b99892011-11-18 16:23:01 +01003177
3178 if (!netif_running(v)) {
3179 ret = -ENETDOWN;
3180 goto error;
3181 }
3182
3183 return v;
3184 error:
3185 dev_put(v);
3186 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003187}
3188
3189static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3190{
Johannes Berg4c476992010-10-04 21:36:35 +02003191 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003192 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003193 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003194 struct station_parameters params;
3195 u8 *mac_addr = NULL;
3196
3197 memset(&params, 0, sizeof(params));
3198
3199 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003200 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003201
3202 if (info->attrs[NL80211_ATTR_STA_AID])
3203 return -EINVAL;
3204
3205 if (!info->attrs[NL80211_ATTR_MAC])
3206 return -EINVAL;
3207
3208 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3209
3210 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3211 params.supported_rates =
3212 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3213 params.supported_rates_len =
3214 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3215 }
3216
3217 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3218 params.listen_interval =
3219 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3220
Jouni Malinen36aedc902008-08-25 11:58:58 +03003221 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3222 params.ht_capa =
3223 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3224
Johannes Bergbdd90d52011-12-14 12:20:27 +01003225 if (!rdev->ops->change_station)
3226 return -EOPNOTSUPP;
3227
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003228 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003229 return -EINVAL;
3230
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003231 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3232 params.plink_action =
3233 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3234
Javier Cardona9c3990a2011-05-03 16:57:11 -07003235 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3236 params.plink_state =
3237 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3238
Johannes Berga97f4422009-06-18 17:23:43 +02003239 switch (dev->ieee80211_ptr->iftype) {
3240 case NL80211_IFTYPE_AP:
3241 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003242 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003243 /* disallow mesh-specific things */
3244 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003245 return -EINVAL;
3246
3247 /* TDLS can't be set, ... */
3248 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3249 return -EINVAL;
3250 /*
3251 * ... but don't bother the driver with it. This works around
3252 * a hostapd/wpa_supplicant issue -- it always includes the
3253 * TLDS_PEER flag in the mask even for AP mode.
3254 */
3255 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3256
3257 /* accept only the listed bits */
3258 if (params.sta_flags_mask &
3259 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3260 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3261 BIT(NL80211_STA_FLAG_WME) |
3262 BIT(NL80211_STA_FLAG_MFP)))
3263 return -EINVAL;
3264
3265 /* must be last in here for error handling */
3266 params.vlan = get_vlan(info, rdev);
3267 if (IS_ERR(params.vlan))
3268 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003269 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003270 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003271 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003272 /*
3273 * Don't allow userspace to change the TDLS_PEER flag,
3274 * but silently ignore attempts to change it since we
3275 * don't have state here to verify that it doesn't try
3276 * to change the flag.
3277 */
3278 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003279 /* fall through */
3280 case NL80211_IFTYPE_ADHOC:
3281 /* disallow things sta doesn't support */
3282 if (params.plink_action)
3283 return -EINVAL;
3284 if (params.ht_capa)
3285 return -EINVAL;
3286 if (params.listen_interval >= 0)
3287 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003288 /* reject any changes other than AUTHORIZED */
3289 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3290 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003291 break;
3292 case NL80211_IFTYPE_MESH_POINT:
3293 /* disallow things mesh doesn't support */
3294 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003295 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003296 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003297 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003298 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003299 return -EINVAL;
3300 /*
3301 * No special handling for TDLS here -- the userspace
3302 * mesh code doesn't have this bug.
3303 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003304 if (params.sta_flags_mask &
3305 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003306 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003307 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003308 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003309 break;
3310 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003311 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003312 }
3313
Johannes Bergbdd90d52011-12-14 12:20:27 +01003314 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003315
Hila Gonene35e4d22012-06-27 17:19:42 +03003316 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003317
Johannes Berg5727ef12007-12-19 02:03:34 +01003318 if (params.vlan)
3319 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003320
Johannes Berg5727ef12007-12-19 02:03:34 +01003321 return err;
3322}
3323
Eliad Pellerc75786c2011-08-23 14:37:46 +03003324static struct nla_policy
3325nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3326 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3327 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3328};
3329
Johannes Berg5727ef12007-12-19 02:03:34 +01003330static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3331{
Johannes Berg4c476992010-10-04 21:36:35 +02003332 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003333 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003334 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003335 struct station_parameters params;
3336 u8 *mac_addr = NULL;
3337
3338 memset(&params, 0, sizeof(params));
3339
3340 if (!info->attrs[NL80211_ATTR_MAC])
3341 return -EINVAL;
3342
Johannes Berg5727ef12007-12-19 02:03:34 +01003343 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3344 return -EINVAL;
3345
3346 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3347 return -EINVAL;
3348
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003349 if (!info->attrs[NL80211_ATTR_STA_AID])
3350 return -EINVAL;
3351
Johannes Berg5727ef12007-12-19 02:03:34 +01003352 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3353 params.supported_rates =
3354 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3355 params.supported_rates_len =
3356 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3357 params.listen_interval =
3358 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003359
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003360 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3361 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3362 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003363
Jouni Malinen36aedc902008-08-25 11:58:58 +03003364 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3365 params.ht_capa =
3366 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003367
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003368 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3369 params.vht_capa =
3370 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3371
Javier Cardona96b78df2011-04-07 15:08:33 -07003372 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3373 params.plink_action =
3374 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3375
Johannes Bergbdd90d52011-12-14 12:20:27 +01003376 if (!rdev->ops->add_station)
3377 return -EOPNOTSUPP;
3378
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003379 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003380 return -EINVAL;
3381
Johannes Bergbdd90d52011-12-14 12:20:27 +01003382 switch (dev->ieee80211_ptr->iftype) {
3383 case NL80211_IFTYPE_AP:
3384 case NL80211_IFTYPE_AP_VLAN:
3385 case NL80211_IFTYPE_P2P_GO:
3386 /* parse WME attributes if sta is WME capable */
3387 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3388 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3389 info->attrs[NL80211_ATTR_STA_WME]) {
3390 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3391 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003392
Johannes Bergbdd90d52011-12-14 12:20:27 +01003393 nla = info->attrs[NL80211_ATTR_STA_WME];
3394 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3395 nl80211_sta_wme_policy);
3396 if (err)
3397 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003398
Johannes Bergbdd90d52011-12-14 12:20:27 +01003399 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3400 params.uapsd_queues =
3401 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3402 if (params.uapsd_queues &
3403 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3404 return -EINVAL;
3405
3406 if (tb[NL80211_STA_WME_MAX_SP])
3407 params.max_sp =
3408 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3409
3410 if (params.max_sp &
3411 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3412 return -EINVAL;
3413
3414 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3415 }
3416 /* TDLS peers cannot be added */
3417 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003418 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003419 /* but don't bother the driver with it */
3420 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003421
Johannes Bergbdd90d52011-12-14 12:20:27 +01003422 /* must be last in here for error handling */
3423 params.vlan = get_vlan(info, rdev);
3424 if (IS_ERR(params.vlan))
3425 return PTR_ERR(params.vlan);
3426 break;
3427 case NL80211_IFTYPE_MESH_POINT:
3428 /* TDLS peers cannot be added */
3429 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003430 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003431 break;
3432 case NL80211_IFTYPE_STATION:
3433 /* Only TDLS peers can be added */
3434 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3435 return -EINVAL;
3436 /* Can only add if TDLS ... */
3437 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3438 return -EOPNOTSUPP;
3439 /* ... with external setup is supported */
3440 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3441 return -EOPNOTSUPP;
3442 break;
3443 default:
3444 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003445 }
3446
Johannes Bergbdd90d52011-12-14 12:20:27 +01003447 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003448
Hila Gonene35e4d22012-06-27 17:19:42 +03003449 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003450
Johannes Berg5727ef12007-12-19 02:03:34 +01003451 if (params.vlan)
3452 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003453 return err;
3454}
3455
3456static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3457{
Johannes Berg4c476992010-10-04 21:36:35 +02003458 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3459 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003460 u8 *mac_addr = NULL;
3461
3462 if (info->attrs[NL80211_ATTR_MAC])
3463 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3464
Johannes Berge80cf852009-05-11 14:43:13 +02003465 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003466 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003467 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003468 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3469 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003470
Johannes Berg4c476992010-10-04 21:36:35 +02003471 if (!rdev->ops->del_station)
3472 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003473
Hila Gonene35e4d22012-06-27 17:19:42 +03003474 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003475}
3476
Eric W. Biederman15e47302012-09-07 20:12:54 +00003477static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003478 int flags, struct net_device *dev,
3479 u8 *dst, u8 *next_hop,
3480 struct mpath_info *pinfo)
3481{
3482 void *hdr;
3483 struct nlattr *pinfoattr;
3484
Eric W. Biederman15e47302012-09-07 20:12:54 +00003485 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003486 if (!hdr)
3487 return -1;
3488
David S. Miller9360ffd2012-03-29 04:41:26 -04003489 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3490 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3491 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3492 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3493 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003494
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003495 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3496 if (!pinfoattr)
3497 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003498 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3499 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3500 pinfo->frame_qlen))
3501 goto nla_put_failure;
3502 if (((pinfo->filled & MPATH_INFO_SN) &&
3503 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3504 ((pinfo->filled & MPATH_INFO_METRIC) &&
3505 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3506 pinfo->metric)) ||
3507 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3508 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3509 pinfo->exptime)) ||
3510 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3511 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3512 pinfo->flags)) ||
3513 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3514 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3515 pinfo->discovery_timeout)) ||
3516 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3517 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3518 pinfo->discovery_retries)))
3519 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003520
3521 nla_nest_end(msg, pinfoattr);
3522
3523 return genlmsg_end(msg, hdr);
3524
3525 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003526 genlmsg_cancel(msg, hdr);
3527 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003528}
3529
3530static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003531 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003532{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003533 struct mpath_info pinfo;
3534 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003535 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003536 u8 dst[ETH_ALEN];
3537 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003538 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003539 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003540
Johannes Berg67748892010-10-04 21:14:06 +02003541 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3542 if (err)
3543 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003544
3545 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003546 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003547 goto out_err;
3548 }
3549
Jouni Malineneec60b02009-03-20 21:21:19 +02003550 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3551 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003552 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003553 }
3554
Johannes Bergbba95fe2008-07-29 13:22:51 +02003555 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003556 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3557 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003558 if (err == -ENOENT)
3559 break;
3560 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003561 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003562
Eric W. Biederman15e47302012-09-07 20:12:54 +00003563 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003564 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3565 netdev, dst, next_hop,
3566 &pinfo) < 0)
3567 goto out;
3568
3569 path_idx++;
3570 }
3571
3572
3573 out:
3574 cb->args[1] = path_idx;
3575 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003576 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003577 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003578 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003579}
3580
3581static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3582{
Johannes Berg4c476992010-10-04 21:36:35 +02003583 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003584 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003585 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003586 struct mpath_info pinfo;
3587 struct sk_buff *msg;
3588 u8 *dst = NULL;
3589 u8 next_hop[ETH_ALEN];
3590
3591 memset(&pinfo, 0, sizeof(pinfo));
3592
3593 if (!info->attrs[NL80211_ATTR_MAC])
3594 return -EINVAL;
3595
3596 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3597
Johannes Berg4c476992010-10-04 21:36:35 +02003598 if (!rdev->ops->get_mpath)
3599 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003600
Johannes Berg4c476992010-10-04 21:36:35 +02003601 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3602 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003603
Hila Gonene35e4d22012-06-27 17:19:42 +03003604 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003605 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003606 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003607
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003608 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003609 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003610 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003611
Eric W. Biederman15e47302012-09-07 20:12:54 +00003612 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003613 dev, dst, next_hop, &pinfo) < 0) {
3614 nlmsg_free(msg);
3615 return -ENOBUFS;
3616 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003617
Johannes Berg4c476992010-10-04 21:36:35 +02003618 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003619}
3620
3621static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3622{
Johannes Berg4c476992010-10-04 21:36:35 +02003623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3624 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003625 u8 *dst = NULL;
3626 u8 *next_hop = NULL;
3627
3628 if (!info->attrs[NL80211_ATTR_MAC])
3629 return -EINVAL;
3630
3631 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3632 return -EINVAL;
3633
3634 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3635 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3636
Johannes Berg4c476992010-10-04 21:36:35 +02003637 if (!rdev->ops->change_mpath)
3638 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003639
Johannes Berg4c476992010-10-04 21:36:35 +02003640 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3641 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003642
Hila Gonene35e4d22012-06-27 17:19:42 +03003643 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003644}
Johannes Berg4c476992010-10-04 21:36:35 +02003645
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003646static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3647{
Johannes Berg4c476992010-10-04 21:36:35 +02003648 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3649 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003650 u8 *dst = NULL;
3651 u8 *next_hop = NULL;
3652
3653 if (!info->attrs[NL80211_ATTR_MAC])
3654 return -EINVAL;
3655
3656 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3657 return -EINVAL;
3658
3659 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3660 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3661
Johannes Berg4c476992010-10-04 21:36:35 +02003662 if (!rdev->ops->add_mpath)
3663 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003664
Johannes Berg4c476992010-10-04 21:36:35 +02003665 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3666 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003667
Hila Gonene35e4d22012-06-27 17:19:42 +03003668 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003669}
3670
3671static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3672{
Johannes Berg4c476992010-10-04 21:36:35 +02003673 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3674 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003675 u8 *dst = NULL;
3676
3677 if (info->attrs[NL80211_ATTR_MAC])
3678 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3679
Johannes Berg4c476992010-10-04 21:36:35 +02003680 if (!rdev->ops->del_mpath)
3681 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003682
Hila Gonene35e4d22012-06-27 17:19:42 +03003683 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003684}
3685
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003686static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3687{
Johannes Berg4c476992010-10-04 21:36:35 +02003688 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3689 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003690 struct bss_parameters params;
3691
3692 memset(&params, 0, sizeof(params));
3693 /* default to not changing parameters */
3694 params.use_cts_prot = -1;
3695 params.use_short_preamble = -1;
3696 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003697 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003698 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01003699 params.p2p_ctwindow = -1;
3700 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003701
3702 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3703 params.use_cts_prot =
3704 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3705 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3706 params.use_short_preamble =
3707 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3708 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3709 params.use_short_slot_time =
3710 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003711 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3712 params.basic_rates =
3713 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3714 params.basic_rates_len =
3715 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3716 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003717 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3718 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003719 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3720 params.ht_opmode =
3721 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003722
Johannes Berg53cabad2012-11-14 15:17:28 +01003723 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3724 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3725 return -EINVAL;
3726 params.p2p_ctwindow =
3727 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3728 if (params.p2p_ctwindow < 0)
3729 return -EINVAL;
3730 if (params.p2p_ctwindow != 0 &&
3731 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3732 return -EINVAL;
3733 }
3734
3735 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3736 u8 tmp;
3737
3738 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3739 return -EINVAL;
3740 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3741 if (tmp > 1)
3742 return -EINVAL;
3743 params.p2p_opp_ps = tmp;
3744 if (params.p2p_opp_ps &&
3745 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3746 return -EINVAL;
3747 }
3748
Johannes Berg4c476992010-10-04 21:36:35 +02003749 if (!rdev->ops->change_bss)
3750 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003751
Johannes Berg074ac8d2010-09-16 14:58:22 +02003752 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003753 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3754 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003755
Hila Gonene35e4d22012-06-27 17:19:42 +03003756 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003757}
3758
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003759static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003760 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3761 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3762 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3763 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3764 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3765 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3766};
3767
3768static int parse_reg_rule(struct nlattr *tb[],
3769 struct ieee80211_reg_rule *reg_rule)
3770{
3771 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3772 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3773
3774 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3775 return -EINVAL;
3776 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3777 return -EINVAL;
3778 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3779 return -EINVAL;
3780 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3781 return -EINVAL;
3782 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3783 return -EINVAL;
3784
3785 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3786
3787 freq_range->start_freq_khz =
3788 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3789 freq_range->end_freq_khz =
3790 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3791 freq_range->max_bandwidth_khz =
3792 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3793
3794 power_rule->max_eirp =
3795 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3796
3797 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3798 power_rule->max_antenna_gain =
3799 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3800
3801 return 0;
3802}
3803
3804static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3805{
3806 int r;
3807 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003808 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003809
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003810 /*
3811 * You should only get this when cfg80211 hasn't yet initialized
3812 * completely when built-in to the kernel right between the time
3813 * window between nl80211_init() and regulatory_init(), if that is
3814 * even possible.
3815 */
3816 mutex_lock(&cfg80211_mutex);
3817 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003818 mutex_unlock(&cfg80211_mutex);
3819 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003820 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003821 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003822
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003823 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3824 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003825
3826 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3827
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003828 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3829 user_reg_hint_type =
3830 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3831 else
3832 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3833
3834 switch (user_reg_hint_type) {
3835 case NL80211_USER_REG_HINT_USER:
3836 case NL80211_USER_REG_HINT_CELL_BASE:
3837 break;
3838 default:
3839 return -EINVAL;
3840 }
3841
3842 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003843
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003844 return r;
3845}
3846
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003847static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003848 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003849{
Johannes Berg4c476992010-10-04 21:36:35 +02003850 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003851 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003852 struct wireless_dev *wdev = dev->ieee80211_ptr;
3853 struct mesh_config cur_params;
3854 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003855 void *hdr;
3856 struct nlattr *pinfoattr;
3857 struct sk_buff *msg;
3858
Johannes Berg29cbe682010-12-03 09:20:44 +01003859 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3860 return -EOPNOTSUPP;
3861
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003862 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003863 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003864
Johannes Berg29cbe682010-12-03 09:20:44 +01003865 wdev_lock(wdev);
3866 /* If not connected, get default parameters */
3867 if (!wdev->mesh_id_len)
3868 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3869 else
Hila Gonene35e4d22012-06-27 17:19:42 +03003870 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01003871 wdev_unlock(wdev);
3872
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003873 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003874 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003875
3876 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003877 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003878 if (!msg)
3879 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003880 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003881 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003882 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003883 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003884 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003885 if (!pinfoattr)
3886 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003887 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3888 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3889 cur_params.dot11MeshRetryTimeout) ||
3890 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3891 cur_params.dot11MeshConfirmTimeout) ||
3892 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3893 cur_params.dot11MeshHoldingTimeout) ||
3894 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3895 cur_params.dot11MeshMaxPeerLinks) ||
3896 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3897 cur_params.dot11MeshMaxRetries) ||
3898 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3899 cur_params.dot11MeshTTL) ||
3900 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3901 cur_params.element_ttl) ||
3902 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3903 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003904 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3905 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003906 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3907 cur_params.dot11MeshHWMPmaxPREQretries) ||
3908 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3909 cur_params.path_refresh_time) ||
3910 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3911 cur_params.min_discovery_timeout) ||
3912 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3913 cur_params.dot11MeshHWMPactivePathTimeout) ||
3914 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3915 cur_params.dot11MeshHWMPpreqMinInterval) ||
3916 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3917 cur_params.dot11MeshHWMPperrMinInterval) ||
3918 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3919 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3920 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3921 cur_params.dot11MeshHWMPRootMode) ||
3922 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3923 cur_params.dot11MeshHWMPRannInterval) ||
3924 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3925 cur_params.dot11MeshGateAnnouncementProtocol) ||
3926 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3927 cur_params.dot11MeshForwarding) ||
3928 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003929 cur_params.rssi_threshold) ||
3930 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003931 cur_params.ht_opmode) ||
3932 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3933 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3934 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003935 cur_params.dot11MeshHWMProotInterval) ||
3936 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3937 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003938 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003939 nla_nest_end(msg, pinfoattr);
3940 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003941 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003942
Johannes Berg3b858752009-03-12 09:55:09 +01003943 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003944 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003945 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003946 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003947 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003948}
3949
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003950static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003951 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3952 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3953 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3954 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3955 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3956 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003957 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003958 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003959 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003960 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3961 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3962 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3963 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3964 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003965 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003966 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003967 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003968 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003969 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003970 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003971 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3972 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003973 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3974 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003975 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003976};
3977
Javier Cardonac80d5452010-12-16 17:37:49 -08003978static const struct nla_policy
3979 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003980 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003981 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3982 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003983 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003984 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003985 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003986 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003987};
3988
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003989static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003990 struct mesh_config *cfg,
3991 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003992{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003993 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003994 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003995
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003996#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3997do {\
3998 if (table[attr_num]) {\
3999 cfg->param = nla_fn(table[attr_num]); \
4000 mask |= (1 << (attr_num - 1)); \
4001 } \
4002} while (0);\
4003
4004
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004005 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004006 return -EINVAL;
4007 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004008 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004009 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004010 return -EINVAL;
4011
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004012 /* This makes sure that there aren't more than 32 mesh config
4013 * parameters (otherwise our bitfield scheme would not work.) */
4014 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4015
4016 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004017 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004018 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4019 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004020 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004021 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4022 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004023 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004024 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4025 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004026 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004027 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4028 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004029 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004030 mask, NL80211_MESHCONF_MAX_RETRIES,
4031 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004032 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004033 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01004034 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004035 mask, NL80211_MESHCONF_ELEMENT_TTL,
4036 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004037 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004038 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4039 nla_get_u8);
4040 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
4041 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4042 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004043 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004044 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4045 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004046 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004047 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4048 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004049 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004050 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4051 nla_get_u16);
4052 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
4053 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4054 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004055 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004056 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4057 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004058 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004059 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4060 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004061 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004062 dot11MeshHWMPnetDiameterTraversalTime, mask,
4063 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4064 nla_get_u16);
4065 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
4066 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
4067 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
4068 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4069 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004070 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004071 dot11MeshGateAnnouncementProtocol, mask,
4072 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4073 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004074 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004075 mask, NL80211_MESHCONF_FORWARDING,
4076 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08004077 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004078 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4079 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004080 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004081 mask, NL80211_MESHCONF_HT_OPMODE,
4082 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004083 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
4084 mask,
4085 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4086 nla_get_u32);
4087 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
4088 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4089 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004090 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
4091 dot11MeshHWMPconfirmationInterval, mask,
4092 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4093 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004094 if (mask_out)
4095 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004096
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004097 return 0;
4098
4099#undef FILL_IN_MESH_PARAM_IF_SET
4100}
4101
Javier Cardonac80d5452010-12-16 17:37:49 -08004102static int nl80211_parse_mesh_setup(struct genl_info *info,
4103 struct mesh_setup *setup)
4104{
4105 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4106
4107 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4108 return -EINVAL;
4109 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4110 info->attrs[NL80211_ATTR_MESH_SETUP],
4111 nl80211_mesh_setup_params_policy))
4112 return -EINVAL;
4113
Javier Cardonad299a1f2012-03-31 11:31:33 -07004114 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4115 setup->sync_method =
4116 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4117 IEEE80211_SYNC_METHOD_VENDOR :
4118 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4119
Javier Cardonac80d5452010-12-16 17:37:49 -08004120 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4121 setup->path_sel_proto =
4122 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4123 IEEE80211_PATH_PROTOCOL_VENDOR :
4124 IEEE80211_PATH_PROTOCOL_HWMP;
4125
4126 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4127 setup->path_metric =
4128 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4129 IEEE80211_PATH_METRIC_VENDOR :
4130 IEEE80211_PATH_METRIC_AIRTIME;
4131
Javier Cardona581a8b02011-04-07 15:08:27 -07004132
4133 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004134 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004135 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004136 if (!is_valid_ie_attr(ieattr))
4137 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004138 setup->ie = nla_data(ieattr);
4139 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004140 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004141 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4142 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004143
4144 return 0;
4145}
4146
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004147static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004148 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004149{
4150 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4151 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004152 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004153 struct mesh_config cfg;
4154 u32 mask;
4155 int err;
4156
Johannes Berg29cbe682010-12-03 09:20:44 +01004157 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4158 return -EOPNOTSUPP;
4159
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004160 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004161 return -EOPNOTSUPP;
4162
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004163 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004164 if (err)
4165 return err;
4166
Johannes Berg29cbe682010-12-03 09:20:44 +01004167 wdev_lock(wdev);
4168 if (!wdev->mesh_id_len)
4169 err = -ENOLINK;
4170
4171 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004172 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004173
4174 wdev_unlock(wdev);
4175
4176 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004177}
4178
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004179static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4180{
4181 struct sk_buff *msg;
4182 void *hdr = NULL;
4183 struct nlattr *nl_reg_rules;
4184 unsigned int i;
4185 int err = -EINVAL;
4186
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004187 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004188
4189 if (!cfg80211_regdomain)
4190 goto out;
4191
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004192 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004193 if (!msg) {
4194 err = -ENOBUFS;
4195 goto out;
4196 }
4197
Eric W. Biederman15e47302012-09-07 20:12:54 +00004198 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004199 NL80211_CMD_GET_REG);
4200 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004201 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004202
David S. Miller9360ffd2012-03-29 04:41:26 -04004203 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4204 cfg80211_regdomain->alpha2) ||
4205 (cfg80211_regdomain->dfs_region &&
4206 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4207 cfg80211_regdomain->dfs_region)))
4208 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004209
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004210 if (reg_last_request_cell_base() &&
4211 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4212 NL80211_USER_REG_HINT_CELL_BASE))
4213 goto nla_put_failure;
4214
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004215 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4216 if (!nl_reg_rules)
4217 goto nla_put_failure;
4218
4219 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4220 struct nlattr *nl_reg_rule;
4221 const struct ieee80211_reg_rule *reg_rule;
4222 const struct ieee80211_freq_range *freq_range;
4223 const struct ieee80211_power_rule *power_rule;
4224
4225 reg_rule = &cfg80211_regdomain->reg_rules[i];
4226 freq_range = &reg_rule->freq_range;
4227 power_rule = &reg_rule->power_rule;
4228
4229 nl_reg_rule = nla_nest_start(msg, i);
4230 if (!nl_reg_rule)
4231 goto nla_put_failure;
4232
David S. Miller9360ffd2012-03-29 04:41:26 -04004233 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4234 reg_rule->flags) ||
4235 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4236 freq_range->start_freq_khz) ||
4237 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4238 freq_range->end_freq_khz) ||
4239 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4240 freq_range->max_bandwidth_khz) ||
4241 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4242 power_rule->max_antenna_gain) ||
4243 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4244 power_rule->max_eirp))
4245 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004246
4247 nla_nest_end(msg, nl_reg_rule);
4248 }
4249
4250 nla_nest_end(msg, nl_reg_rules);
4251
4252 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004253 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004254 goto out;
4255
4256nla_put_failure:
4257 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004258put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004259 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004260 err = -EMSGSIZE;
4261out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004262 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004263 return err;
4264}
4265
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004266static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4267{
4268 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4269 struct nlattr *nl_reg_rule;
4270 char *alpha2 = NULL;
4271 int rem_reg_rules = 0, r = 0;
4272 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004273 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004274 struct ieee80211_regdomain *rd = NULL;
4275
4276 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4277 return -EINVAL;
4278
4279 if (!info->attrs[NL80211_ATTR_REG_RULES])
4280 return -EINVAL;
4281
4282 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4283
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004284 if (info->attrs[NL80211_ATTR_DFS_REGION])
4285 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4286
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004287 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4288 rem_reg_rules) {
4289 num_rules++;
4290 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004291 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004292 }
4293
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004294 mutex_lock(&cfg80211_mutex);
4295
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004296 if (!reg_is_valid_request(alpha2)) {
4297 r = -EINVAL;
4298 goto bad_reg;
4299 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004300
4301 size_of_regd = sizeof(struct ieee80211_regdomain) +
4302 (num_rules * sizeof(struct ieee80211_reg_rule));
4303
4304 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004305 if (!rd) {
4306 r = -ENOMEM;
4307 goto bad_reg;
4308 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004309
4310 rd->n_reg_rules = num_rules;
4311 rd->alpha2[0] = alpha2[0];
4312 rd->alpha2[1] = alpha2[1];
4313
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004314 /*
4315 * Disable DFS master mode if the DFS region was
4316 * not supported or known on this kernel.
4317 */
4318 if (reg_supported_dfs_region(dfs_region))
4319 rd->dfs_region = dfs_region;
4320
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004321 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4322 rem_reg_rules) {
4323 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4324 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4325 reg_rule_policy);
4326 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4327 if (r)
4328 goto bad_reg;
4329
4330 rule_idx++;
4331
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004332 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4333 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004334 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004335 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004336 }
4337
4338 BUG_ON(rule_idx != num_rules);
4339
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004340 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004341
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004342 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004343
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004344 return r;
4345
Johannes Bergd2372b32008-10-24 20:32:20 +02004346 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004347 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004348 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004349 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004350}
4351
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004352static int validate_scan_freqs(struct nlattr *freqs)
4353{
4354 struct nlattr *attr1, *attr2;
4355 int n_channels = 0, tmp1, tmp2;
4356
4357 nla_for_each_nested(attr1, freqs, tmp1) {
4358 n_channels++;
4359 /*
4360 * Some hardware has a limited channel list for
4361 * scanning, and it is pretty much nonsensical
4362 * to scan for a channel twice, so disallow that
4363 * and don't require drivers to check that the
4364 * channel list they get isn't longer than what
4365 * they can scan, as long as they can scan all
4366 * the channels they registered at once.
4367 */
4368 nla_for_each_nested(attr2, freqs, tmp2)
4369 if (attr1 != attr2 &&
4370 nla_get_u32(attr1) == nla_get_u32(attr2))
4371 return 0;
4372 }
4373
4374 return n_channels;
4375}
4376
Johannes Berg2a519312009-02-10 21:25:55 +01004377static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4378{
Johannes Berg4c476992010-10-04 21:36:35 +02004379 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004380 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004381 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004382 struct nlattr *attr;
4383 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004384 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004385 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004386
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004387 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4388 return -EINVAL;
4389
Johannes Berg79c97e92009-07-07 03:56:12 +02004390 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004391
Johannes Berg4c476992010-10-04 21:36:35 +02004392 if (!rdev->ops->scan)
4393 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004394
Johannes Berg4c476992010-10-04 21:36:35 +02004395 if (rdev->scan_req)
4396 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004397
4398 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004399 n_channels = validate_scan_freqs(
4400 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004401 if (!n_channels)
4402 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004403 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004404 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004405 n_channels = 0;
4406
Johannes Berg2a519312009-02-10 21:25:55 +01004407 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4408 if (wiphy->bands[band])
4409 n_channels += wiphy->bands[band]->n_channels;
4410 }
4411
4412 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4413 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4414 n_ssids++;
4415
Johannes Berg4c476992010-10-04 21:36:35 +02004416 if (n_ssids > wiphy->max_scan_ssids)
4417 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004418
Jouni Malinen70692ad2009-02-16 19:39:13 +02004419 if (info->attrs[NL80211_ATTR_IE])
4420 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4421 else
4422 ie_len = 0;
4423
Johannes Berg4c476992010-10-04 21:36:35 +02004424 if (ie_len > wiphy->max_scan_ie_len)
4425 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004426
Johannes Berg2a519312009-02-10 21:25:55 +01004427 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004428 + sizeof(*request->ssids) * n_ssids
4429 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004430 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004431 if (!request)
4432 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004433
Johannes Berg2a519312009-02-10 21:25:55 +01004434 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004435 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004436 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004437 if (ie_len) {
4438 if (request->ssids)
4439 request->ie = (void *)(request->ssids + n_ssids);
4440 else
4441 request->ie = (void *)(request->channels + n_channels);
4442 }
Johannes Berg2a519312009-02-10 21:25:55 +01004443
Johannes Berg584991d2009-11-02 13:32:03 +01004444 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004445 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4446 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004447 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004448 struct ieee80211_channel *chan;
4449
4450 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4451
4452 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004453 err = -EINVAL;
4454 goto out_free;
4455 }
Johannes Berg584991d2009-11-02 13:32:03 +01004456
4457 /* ignore disabled channels */
4458 if (chan->flags & IEEE80211_CHAN_DISABLED)
4459 continue;
4460
4461 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004462 i++;
4463 }
4464 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004465 enum ieee80211_band band;
4466
Johannes Berg2a519312009-02-10 21:25:55 +01004467 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004468 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4469 int j;
4470 if (!wiphy->bands[band])
4471 continue;
4472 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004473 struct ieee80211_channel *chan;
4474
4475 chan = &wiphy->bands[band]->channels[j];
4476
4477 if (chan->flags & IEEE80211_CHAN_DISABLED)
4478 continue;
4479
4480 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004481 i++;
4482 }
4483 }
4484 }
4485
Johannes Berg584991d2009-11-02 13:32:03 +01004486 if (!i) {
4487 err = -EINVAL;
4488 goto out_free;
4489 }
4490
4491 request->n_channels = i;
4492
Johannes Berg2a519312009-02-10 21:25:55 +01004493 i = 0;
4494 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4495 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004496 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004497 err = -EINVAL;
4498 goto out_free;
4499 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004500 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004501 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004502 i++;
4503 }
4504 }
4505
Jouni Malinen70692ad2009-02-16 19:39:13 +02004506 if (info->attrs[NL80211_ATTR_IE]) {
4507 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004508 memcpy((void *)request->ie,
4509 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004510 request->ie_len);
4511 }
4512
Johannes Berg34850ab2011-07-18 18:08:35 +02004513 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004514 if (wiphy->bands[i])
4515 request->rates[i] =
4516 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004517
4518 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4519 nla_for_each_nested(attr,
4520 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4521 tmp) {
4522 enum ieee80211_band band = nla_type(attr);
4523
Dan Carpenter84404622011-07-29 11:52:18 +03004524 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004525 err = -EINVAL;
4526 goto out_free;
4527 }
4528 err = ieee80211_get_ratemask(wiphy->bands[band],
4529 nla_data(attr),
4530 nla_len(attr),
4531 &request->rates[band]);
4532 if (err)
4533 goto out_free;
4534 }
4535 }
4536
Sam Leffler46856bb2012-10-11 21:03:32 -07004537 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004538 request->flags = nla_get_u32(
4539 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004540 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4541 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4542 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4543 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004544 err = -EOPNOTSUPP;
4545 goto out_free;
4546 }
4547 }
Sam Lefflered4737712012-10-11 21:03:31 -07004548
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304549 request->no_cck =
4550 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4551
Johannes Bergfd014282012-06-18 19:17:03 +02004552 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004553 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004554 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004555
Johannes Berg79c97e92009-07-07 03:56:12 +02004556 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004557 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004558
Johannes Berg463d0182009-07-14 00:33:35 +02004559 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004560 nl80211_send_scan_start(rdev, wdev);
4561 if (wdev->netdev)
4562 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004563 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004564 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004565 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004566 kfree(request);
4567 }
Johannes Berg3b858752009-03-12 09:55:09 +01004568
Johannes Berg2a519312009-02-10 21:25:55 +01004569 return err;
4570}
4571
Luciano Coelho807f8a82011-05-11 17:09:35 +03004572static int nl80211_start_sched_scan(struct sk_buff *skb,
4573 struct genl_info *info)
4574{
4575 struct cfg80211_sched_scan_request *request;
4576 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4577 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004578 struct nlattr *attr;
4579 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004580 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004581 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004582 enum ieee80211_band band;
4583 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004584 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004585
4586 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4587 !rdev->ops->sched_scan_start)
4588 return -EOPNOTSUPP;
4589
4590 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4591 return -EINVAL;
4592
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004593 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4594 return -EINVAL;
4595
4596 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4597 if (interval == 0)
4598 return -EINVAL;
4599
Luciano Coelho807f8a82011-05-11 17:09:35 +03004600 wiphy = &rdev->wiphy;
4601
4602 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4603 n_channels = validate_scan_freqs(
4604 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4605 if (!n_channels)
4606 return -EINVAL;
4607 } else {
4608 n_channels = 0;
4609
4610 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4611 if (wiphy->bands[band])
4612 n_channels += wiphy->bands[band]->n_channels;
4613 }
4614
4615 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4616 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4617 tmp)
4618 n_ssids++;
4619
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004620 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004621 return -EINVAL;
4622
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004623 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4624 nla_for_each_nested(attr,
4625 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4626 tmp)
4627 n_match_sets++;
4628
4629 if (n_match_sets > wiphy->max_match_sets)
4630 return -EINVAL;
4631
Luciano Coelho807f8a82011-05-11 17:09:35 +03004632 if (info->attrs[NL80211_ATTR_IE])
4633 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4634 else
4635 ie_len = 0;
4636
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004637 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004638 return -EINVAL;
4639
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004640 mutex_lock(&rdev->sched_scan_mtx);
4641
4642 if (rdev->sched_scan_req) {
4643 err = -EINPROGRESS;
4644 goto out;
4645 }
4646
Luciano Coelho807f8a82011-05-11 17:09:35 +03004647 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004648 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004649 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004650 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004651 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004652 if (!request) {
4653 err = -ENOMEM;
4654 goto out;
4655 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004656
4657 if (n_ssids)
4658 request->ssids = (void *)&request->channels[n_channels];
4659 request->n_ssids = n_ssids;
4660 if (ie_len) {
4661 if (request->ssids)
4662 request->ie = (void *)(request->ssids + n_ssids);
4663 else
4664 request->ie = (void *)(request->channels + n_channels);
4665 }
4666
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004667 if (n_match_sets) {
4668 if (request->ie)
4669 request->match_sets = (void *)(request->ie + ie_len);
4670 else if (request->ssids)
4671 request->match_sets =
4672 (void *)(request->ssids + n_ssids);
4673 else
4674 request->match_sets =
4675 (void *)(request->channels + n_channels);
4676 }
4677 request->n_match_sets = n_match_sets;
4678
Luciano Coelho807f8a82011-05-11 17:09:35 +03004679 i = 0;
4680 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4681 /* user specified, bail out if channel not found */
4682 nla_for_each_nested(attr,
4683 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4684 tmp) {
4685 struct ieee80211_channel *chan;
4686
4687 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4688
4689 if (!chan) {
4690 err = -EINVAL;
4691 goto out_free;
4692 }
4693
4694 /* ignore disabled channels */
4695 if (chan->flags & IEEE80211_CHAN_DISABLED)
4696 continue;
4697
4698 request->channels[i] = chan;
4699 i++;
4700 }
4701 } else {
4702 /* all channels */
4703 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4704 int j;
4705 if (!wiphy->bands[band])
4706 continue;
4707 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4708 struct ieee80211_channel *chan;
4709
4710 chan = &wiphy->bands[band]->channels[j];
4711
4712 if (chan->flags & IEEE80211_CHAN_DISABLED)
4713 continue;
4714
4715 request->channels[i] = chan;
4716 i++;
4717 }
4718 }
4719 }
4720
4721 if (!i) {
4722 err = -EINVAL;
4723 goto out_free;
4724 }
4725
4726 request->n_channels = i;
4727
4728 i = 0;
4729 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4730 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4731 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004732 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004733 err = -EINVAL;
4734 goto out_free;
4735 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004736 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004737 memcpy(request->ssids[i].ssid, nla_data(attr),
4738 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004739 i++;
4740 }
4741 }
4742
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004743 i = 0;
4744 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4745 nla_for_each_nested(attr,
4746 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4747 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004748 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004749
4750 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4751 nla_data(attr), nla_len(attr),
4752 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004753 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004754 if (ssid) {
4755 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4756 err = -EINVAL;
4757 goto out_free;
4758 }
4759 memcpy(request->match_sets[i].ssid.ssid,
4760 nla_data(ssid), nla_len(ssid));
4761 request->match_sets[i].ssid.ssid_len =
4762 nla_len(ssid);
4763 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004764 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4765 if (rssi)
4766 request->rssi_thold = nla_get_u32(rssi);
4767 else
4768 request->rssi_thold =
4769 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004770 i++;
4771 }
4772 }
4773
Luciano Coelho807f8a82011-05-11 17:09:35 +03004774 if (info->attrs[NL80211_ATTR_IE]) {
4775 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4776 memcpy((void *)request->ie,
4777 nla_data(info->attrs[NL80211_ATTR_IE]),
4778 request->ie_len);
4779 }
4780
Sam Leffler46856bb2012-10-11 21:03:32 -07004781 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004782 request->flags = nla_get_u32(
4783 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004784 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4785 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4786 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4787 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004788 err = -EOPNOTSUPP;
4789 goto out_free;
4790 }
4791 }
Sam Lefflered4737712012-10-11 21:03:31 -07004792
Luciano Coelho807f8a82011-05-11 17:09:35 +03004793 request->dev = dev;
4794 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004795 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004796 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004797
Hila Gonene35e4d22012-06-27 17:19:42 +03004798 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004799 if (!err) {
4800 rdev->sched_scan_req = request;
4801 nl80211_send_sched_scan(rdev, dev,
4802 NL80211_CMD_START_SCHED_SCAN);
4803 goto out;
4804 }
4805
4806out_free:
4807 kfree(request);
4808out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004809 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004810 return err;
4811}
4812
4813static int nl80211_stop_sched_scan(struct sk_buff *skb,
4814 struct genl_info *info)
4815{
4816 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004817 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004818
4819 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4820 !rdev->ops->sched_scan_stop)
4821 return -EOPNOTSUPP;
4822
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004823 mutex_lock(&rdev->sched_scan_mtx);
4824 err = __cfg80211_stop_sched_scan(rdev, false);
4825 mutex_unlock(&rdev->sched_scan_mtx);
4826
4827 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004828}
4829
Johannes Berg9720bb32011-06-21 09:45:33 +02004830static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4831 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004832 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004833 struct wireless_dev *wdev,
4834 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004835{
Johannes Berg48ab9052009-07-10 18:42:31 +02004836 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004837 void *hdr;
4838 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004839
4840 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004841
Eric W. Biederman15e47302012-09-07 20:12:54 +00004842 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004843 NL80211_CMD_NEW_SCAN_RESULTS);
4844 if (!hdr)
4845 return -1;
4846
Johannes Berg9720bb32011-06-21 09:45:33 +02004847 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4848
David S. Miller9360ffd2012-03-29 04:41:26 -04004849 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4850 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4851 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004852
4853 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4854 if (!bss)
4855 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004856 if ((!is_zero_ether_addr(res->bssid) &&
4857 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4858 (res->information_elements && res->len_information_elements &&
4859 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4860 res->len_information_elements,
4861 res->information_elements)) ||
4862 (res->beacon_ies && res->len_beacon_ies &&
4863 res->beacon_ies != res->information_elements &&
4864 nla_put(msg, NL80211_BSS_BEACON_IES,
4865 res->len_beacon_ies, res->beacon_ies)))
4866 goto nla_put_failure;
4867 if (res->tsf &&
4868 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4869 goto nla_put_failure;
4870 if (res->beacon_interval &&
4871 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4872 goto nla_put_failure;
4873 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4874 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4875 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4876 jiffies_to_msecs(jiffies - intbss->ts)))
4877 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004878
Johannes Berg77965c92009-02-18 18:45:06 +01004879 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004880 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004881 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4882 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004883 break;
4884 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004885 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4886 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004887 break;
4888 default:
4889 break;
4890 }
4891
Johannes Berg48ab9052009-07-10 18:42:31 +02004892 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004893 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004894 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004895 if (intbss == wdev->current_bss &&
4896 nla_put_u32(msg, NL80211_BSS_STATUS,
4897 NL80211_BSS_STATUS_ASSOCIATED))
4898 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004899 break;
4900 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004901 if (intbss == wdev->current_bss &&
4902 nla_put_u32(msg, NL80211_BSS_STATUS,
4903 NL80211_BSS_STATUS_IBSS_JOINED))
4904 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004905 break;
4906 default:
4907 break;
4908 }
4909
Johannes Berg2a519312009-02-10 21:25:55 +01004910 nla_nest_end(msg, bss);
4911
4912 return genlmsg_end(msg, hdr);
4913
4914 nla_put_failure:
4915 genlmsg_cancel(msg, hdr);
4916 return -EMSGSIZE;
4917}
4918
4919static int nl80211_dump_scan(struct sk_buff *skb,
4920 struct netlink_callback *cb)
4921{
Johannes Berg48ab9052009-07-10 18:42:31 +02004922 struct cfg80211_registered_device *rdev;
4923 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004924 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004925 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004926 int start = cb->args[1], idx = 0;
4927 int err;
4928
Johannes Berg67748892010-10-04 21:14:06 +02004929 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4930 if (err)
4931 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004932
Johannes Berg48ab9052009-07-10 18:42:31 +02004933 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004934
Johannes Berg48ab9052009-07-10 18:42:31 +02004935 wdev_lock(wdev);
4936 spin_lock_bh(&rdev->bss_lock);
4937 cfg80211_bss_expire(rdev);
4938
Johannes Berg9720bb32011-06-21 09:45:33 +02004939 cb->seq = rdev->bss_generation;
4940
Johannes Berg48ab9052009-07-10 18:42:31 +02004941 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004942 if (++idx <= start)
4943 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004944 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004945 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004946 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004947 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004948 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004949 }
4950 }
4951
Johannes Berg48ab9052009-07-10 18:42:31 +02004952 spin_unlock_bh(&rdev->bss_lock);
4953 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004954
4955 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004956 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004957
Johannes Berg67748892010-10-04 21:14:06 +02004958 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004959}
4960
Eric W. Biederman15e47302012-09-07 20:12:54 +00004961static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004962 int flags, struct net_device *dev,
4963 struct survey_info *survey)
4964{
4965 void *hdr;
4966 struct nlattr *infoattr;
4967
Eric W. Biederman15e47302012-09-07 20:12:54 +00004968 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004969 NL80211_CMD_NEW_SURVEY_RESULTS);
4970 if (!hdr)
4971 return -ENOMEM;
4972
David S. Miller9360ffd2012-03-29 04:41:26 -04004973 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4974 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004975
4976 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4977 if (!infoattr)
4978 goto nla_put_failure;
4979
David S. Miller9360ffd2012-03-29 04:41:26 -04004980 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4981 survey->channel->center_freq))
4982 goto nla_put_failure;
4983
4984 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4985 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4986 goto nla_put_failure;
4987 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4988 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4989 goto nla_put_failure;
4990 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4991 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4992 survey->channel_time))
4993 goto nla_put_failure;
4994 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4995 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4996 survey->channel_time_busy))
4997 goto nla_put_failure;
4998 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4999 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5000 survey->channel_time_ext_busy))
5001 goto nla_put_failure;
5002 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5003 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5004 survey->channel_time_rx))
5005 goto nla_put_failure;
5006 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5007 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5008 survey->channel_time_tx))
5009 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005010
5011 nla_nest_end(msg, infoattr);
5012
5013 return genlmsg_end(msg, hdr);
5014
5015 nla_put_failure:
5016 genlmsg_cancel(msg, hdr);
5017 return -EMSGSIZE;
5018}
5019
5020static int nl80211_dump_survey(struct sk_buff *skb,
5021 struct netlink_callback *cb)
5022{
5023 struct survey_info survey;
5024 struct cfg80211_registered_device *dev;
5025 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005026 int survey_idx = cb->args[1];
5027 int res;
5028
Johannes Berg67748892010-10-04 21:14:06 +02005029 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5030 if (res)
5031 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005032
5033 if (!dev->ops->dump_survey) {
5034 res = -EOPNOTSUPP;
5035 goto out_err;
5036 }
5037
5038 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005039 struct ieee80211_channel *chan;
5040
Hila Gonene35e4d22012-06-27 17:19:42 +03005041 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005042 if (res == -ENOENT)
5043 break;
5044 if (res)
5045 goto out_err;
5046
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005047 /* Survey without a channel doesn't make sense */
5048 if (!survey.channel) {
5049 res = -EINVAL;
5050 goto out;
5051 }
5052
5053 chan = ieee80211_get_channel(&dev->wiphy,
5054 survey.channel->center_freq);
5055 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5056 survey_idx++;
5057 continue;
5058 }
5059
Holger Schurig61fa7132009-11-11 12:25:40 +01005060 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005061 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005062 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5063 netdev,
5064 &survey) < 0)
5065 goto out;
5066 survey_idx++;
5067 }
5068
5069 out:
5070 cb->args[1] = survey_idx;
5071 res = skb->len;
5072 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005073 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005074 return res;
5075}
5076
Samuel Ortizb23aa672009-07-01 21:26:54 +02005077static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5078{
5079 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5080 NL80211_WPA_VERSION_2));
5081}
5082
Jouni Malinen636a5d32009-03-19 13:39:22 +02005083static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5084{
Johannes Berg4c476992010-10-04 21:36:35 +02005085 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5086 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005087 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005088 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5089 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005090 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005091 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005092 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005093
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005094 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5095 return -EINVAL;
5096
5097 if (!info->attrs[NL80211_ATTR_MAC])
5098 return -EINVAL;
5099
Jouni Malinen17780922009-03-27 20:52:47 +02005100 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5101 return -EINVAL;
5102
Johannes Berg19957bb2009-07-02 17:20:43 +02005103 if (!info->attrs[NL80211_ATTR_SSID])
5104 return -EINVAL;
5105
5106 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5107 return -EINVAL;
5108
Johannes Bergfffd0932009-07-08 14:22:54 +02005109 err = nl80211_parse_key(info, &key);
5110 if (err)
5111 return err;
5112
5113 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005114 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5115 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005116 if (!key.p.key || !key.p.key_len)
5117 return -EINVAL;
5118 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5119 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5120 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5121 key.p.key_len != WLAN_KEY_LEN_WEP104))
5122 return -EINVAL;
5123 if (key.idx > 4)
5124 return -EINVAL;
5125 } else {
5126 key.p.key_len = 0;
5127 key.p.key = NULL;
5128 }
5129
Johannes Bergafea0b72010-08-10 09:46:42 +02005130 if (key.idx >= 0) {
5131 int i;
5132 bool ok = false;
5133 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5134 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5135 ok = true;
5136 break;
5137 }
5138 }
Johannes Berg4c476992010-10-04 21:36:35 +02005139 if (!ok)
5140 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005141 }
5142
Johannes Berg4c476992010-10-04 21:36:35 +02005143 if (!rdev->ops->auth)
5144 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005145
Johannes Berg074ac8d2010-09-16 14:58:22 +02005146 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005147 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5148 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005149
Johannes Berg19957bb2009-07-02 17:20:43 +02005150 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005151 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005152 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005153 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5154 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005155
Johannes Berg19957bb2009-07-02 17:20:43 +02005156 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5157 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5158
5159 if (info->attrs[NL80211_ATTR_IE]) {
5160 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5161 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5162 }
5163
5164 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005165 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005166 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005167
Jouni Malinene39e5b52012-09-30 19:29:39 +03005168 if (auth_type == NL80211_AUTHTYPE_SAE &&
5169 !info->attrs[NL80211_ATTR_SAE_DATA])
5170 return -EINVAL;
5171
5172 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5173 if (auth_type != NL80211_AUTHTYPE_SAE)
5174 return -EINVAL;
5175 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5176 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5177 /* need to include at least Auth Transaction and Status Code */
5178 if (sae_data_len < 4)
5179 return -EINVAL;
5180 }
5181
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005182 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5183
Johannes Berg95de8172012-01-20 13:55:25 +01005184 /*
5185 * Since we no longer track auth state, ignore
5186 * requests to only change local state.
5187 */
5188 if (local_state_change)
5189 return 0;
5190
Johannes Berg4c476992010-10-04 21:36:35 +02005191 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5192 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005193 key.p.key, key.p.key_len, key.idx,
5194 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005195}
5196
Johannes Bergc0692b82010-08-27 14:26:53 +03005197static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5198 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005199 struct cfg80211_crypto_settings *settings,
5200 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005201{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005202 memset(settings, 0, sizeof(*settings));
5203
Samuel Ortizb23aa672009-07-01 21:26:54 +02005204 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5205
Johannes Bergc0692b82010-08-27 14:26:53 +03005206 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5207 u16 proto;
5208 proto = nla_get_u16(
5209 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5210 settings->control_port_ethertype = cpu_to_be16(proto);
5211 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5212 proto != ETH_P_PAE)
5213 return -EINVAL;
5214 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5215 settings->control_port_no_encrypt = true;
5216 } else
5217 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5218
Samuel Ortizb23aa672009-07-01 21:26:54 +02005219 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5220 void *data;
5221 int len, i;
5222
5223 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5224 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5225 settings->n_ciphers_pairwise = len / sizeof(u32);
5226
5227 if (len % sizeof(u32))
5228 return -EINVAL;
5229
Johannes Berg3dc27d22009-07-02 21:36:37 +02005230 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005231 return -EINVAL;
5232
5233 memcpy(settings->ciphers_pairwise, data, len);
5234
5235 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005236 if (!cfg80211_supported_cipher_suite(
5237 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005238 settings->ciphers_pairwise[i]))
5239 return -EINVAL;
5240 }
5241
5242 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5243 settings->cipher_group =
5244 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005245 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5246 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005247 return -EINVAL;
5248 }
5249
5250 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5251 settings->wpa_versions =
5252 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5253 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5254 return -EINVAL;
5255 }
5256
5257 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5258 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005259 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005260
5261 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5262 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5263 settings->n_akm_suites = len / sizeof(u32);
5264
5265 if (len % sizeof(u32))
5266 return -EINVAL;
5267
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005268 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5269 return -EINVAL;
5270
Samuel Ortizb23aa672009-07-01 21:26:54 +02005271 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005272 }
5273
5274 return 0;
5275}
5276
Jouni Malinen636a5d32009-03-19 13:39:22 +02005277static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5278{
Johannes Berg4c476992010-10-04 21:36:35 +02005279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5280 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005281 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005282 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005283 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005284 int err, ssid_len, ie_len = 0;
5285 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005286 u32 flags = 0;
5287 struct ieee80211_ht_cap *ht_capa = NULL;
5288 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005289
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005290 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5291 return -EINVAL;
5292
5293 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005294 !info->attrs[NL80211_ATTR_SSID] ||
5295 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005296 return -EINVAL;
5297
Johannes Berg4c476992010-10-04 21:36:35 +02005298 if (!rdev->ops->assoc)
5299 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005300
Johannes Berg074ac8d2010-09-16 14:58:22 +02005301 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005302 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5303 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005304
Johannes Berg19957bb2009-07-02 17:20:43 +02005305 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005306
Johannes Berg19957bb2009-07-02 17:20:43 +02005307 chan = ieee80211_get_channel(&rdev->wiphy,
5308 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005309 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5310 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005311
Johannes Berg19957bb2009-07-02 17:20:43 +02005312 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5313 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005314
5315 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005316 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5317 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005318 }
5319
Jouni Malinendc6382c2009-05-06 22:09:37 +03005320 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005321 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005322 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005323 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005324 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005325 else if (mfp != NL80211_MFP_NO)
5326 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005327 }
5328
Johannes Berg3e5d7642009-07-07 14:37:26 +02005329 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5330 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5331
Ben Greear7e7c8922011-11-18 11:31:59 -08005332 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5333 flags |= ASSOC_REQ_DISABLE_HT;
5334
5335 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5336 ht_capa_mask =
5337 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5338
5339 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5340 if (!ht_capa_mask)
5341 return -EINVAL;
5342 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5343 }
5344
Johannes Bergc0692b82010-08-27 14:26:53 +03005345 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005346 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005347 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5348 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005349 &crypto, flags, ht_capa,
5350 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005351
Jouni Malinen636a5d32009-03-19 13:39:22 +02005352 return err;
5353}
5354
5355static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5356{
Johannes Berg4c476992010-10-04 21:36:35 +02005357 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5358 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005359 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005360 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005361 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005362 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005363
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005364 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5365 return -EINVAL;
5366
5367 if (!info->attrs[NL80211_ATTR_MAC])
5368 return -EINVAL;
5369
5370 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5371 return -EINVAL;
5372
Johannes Berg4c476992010-10-04 21:36:35 +02005373 if (!rdev->ops->deauth)
5374 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005375
Johannes Berg074ac8d2010-09-16 14:58:22 +02005376 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005377 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5378 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005379
Johannes Berg19957bb2009-07-02 17:20:43 +02005380 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005381
Johannes Berg19957bb2009-07-02 17:20:43 +02005382 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5383 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005384 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005385 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005386 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005387
5388 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005389 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5390 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005391 }
5392
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005393 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5394
Johannes Berg4c476992010-10-04 21:36:35 +02005395 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5396 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005397}
5398
5399static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5400{
Johannes Berg4c476992010-10-04 21:36:35 +02005401 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5402 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005403 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005404 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005405 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005406 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005407
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005408 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5409 return -EINVAL;
5410
5411 if (!info->attrs[NL80211_ATTR_MAC])
5412 return -EINVAL;
5413
5414 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5415 return -EINVAL;
5416
Johannes Berg4c476992010-10-04 21:36:35 +02005417 if (!rdev->ops->disassoc)
5418 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005419
Johannes Berg074ac8d2010-09-16 14:58:22 +02005420 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005421 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5422 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005423
Johannes Berg19957bb2009-07-02 17:20:43 +02005424 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005425
Johannes Berg19957bb2009-07-02 17:20:43 +02005426 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5427 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005428 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005429 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005430 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005431
5432 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005433 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5434 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005435 }
5436
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005437 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5438
Johannes Berg4c476992010-10-04 21:36:35 +02005439 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5440 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005441}
5442
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005443static bool
5444nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5445 int mcast_rate[IEEE80211_NUM_BANDS],
5446 int rateval)
5447{
5448 struct wiphy *wiphy = &rdev->wiphy;
5449 bool found = false;
5450 int band, i;
5451
5452 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5453 struct ieee80211_supported_band *sband;
5454
5455 sband = wiphy->bands[band];
5456 if (!sband)
5457 continue;
5458
5459 for (i = 0; i < sband->n_bitrates; i++) {
5460 if (sband->bitrates[i].bitrate == rateval) {
5461 mcast_rate[band] = i + 1;
5462 found = true;
5463 break;
5464 }
5465 }
5466 }
5467
5468 return found;
5469}
5470
Johannes Berg04a773a2009-04-19 21:24:32 +02005471static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5472{
Johannes Berg4c476992010-10-04 21:36:35 +02005473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5474 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005475 struct cfg80211_ibss_params ibss;
5476 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005477 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005478 int err;
5479
Johannes Berg8e30bc52009-04-22 17:45:38 +02005480 memset(&ibss, 0, sizeof(ibss));
5481
Johannes Berg04a773a2009-04-19 21:24:32 +02005482 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5483 return -EINVAL;
5484
Johannes Berg683b6d32012-11-08 21:25:48 +01005485 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005486 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5487 return -EINVAL;
5488
Johannes Berg8e30bc52009-04-22 17:45:38 +02005489 ibss.beacon_interval = 100;
5490
5491 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5492 ibss.beacon_interval =
5493 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5494 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5495 return -EINVAL;
5496 }
5497
Johannes Berg4c476992010-10-04 21:36:35 +02005498 if (!rdev->ops->join_ibss)
5499 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005500
Johannes Berg4c476992010-10-04 21:36:35 +02005501 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5502 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005503
Johannes Berg79c97e92009-07-07 03:56:12 +02005504 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005505
Johannes Berg39193492011-09-16 13:45:25 +02005506 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005507 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005508
5509 if (!is_valid_ether_addr(ibss.bssid))
5510 return -EINVAL;
5511 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005512 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5513 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5514
5515 if (info->attrs[NL80211_ATTR_IE]) {
5516 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5517 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5518 }
5519
Johannes Berg683b6d32012-11-08 21:25:48 +01005520 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5521 if (err)
5522 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005523
Johannes Berg683b6d32012-11-08 21:25:48 +01005524 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005525 return -EINVAL;
5526
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005527 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5528 return -EINVAL;
5529 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5530 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
5531
Johannes Berg04a773a2009-04-19 21:24:32 +02005532 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005533 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005534
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005535 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5536 u8 *rates =
5537 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5538 int n_rates =
5539 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5540 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005541 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005542
Johannes Berg34850ab2011-07-18 18:08:35 +02005543 err = ieee80211_get_ratemask(sband, rates, n_rates,
5544 &ibss.basic_rates);
5545 if (err)
5546 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005547 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005548
5549 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5550 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5551 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5552 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005553
Johannes Berg4c476992010-10-04 21:36:35 +02005554 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305555 bool no_ht = false;
5556
Johannes Berg4c476992010-10-04 21:36:35 +02005557 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305558 info->attrs[NL80211_ATTR_KEYS],
5559 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005560 if (IS_ERR(connkeys))
5561 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305562
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005563 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5564 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305565 kfree(connkeys);
5566 return -EINVAL;
5567 }
Johannes Berg4c476992010-10-04 21:36:35 +02005568 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005569
Antonio Quartulli267335d2012-01-31 20:25:47 +01005570 ibss.control_port =
5571 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5572
Johannes Berg4c476992010-10-04 21:36:35 +02005573 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005574 if (err)
5575 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005576 return err;
5577}
5578
5579static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5580{
Johannes Berg4c476992010-10-04 21:36:35 +02005581 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5582 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005583
Johannes Berg4c476992010-10-04 21:36:35 +02005584 if (!rdev->ops->leave_ibss)
5585 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005586
Johannes Berg4c476992010-10-04 21:36:35 +02005587 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5588 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005589
Johannes Berg4c476992010-10-04 21:36:35 +02005590 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005591}
5592
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005593static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5594{
5595 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5596 struct net_device *dev = info->user_ptr[1];
5597 int mcast_rate[IEEE80211_NUM_BANDS];
5598 u32 nla_rate;
5599 int err;
5600
5601 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5602 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5603 return -EOPNOTSUPP;
5604
5605 if (!rdev->ops->set_mcast_rate)
5606 return -EOPNOTSUPP;
5607
5608 memset(mcast_rate, 0, sizeof(mcast_rate));
5609
5610 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5611 return -EINVAL;
5612
5613 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5614 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5615 return -EINVAL;
5616
5617 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5618
5619 return err;
5620}
5621
5622
Johannes Bergaff89a92009-07-01 21:26:51 +02005623#ifdef CONFIG_NL80211_TESTMODE
5624static struct genl_multicast_group nl80211_testmode_mcgrp = {
5625 .name = "testmode",
5626};
5627
5628static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5629{
Johannes Berg4c476992010-10-04 21:36:35 +02005630 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005631 int err;
5632
5633 if (!info->attrs[NL80211_ATTR_TESTDATA])
5634 return -EINVAL;
5635
Johannes Bergaff89a92009-07-01 21:26:51 +02005636 err = -EOPNOTSUPP;
5637 if (rdev->ops->testmode_cmd) {
5638 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005639 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005640 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5641 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5642 rdev->testmode_info = NULL;
5643 }
5644
Johannes Bergaff89a92009-07-01 21:26:51 +02005645 return err;
5646}
5647
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005648static int nl80211_testmode_dump(struct sk_buff *skb,
5649 struct netlink_callback *cb)
5650{
Johannes Berg00918d32011-12-13 17:22:05 +01005651 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005652 int err;
5653 long phy_idx;
5654 void *data = NULL;
5655 int data_len = 0;
5656
5657 if (cb->args[0]) {
5658 /*
5659 * 0 is a valid index, but not valid for args[0],
5660 * so we need to offset by 1.
5661 */
5662 phy_idx = cb->args[0] - 1;
5663 } else {
5664 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5665 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5666 nl80211_policy);
5667 if (err)
5668 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005669
Johannes Berg2bd7e352012-06-15 14:23:16 +02005670 mutex_lock(&cfg80211_mutex);
5671 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5672 nl80211_fam.attrbuf);
5673 if (IS_ERR(rdev)) {
5674 mutex_unlock(&cfg80211_mutex);
5675 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005676 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005677 phy_idx = rdev->wiphy_idx;
5678 rdev = NULL;
5679 mutex_unlock(&cfg80211_mutex);
5680
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005681 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5682 cb->args[1] =
5683 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5684 }
5685
5686 if (cb->args[1]) {
5687 data = nla_data((void *)cb->args[1]);
5688 data_len = nla_len((void *)cb->args[1]);
5689 }
5690
5691 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005692 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5693 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005694 mutex_unlock(&cfg80211_mutex);
5695 return -ENOENT;
5696 }
Johannes Berg00918d32011-12-13 17:22:05 +01005697 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005698 mutex_unlock(&cfg80211_mutex);
5699
Johannes Berg00918d32011-12-13 17:22:05 +01005700 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005701 err = -EOPNOTSUPP;
5702 goto out_err;
5703 }
5704
5705 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005706 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005707 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5708 NL80211_CMD_TESTMODE);
5709 struct nlattr *tmdata;
5710
David S. Miller9360ffd2012-03-29 04:41:26 -04005711 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005712 genlmsg_cancel(skb, hdr);
5713 break;
5714 }
5715
5716 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5717 if (!tmdata) {
5718 genlmsg_cancel(skb, hdr);
5719 break;
5720 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005721 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005722 nla_nest_end(skb, tmdata);
5723
5724 if (err == -ENOBUFS || err == -ENOENT) {
5725 genlmsg_cancel(skb, hdr);
5726 break;
5727 } else if (err) {
5728 genlmsg_cancel(skb, hdr);
5729 goto out_err;
5730 }
5731
5732 genlmsg_end(skb, hdr);
5733 }
5734
5735 err = skb->len;
5736 /* see above */
5737 cb->args[0] = phy_idx + 1;
5738 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005739 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005740 return err;
5741}
5742
Johannes Bergaff89a92009-07-01 21:26:51 +02005743static struct sk_buff *
5744__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005745 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005746{
5747 struct sk_buff *skb;
5748 void *hdr;
5749 struct nlattr *data;
5750
5751 skb = nlmsg_new(approxlen + 100, gfp);
5752 if (!skb)
5753 return NULL;
5754
Eric W. Biederman15e47302012-09-07 20:12:54 +00005755 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005756 if (!hdr) {
5757 kfree_skb(skb);
5758 return NULL;
5759 }
5760
David S. Miller9360ffd2012-03-29 04:41:26 -04005761 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5762 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005763 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5764
5765 ((void **)skb->cb)[0] = rdev;
5766 ((void **)skb->cb)[1] = hdr;
5767 ((void **)skb->cb)[2] = data;
5768
5769 return skb;
5770
5771 nla_put_failure:
5772 kfree_skb(skb);
5773 return NULL;
5774}
5775
5776struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5777 int approxlen)
5778{
5779 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5780
5781 if (WARN_ON(!rdev->testmode_info))
5782 return NULL;
5783
5784 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005785 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005786 rdev->testmode_info->snd_seq,
5787 GFP_KERNEL);
5788}
5789EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5790
5791int cfg80211_testmode_reply(struct sk_buff *skb)
5792{
5793 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5794 void *hdr = ((void **)skb->cb)[1];
5795 struct nlattr *data = ((void **)skb->cb)[2];
5796
5797 if (WARN_ON(!rdev->testmode_info)) {
5798 kfree_skb(skb);
5799 return -EINVAL;
5800 }
5801
5802 nla_nest_end(skb, data);
5803 genlmsg_end(skb, hdr);
5804 return genlmsg_reply(skb, rdev->testmode_info);
5805}
5806EXPORT_SYMBOL(cfg80211_testmode_reply);
5807
5808struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5809 int approxlen, gfp_t gfp)
5810{
5811 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5812
5813 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5814}
5815EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5816
5817void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5818{
5819 void *hdr = ((void **)skb->cb)[1];
5820 struct nlattr *data = ((void **)skb->cb)[2];
5821
5822 nla_nest_end(skb, data);
5823 genlmsg_end(skb, hdr);
5824 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5825}
5826EXPORT_SYMBOL(cfg80211_testmode_event);
5827#endif
5828
Samuel Ortizb23aa672009-07-01 21:26:54 +02005829static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5830{
Johannes Berg4c476992010-10-04 21:36:35 +02005831 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5832 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005833 struct cfg80211_connect_params connect;
5834 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005835 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005836 int err;
5837
5838 memset(&connect, 0, sizeof(connect));
5839
5840 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5841 return -EINVAL;
5842
5843 if (!info->attrs[NL80211_ATTR_SSID] ||
5844 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5845 return -EINVAL;
5846
5847 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5848 connect.auth_type =
5849 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005850 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5851 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005852 return -EINVAL;
5853 } else
5854 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5855
5856 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5857
Johannes Bergc0692b82010-08-27 14:26:53 +03005858 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005859 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005860 if (err)
5861 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005862
Johannes Berg074ac8d2010-09-16 14:58:22 +02005863 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005864 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5865 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005866
Johannes Berg79c97e92009-07-07 03:56:12 +02005867 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005868
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305869 connect.bg_scan_period = -1;
5870 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5871 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5872 connect.bg_scan_period =
5873 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5874 }
5875
Samuel Ortizb23aa672009-07-01 21:26:54 +02005876 if (info->attrs[NL80211_ATTR_MAC])
5877 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5878 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5879 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5880
5881 if (info->attrs[NL80211_ATTR_IE]) {
5882 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5883 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5884 }
5885
5886 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5887 connect.channel =
5888 ieee80211_get_channel(wiphy,
5889 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5890 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005891 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5892 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005893 }
5894
Johannes Bergfffd0932009-07-08 14:22:54 +02005895 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5896 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305897 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005898 if (IS_ERR(connkeys))
5899 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005900 }
5901
Ben Greear7e7c8922011-11-18 11:31:59 -08005902 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5903 connect.flags |= ASSOC_REQ_DISABLE_HT;
5904
5905 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5906 memcpy(&connect.ht_capa_mask,
5907 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5908 sizeof(connect.ht_capa_mask));
5909
5910 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005911 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5912 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005913 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005914 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005915 memcpy(&connect.ht_capa,
5916 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5917 sizeof(connect.ht_capa));
5918 }
5919
Johannes Bergfffd0932009-07-08 14:22:54 +02005920 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005921 if (err)
5922 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005923 return err;
5924}
5925
5926static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5927{
Johannes Berg4c476992010-10-04 21:36:35 +02005928 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5929 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005930 u16 reason;
5931
5932 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5933 reason = WLAN_REASON_DEAUTH_LEAVING;
5934 else
5935 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5936
5937 if (reason == 0)
5938 return -EINVAL;
5939
Johannes Berg074ac8d2010-09-16 14:58:22 +02005940 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005941 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5942 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005943
Johannes Berg4c476992010-10-04 21:36:35 +02005944 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005945}
5946
Johannes Berg463d0182009-07-14 00:33:35 +02005947static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5948{
Johannes Berg4c476992010-10-04 21:36:35 +02005949 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005950 struct net *net;
5951 int err;
5952 u32 pid;
5953
5954 if (!info->attrs[NL80211_ATTR_PID])
5955 return -EINVAL;
5956
5957 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5958
Johannes Berg463d0182009-07-14 00:33:35 +02005959 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005960 if (IS_ERR(net))
5961 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005962
5963 err = 0;
5964
5965 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005966 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5967 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005968
Johannes Berg463d0182009-07-14 00:33:35 +02005969 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005970 return err;
5971}
5972
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005973static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5974{
Johannes Berg4c476992010-10-04 21:36:35 +02005975 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005976 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5977 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005978 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005979 struct cfg80211_pmksa pmksa;
5980
5981 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5982
5983 if (!info->attrs[NL80211_ATTR_MAC])
5984 return -EINVAL;
5985
5986 if (!info->attrs[NL80211_ATTR_PMKID])
5987 return -EINVAL;
5988
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005989 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5990 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5991
Johannes Berg074ac8d2010-09-16 14:58:22 +02005992 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005993 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5994 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005995
5996 switch (info->genlhdr->cmd) {
5997 case NL80211_CMD_SET_PMKSA:
5998 rdev_ops = rdev->ops->set_pmksa;
5999 break;
6000 case NL80211_CMD_DEL_PMKSA:
6001 rdev_ops = rdev->ops->del_pmksa;
6002 break;
6003 default:
6004 WARN_ON(1);
6005 break;
6006 }
6007
Johannes Berg4c476992010-10-04 21:36:35 +02006008 if (!rdev_ops)
6009 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006010
Johannes Berg4c476992010-10-04 21:36:35 +02006011 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006012}
6013
6014static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6015{
Johannes Berg4c476992010-10-04 21:36:35 +02006016 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6017 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006018
Johannes Berg074ac8d2010-09-16 14:58:22 +02006019 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006020 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6021 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006022
Johannes Berg4c476992010-10-04 21:36:35 +02006023 if (!rdev->ops->flush_pmksa)
6024 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006025
Hila Gonene35e4d22012-06-27 17:19:42 +03006026 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006027}
6028
Arik Nemtsov109086c2011-09-28 14:12:50 +03006029static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6030{
6031 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6032 struct net_device *dev = info->user_ptr[1];
6033 u8 action_code, dialog_token;
6034 u16 status_code;
6035 u8 *peer;
6036
6037 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6038 !rdev->ops->tdls_mgmt)
6039 return -EOPNOTSUPP;
6040
6041 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6042 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6043 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6044 !info->attrs[NL80211_ATTR_IE] ||
6045 !info->attrs[NL80211_ATTR_MAC])
6046 return -EINVAL;
6047
6048 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6049 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6050 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6051 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6052
Hila Gonene35e4d22012-06-27 17:19:42 +03006053 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6054 dialog_token, status_code,
6055 nla_data(info->attrs[NL80211_ATTR_IE]),
6056 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006057}
6058
6059static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6060{
6061 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6062 struct net_device *dev = info->user_ptr[1];
6063 enum nl80211_tdls_operation operation;
6064 u8 *peer;
6065
6066 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6067 !rdev->ops->tdls_oper)
6068 return -EOPNOTSUPP;
6069
6070 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6071 !info->attrs[NL80211_ATTR_MAC])
6072 return -EINVAL;
6073
6074 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6075 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6076
Hila Gonene35e4d22012-06-27 17:19:42 +03006077 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006078}
6079
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006080static int nl80211_remain_on_channel(struct sk_buff *skb,
6081 struct genl_info *info)
6082{
Johannes Berg4c476992010-10-04 21:36:35 +02006083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006084 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006085 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006086 struct sk_buff *msg;
6087 void *hdr;
6088 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006089 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006090 int err;
6091
6092 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6093 !info->attrs[NL80211_ATTR_DURATION])
6094 return -EINVAL;
6095
6096 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6097
Johannes Berg7c4ef712011-11-18 15:33:48 +01006098 if (!rdev->ops->remain_on_channel ||
6099 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006100 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006101
Johannes Bergebf348f2012-06-01 12:50:54 +02006102 /*
6103 * We should be on that channel for at least a minimum amount of
6104 * time (10ms) but no longer than the driver supports.
6105 */
6106 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6107 duration > rdev->wiphy.max_remain_on_channel_duration)
6108 return -EINVAL;
6109
Johannes Berg683b6d32012-11-08 21:25:48 +01006110 err = nl80211_parse_chandef(rdev, info, &chandef);
6111 if (err)
6112 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006113
6114 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006115 if (!msg)
6116 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006117
Eric W. Biederman15e47302012-09-07 20:12:54 +00006118 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006119 NL80211_CMD_REMAIN_ON_CHANNEL);
6120
6121 if (IS_ERR(hdr)) {
6122 err = PTR_ERR(hdr);
6123 goto free_msg;
6124 }
6125
Johannes Berg683b6d32012-11-08 21:25:48 +01006126 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6127 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006128
6129 if (err)
6130 goto free_msg;
6131
David S. Miller9360ffd2012-03-29 04:41:26 -04006132 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6133 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006134
6135 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006136
6137 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006138
6139 nla_put_failure:
6140 err = -ENOBUFS;
6141 free_msg:
6142 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006143 return err;
6144}
6145
6146static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6147 struct genl_info *info)
6148{
Johannes Berg4c476992010-10-04 21:36:35 +02006149 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006150 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006151 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006152
6153 if (!info->attrs[NL80211_ATTR_COOKIE])
6154 return -EINVAL;
6155
Johannes Berg4c476992010-10-04 21:36:35 +02006156 if (!rdev->ops->cancel_remain_on_channel)
6157 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006158
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006159 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6160
Hila Gonene35e4d22012-06-27 17:19:42 +03006161 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006162}
6163
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006164static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6165 u8 *rates, u8 rates_len)
6166{
6167 u8 i;
6168 u32 mask = 0;
6169
6170 for (i = 0; i < rates_len; i++) {
6171 int rate = (rates[i] & 0x7f) * 5;
6172 int ridx;
6173 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6174 struct ieee80211_rate *srate =
6175 &sband->bitrates[ridx];
6176 if (rate == srate->bitrate) {
6177 mask |= 1 << ridx;
6178 break;
6179 }
6180 }
6181 if (ridx == sband->n_bitrates)
6182 return 0; /* rate not found */
6183 }
6184
6185 return mask;
6186}
6187
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006188static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6189 u8 *rates, u8 rates_len,
6190 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6191{
6192 u8 i;
6193
6194 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6195
6196 for (i = 0; i < rates_len; i++) {
6197 int ridx, rbit;
6198
6199 ridx = rates[i] / 8;
6200 rbit = BIT(rates[i] % 8);
6201
6202 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006203 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006204 return false;
6205
6206 /* check availability */
6207 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6208 mcs[ridx] |= rbit;
6209 else
6210 return false;
6211 }
6212
6213 return true;
6214}
6215
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006216static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006217 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6218 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006219 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6220 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006221};
6222
6223static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6224 struct genl_info *info)
6225{
6226 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006227 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006228 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006229 int rem, i;
6230 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006231 struct nlattr *tx_rates;
6232 struct ieee80211_supported_band *sband;
6233
6234 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6235 return -EINVAL;
6236
Johannes Berg4c476992010-10-04 21:36:35 +02006237 if (!rdev->ops->set_bitrate_mask)
6238 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006239
6240 memset(&mask, 0, sizeof(mask));
6241 /* Default to all rates enabled */
6242 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6243 sband = rdev->wiphy.bands[i];
6244 mask.control[i].legacy =
6245 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006246 if (sband)
6247 memcpy(mask.control[i].mcs,
6248 sband->ht_cap.mcs.rx_mask,
6249 sizeof(mask.control[i].mcs));
6250 else
6251 memset(mask.control[i].mcs, 0,
6252 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006253 }
6254
6255 /*
6256 * The nested attribute uses enum nl80211_band as the index. This maps
6257 * directly to the enum ieee80211_band values used in cfg80211.
6258 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006259 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006260 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6261 {
6262 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006263 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6264 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006265 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006266 if (sband == NULL)
6267 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006268 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6269 nla_len(tx_rates), nl80211_txattr_policy);
6270 if (tb[NL80211_TXRATE_LEGACY]) {
6271 mask.control[band].legacy = rateset_to_mask(
6272 sband,
6273 nla_data(tb[NL80211_TXRATE_LEGACY]),
6274 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306275 if ((mask.control[band].legacy == 0) &&
6276 nla_len(tb[NL80211_TXRATE_LEGACY]))
6277 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006278 }
6279 if (tb[NL80211_TXRATE_MCS]) {
6280 if (!ht_rateset_to_mask(
6281 sband,
6282 nla_data(tb[NL80211_TXRATE_MCS]),
6283 nla_len(tb[NL80211_TXRATE_MCS]),
6284 mask.control[band].mcs))
6285 return -EINVAL;
6286 }
6287
6288 if (mask.control[band].legacy == 0) {
6289 /* don't allow empty legacy rates if HT
6290 * is not even supported. */
6291 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6292 return -EINVAL;
6293
6294 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6295 if (mask.control[band].mcs[i])
6296 break;
6297
6298 /* legacy and mcs rates may not be both empty */
6299 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006300 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006301 }
6302 }
6303
Hila Gonene35e4d22012-06-27 17:19:42 +03006304 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006305}
6306
Johannes Berg2e161f72010-08-12 15:38:38 +02006307static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006308{
Johannes Berg4c476992010-10-04 21:36:35 +02006309 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006310 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006311 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006312
6313 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6314 return -EINVAL;
6315
Johannes Berg2e161f72010-08-12 15:38:38 +02006316 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6317 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006318
Johannes Berg71bbc992012-06-15 15:30:18 +02006319 switch (wdev->iftype) {
6320 case NL80211_IFTYPE_STATION:
6321 case NL80211_IFTYPE_ADHOC:
6322 case NL80211_IFTYPE_P2P_CLIENT:
6323 case NL80211_IFTYPE_AP:
6324 case NL80211_IFTYPE_AP_VLAN:
6325 case NL80211_IFTYPE_MESH_POINT:
6326 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006327 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006328 break;
6329 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006330 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006331 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006332
6333 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006334 if (!rdev->ops->mgmt_tx)
6335 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006336
Eric W. Biederman15e47302012-09-07 20:12:54 +00006337 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006338 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6339 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006340}
6341
Johannes Berg2e161f72010-08-12 15:38:38 +02006342static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006343{
Johannes Berg4c476992010-10-04 21:36:35 +02006344 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006345 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006346 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006347 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006348 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006349 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006350 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006351 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006352 bool offchan, no_cck, dont_wait_for_ack;
6353
6354 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006355
Johannes Berg683b6d32012-11-08 21:25:48 +01006356 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006357 return -EINVAL;
6358
Johannes Berg4c476992010-10-04 21:36:35 +02006359 if (!rdev->ops->mgmt_tx)
6360 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006361
Johannes Berg71bbc992012-06-15 15:30:18 +02006362 switch (wdev->iftype) {
6363 case NL80211_IFTYPE_STATION:
6364 case NL80211_IFTYPE_ADHOC:
6365 case NL80211_IFTYPE_P2P_CLIENT:
6366 case NL80211_IFTYPE_AP:
6367 case NL80211_IFTYPE_AP_VLAN:
6368 case NL80211_IFTYPE_MESH_POINT:
6369 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006370 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006371 break;
6372 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006373 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006374 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006375
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006376 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006377 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006378 return -EINVAL;
6379 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006380
6381 /*
6382 * We should wait on the channel for at least a minimum amount
6383 * of time (10ms) but no longer than the driver supports.
6384 */
6385 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6386 wait > rdev->wiphy.max_remain_on_channel_duration)
6387 return -EINVAL;
6388
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006389 }
6390
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006391 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6392
Johannes Berg7c4ef712011-11-18 15:33:48 +01006393 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6394 return -EINVAL;
6395
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306396 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6397
Johannes Berg683b6d32012-11-08 21:25:48 +01006398 err = nl80211_parse_chandef(rdev, info, &chandef);
6399 if (err)
6400 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006401
Johannes Berge247bd902011-11-04 11:18:21 +01006402 if (!dont_wait_for_ack) {
6403 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6404 if (!msg)
6405 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006406
Eric W. Biederman15e47302012-09-07 20:12:54 +00006407 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006408 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006409
Johannes Berge247bd902011-11-04 11:18:21 +01006410 if (IS_ERR(hdr)) {
6411 err = PTR_ERR(hdr);
6412 goto free_msg;
6413 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006414 }
Johannes Berge247bd902011-11-04 11:18:21 +01006415
Johannes Berg683b6d32012-11-08 21:25:48 +01006416 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006417 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6418 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006419 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006420 if (err)
6421 goto free_msg;
6422
Johannes Berge247bd902011-11-04 11:18:21 +01006423 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006424 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6425 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006426
Johannes Berge247bd902011-11-04 11:18:21 +01006427 genlmsg_end(msg, hdr);
6428 return genlmsg_reply(msg, info);
6429 }
6430
6431 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006432
6433 nla_put_failure:
6434 err = -ENOBUFS;
6435 free_msg:
6436 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006437 return err;
6438}
6439
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006440static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6441{
6442 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006443 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006444 u64 cookie;
6445
6446 if (!info->attrs[NL80211_ATTR_COOKIE])
6447 return -EINVAL;
6448
6449 if (!rdev->ops->mgmt_tx_cancel_wait)
6450 return -EOPNOTSUPP;
6451
Johannes Berg71bbc992012-06-15 15:30:18 +02006452 switch (wdev->iftype) {
6453 case NL80211_IFTYPE_STATION:
6454 case NL80211_IFTYPE_ADHOC:
6455 case NL80211_IFTYPE_P2P_CLIENT:
6456 case NL80211_IFTYPE_AP:
6457 case NL80211_IFTYPE_AP_VLAN:
6458 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006459 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006460 break;
6461 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006462 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006463 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006464
6465 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6466
Hila Gonene35e4d22012-06-27 17:19:42 +03006467 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006468}
6469
Kalle Valoffb9eb32010-02-17 17:58:10 +02006470static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6471{
Johannes Berg4c476992010-10-04 21:36:35 +02006472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006473 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006474 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006475 u8 ps_state;
6476 bool state;
6477 int err;
6478
Johannes Berg4c476992010-10-04 21:36:35 +02006479 if (!info->attrs[NL80211_ATTR_PS_STATE])
6480 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006481
6482 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6483
Johannes Berg4c476992010-10-04 21:36:35 +02006484 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6485 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006486
6487 wdev = dev->ieee80211_ptr;
6488
Johannes Berg4c476992010-10-04 21:36:35 +02006489 if (!rdev->ops->set_power_mgmt)
6490 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006491
6492 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6493
6494 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006495 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006496
Hila Gonene35e4d22012-06-27 17:19:42 +03006497 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006498 if (!err)
6499 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006500 return err;
6501}
6502
6503static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6504{
Johannes Berg4c476992010-10-04 21:36:35 +02006505 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006506 enum nl80211_ps_state ps_state;
6507 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006508 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006509 struct sk_buff *msg;
6510 void *hdr;
6511 int err;
6512
Kalle Valoffb9eb32010-02-17 17:58:10 +02006513 wdev = dev->ieee80211_ptr;
6514
Johannes Berg4c476992010-10-04 21:36:35 +02006515 if (!rdev->ops->set_power_mgmt)
6516 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006517
6518 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006519 if (!msg)
6520 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006521
Eric W. Biederman15e47302012-09-07 20:12:54 +00006522 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006523 NL80211_CMD_GET_POWER_SAVE);
6524 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006525 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006526 goto free_msg;
6527 }
6528
6529 if (wdev->ps)
6530 ps_state = NL80211_PS_ENABLED;
6531 else
6532 ps_state = NL80211_PS_DISABLED;
6533
David S. Miller9360ffd2012-03-29 04:41:26 -04006534 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6535 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006536
6537 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006538 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006539
Johannes Berg4c476992010-10-04 21:36:35 +02006540 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006541 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006542 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006543 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006544 return err;
6545}
6546
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006547static struct nla_policy
6548nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6549 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6550 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6551 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006552 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6553 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6554 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006555};
6556
Thomas Pedersen84f10702012-07-12 16:17:33 -07006557static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006558 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006559{
6560 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6561 struct wireless_dev *wdev;
6562 struct net_device *dev = info->user_ptr[1];
6563
Johannes Bergd9d8b012012-11-26 12:51:52 +01006564 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006565 return -EINVAL;
6566
6567 wdev = dev->ieee80211_ptr;
6568
6569 if (!rdev->ops->set_cqm_txe_config)
6570 return -EOPNOTSUPP;
6571
6572 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6573 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6574 return -EOPNOTSUPP;
6575
Hila Gonene35e4d22012-06-27 17:19:42 +03006576 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006577}
6578
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006579static int nl80211_set_cqm_rssi(struct genl_info *info,
6580 s32 threshold, u32 hysteresis)
6581{
Johannes Berg4c476992010-10-04 21:36:35 +02006582 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006583 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006584 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006585
6586 if (threshold > 0)
6587 return -EINVAL;
6588
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006589 wdev = dev->ieee80211_ptr;
6590
Johannes Berg4c476992010-10-04 21:36:35 +02006591 if (!rdev->ops->set_cqm_rssi_config)
6592 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006593
Johannes Berg074ac8d2010-09-16 14:58:22 +02006594 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006595 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6596 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006597
Hila Gonene35e4d22012-06-27 17:19:42 +03006598 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006599}
6600
6601static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6602{
6603 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6604 struct nlattr *cqm;
6605 int err;
6606
6607 cqm = info->attrs[NL80211_ATTR_CQM];
6608 if (!cqm) {
6609 err = -EINVAL;
6610 goto out;
6611 }
6612
6613 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6614 nl80211_attr_cqm_policy);
6615 if (err)
6616 goto out;
6617
6618 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6619 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6620 s32 threshold;
6621 u32 hysteresis;
6622 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6623 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6624 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006625 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6626 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6627 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6628 u32 rate, pkts, intvl;
6629 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6630 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6631 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6632 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006633 } else
6634 err = -EINVAL;
6635
6636out:
6637 return err;
6638}
6639
Johannes Berg29cbe682010-12-03 09:20:44 +01006640static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6641{
6642 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6643 struct net_device *dev = info->user_ptr[1];
6644 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006645 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006646 int err;
6647
6648 /* start with default */
6649 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006650 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006651
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006652 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006653 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006654 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006655 if (err)
6656 return err;
6657 }
6658
6659 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6660 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6661 return -EINVAL;
6662
Javier Cardonac80d5452010-12-16 17:37:49 -08006663 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6664 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6665
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006666 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6667 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6668 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6669 return -EINVAL;
6670
Javier Cardonac80d5452010-12-16 17:37:49 -08006671 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6672 /* parse additional setup parameters if given */
6673 err = nl80211_parse_mesh_setup(info, &setup);
6674 if (err)
6675 return err;
6676 }
6677
Johannes Bergcc1d2802012-05-16 23:50:20 +02006678 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01006679 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
6680 if (err)
6681 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006682 } else {
6683 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01006684 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006685 }
6686
Javier Cardonac80d5452010-12-16 17:37:49 -08006687 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006688}
6689
6690static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6691{
6692 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6693 struct net_device *dev = info->user_ptr[1];
6694
6695 return cfg80211_leave_mesh(rdev, dev);
6696}
6697
Johannes Bergdfb89c52012-06-27 09:23:48 +02006698#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006699static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6700{
6701 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6702 struct sk_buff *msg;
6703 void *hdr;
6704
6705 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6706 return -EOPNOTSUPP;
6707
6708 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6709 if (!msg)
6710 return -ENOMEM;
6711
Eric W. Biederman15e47302012-09-07 20:12:54 +00006712 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006713 NL80211_CMD_GET_WOWLAN);
6714 if (!hdr)
6715 goto nla_put_failure;
6716
6717 if (rdev->wowlan) {
6718 struct nlattr *nl_wowlan;
6719
6720 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6721 if (!nl_wowlan)
6722 goto nla_put_failure;
6723
David S. Miller9360ffd2012-03-29 04:41:26 -04006724 if ((rdev->wowlan->any &&
6725 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6726 (rdev->wowlan->disconnect &&
6727 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6728 (rdev->wowlan->magic_pkt &&
6729 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6730 (rdev->wowlan->gtk_rekey_failure &&
6731 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6732 (rdev->wowlan->eap_identity_req &&
6733 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6734 (rdev->wowlan->four_way_handshake &&
6735 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6736 (rdev->wowlan->rfkill_release &&
6737 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6738 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006739 if (rdev->wowlan->n_patterns) {
6740 struct nlattr *nl_pats, *nl_pat;
6741 int i, pat_len;
6742
6743 nl_pats = nla_nest_start(msg,
6744 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6745 if (!nl_pats)
6746 goto nla_put_failure;
6747
6748 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6749 nl_pat = nla_nest_start(msg, i + 1);
6750 if (!nl_pat)
6751 goto nla_put_failure;
6752 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006753 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6754 DIV_ROUND_UP(pat_len, 8),
6755 rdev->wowlan->patterns[i].mask) ||
6756 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6757 pat_len,
6758 rdev->wowlan->patterns[i].pattern))
6759 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006760 nla_nest_end(msg, nl_pat);
6761 }
6762 nla_nest_end(msg, nl_pats);
6763 }
6764
6765 nla_nest_end(msg, nl_wowlan);
6766 }
6767
6768 genlmsg_end(msg, hdr);
6769 return genlmsg_reply(msg, info);
6770
6771nla_put_failure:
6772 nlmsg_free(msg);
6773 return -ENOBUFS;
6774}
6775
6776static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6777{
6778 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6779 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006780 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006781 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006782 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6783 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006784 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006785
6786 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6787 return -EOPNOTSUPP;
6788
Johannes Bergae33bd82012-07-12 16:25:02 +02006789 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6790 cfg80211_rdev_free_wowlan(rdev);
6791 rdev->wowlan = NULL;
6792 goto set_wakeup;
6793 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006794
6795 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6796 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6797 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6798 nl80211_wowlan_policy);
6799 if (err)
6800 return err;
6801
6802 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6803 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6804 return -EINVAL;
6805 new_triggers.any = true;
6806 }
6807
6808 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6809 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6810 return -EINVAL;
6811 new_triggers.disconnect = true;
6812 }
6813
6814 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6815 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6816 return -EINVAL;
6817 new_triggers.magic_pkt = true;
6818 }
6819
Johannes Berg77dbbb12011-07-13 10:48:55 +02006820 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6821 return -EINVAL;
6822
6823 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6824 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6825 return -EINVAL;
6826 new_triggers.gtk_rekey_failure = true;
6827 }
6828
6829 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6830 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6831 return -EINVAL;
6832 new_triggers.eap_identity_req = true;
6833 }
6834
6835 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6836 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6837 return -EINVAL;
6838 new_triggers.four_way_handshake = true;
6839 }
6840
6841 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6842 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6843 return -EINVAL;
6844 new_triggers.rfkill_release = true;
6845 }
6846
Johannes Bergff1b6e62011-05-04 15:37:28 +02006847 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6848 struct nlattr *pat;
6849 int n_patterns = 0;
6850 int rem, pat_len, mask_len;
6851 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6852
6853 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6854 rem)
6855 n_patterns++;
6856 if (n_patterns > wowlan->n_patterns)
6857 return -EINVAL;
6858
6859 new_triggers.patterns = kcalloc(n_patterns,
6860 sizeof(new_triggers.patterns[0]),
6861 GFP_KERNEL);
6862 if (!new_triggers.patterns)
6863 return -ENOMEM;
6864
6865 new_triggers.n_patterns = n_patterns;
6866 i = 0;
6867
6868 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6869 rem) {
6870 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6871 nla_data(pat), nla_len(pat), NULL);
6872 err = -EINVAL;
6873 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6874 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6875 goto error;
6876 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6877 mask_len = DIV_ROUND_UP(pat_len, 8);
6878 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6879 mask_len)
6880 goto error;
6881 if (pat_len > wowlan->pattern_max_len ||
6882 pat_len < wowlan->pattern_min_len)
6883 goto error;
6884
6885 new_triggers.patterns[i].mask =
6886 kmalloc(mask_len + pat_len, GFP_KERNEL);
6887 if (!new_triggers.patterns[i].mask) {
6888 err = -ENOMEM;
6889 goto error;
6890 }
6891 new_triggers.patterns[i].pattern =
6892 new_triggers.patterns[i].mask + mask_len;
6893 memcpy(new_triggers.patterns[i].mask,
6894 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6895 mask_len);
6896 new_triggers.patterns[i].pattern_len = pat_len;
6897 memcpy(new_triggers.patterns[i].pattern,
6898 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6899 pat_len);
6900 i++;
6901 }
6902 }
6903
Johannes Bergae33bd82012-07-12 16:25:02 +02006904 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6905 if (!ntrig) {
6906 err = -ENOMEM;
6907 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006908 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006909 cfg80211_rdev_free_wowlan(rdev);
6910 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006911
Johannes Bergae33bd82012-07-12 16:25:02 +02006912 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006913 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03006914 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02006915
Johannes Bergff1b6e62011-05-04 15:37:28 +02006916 return 0;
6917 error:
6918 for (i = 0; i < new_triggers.n_patterns; i++)
6919 kfree(new_triggers.patterns[i].mask);
6920 kfree(new_triggers.patterns);
6921 return err;
6922}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006923#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006924
Johannes Berge5497d72011-07-05 16:35:40 +02006925static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6926{
6927 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6928 struct net_device *dev = info->user_ptr[1];
6929 struct wireless_dev *wdev = dev->ieee80211_ptr;
6930 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6931 struct cfg80211_gtk_rekey_data rekey_data;
6932 int err;
6933
6934 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6935 return -EINVAL;
6936
6937 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6938 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6939 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6940 nl80211_rekey_policy);
6941 if (err)
6942 return err;
6943
6944 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6945 return -ERANGE;
6946 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6947 return -ERANGE;
6948 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6949 return -ERANGE;
6950
6951 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6952 NL80211_KEK_LEN);
6953 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6954 NL80211_KCK_LEN);
6955 memcpy(rekey_data.replay_ctr,
6956 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6957 NL80211_REPLAY_CTR_LEN);
6958
6959 wdev_lock(wdev);
6960 if (!wdev->current_bss) {
6961 err = -ENOTCONN;
6962 goto out;
6963 }
6964
6965 if (!rdev->ops->set_rekey_data) {
6966 err = -EOPNOTSUPP;
6967 goto out;
6968 }
6969
Hila Gonene35e4d22012-06-27 17:19:42 +03006970 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02006971 out:
6972 wdev_unlock(wdev);
6973 return err;
6974}
6975
Johannes Berg28946da2011-11-04 11:18:12 +01006976static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6977 struct genl_info *info)
6978{
6979 struct net_device *dev = info->user_ptr[1];
6980 struct wireless_dev *wdev = dev->ieee80211_ptr;
6981
6982 if (wdev->iftype != NL80211_IFTYPE_AP &&
6983 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6984 return -EINVAL;
6985
Eric W. Biederman15e47302012-09-07 20:12:54 +00006986 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006987 return -EBUSY;
6988
Eric W. Biederman15e47302012-09-07 20:12:54 +00006989 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006990 return 0;
6991}
6992
Johannes Berg7f6cf312011-11-04 11:18:15 +01006993static int nl80211_probe_client(struct sk_buff *skb,
6994 struct genl_info *info)
6995{
6996 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6997 struct net_device *dev = info->user_ptr[1];
6998 struct wireless_dev *wdev = dev->ieee80211_ptr;
6999 struct sk_buff *msg;
7000 void *hdr;
7001 const u8 *addr;
7002 u64 cookie;
7003 int err;
7004
7005 if (wdev->iftype != NL80211_IFTYPE_AP &&
7006 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7007 return -EOPNOTSUPP;
7008
7009 if (!info->attrs[NL80211_ATTR_MAC])
7010 return -EINVAL;
7011
7012 if (!rdev->ops->probe_client)
7013 return -EOPNOTSUPP;
7014
7015 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7016 if (!msg)
7017 return -ENOMEM;
7018
Eric W. Biederman15e47302012-09-07 20:12:54 +00007019 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007020 NL80211_CMD_PROBE_CLIENT);
7021
7022 if (IS_ERR(hdr)) {
7023 err = PTR_ERR(hdr);
7024 goto free_msg;
7025 }
7026
7027 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7028
Hila Gonene35e4d22012-06-27 17:19:42 +03007029 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007030 if (err)
7031 goto free_msg;
7032
David S. Miller9360ffd2012-03-29 04:41:26 -04007033 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7034 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007035
7036 genlmsg_end(msg, hdr);
7037
7038 return genlmsg_reply(msg, info);
7039
7040 nla_put_failure:
7041 err = -ENOBUFS;
7042 free_msg:
7043 nlmsg_free(msg);
7044 return err;
7045}
7046
Johannes Berg5e7602302011-11-04 11:18:17 +01007047static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7048{
7049 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007050 struct cfg80211_beacon_registration *reg, *nreg;
7051 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007052
7053 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7054 return -EOPNOTSUPP;
7055
Ben Greear37c73b52012-10-26 14:49:25 -07007056 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7057 if (!nreg)
7058 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01007059
Ben Greear37c73b52012-10-26 14:49:25 -07007060 /* First, check if already registered. */
7061 spin_lock_bh(&rdev->beacon_registrations_lock);
7062 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7063 if (reg->nlportid == info->snd_portid) {
7064 rv = -EALREADY;
7065 goto out_err;
7066 }
7067 }
7068 /* Add it to the list */
7069 nreg->nlportid = info->snd_portid;
7070 list_add(&nreg->list, &rdev->beacon_registrations);
7071
7072 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01007073
7074 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007075out_err:
7076 spin_unlock_bh(&rdev->beacon_registrations_lock);
7077 kfree(nreg);
7078 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007079}
7080
Johannes Berg98104fde2012-06-16 00:19:54 +02007081static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7082{
7083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7084 struct wireless_dev *wdev = info->user_ptr[1];
7085 int err;
7086
7087 if (!rdev->ops->start_p2p_device)
7088 return -EOPNOTSUPP;
7089
7090 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7091 return -EOPNOTSUPP;
7092
7093 if (wdev->p2p_started)
7094 return 0;
7095
7096 mutex_lock(&rdev->devlist_mtx);
7097 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7098 mutex_unlock(&rdev->devlist_mtx);
7099 if (err)
7100 return err;
7101
Johannes Bergeeb126e2012-10-23 15:16:50 +02007102 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007103 if (err)
7104 return err;
7105
7106 wdev->p2p_started = true;
7107 mutex_lock(&rdev->devlist_mtx);
7108 rdev->opencount++;
7109 mutex_unlock(&rdev->devlist_mtx);
7110
7111 return 0;
7112}
7113
7114static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7115{
7116 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7117 struct wireless_dev *wdev = info->user_ptr[1];
7118
7119 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7120 return -EOPNOTSUPP;
7121
7122 if (!rdev->ops->stop_p2p_device)
7123 return -EOPNOTSUPP;
7124
7125 if (!wdev->p2p_started)
7126 return 0;
7127
Johannes Bergeeb126e2012-10-23 15:16:50 +02007128 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007129 wdev->p2p_started = false;
7130
7131 mutex_lock(&rdev->devlist_mtx);
7132 rdev->opencount--;
7133 mutex_unlock(&rdev->devlist_mtx);
7134
7135 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7136 rdev->scan_req->aborted = true;
7137 ___cfg80211_scan_done(rdev, true);
7138 }
7139
7140 return 0;
7141}
7142
Johannes Berg4c476992010-10-04 21:36:35 +02007143#define NL80211_FLAG_NEED_WIPHY 0x01
7144#define NL80211_FLAG_NEED_NETDEV 0x02
7145#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007146#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7147#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7148 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007149#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007150/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007151#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7152 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007153
7154static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7155 struct genl_info *info)
7156{
7157 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007158 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007159 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007160 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7161
7162 if (rtnl)
7163 rtnl_lock();
7164
7165 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007166 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007167 if (IS_ERR(rdev)) {
7168 if (rtnl)
7169 rtnl_unlock();
7170 return PTR_ERR(rdev);
7171 }
7172 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007173 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7174 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007175 mutex_lock(&cfg80211_mutex);
7176 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7177 info->attrs);
7178 if (IS_ERR(wdev)) {
7179 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007180 if (rtnl)
7181 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007182 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007183 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007184
Johannes Berg89a54e42012-06-15 14:33:17 +02007185 dev = wdev->netdev;
7186 rdev = wiphy_to_dev(wdev->wiphy);
7187
Johannes Berg1bf614e2012-06-15 15:23:36 +02007188 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7189 if (!dev) {
7190 mutex_unlock(&cfg80211_mutex);
7191 if (rtnl)
7192 rtnl_unlock();
7193 return -EINVAL;
7194 }
7195
7196 info->user_ptr[1] = dev;
7197 } else {
7198 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007199 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007200
Johannes Berg1bf614e2012-06-15 15:23:36 +02007201 if (dev) {
7202 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7203 !netif_running(dev)) {
7204 mutex_unlock(&cfg80211_mutex);
7205 if (rtnl)
7206 rtnl_unlock();
7207 return -ENETDOWN;
7208 }
7209
7210 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007211 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7212 if (!wdev->p2p_started) {
7213 mutex_unlock(&cfg80211_mutex);
7214 if (rtnl)
7215 rtnl_unlock();
7216 return -ENETDOWN;
7217 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007218 }
7219
Johannes Berg89a54e42012-06-15 14:33:17 +02007220 cfg80211_lock_rdev(rdev);
7221
7222 mutex_unlock(&cfg80211_mutex);
7223
Johannes Berg4c476992010-10-04 21:36:35 +02007224 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007225 }
7226
7227 return 0;
7228}
7229
7230static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7231 struct genl_info *info)
7232{
7233 if (info->user_ptr[0])
7234 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007235 if (info->user_ptr[1]) {
7236 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7237 struct wireless_dev *wdev = info->user_ptr[1];
7238
7239 if (wdev->netdev)
7240 dev_put(wdev->netdev);
7241 } else {
7242 dev_put(info->user_ptr[1]);
7243 }
7244 }
Johannes Berg4c476992010-10-04 21:36:35 +02007245 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7246 rtnl_unlock();
7247}
7248
Johannes Berg55682962007-09-20 13:09:35 -04007249static struct genl_ops nl80211_ops[] = {
7250 {
7251 .cmd = NL80211_CMD_GET_WIPHY,
7252 .doit = nl80211_get_wiphy,
7253 .dumpit = nl80211_dump_wiphy,
7254 .policy = nl80211_policy,
7255 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007256 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007257 },
7258 {
7259 .cmd = NL80211_CMD_SET_WIPHY,
7260 .doit = nl80211_set_wiphy,
7261 .policy = nl80211_policy,
7262 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007263 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007264 },
7265 {
7266 .cmd = NL80211_CMD_GET_INTERFACE,
7267 .doit = nl80211_get_interface,
7268 .dumpit = nl80211_dump_interface,
7269 .policy = nl80211_policy,
7270 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007271 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007272 },
7273 {
7274 .cmd = NL80211_CMD_SET_INTERFACE,
7275 .doit = nl80211_set_interface,
7276 .policy = nl80211_policy,
7277 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007278 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7279 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007280 },
7281 {
7282 .cmd = NL80211_CMD_NEW_INTERFACE,
7283 .doit = nl80211_new_interface,
7284 .policy = nl80211_policy,
7285 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007286 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7287 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007288 },
7289 {
7290 .cmd = NL80211_CMD_DEL_INTERFACE,
7291 .doit = nl80211_del_interface,
7292 .policy = nl80211_policy,
7293 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007294 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007295 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007296 },
Johannes Berg41ade002007-12-19 02:03:29 +01007297 {
7298 .cmd = NL80211_CMD_GET_KEY,
7299 .doit = nl80211_get_key,
7300 .policy = nl80211_policy,
7301 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007302 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007303 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007304 },
7305 {
7306 .cmd = NL80211_CMD_SET_KEY,
7307 .doit = nl80211_set_key,
7308 .policy = nl80211_policy,
7309 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007310 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007311 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007312 },
7313 {
7314 .cmd = NL80211_CMD_NEW_KEY,
7315 .doit = nl80211_new_key,
7316 .policy = nl80211_policy,
7317 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007318 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007319 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007320 },
7321 {
7322 .cmd = NL80211_CMD_DEL_KEY,
7323 .doit = nl80211_del_key,
7324 .policy = nl80211_policy,
7325 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007326 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007327 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007328 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007329 {
7330 .cmd = NL80211_CMD_SET_BEACON,
7331 .policy = nl80211_policy,
7332 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007333 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007334 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007335 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007336 },
7337 {
Johannes Berg88600202012-02-13 15:17:18 +01007338 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007339 .policy = nl80211_policy,
7340 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007341 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007342 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007343 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007344 },
7345 {
Johannes Berg88600202012-02-13 15:17:18 +01007346 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007347 .policy = nl80211_policy,
7348 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007349 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007350 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007351 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007352 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007353 {
7354 .cmd = NL80211_CMD_GET_STATION,
7355 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007356 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007357 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007358 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7359 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007360 },
7361 {
7362 .cmd = NL80211_CMD_SET_STATION,
7363 .doit = nl80211_set_station,
7364 .policy = nl80211_policy,
7365 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007366 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007367 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007368 },
7369 {
7370 .cmd = NL80211_CMD_NEW_STATION,
7371 .doit = nl80211_new_station,
7372 .policy = nl80211_policy,
7373 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007374 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007375 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007376 },
7377 {
7378 .cmd = NL80211_CMD_DEL_STATION,
7379 .doit = nl80211_del_station,
7380 .policy = nl80211_policy,
7381 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007382 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007383 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007384 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007385 {
7386 .cmd = NL80211_CMD_GET_MPATH,
7387 .doit = nl80211_get_mpath,
7388 .dumpit = nl80211_dump_mpath,
7389 .policy = nl80211_policy,
7390 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007391 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007392 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007393 },
7394 {
7395 .cmd = NL80211_CMD_SET_MPATH,
7396 .doit = nl80211_set_mpath,
7397 .policy = nl80211_policy,
7398 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007399 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007400 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007401 },
7402 {
7403 .cmd = NL80211_CMD_NEW_MPATH,
7404 .doit = nl80211_new_mpath,
7405 .policy = nl80211_policy,
7406 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007407 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007408 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007409 },
7410 {
7411 .cmd = NL80211_CMD_DEL_MPATH,
7412 .doit = nl80211_del_mpath,
7413 .policy = nl80211_policy,
7414 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007415 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007416 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007417 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007418 {
7419 .cmd = NL80211_CMD_SET_BSS,
7420 .doit = nl80211_set_bss,
7421 .policy = nl80211_policy,
7422 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007423 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007424 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007425 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007426 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007427 .cmd = NL80211_CMD_GET_REG,
7428 .doit = nl80211_get_reg,
7429 .policy = nl80211_policy,
7430 /* can be retrieved by unprivileged users */
7431 },
7432 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007433 .cmd = NL80211_CMD_SET_REG,
7434 .doit = nl80211_set_reg,
7435 .policy = nl80211_policy,
7436 .flags = GENL_ADMIN_PERM,
7437 },
7438 {
7439 .cmd = NL80211_CMD_REQ_SET_REG,
7440 .doit = nl80211_req_set_reg,
7441 .policy = nl80211_policy,
7442 .flags = GENL_ADMIN_PERM,
7443 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007444 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007445 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7446 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007447 .policy = nl80211_policy,
7448 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007449 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007450 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007451 },
7452 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007453 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7454 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007455 .policy = nl80211_policy,
7456 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007457 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007458 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007459 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007460 {
Johannes Berg2a519312009-02-10 21:25:55 +01007461 .cmd = NL80211_CMD_TRIGGER_SCAN,
7462 .doit = nl80211_trigger_scan,
7463 .policy = nl80211_policy,
7464 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007465 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007466 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007467 },
7468 {
7469 .cmd = NL80211_CMD_GET_SCAN,
7470 .policy = nl80211_policy,
7471 .dumpit = nl80211_dump_scan,
7472 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007473 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007474 .cmd = NL80211_CMD_START_SCHED_SCAN,
7475 .doit = nl80211_start_sched_scan,
7476 .policy = nl80211_policy,
7477 .flags = GENL_ADMIN_PERM,
7478 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7479 NL80211_FLAG_NEED_RTNL,
7480 },
7481 {
7482 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7483 .doit = nl80211_stop_sched_scan,
7484 .policy = nl80211_policy,
7485 .flags = GENL_ADMIN_PERM,
7486 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7487 NL80211_FLAG_NEED_RTNL,
7488 },
7489 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007490 .cmd = NL80211_CMD_AUTHENTICATE,
7491 .doit = nl80211_authenticate,
7492 .policy = nl80211_policy,
7493 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007494 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007495 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007496 },
7497 {
7498 .cmd = NL80211_CMD_ASSOCIATE,
7499 .doit = nl80211_associate,
7500 .policy = nl80211_policy,
7501 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007502 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007503 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007504 },
7505 {
7506 .cmd = NL80211_CMD_DEAUTHENTICATE,
7507 .doit = nl80211_deauthenticate,
7508 .policy = nl80211_policy,
7509 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007510 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007511 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007512 },
7513 {
7514 .cmd = NL80211_CMD_DISASSOCIATE,
7515 .doit = nl80211_disassociate,
7516 .policy = nl80211_policy,
7517 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007518 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007519 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007520 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007521 {
7522 .cmd = NL80211_CMD_JOIN_IBSS,
7523 .doit = nl80211_join_ibss,
7524 .policy = nl80211_policy,
7525 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007526 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007527 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007528 },
7529 {
7530 .cmd = NL80211_CMD_LEAVE_IBSS,
7531 .doit = nl80211_leave_ibss,
7532 .policy = nl80211_policy,
7533 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007534 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007535 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007536 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007537#ifdef CONFIG_NL80211_TESTMODE
7538 {
7539 .cmd = NL80211_CMD_TESTMODE,
7540 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007541 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007542 .policy = nl80211_policy,
7543 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007544 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7545 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007546 },
7547#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007548 {
7549 .cmd = NL80211_CMD_CONNECT,
7550 .doit = nl80211_connect,
7551 .policy = nl80211_policy,
7552 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007553 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007554 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007555 },
7556 {
7557 .cmd = NL80211_CMD_DISCONNECT,
7558 .doit = nl80211_disconnect,
7559 .policy = nl80211_policy,
7560 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007561 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007562 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007563 },
Johannes Berg463d0182009-07-14 00:33:35 +02007564 {
7565 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7566 .doit = nl80211_wiphy_netns,
7567 .policy = nl80211_policy,
7568 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007569 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7570 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007571 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007572 {
7573 .cmd = NL80211_CMD_GET_SURVEY,
7574 .policy = nl80211_policy,
7575 .dumpit = nl80211_dump_survey,
7576 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007577 {
7578 .cmd = NL80211_CMD_SET_PMKSA,
7579 .doit = nl80211_setdel_pmksa,
7580 .policy = nl80211_policy,
7581 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007582 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007583 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007584 },
7585 {
7586 .cmd = NL80211_CMD_DEL_PMKSA,
7587 .doit = nl80211_setdel_pmksa,
7588 .policy = nl80211_policy,
7589 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007590 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007591 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007592 },
7593 {
7594 .cmd = NL80211_CMD_FLUSH_PMKSA,
7595 .doit = nl80211_flush_pmksa,
7596 .policy = nl80211_policy,
7597 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007598 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007599 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007600 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007601 {
7602 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7603 .doit = nl80211_remain_on_channel,
7604 .policy = nl80211_policy,
7605 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007606 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007607 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007608 },
7609 {
7610 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7611 .doit = nl80211_cancel_remain_on_channel,
7612 .policy = nl80211_policy,
7613 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007614 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007615 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007616 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007617 {
7618 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7619 .doit = nl80211_set_tx_bitrate_mask,
7620 .policy = nl80211_policy,
7621 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007622 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7623 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007624 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007625 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007626 .cmd = NL80211_CMD_REGISTER_FRAME,
7627 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007628 .policy = nl80211_policy,
7629 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007630 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007631 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007632 },
7633 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007634 .cmd = NL80211_CMD_FRAME,
7635 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007636 .policy = nl80211_policy,
7637 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007638 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007639 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007640 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007641 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007642 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7643 .doit = nl80211_tx_mgmt_cancel_wait,
7644 .policy = nl80211_policy,
7645 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007646 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007647 NL80211_FLAG_NEED_RTNL,
7648 },
7649 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007650 .cmd = NL80211_CMD_SET_POWER_SAVE,
7651 .doit = nl80211_set_power_save,
7652 .policy = nl80211_policy,
7653 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007654 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7655 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007656 },
7657 {
7658 .cmd = NL80211_CMD_GET_POWER_SAVE,
7659 .doit = nl80211_get_power_save,
7660 .policy = nl80211_policy,
7661 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007662 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7663 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007664 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007665 {
7666 .cmd = NL80211_CMD_SET_CQM,
7667 .doit = nl80211_set_cqm,
7668 .policy = nl80211_policy,
7669 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007670 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7671 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007672 },
Johannes Bergf444de02010-05-05 15:25:02 +02007673 {
7674 .cmd = NL80211_CMD_SET_CHANNEL,
7675 .doit = nl80211_set_channel,
7676 .policy = nl80211_policy,
7677 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007678 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7679 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007680 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007681 {
7682 .cmd = NL80211_CMD_SET_WDS_PEER,
7683 .doit = nl80211_set_wds_peer,
7684 .policy = nl80211_policy,
7685 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007686 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7687 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007688 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007689 {
7690 .cmd = NL80211_CMD_JOIN_MESH,
7691 .doit = nl80211_join_mesh,
7692 .policy = nl80211_policy,
7693 .flags = GENL_ADMIN_PERM,
7694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7695 NL80211_FLAG_NEED_RTNL,
7696 },
7697 {
7698 .cmd = NL80211_CMD_LEAVE_MESH,
7699 .doit = nl80211_leave_mesh,
7700 .policy = nl80211_policy,
7701 .flags = GENL_ADMIN_PERM,
7702 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7703 NL80211_FLAG_NEED_RTNL,
7704 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007705#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007706 {
7707 .cmd = NL80211_CMD_GET_WOWLAN,
7708 .doit = nl80211_get_wowlan,
7709 .policy = nl80211_policy,
7710 /* can be retrieved by unprivileged users */
7711 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7712 NL80211_FLAG_NEED_RTNL,
7713 },
7714 {
7715 .cmd = NL80211_CMD_SET_WOWLAN,
7716 .doit = nl80211_set_wowlan,
7717 .policy = nl80211_policy,
7718 .flags = GENL_ADMIN_PERM,
7719 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7720 NL80211_FLAG_NEED_RTNL,
7721 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007722#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007723 {
7724 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7725 .doit = nl80211_set_rekey_data,
7726 .policy = nl80211_policy,
7727 .flags = GENL_ADMIN_PERM,
7728 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7729 NL80211_FLAG_NEED_RTNL,
7730 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007731 {
7732 .cmd = NL80211_CMD_TDLS_MGMT,
7733 .doit = nl80211_tdls_mgmt,
7734 .policy = nl80211_policy,
7735 .flags = GENL_ADMIN_PERM,
7736 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7737 NL80211_FLAG_NEED_RTNL,
7738 },
7739 {
7740 .cmd = NL80211_CMD_TDLS_OPER,
7741 .doit = nl80211_tdls_oper,
7742 .policy = nl80211_policy,
7743 .flags = GENL_ADMIN_PERM,
7744 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7745 NL80211_FLAG_NEED_RTNL,
7746 },
Johannes Berg28946da2011-11-04 11:18:12 +01007747 {
7748 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7749 .doit = nl80211_register_unexpected_frame,
7750 .policy = nl80211_policy,
7751 .flags = GENL_ADMIN_PERM,
7752 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7753 NL80211_FLAG_NEED_RTNL,
7754 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007755 {
7756 .cmd = NL80211_CMD_PROBE_CLIENT,
7757 .doit = nl80211_probe_client,
7758 .policy = nl80211_policy,
7759 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007760 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007761 NL80211_FLAG_NEED_RTNL,
7762 },
Johannes Berg5e7602302011-11-04 11:18:17 +01007763 {
7764 .cmd = NL80211_CMD_REGISTER_BEACONS,
7765 .doit = nl80211_register_beacons,
7766 .policy = nl80211_policy,
7767 .flags = GENL_ADMIN_PERM,
7768 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7769 NL80211_FLAG_NEED_RTNL,
7770 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007771 {
7772 .cmd = NL80211_CMD_SET_NOACK_MAP,
7773 .doit = nl80211_set_noack_map,
7774 .policy = nl80211_policy,
7775 .flags = GENL_ADMIN_PERM,
7776 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7777 NL80211_FLAG_NEED_RTNL,
7778 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007779 {
7780 .cmd = NL80211_CMD_START_P2P_DEVICE,
7781 .doit = nl80211_start_p2p_device,
7782 .policy = nl80211_policy,
7783 .flags = GENL_ADMIN_PERM,
7784 .internal_flags = NL80211_FLAG_NEED_WDEV |
7785 NL80211_FLAG_NEED_RTNL,
7786 },
7787 {
7788 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7789 .doit = nl80211_stop_p2p_device,
7790 .policy = nl80211_policy,
7791 .flags = GENL_ADMIN_PERM,
7792 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7793 NL80211_FLAG_NEED_RTNL,
7794 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007795 {
7796 .cmd = NL80211_CMD_SET_MCAST_RATE,
7797 .doit = nl80211_set_mcast_rate,
7798 .policy = nl80211_policy,
7799 .flags = GENL_ADMIN_PERM,
7800 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7801 NL80211_FLAG_NEED_RTNL,
7802 },
Johannes Berg55682962007-09-20 13:09:35 -04007803};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007804
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007805static struct genl_multicast_group nl80211_mlme_mcgrp = {
7806 .name = "mlme",
7807};
Johannes Berg55682962007-09-20 13:09:35 -04007808
7809/* multicast groups */
7810static struct genl_multicast_group nl80211_config_mcgrp = {
7811 .name = "config",
7812};
Johannes Berg2a519312009-02-10 21:25:55 +01007813static struct genl_multicast_group nl80211_scan_mcgrp = {
7814 .name = "scan",
7815};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007816static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7817 .name = "regulatory",
7818};
Johannes Berg55682962007-09-20 13:09:35 -04007819
7820/* notification functions */
7821
7822void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7823{
7824 struct sk_buff *msg;
7825
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007826 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007827 if (!msg)
7828 return;
7829
7830 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7831 nlmsg_free(msg);
7832 return;
7833 }
7834
Johannes Berg463d0182009-07-14 00:33:35 +02007835 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7836 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007837}
7838
Johannes Berg362a4152009-05-24 16:43:15 +02007839static int nl80211_add_scan_req(struct sk_buff *msg,
7840 struct cfg80211_registered_device *rdev)
7841{
7842 struct cfg80211_scan_request *req = rdev->scan_req;
7843 struct nlattr *nest;
7844 int i;
7845
Johannes Berg667503dd2009-07-07 03:56:11 +02007846 ASSERT_RDEV_LOCK(rdev);
7847
Johannes Berg362a4152009-05-24 16:43:15 +02007848 if (WARN_ON(!req))
7849 return 0;
7850
7851 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7852 if (!nest)
7853 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007854 for (i = 0; i < req->n_ssids; i++) {
7855 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7856 goto nla_put_failure;
7857 }
Johannes Berg362a4152009-05-24 16:43:15 +02007858 nla_nest_end(msg, nest);
7859
7860 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7861 if (!nest)
7862 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007863 for (i = 0; i < req->n_channels; i++) {
7864 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7865 goto nla_put_failure;
7866 }
Johannes Berg362a4152009-05-24 16:43:15 +02007867 nla_nest_end(msg, nest);
7868
David S. Miller9360ffd2012-03-29 04:41:26 -04007869 if (req->ie &&
7870 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7871 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007872
Sam Lefflered4737712012-10-11 21:03:31 -07007873 if (req->flags)
7874 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7875
Johannes Berg362a4152009-05-24 16:43:15 +02007876 return 0;
7877 nla_put_failure:
7878 return -ENOBUFS;
7879}
7880
Johannes Berga538e2d2009-06-16 19:56:42 +02007881static int nl80211_send_scan_msg(struct sk_buff *msg,
7882 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007883 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007884 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007885 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007886{
7887 void *hdr;
7888
Eric W. Biederman15e47302012-09-07 20:12:54 +00007889 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007890 if (!hdr)
7891 return -1;
7892
David S. Miller9360ffd2012-03-29 04:41:26 -04007893 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007894 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7895 wdev->netdev->ifindex)) ||
7896 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007897 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007898
Johannes Berg362a4152009-05-24 16:43:15 +02007899 /* ignore errors and send incomplete event anyway */
7900 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007901
7902 return genlmsg_end(msg, hdr);
7903
7904 nla_put_failure:
7905 genlmsg_cancel(msg, hdr);
7906 return -EMSGSIZE;
7907}
7908
Luciano Coelho807f8a82011-05-11 17:09:35 +03007909static int
7910nl80211_send_sched_scan_msg(struct sk_buff *msg,
7911 struct cfg80211_registered_device *rdev,
7912 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007913 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007914{
7915 void *hdr;
7916
Eric W. Biederman15e47302012-09-07 20:12:54 +00007917 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007918 if (!hdr)
7919 return -1;
7920
David S. Miller9360ffd2012-03-29 04:41:26 -04007921 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7922 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7923 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007924
7925 return genlmsg_end(msg, hdr);
7926
7927 nla_put_failure:
7928 genlmsg_cancel(msg, hdr);
7929 return -EMSGSIZE;
7930}
7931
Johannes Berga538e2d2009-06-16 19:56:42 +02007932void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007933 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007934{
7935 struct sk_buff *msg;
7936
Thomas Graf58050fc2012-06-28 03:57:45 +00007937 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007938 if (!msg)
7939 return;
7940
Johannes Bergfd014282012-06-18 19:17:03 +02007941 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007942 NL80211_CMD_TRIGGER_SCAN) < 0) {
7943 nlmsg_free(msg);
7944 return;
7945 }
7946
Johannes Berg463d0182009-07-14 00:33:35 +02007947 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7948 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007949}
7950
Johannes Berg2a519312009-02-10 21:25:55 +01007951void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007952 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007953{
7954 struct sk_buff *msg;
7955
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007956 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007957 if (!msg)
7958 return;
7959
Johannes Bergfd014282012-06-18 19:17:03 +02007960 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007961 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007962 nlmsg_free(msg);
7963 return;
7964 }
7965
Johannes Berg463d0182009-07-14 00:33:35 +02007966 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7967 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007968}
7969
7970void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007971 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007972{
7973 struct sk_buff *msg;
7974
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007975 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007976 if (!msg)
7977 return;
7978
Johannes Bergfd014282012-06-18 19:17:03 +02007979 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007980 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007981 nlmsg_free(msg);
7982 return;
7983 }
7984
Johannes Berg463d0182009-07-14 00:33:35 +02007985 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7986 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007987}
7988
Luciano Coelho807f8a82011-05-11 17:09:35 +03007989void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7990 struct net_device *netdev)
7991{
7992 struct sk_buff *msg;
7993
7994 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7995 if (!msg)
7996 return;
7997
7998 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7999 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8000 nlmsg_free(msg);
8001 return;
8002 }
8003
8004 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8005 nl80211_scan_mcgrp.id, GFP_KERNEL);
8006}
8007
8008void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8009 struct net_device *netdev, u32 cmd)
8010{
8011 struct sk_buff *msg;
8012
Thomas Graf58050fc2012-06-28 03:57:45 +00008013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008014 if (!msg)
8015 return;
8016
8017 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8018 nlmsg_free(msg);
8019 return;
8020 }
8021
8022 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8023 nl80211_scan_mcgrp.id, GFP_KERNEL);
8024}
8025
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008026/*
8027 * This can happen on global regulatory changes or device specific settings
8028 * based on custom world regulatory domains.
8029 */
8030void nl80211_send_reg_change_event(struct regulatory_request *request)
8031{
8032 struct sk_buff *msg;
8033 void *hdr;
8034
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008035 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008036 if (!msg)
8037 return;
8038
8039 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8040 if (!hdr) {
8041 nlmsg_free(msg);
8042 return;
8043 }
8044
8045 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008046 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8047 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008048
David S. Miller9360ffd2012-03-29 04:41:26 -04008049 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8050 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8051 NL80211_REGDOM_TYPE_WORLD))
8052 goto nla_put_failure;
8053 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8054 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8055 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8056 goto nla_put_failure;
8057 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8058 request->intersect) {
8059 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8060 NL80211_REGDOM_TYPE_INTERSECTION))
8061 goto nla_put_failure;
8062 } else {
8063 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8064 NL80211_REGDOM_TYPE_COUNTRY) ||
8065 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8066 request->alpha2))
8067 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008068 }
8069
David S. Miller9360ffd2012-03-29 04:41:26 -04008070 if (wiphy_idx_valid(request->wiphy_idx) &&
8071 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8072 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008073
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008074 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008075
Johannes Bergbc43b282009-07-25 10:54:13 +02008076 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008077 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008078 GFP_ATOMIC);
8079 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008080
8081 return;
8082
8083nla_put_failure:
8084 genlmsg_cancel(msg, hdr);
8085 nlmsg_free(msg);
8086}
8087
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008088static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8089 struct net_device *netdev,
8090 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008091 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008092{
8093 struct sk_buff *msg;
8094 void *hdr;
8095
Johannes Berge6d6e342009-07-01 21:26:47 +02008096 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008097 if (!msg)
8098 return;
8099
8100 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8101 if (!hdr) {
8102 nlmsg_free(msg);
8103 return;
8104 }
8105
David S. Miller9360ffd2012-03-29 04:41:26 -04008106 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8107 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8108 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8109 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008110
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008111 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008112
Johannes Berg463d0182009-07-14 00:33:35 +02008113 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8114 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008115 return;
8116
8117 nla_put_failure:
8118 genlmsg_cancel(msg, hdr);
8119 nlmsg_free(msg);
8120}
8121
8122void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008123 struct net_device *netdev, const u8 *buf,
8124 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008125{
8126 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008127 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008128}
8129
8130void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8131 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008132 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008133{
Johannes Berge6d6e342009-07-01 21:26:47 +02008134 nl80211_send_mlme_event(rdev, netdev, buf, len,
8135 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008136}
8137
Jouni Malinen53b46b82009-03-27 20:53:56 +02008138void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008139 struct net_device *netdev, const u8 *buf,
8140 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008141{
8142 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008143 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008144}
8145
Jouni Malinen53b46b82009-03-27 20:53:56 +02008146void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8147 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008148 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008149{
8150 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008151 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008152}
8153
Jouni Malinencf4e5942010-12-16 00:52:40 +02008154void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8155 struct net_device *netdev, const u8 *buf,
8156 size_t len, gfp_t gfp)
8157{
8158 nl80211_send_mlme_event(rdev, netdev, buf, len,
8159 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8160}
8161
8162void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8163 struct net_device *netdev, const u8 *buf,
8164 size_t len, gfp_t gfp)
8165{
8166 nl80211_send_mlme_event(rdev, netdev, buf, len,
8167 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8168}
8169
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008170static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8171 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008172 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008173{
8174 struct sk_buff *msg;
8175 void *hdr;
8176
Johannes Berge6d6e342009-07-01 21:26:47 +02008177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008178 if (!msg)
8179 return;
8180
8181 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8182 if (!hdr) {
8183 nlmsg_free(msg);
8184 return;
8185 }
8186
David S. Miller9360ffd2012-03-29 04:41:26 -04008187 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8188 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8189 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8190 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8191 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008192
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008193 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008194
Johannes Berg463d0182009-07-14 00:33:35 +02008195 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8196 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008197 return;
8198
8199 nla_put_failure:
8200 genlmsg_cancel(msg, hdr);
8201 nlmsg_free(msg);
8202}
8203
8204void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008205 struct net_device *netdev, const u8 *addr,
8206 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008207{
8208 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008209 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008210}
8211
8212void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008213 struct net_device *netdev, const u8 *addr,
8214 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008215{
Johannes Berge6d6e342009-07-01 21:26:47 +02008216 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8217 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008218}
8219
Samuel Ortizb23aa672009-07-01 21:26:54 +02008220void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8221 struct net_device *netdev, const u8 *bssid,
8222 const u8 *req_ie, size_t req_ie_len,
8223 const u8 *resp_ie, size_t resp_ie_len,
8224 u16 status, gfp_t gfp)
8225{
8226 struct sk_buff *msg;
8227 void *hdr;
8228
Thomas Graf58050fc2012-06-28 03:57:45 +00008229 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008230 if (!msg)
8231 return;
8232
8233 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8234 if (!hdr) {
8235 nlmsg_free(msg);
8236 return;
8237 }
8238
David S. Miller9360ffd2012-03-29 04:41:26 -04008239 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8240 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8241 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8242 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8243 (req_ie &&
8244 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8245 (resp_ie &&
8246 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8247 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008248
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008249 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008250
Johannes Berg463d0182009-07-14 00:33:35 +02008251 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8252 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008253 return;
8254
8255 nla_put_failure:
8256 genlmsg_cancel(msg, hdr);
8257 nlmsg_free(msg);
8258
8259}
8260
8261void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8262 struct net_device *netdev, const u8 *bssid,
8263 const u8 *req_ie, size_t req_ie_len,
8264 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8265{
8266 struct sk_buff *msg;
8267 void *hdr;
8268
Thomas Graf58050fc2012-06-28 03:57:45 +00008269 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008270 if (!msg)
8271 return;
8272
8273 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8274 if (!hdr) {
8275 nlmsg_free(msg);
8276 return;
8277 }
8278
David S. Miller9360ffd2012-03-29 04:41:26 -04008279 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8280 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8281 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8282 (req_ie &&
8283 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8284 (resp_ie &&
8285 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8286 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008287
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008288 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008289
Johannes Berg463d0182009-07-14 00:33:35 +02008290 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8291 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008292 return;
8293
8294 nla_put_failure:
8295 genlmsg_cancel(msg, hdr);
8296 nlmsg_free(msg);
8297
8298}
8299
8300void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8301 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008302 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008303{
8304 struct sk_buff *msg;
8305 void *hdr;
8306
Thomas Graf58050fc2012-06-28 03:57:45 +00008307 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008308 if (!msg)
8309 return;
8310
8311 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8312 if (!hdr) {
8313 nlmsg_free(msg);
8314 return;
8315 }
8316
David S. Miller9360ffd2012-03-29 04:41:26 -04008317 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8318 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8319 (from_ap && reason &&
8320 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8321 (from_ap &&
8322 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8323 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8324 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008325
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008326 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008327
Johannes Berg463d0182009-07-14 00:33:35 +02008328 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8329 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008330 return;
8331
8332 nla_put_failure:
8333 genlmsg_cancel(msg, hdr);
8334 nlmsg_free(msg);
8335
8336}
8337
Johannes Berg04a773a2009-04-19 21:24:32 +02008338void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8339 struct net_device *netdev, const u8 *bssid,
8340 gfp_t gfp)
8341{
8342 struct sk_buff *msg;
8343 void *hdr;
8344
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008345 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008346 if (!msg)
8347 return;
8348
8349 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8350 if (!hdr) {
8351 nlmsg_free(msg);
8352 return;
8353 }
8354
David S. Miller9360ffd2012-03-29 04:41:26 -04008355 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8356 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8357 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8358 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008359
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008360 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008361
Johannes Berg463d0182009-07-14 00:33:35 +02008362 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8363 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008364 return;
8365
8366 nla_put_failure:
8367 genlmsg_cancel(msg, hdr);
8368 nlmsg_free(msg);
8369}
8370
Javier Cardonac93b5e72011-04-07 15:08:34 -07008371void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8372 struct net_device *netdev,
8373 const u8 *macaddr, const u8* ie, u8 ie_len,
8374 gfp_t gfp)
8375{
8376 struct sk_buff *msg;
8377 void *hdr;
8378
8379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8380 if (!msg)
8381 return;
8382
8383 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8384 if (!hdr) {
8385 nlmsg_free(msg);
8386 return;
8387 }
8388
David S. Miller9360ffd2012-03-29 04:41:26 -04008389 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8390 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8391 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8392 (ie_len && ie &&
8393 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8394 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008395
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008396 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008397
8398 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8399 nl80211_mlme_mcgrp.id, gfp);
8400 return;
8401
8402 nla_put_failure:
8403 genlmsg_cancel(msg, hdr);
8404 nlmsg_free(msg);
8405}
8406
Jouni Malinena3b8b052009-03-27 21:59:49 +02008407void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8408 struct net_device *netdev, const u8 *addr,
8409 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008410 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008411{
8412 struct sk_buff *msg;
8413 void *hdr;
8414
Johannes Berge6d6e342009-07-01 21:26:47 +02008415 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008416 if (!msg)
8417 return;
8418
8419 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8420 if (!hdr) {
8421 nlmsg_free(msg);
8422 return;
8423 }
8424
David S. Miller9360ffd2012-03-29 04:41:26 -04008425 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8426 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8427 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8428 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8429 (key_id != -1 &&
8430 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8431 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8432 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008433
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008434 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008435
Johannes Berg463d0182009-07-14 00:33:35 +02008436 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8437 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008438 return;
8439
8440 nla_put_failure:
8441 genlmsg_cancel(msg, hdr);
8442 nlmsg_free(msg);
8443}
8444
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008445void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8446 struct ieee80211_channel *channel_before,
8447 struct ieee80211_channel *channel_after)
8448{
8449 struct sk_buff *msg;
8450 void *hdr;
8451 struct nlattr *nl_freq;
8452
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008453 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008454 if (!msg)
8455 return;
8456
8457 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8458 if (!hdr) {
8459 nlmsg_free(msg);
8460 return;
8461 }
8462
8463 /*
8464 * Since we are applying the beacon hint to a wiphy we know its
8465 * wiphy_idx is valid
8466 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008467 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8468 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008469
8470 /* Before */
8471 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8472 if (!nl_freq)
8473 goto nla_put_failure;
8474 if (nl80211_msg_put_channel(msg, channel_before))
8475 goto nla_put_failure;
8476 nla_nest_end(msg, nl_freq);
8477
8478 /* After */
8479 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8480 if (!nl_freq)
8481 goto nla_put_failure;
8482 if (nl80211_msg_put_channel(msg, channel_after))
8483 goto nla_put_failure;
8484 nla_nest_end(msg, nl_freq);
8485
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008486 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008487
Johannes Berg463d0182009-07-14 00:33:35 +02008488 rcu_read_lock();
8489 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8490 GFP_ATOMIC);
8491 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008492
8493 return;
8494
8495nla_put_failure:
8496 genlmsg_cancel(msg, hdr);
8497 nlmsg_free(msg);
8498}
8499
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008500static void nl80211_send_remain_on_chan_event(
8501 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008502 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008503 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008504 unsigned int duration, gfp_t gfp)
8505{
8506 struct sk_buff *msg;
8507 void *hdr;
8508
8509 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8510 if (!msg)
8511 return;
8512
8513 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8514 if (!hdr) {
8515 nlmsg_free(msg);
8516 return;
8517 }
8518
David S. Miller9360ffd2012-03-29 04:41:26 -04008519 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008520 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8521 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008522 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008523 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01008524 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
8525 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008526 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8527 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008528
David S. Miller9360ffd2012-03-29 04:41:26 -04008529 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8530 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8531 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008532
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008533 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008534
8535 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8536 nl80211_mlme_mcgrp.id, gfp);
8537 return;
8538
8539 nla_put_failure:
8540 genlmsg_cancel(msg, hdr);
8541 nlmsg_free(msg);
8542}
8543
8544void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008545 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008546 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008547 unsigned int duration, gfp_t gfp)
8548{
8549 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008550 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01008551 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008552}
8553
8554void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008555 struct cfg80211_registered_device *rdev,
8556 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01008557 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008558{
8559 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01008560 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008561}
8562
Johannes Berg98b62182009-12-23 13:15:44 +01008563void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8564 struct net_device *dev, const u8 *mac_addr,
8565 struct station_info *sinfo, gfp_t gfp)
8566{
8567 struct sk_buff *msg;
8568
Thomas Graf58050fc2012-06-28 03:57:45 +00008569 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008570 if (!msg)
8571 return;
8572
John W. Linville66266b32012-03-15 13:25:41 -04008573 if (nl80211_send_station(msg, 0, 0, 0,
8574 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008575 nlmsg_free(msg);
8576 return;
8577 }
8578
8579 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8580 nl80211_mlme_mcgrp.id, gfp);
8581}
8582
Jouni Malinenec15e682011-03-23 15:29:52 +02008583void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8584 struct net_device *dev, const u8 *mac_addr,
8585 gfp_t gfp)
8586{
8587 struct sk_buff *msg;
8588 void *hdr;
8589
Thomas Graf58050fc2012-06-28 03:57:45 +00008590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008591 if (!msg)
8592 return;
8593
8594 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8595 if (!hdr) {
8596 nlmsg_free(msg);
8597 return;
8598 }
8599
David S. Miller9360ffd2012-03-29 04:41:26 -04008600 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8601 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8602 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008603
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008604 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008605
8606 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8607 nl80211_mlme_mcgrp.id, gfp);
8608 return;
8609
8610 nla_put_failure:
8611 genlmsg_cancel(msg, hdr);
8612 nlmsg_free(msg);
8613}
8614
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308615void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8616 struct net_device *dev, const u8 *mac_addr,
8617 enum nl80211_connect_failed_reason reason,
8618 gfp_t gfp)
8619{
8620 struct sk_buff *msg;
8621 void *hdr;
8622
8623 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8624 if (!msg)
8625 return;
8626
8627 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8628 if (!hdr) {
8629 nlmsg_free(msg);
8630 return;
8631 }
8632
8633 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8634 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8635 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8636 goto nla_put_failure;
8637
8638 genlmsg_end(msg, hdr);
8639
8640 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8641 nl80211_mlme_mcgrp.id, gfp);
8642 return;
8643
8644 nla_put_failure:
8645 genlmsg_cancel(msg, hdr);
8646 nlmsg_free(msg);
8647}
8648
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008649static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8650 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008651{
8652 struct wireless_dev *wdev = dev->ieee80211_ptr;
8653 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8654 struct sk_buff *msg;
8655 void *hdr;
8656 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008657 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008658
Eric W. Biederman15e47302012-09-07 20:12:54 +00008659 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008660 return false;
8661
8662 msg = nlmsg_new(100, gfp);
8663 if (!msg)
8664 return true;
8665
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008666 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008667 if (!hdr) {
8668 nlmsg_free(msg);
8669 return true;
8670 }
8671
David S. Miller9360ffd2012-03-29 04:41:26 -04008672 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8673 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8674 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8675 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008676
8677 err = genlmsg_end(msg, hdr);
8678 if (err < 0) {
8679 nlmsg_free(msg);
8680 return true;
8681 }
8682
Eric W. Biederman15e47302012-09-07 20:12:54 +00008683 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008684 return true;
8685
8686 nla_put_failure:
8687 genlmsg_cancel(msg, hdr);
8688 nlmsg_free(msg);
8689 return true;
8690}
8691
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008692bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8693{
8694 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8695 addr, gfp);
8696}
8697
8698bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8699 const u8 *addr, gfp_t gfp)
8700{
8701 return __nl80211_unexpected_frame(dev,
8702 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8703 addr, gfp);
8704}
8705
Johannes Berg2e161f72010-08-12 15:38:38 +02008706int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008707 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008708 int freq, int sig_dbm,
8709 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008710{
Johannes Berg71bbc992012-06-15 15:30:18 +02008711 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008712 struct sk_buff *msg;
8713 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008714
8715 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8716 if (!msg)
8717 return -ENOMEM;
8718
Johannes Berg2e161f72010-08-12 15:38:38 +02008719 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008720 if (!hdr) {
8721 nlmsg_free(msg);
8722 return -ENOMEM;
8723 }
8724
David S. Miller9360ffd2012-03-29 04:41:26 -04008725 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008726 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8727 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008728 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8729 (sig_dbm &&
8730 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8731 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8732 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008733
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008734 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008735
Eric W. Biederman15e47302012-09-07 20:12:54 +00008736 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008737
8738 nla_put_failure:
8739 genlmsg_cancel(msg, hdr);
8740 nlmsg_free(msg);
8741 return -ENOBUFS;
8742}
8743
Johannes Berg2e161f72010-08-12 15:38:38 +02008744void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008745 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008746 const u8 *buf, size_t len, bool ack,
8747 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008748{
Johannes Berg71bbc992012-06-15 15:30:18 +02008749 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008750 struct sk_buff *msg;
8751 void *hdr;
8752
8753 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8754 if (!msg)
8755 return;
8756
Johannes Berg2e161f72010-08-12 15:38:38 +02008757 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008758 if (!hdr) {
8759 nlmsg_free(msg);
8760 return;
8761 }
8762
David S. Miller9360ffd2012-03-29 04:41:26 -04008763 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008764 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8765 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008766 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8767 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8768 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8769 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008770
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008771 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008772
8773 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8774 return;
8775
8776 nla_put_failure:
8777 genlmsg_cancel(msg, hdr);
8778 nlmsg_free(msg);
8779}
8780
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008781void
8782nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8783 struct net_device *netdev,
8784 enum nl80211_cqm_rssi_threshold_event rssi_event,
8785 gfp_t gfp)
8786{
8787 struct sk_buff *msg;
8788 struct nlattr *pinfoattr;
8789 void *hdr;
8790
Thomas Graf58050fc2012-06-28 03:57:45 +00008791 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008792 if (!msg)
8793 return;
8794
8795 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8796 if (!hdr) {
8797 nlmsg_free(msg);
8798 return;
8799 }
8800
David S. Miller9360ffd2012-03-29 04:41:26 -04008801 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8802 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8803 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008804
8805 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8806 if (!pinfoattr)
8807 goto nla_put_failure;
8808
David S. Miller9360ffd2012-03-29 04:41:26 -04008809 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8810 rssi_event))
8811 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008812
8813 nla_nest_end(msg, pinfoattr);
8814
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008815 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008816
8817 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8818 nl80211_mlme_mcgrp.id, gfp);
8819 return;
8820
8821 nla_put_failure:
8822 genlmsg_cancel(msg, hdr);
8823 nlmsg_free(msg);
8824}
8825
Johannes Berge5497d72011-07-05 16:35:40 +02008826void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8827 struct net_device *netdev, const u8 *bssid,
8828 const u8 *replay_ctr, gfp_t gfp)
8829{
8830 struct sk_buff *msg;
8831 struct nlattr *rekey_attr;
8832 void *hdr;
8833
Thomas Graf58050fc2012-06-28 03:57:45 +00008834 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008835 if (!msg)
8836 return;
8837
8838 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8839 if (!hdr) {
8840 nlmsg_free(msg);
8841 return;
8842 }
8843
David S. Miller9360ffd2012-03-29 04:41:26 -04008844 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8845 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8846 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8847 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008848
8849 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8850 if (!rekey_attr)
8851 goto nla_put_failure;
8852
David S. Miller9360ffd2012-03-29 04:41:26 -04008853 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8854 NL80211_REPLAY_CTR_LEN, replay_ctr))
8855 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008856
8857 nla_nest_end(msg, rekey_attr);
8858
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008859 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008860
8861 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8862 nl80211_mlme_mcgrp.id, gfp);
8863 return;
8864
8865 nla_put_failure:
8866 genlmsg_cancel(msg, hdr);
8867 nlmsg_free(msg);
8868}
8869
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008870void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8871 struct net_device *netdev, int index,
8872 const u8 *bssid, bool preauth, gfp_t gfp)
8873{
8874 struct sk_buff *msg;
8875 struct nlattr *attr;
8876 void *hdr;
8877
Thomas Graf58050fc2012-06-28 03:57:45 +00008878 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008879 if (!msg)
8880 return;
8881
8882 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8883 if (!hdr) {
8884 nlmsg_free(msg);
8885 return;
8886 }
8887
David S. Miller9360ffd2012-03-29 04:41:26 -04008888 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8889 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8890 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008891
8892 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8893 if (!attr)
8894 goto nla_put_failure;
8895
David S. Miller9360ffd2012-03-29 04:41:26 -04008896 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8897 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8898 (preauth &&
8899 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8900 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008901
8902 nla_nest_end(msg, attr);
8903
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008904 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008905
8906 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8907 nl80211_mlme_mcgrp.id, gfp);
8908 return;
8909
8910 nla_put_failure:
8911 genlmsg_cancel(msg, hdr);
8912 nlmsg_free(msg);
8913}
8914
Thomas Pedersen53145262012-04-06 13:35:47 -07008915void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01008916 struct net_device *netdev,
8917 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07008918{
8919 struct sk_buff *msg;
8920 void *hdr;
8921
Thomas Graf58050fc2012-06-28 03:57:45 +00008922 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008923 if (!msg)
8924 return;
8925
8926 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8927 if (!hdr) {
8928 nlmsg_free(msg);
8929 return;
8930 }
8931
Johannes Berg683b6d32012-11-08 21:25:48 +01008932 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8933 goto nla_put_failure;
8934
8935 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04008936 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008937
8938 genlmsg_end(msg, hdr);
8939
8940 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8941 nl80211_mlme_mcgrp.id, gfp);
8942 return;
8943
8944 nla_put_failure:
8945 genlmsg_cancel(msg, hdr);
8946 nlmsg_free(msg);
8947}
8948
Johannes Bergc063dbf2010-11-24 08:10:05 +01008949void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008950nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8951 struct net_device *netdev, const u8 *peer,
8952 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8953{
8954 struct sk_buff *msg;
8955 struct nlattr *pinfoattr;
8956 void *hdr;
8957
8958 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8959 if (!msg)
8960 return;
8961
8962 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8963 if (!hdr) {
8964 nlmsg_free(msg);
8965 return;
8966 }
8967
8968 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8969 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8970 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8971 goto nla_put_failure;
8972
8973 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8974 if (!pinfoattr)
8975 goto nla_put_failure;
8976
8977 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8978 goto nla_put_failure;
8979
8980 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8981 goto nla_put_failure;
8982
8983 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8984 goto nla_put_failure;
8985
8986 nla_nest_end(msg, pinfoattr);
8987
8988 genlmsg_end(msg, hdr);
8989
8990 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8991 nl80211_mlme_mcgrp.id, gfp);
8992 return;
8993
8994 nla_put_failure:
8995 genlmsg_cancel(msg, hdr);
8996 nlmsg_free(msg);
8997}
8998
8999void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009000nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9001 struct net_device *netdev, const u8 *peer,
9002 u32 num_packets, gfp_t gfp)
9003{
9004 struct sk_buff *msg;
9005 struct nlattr *pinfoattr;
9006 void *hdr;
9007
Thomas Graf58050fc2012-06-28 03:57:45 +00009008 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009009 if (!msg)
9010 return;
9011
9012 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9013 if (!hdr) {
9014 nlmsg_free(msg);
9015 return;
9016 }
9017
David S. Miller9360ffd2012-03-29 04:41:26 -04009018 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9019 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9020 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9021 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009022
9023 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9024 if (!pinfoattr)
9025 goto nla_put_failure;
9026
David S. Miller9360ffd2012-03-29 04:41:26 -04009027 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9028 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009029
9030 nla_nest_end(msg, pinfoattr);
9031
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009032 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009033
9034 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9035 nl80211_mlme_mcgrp.id, gfp);
9036 return;
9037
9038 nla_put_failure:
9039 genlmsg_cancel(msg, hdr);
9040 nlmsg_free(msg);
9041}
9042
Johannes Berg7f6cf312011-11-04 11:18:15 +01009043void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9044 u64 cookie, bool acked, gfp_t gfp)
9045{
9046 struct wireless_dev *wdev = dev->ieee80211_ptr;
9047 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9048 struct sk_buff *msg;
9049 void *hdr;
9050 int err;
9051
Beni Lev4ee3e062012-08-27 12:49:39 +03009052 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9053
Thomas Graf58050fc2012-06-28 03:57:45 +00009054 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009055
Johannes Berg7f6cf312011-11-04 11:18:15 +01009056 if (!msg)
9057 return;
9058
9059 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9060 if (!hdr) {
9061 nlmsg_free(msg);
9062 return;
9063 }
9064
David S. Miller9360ffd2012-03-29 04:41:26 -04009065 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9066 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9067 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9068 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9069 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9070 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009071
9072 err = genlmsg_end(msg, hdr);
9073 if (err < 0) {
9074 nlmsg_free(msg);
9075 return;
9076 }
9077
9078 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9079 nl80211_mlme_mcgrp.id, gfp);
9080 return;
9081
9082 nla_put_failure:
9083 genlmsg_cancel(msg, hdr);
9084 nlmsg_free(msg);
9085}
9086EXPORT_SYMBOL(cfg80211_probe_status);
9087
Johannes Berg5e7602302011-11-04 11:18:17 +01009088void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9089 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009090 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +01009091{
9092 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9093 struct sk_buff *msg;
9094 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009095 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +01009096
Beni Lev4ee3e062012-08-27 12:49:39 +03009097 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9098
Ben Greear37c73b52012-10-26 14:49:25 -07009099 spin_lock_bh(&rdev->beacon_registrations_lock);
9100 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9101 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9102 if (!msg) {
9103 spin_unlock_bh(&rdev->beacon_registrations_lock);
9104 return;
9105 }
Johannes Berg5e7602302011-11-04 11:18:17 +01009106
Ben Greear37c73b52012-10-26 14:49:25 -07009107 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9108 if (!hdr)
9109 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01009110
Ben Greear37c73b52012-10-26 14:49:25 -07009111 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9112 (freq &&
9113 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9114 (sig_dbm &&
9115 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9116 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9117 goto nla_put_failure;
9118
9119 genlmsg_end(msg, hdr);
9120
9121 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +01009122 }
Ben Greear37c73b52012-10-26 14:49:25 -07009123 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01009124 return;
9125
9126 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009127 spin_unlock_bh(&rdev->beacon_registrations_lock);
9128 if (hdr)
9129 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +01009130 nlmsg_free(msg);
9131}
9132EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9133
Jouni Malinen3475b092012-11-16 22:49:57 +02009134void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9135 enum nl80211_tdls_operation oper,
9136 u16 reason_code, gfp_t gfp)
9137{
9138 struct wireless_dev *wdev = dev->ieee80211_ptr;
9139 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9140 struct sk_buff *msg;
9141 void *hdr;
9142 int err;
9143
9144 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9145 reason_code);
9146
9147 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9148 if (!msg)
9149 return;
9150
9151 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9152 if (!hdr) {
9153 nlmsg_free(msg);
9154 return;
9155 }
9156
9157 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9158 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9159 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9160 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9161 (reason_code > 0 &&
9162 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9163 goto nla_put_failure;
9164
9165 err = genlmsg_end(msg, hdr);
9166 if (err < 0) {
9167 nlmsg_free(msg);
9168 return;
9169 }
9170
9171 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9172 nl80211_mlme_mcgrp.id, gfp);
9173 return;
9174
9175 nla_put_failure:
9176 genlmsg_cancel(msg, hdr);
9177 nlmsg_free(msg);
9178}
9179EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9180
Jouni Malinen026331c2010-02-15 12:53:10 +02009181static int nl80211_netlink_notify(struct notifier_block * nb,
9182 unsigned long state,
9183 void *_notify)
9184{
9185 struct netlink_notify *notify = _notify;
9186 struct cfg80211_registered_device *rdev;
9187 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009188 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009189
9190 if (state != NETLINK_URELEASE)
9191 return NOTIFY_DONE;
9192
9193 rcu_read_lock();
9194
Johannes Berg5e7602302011-11-04 11:18:17 +01009195 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009196 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009197 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009198
9199 spin_lock_bh(&rdev->beacon_registrations_lock);
9200 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9201 list) {
9202 if (reg->nlportid == notify->portid) {
9203 list_del(&reg->list);
9204 kfree(reg);
9205 break;
9206 }
9207 }
9208 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01009209 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009210
9211 rcu_read_unlock();
9212
9213 return NOTIFY_DONE;
9214}
9215
9216static struct notifier_block nl80211_netlink_notifier = {
9217 .notifier_call = nl80211_netlink_notify,
9218};
9219
Johannes Berg55682962007-09-20 13:09:35 -04009220/* initialisation/exit functions */
9221
9222int nl80211_init(void)
9223{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009224 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009225
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009226 err = genl_register_family_with_ops(&nl80211_fam,
9227 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009228 if (err)
9229 return err;
9230
Johannes Berg55682962007-09-20 13:09:35 -04009231 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9232 if (err)
9233 goto err_out;
9234
Johannes Berg2a519312009-02-10 21:25:55 +01009235 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9236 if (err)
9237 goto err_out;
9238
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009239 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9240 if (err)
9241 goto err_out;
9242
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009243 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9244 if (err)
9245 goto err_out;
9246
Johannes Bergaff89a92009-07-01 21:26:51 +02009247#ifdef CONFIG_NL80211_TESTMODE
9248 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9249 if (err)
9250 goto err_out;
9251#endif
9252
Jouni Malinen026331c2010-02-15 12:53:10 +02009253 err = netlink_register_notifier(&nl80211_netlink_notifier);
9254 if (err)
9255 goto err_out;
9256
Johannes Berg55682962007-09-20 13:09:35 -04009257 return 0;
9258 err_out:
9259 genl_unregister_family(&nl80211_fam);
9260 return err;
9261}
9262
9263void nl80211_exit(void)
9264{
Jouni Malinen026331c2010-02-15 12:53:10 +02009265 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009266 genl_unregister_family(&nl80211_fam);
9267}