blob: cc0fad30b8c9de64b9967bb28751f7b2534b53ed [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030025#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040026
Jouni Malinen5fb628e2011-08-10 23:54:35 +030027static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg89a54e42012-06-15 14:33:17 +020049/* returns ERR_PTR values */
50static struct wireless_dev *
51__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040052{
Johannes Berg89a54e42012-06-15 14:33:17 +020053 struct cfg80211_registered_device *rdev;
54 struct wireless_dev *result = NULL;
55 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
56 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
57 u64 wdev_id;
58 int wiphy_idx = -1;
59 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040060
Johannes Berg89a54e42012-06-15 14:33:17 +020061 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040062
Johannes Berg89a54e42012-06-15 14:33:17 +020063 if (!have_ifidx && !have_wdev_id)
64 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040065
Johannes Berg89a54e42012-06-15 14:33:17 +020066 if (have_ifidx)
67 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
68 if (have_wdev_id) {
69 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
70 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040071 }
72
Johannes Berg89a54e42012-06-15 14:33:17 +020073 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
74 struct wireless_dev *wdev;
75
76 if (wiphy_net(&rdev->wiphy) != netns)
77 continue;
78
79 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
80 continue;
81
82 mutex_lock(&rdev->devlist_mtx);
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
88 }
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
92 }
93 }
94 mutex_unlock(&rdev->devlist_mtx);
95
96 if (result)
97 break;
98 }
99
100 if (result)
101 return result;
102 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400103}
104
Johannes Berga9455402012-06-15 13:32:49 +0200105static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200106__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200107{
Johannes Berg7fee4772012-06-15 14:09:58 +0200108 struct cfg80211_registered_device *rdev = NULL, *tmp;
109 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200110
111 assert_cfg80211_lock();
112
Johannes Berg878d9ec2012-06-15 14:18:32 +0200113 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200114 !attrs[NL80211_ATTR_IFINDEX] &&
115 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200116 return ERR_PTR(-EINVAL);
117
Johannes Berg878d9ec2012-06-15 14:18:32 +0200118 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200119 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200120 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200121
Johannes Berg89a54e42012-06-15 14:33:17 +0200122 if (attrs[NL80211_ATTR_WDEV]) {
123 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
124 struct wireless_dev *wdev;
125 bool found = false;
126
127 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
128 if (tmp) {
129 /* make sure wdev exists */
130 mutex_lock(&tmp->devlist_mtx);
131 list_for_each_entry(wdev, &tmp->wdev_list, list) {
132 if (wdev->identifier != (u32)wdev_id)
133 continue;
134 found = true;
135 break;
136 }
137 mutex_unlock(&tmp->devlist_mtx);
138
139 if (!found)
140 tmp = NULL;
141
142 if (rdev && tmp != rdev)
143 return ERR_PTR(-EINVAL);
144 rdev = tmp;
145 }
146 }
147
Johannes Berg878d9ec2012-06-15 14:18:32 +0200148 if (attrs[NL80211_ATTR_IFINDEX]) {
149 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200150 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200151 if (netdev) {
152 if (netdev->ieee80211_ptr)
153 tmp = wiphy_to_dev(
154 netdev->ieee80211_ptr->wiphy);
155 else
156 tmp = NULL;
157
158 dev_put(netdev);
159
160 /* not wireless device -- return error */
161 if (!tmp)
162 return ERR_PTR(-EINVAL);
163
164 /* mismatch -- return error */
165 if (rdev && tmp != rdev)
166 return ERR_PTR(-EINVAL);
167
168 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200169 }
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
171
Johannes Berg4f7eff12012-06-15 14:14:22 +0200172 if (!rdev)
173 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200174
Johannes Berg4f7eff12012-06-15 14:14:22 +0200175 if (netns != wiphy_net(&rdev->wiphy))
176 return ERR_PTR(-ENODEV);
177
178 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200179}
180
181/*
182 * This function returns a pointer to the driver
183 * that the genl_info item that is passed refers to.
184 * If successful, it returns non-NULL and also locks
185 * the driver's mutex!
186 *
187 * This means that you need to call cfg80211_unlock_rdev()
188 * before being allowed to acquire &cfg80211_mutex!
189 *
190 * This is necessary because we need to lock the global
191 * mutex to get an item off the list safely, and then
192 * we lock the rdev mutex so it doesn't go away under us.
193 *
194 * We don't want to keep cfg80211_mutex locked
195 * for all the time in order to allow requests on
196 * other interfaces to go through at the same time.
197 *
198 * The result of this can be a PTR_ERR and hence must
199 * be checked with IS_ERR() for errors.
200 */
201static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200202cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200203{
204 struct cfg80211_registered_device *rdev;
205
206 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200207 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208
209 /* if it is not an error we grab the lock on
210 * it to assure it won't be going away while
211 * we operate on it */
212 if (!IS_ERR(rdev))
213 mutex_lock(&rdev->mtx);
214
215 mutex_unlock(&cfg80211_mutex);
216
217 return rdev;
218}
219
Johannes Berg55682962007-09-20 13:09:35 -0400220/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000221static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400222 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700224 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200225 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100226
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200227 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530228 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100229 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
230 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
232
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200233 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
234 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
236 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100237 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400238
239 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
240 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100242
Eliad Pellere007b852011-11-24 18:13:56 +0200243 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
244 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100245
Johannes Bergb9454e82009-07-08 13:29:08 +0200246 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100247 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
248 .len = WLAN_MAX_KEY_LEN },
249 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
250 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
251 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200252 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200253 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100254
255 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
256 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
257 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
258 .len = IEEE80211_MAX_DATA_LEN },
259 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
260 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100261 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
262 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
263 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
264 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
265 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100266 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100267 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200268 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100269 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800270 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100271 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300272
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700273 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
274 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
275
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300276 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
277 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200279 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
280 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100281 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300282
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800283 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700284 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700285
Johannes Berg6c739412011-11-03 09:27:01 +0100286 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200287
288 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
289 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100291 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
292 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200293
294 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
295 .len = IEEE80211_MAX_SSID_LEN },
296 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
297 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200298 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300299 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300300 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300301 [NL80211_ATTR_STA_FLAGS2] = {
302 .len = sizeof(struct nl80211_sta_flag_update),
303 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300304 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300305 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
306 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200307 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
308 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
309 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200310 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100311 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100312 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
313 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100314 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
315 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200316 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200317 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
318 .len = IEEE80211_MAX_DATA_LEN },
319 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200320 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200321 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300322 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200323 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300324 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
325 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200326 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900327 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
328 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100329 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100330 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100331 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200332 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700333 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300334 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200335 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200336 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300337 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300338 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
339 .len = IEEE80211_MAX_DATA_LEN },
340 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
341 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530342 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300343 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530344 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300345 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
346 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
349 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100350 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200351 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
352 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700353 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800354 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
355 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
356 .len = NL80211_HT_CAPABILITY_LEN
357 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100358 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530359 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530360 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200361 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700362 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300363 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000364 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700365 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100366 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
367 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530368 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
369 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Johannes Berg55682962007-09-20 13:09:35 -0400370};
371
Johannes Berge31b8212010-10-05 19:39:30 +0200372/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000373static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200374 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200375 [NL80211_KEY_IDX] = { .type = NLA_U8 },
376 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200377 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200378 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
379 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200380 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100381 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
382};
383
384/* policy for the key default flags */
385static const struct nla_policy
386nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
387 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
388 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200389};
390
Johannes Bergff1b6e62011-05-04 15:37:28 +0200391/* policy for WoWLAN attributes */
392static const struct nla_policy
393nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
394 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
395 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
396 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
397 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200398 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
400 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
401 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200402};
403
Johannes Berge5497d72011-07-05 16:35:40 +0200404/* policy for GTK rekey offload attributes */
405static const struct nla_policy
406nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
407 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
408 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
409 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
410};
411
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300412static const struct nla_policy
413nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200414 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300415 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700416 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300417};
418
Holger Schuriga0438972009-11-11 11:30:02 +0100419/* ifidx get helper */
420static int nl80211_get_ifidx(struct netlink_callback *cb)
421{
422 int res;
423
424 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
425 nl80211_fam.attrbuf, nl80211_fam.maxattr,
426 nl80211_policy);
427 if (res)
428 return res;
429
430 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
431 return -EINVAL;
432
433 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
434 if (!res)
435 return -EINVAL;
436 return res;
437}
438
Johannes Berg67748892010-10-04 21:14:06 +0200439static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
440 struct netlink_callback *cb,
441 struct cfg80211_registered_device **rdev,
442 struct net_device **dev)
443{
444 int ifidx = cb->args[0];
445 int err;
446
447 if (!ifidx)
448 ifidx = nl80211_get_ifidx(cb);
449 if (ifidx < 0)
450 return ifidx;
451
452 cb->args[0] = ifidx;
453
454 rtnl_lock();
455
456 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
457 if (!*dev) {
458 err = -ENODEV;
459 goto out_rtnl;
460 }
461
462 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100463 if (IS_ERR(*rdev)) {
464 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200465 goto out_rtnl;
466 }
467
468 return 0;
469 out_rtnl:
470 rtnl_unlock();
471 return err;
472}
473
474static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
475{
476 cfg80211_unlock_rdev(rdev);
477 rtnl_unlock();
478}
479
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100480/* IE validation */
481static bool is_valid_ie_attr(const struct nlattr *attr)
482{
483 const u8 *pos;
484 int len;
485
486 if (!attr)
487 return true;
488
489 pos = nla_data(attr);
490 len = nla_len(attr);
491
492 while (len) {
493 u8 elemlen;
494
495 if (len < 2)
496 return false;
497 len -= 2;
498
499 elemlen = pos[1];
500 if (elemlen > len)
501 return false;
502
503 len -= elemlen;
504 pos += 2 + elemlen;
505 }
506
507 return true;
508}
509
Johannes Berg55682962007-09-20 13:09:35 -0400510/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000511static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400512 int flags, u8 cmd)
513{
514 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000515 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400516}
517
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400518static int nl80211_msg_put_channel(struct sk_buff *msg,
519 struct ieee80211_channel *chan)
520{
David S. Miller9360ffd2012-03-29 04:41:26 -0400521 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
522 chan->center_freq))
523 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400524
David S. Miller9360ffd2012-03-29 04:41:26 -0400525 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
526 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
527 goto nla_put_failure;
528 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
529 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
530 goto nla_put_failure;
531 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
532 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
533 goto nla_put_failure;
534 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
535 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
536 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400537
David S. Miller9360ffd2012-03-29 04:41:26 -0400538 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
539 DBM_TO_MBM(chan->max_power)))
540 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400541
542 return 0;
543
544 nla_put_failure:
545 return -ENOBUFS;
546}
547
Johannes Berg55682962007-09-20 13:09:35 -0400548/* netlink command implementations */
549
Johannes Bergb9454e82009-07-08 13:29:08 +0200550struct key_parse {
551 struct key_params p;
552 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200553 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200554 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100555 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200556};
557
558static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
559{
560 struct nlattr *tb[NL80211_KEY_MAX + 1];
561 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
562 nl80211_key_policy);
563 if (err)
564 return err;
565
566 k->def = !!tb[NL80211_KEY_DEFAULT];
567 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
568
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100569 if (k->def) {
570 k->def_uni = true;
571 k->def_multi = true;
572 }
573 if (k->defmgmt)
574 k->def_multi = true;
575
Johannes Bergb9454e82009-07-08 13:29:08 +0200576 if (tb[NL80211_KEY_IDX])
577 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
578
579 if (tb[NL80211_KEY_DATA]) {
580 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
581 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
582 }
583
584 if (tb[NL80211_KEY_SEQ]) {
585 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
586 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
587 }
588
589 if (tb[NL80211_KEY_CIPHER])
590 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
591
Johannes Berge31b8212010-10-05 19:39:30 +0200592 if (tb[NL80211_KEY_TYPE]) {
593 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
594 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
595 return -EINVAL;
596 }
597
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100598 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
599 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100600 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
601 tb[NL80211_KEY_DEFAULT_TYPES],
602 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100603 if (err)
604 return err;
605
606 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
607 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
608 }
609
Johannes Bergb9454e82009-07-08 13:29:08 +0200610 return 0;
611}
612
613static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
614{
615 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
616 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
617 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
618 }
619
620 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
621 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
622 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
623 }
624
625 if (info->attrs[NL80211_ATTR_KEY_IDX])
626 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
627
628 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
629 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
630
631 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
632 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
633
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100634 if (k->def) {
635 k->def_uni = true;
636 k->def_multi = true;
637 }
638 if (k->defmgmt)
639 k->def_multi = true;
640
Johannes Berge31b8212010-10-05 19:39:30 +0200641 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
642 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
643 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
644 return -EINVAL;
645 }
646
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100647 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
648 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
649 int err = nla_parse_nested(
650 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
651 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
652 nl80211_key_default_policy);
653 if (err)
654 return err;
655
656 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
657 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
658 }
659
Johannes Bergb9454e82009-07-08 13:29:08 +0200660 return 0;
661}
662
663static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
664{
665 int err;
666
667 memset(k, 0, sizeof(*k));
668 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200669 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200670
671 if (info->attrs[NL80211_ATTR_KEY])
672 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
673 else
674 err = nl80211_parse_key_old(info, k);
675
676 if (err)
677 return err;
678
679 if (k->def && k->defmgmt)
680 return -EINVAL;
681
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100682 if (k->defmgmt) {
683 if (k->def_uni || !k->def_multi)
684 return -EINVAL;
685 }
686
Johannes Bergb9454e82009-07-08 13:29:08 +0200687 if (k->idx != -1) {
688 if (k->defmgmt) {
689 if (k->idx < 4 || k->idx > 5)
690 return -EINVAL;
691 } else if (k->def) {
692 if (k->idx < 0 || k->idx > 3)
693 return -EINVAL;
694 } else {
695 if (k->idx < 0 || k->idx > 5)
696 return -EINVAL;
697 }
698 }
699
700 return 0;
701}
702
Johannes Bergfffd0932009-07-08 14:22:54 +0200703static struct cfg80211_cached_keys *
704nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530705 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200706{
707 struct key_parse parse;
708 struct nlattr *key;
709 struct cfg80211_cached_keys *result;
710 int rem, err, def = 0;
711
712 result = kzalloc(sizeof(*result), GFP_KERNEL);
713 if (!result)
714 return ERR_PTR(-ENOMEM);
715
716 result->def = -1;
717 result->defmgmt = -1;
718
719 nla_for_each_nested(key, keys, rem) {
720 memset(&parse, 0, sizeof(parse));
721 parse.idx = -1;
722
723 err = nl80211_parse_key_new(key, &parse);
724 if (err)
725 goto error;
726 err = -EINVAL;
727 if (!parse.p.key)
728 goto error;
729 if (parse.idx < 0 || parse.idx > 4)
730 goto error;
731 if (parse.def) {
732 if (def)
733 goto error;
734 def = 1;
735 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100736 if (!parse.def_uni || !parse.def_multi)
737 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200738 } else if (parse.defmgmt)
739 goto error;
740 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200741 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200742 if (err)
743 goto error;
744 result->params[parse.idx].cipher = parse.p.cipher;
745 result->params[parse.idx].key_len = parse.p.key_len;
746 result->params[parse.idx].key = result->data[parse.idx];
747 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530748
749 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
750 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
751 if (no_ht)
752 *no_ht = true;
753 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200754 }
755
756 return result;
757 error:
758 kfree(result);
759 return ERR_PTR(err);
760}
761
762static int nl80211_key_allowed(struct wireless_dev *wdev)
763{
764 ASSERT_WDEV_LOCK(wdev);
765
Johannes Bergfffd0932009-07-08 14:22:54 +0200766 switch (wdev->iftype) {
767 case NL80211_IFTYPE_AP:
768 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200769 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700770 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200771 break;
772 case NL80211_IFTYPE_ADHOC:
773 if (!wdev->current_bss)
774 return -ENOLINK;
775 break;
776 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200777 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200778 if (wdev->sme_state != CFG80211_SME_CONNECTED)
779 return -ENOLINK;
780 break;
781 default:
782 return -EINVAL;
783 }
784
785 return 0;
786}
787
Johannes Berg7527a782011-05-13 10:58:57 +0200788static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
789{
790 struct nlattr *nl_modes = nla_nest_start(msg, attr);
791 int i;
792
793 if (!nl_modes)
794 goto nla_put_failure;
795
796 i = 0;
797 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400798 if ((ifmodes & 1) && nla_put_flag(msg, i))
799 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200800 ifmodes >>= 1;
801 i++;
802 }
803
804 nla_nest_end(msg, nl_modes);
805 return 0;
806
807nla_put_failure:
808 return -ENOBUFS;
809}
810
811static int nl80211_put_iface_combinations(struct wiphy *wiphy,
812 struct sk_buff *msg)
813{
814 struct nlattr *nl_combis;
815 int i, j;
816
817 nl_combis = nla_nest_start(msg,
818 NL80211_ATTR_INTERFACE_COMBINATIONS);
819 if (!nl_combis)
820 goto nla_put_failure;
821
822 for (i = 0; i < wiphy->n_iface_combinations; i++) {
823 const struct ieee80211_iface_combination *c;
824 struct nlattr *nl_combi, *nl_limits;
825
826 c = &wiphy->iface_combinations[i];
827
828 nl_combi = nla_nest_start(msg, i + 1);
829 if (!nl_combi)
830 goto nla_put_failure;
831
832 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
833 if (!nl_limits)
834 goto nla_put_failure;
835
836 for (j = 0; j < c->n_limits; j++) {
837 struct nlattr *nl_limit;
838
839 nl_limit = nla_nest_start(msg, j + 1);
840 if (!nl_limit)
841 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400842 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
843 c->limits[j].max))
844 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200845 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
846 c->limits[j].types))
847 goto nla_put_failure;
848 nla_nest_end(msg, nl_limit);
849 }
850
851 nla_nest_end(msg, nl_limits);
852
David S. Miller9360ffd2012-03-29 04:41:26 -0400853 if (c->beacon_int_infra_match &&
854 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
855 goto nla_put_failure;
856 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
857 c->num_different_channels) ||
858 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
859 c->max_interfaces))
860 goto nla_put_failure;
Simon Wunderlich11c4a072013-01-08 14:04:07 +0100861 if (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
862 c->radar_detect_widths))
863 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200864
865 nla_nest_end(msg, nl_combi);
866 }
867
868 nla_nest_end(msg, nl_combis);
869
870 return 0;
871nla_put_failure:
872 return -ENOBUFS;
873}
874
Eric W. Biederman15e47302012-09-07 20:12:54 +0000875static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400876 struct cfg80211_registered_device *dev)
877{
878 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100879 struct nlattr *nl_bands, *nl_band;
880 struct nlattr *nl_freqs, *nl_freq;
881 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100882 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100883 enum ieee80211_band band;
884 struct ieee80211_channel *chan;
885 struct ieee80211_rate *rate;
886 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200887 const struct ieee80211_txrx_stypes *mgmt_stypes =
888 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400889
Eric W. Biederman15e47302012-09-07 20:12:54 +0000890 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400891 if (!hdr)
892 return -1;
893
David S. Miller9360ffd2012-03-29 04:41:26 -0400894 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
895 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
896 nla_put_u32(msg, NL80211_ATTR_GENERATION,
897 cfg80211_rdev_list_generation) ||
898 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
899 dev->wiphy.retry_short) ||
900 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
901 dev->wiphy.retry_long) ||
902 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
903 dev->wiphy.frag_threshold) ||
904 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
905 dev->wiphy.rts_threshold) ||
906 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
907 dev->wiphy.coverage_class) ||
908 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
909 dev->wiphy.max_scan_ssids) ||
910 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
911 dev->wiphy.max_sched_scan_ssids) ||
912 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
913 dev->wiphy.max_scan_ie_len) ||
914 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
915 dev->wiphy.max_sched_scan_ie_len) ||
916 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
917 dev->wiphy.max_match_sets))
918 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200919
David S. Miller9360ffd2012-03-29 04:41:26 -0400920 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
921 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
922 goto nla_put_failure;
923 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
924 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
925 goto nla_put_failure;
926 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
927 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
928 goto nla_put_failure;
929 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
930 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
931 goto nla_put_failure;
932 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
933 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
934 goto nla_put_failure;
935 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
936 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
937 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200938
David S. Miller9360ffd2012-03-29 04:41:26 -0400939 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
940 sizeof(u32) * dev->wiphy.n_cipher_suites,
941 dev->wiphy.cipher_suites))
942 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100943
David S. Miller9360ffd2012-03-29 04:41:26 -0400944 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
945 dev->wiphy.max_num_pmkids))
946 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530947
David S. Miller9360ffd2012-03-29 04:41:26 -0400948 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
949 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
950 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200951
David S. Miller9360ffd2012-03-29 04:41:26 -0400952 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
953 dev->wiphy.available_antennas_tx) ||
954 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
955 dev->wiphy.available_antennas_rx))
956 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100957
David S. Miller9360ffd2012-03-29 04:41:26 -0400958 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
959 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
960 dev->wiphy.probe_resp_offload))
961 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200962
Bruno Randolf7f531e02010-12-16 11:30:22 +0900963 if ((dev->wiphy.available_antennas_tx ||
964 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900965 u32 tx_ant = 0, rx_ant = 0;
966 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300967 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900968 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400969 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
970 tx_ant) ||
971 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
972 rx_ant))
973 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900974 }
975 }
976
Johannes Berg7527a782011-05-13 10:58:57 +0200977 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
978 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700979 goto nla_put_failure;
980
Johannes Bergee688b002008-01-24 19:38:39 +0100981 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
982 if (!nl_bands)
983 goto nla_put_failure;
984
985 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
986 if (!dev->wiphy.bands[band])
987 continue;
988
989 nl_band = nla_nest_start(msg, band);
990 if (!nl_band)
991 goto nla_put_failure;
992
Johannes Bergd51626d2008-10-09 12:20:13 +0200993 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400994 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
995 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
996 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
997 &dev->wiphy.bands[band]->ht_cap.mcs) ||
998 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
999 dev->wiphy.bands[band]->ht_cap.cap) ||
1000 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1001 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
1002 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1003 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
1004 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001005
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001006 /* add VHT info */
1007 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1008 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1009 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1010 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1011 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1012 dev->wiphy.bands[band]->vht_cap.cap)))
1013 goto nla_put_failure;
1014
Johannes Bergee688b002008-01-24 19:38:39 +01001015 /* add frequencies */
1016 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1017 if (!nl_freqs)
1018 goto nla_put_failure;
1019
1020 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1021 nl_freq = nla_nest_start(msg, i);
1022 if (!nl_freq)
1023 goto nla_put_failure;
1024
1025 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001026
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001027 if (nl80211_msg_put_channel(msg, chan))
1028 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001029
Johannes Bergee688b002008-01-24 19:38:39 +01001030 nla_nest_end(msg, nl_freq);
1031 }
1032
1033 nla_nest_end(msg, nl_freqs);
1034
1035 /* add bitrates */
1036 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1037 if (!nl_rates)
1038 goto nla_put_failure;
1039
1040 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1041 nl_rate = nla_nest_start(msg, i);
1042 if (!nl_rate)
1043 goto nla_put_failure;
1044
1045 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001046 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1047 rate->bitrate))
1048 goto nla_put_failure;
1049 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1050 nla_put_flag(msg,
1051 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1052 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001053
1054 nla_nest_end(msg, nl_rate);
1055 }
1056
1057 nla_nest_end(msg, nl_rates);
1058
1059 nla_nest_end(msg, nl_band);
1060 }
1061 nla_nest_end(msg, nl_bands);
1062
Johannes Berg8fdc6212009-03-14 09:34:01 +01001063 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1064 if (!nl_cmds)
1065 goto nla_put_failure;
1066
1067 i = 0;
1068#define CMD(op, n) \
1069 do { \
1070 if (dev->ops->op) { \
1071 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001072 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1073 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001074 } \
1075 } while (0)
1076
1077 CMD(add_virtual_intf, NEW_INTERFACE);
1078 CMD(change_virtual_intf, SET_INTERFACE);
1079 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001080 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001081 CMD(add_station, NEW_STATION);
1082 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001083 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001084 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001085 CMD(auth, AUTHENTICATE);
1086 CMD(assoc, ASSOCIATE);
1087 CMD(deauth, DEAUTHENTICATE);
1088 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001089 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001090 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001091 CMD(set_pmksa, SET_PMKSA);
1092 CMD(del_pmksa, DEL_PMKSA);
1093 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001094 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1095 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001096 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001097 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001098 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001099 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001100 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001101 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1102 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001103 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001104 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001105 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001106 i++;
1107 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1108 goto nla_put_failure;
1109 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001110 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001111 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1112 CMD(tdls_mgmt, TDLS_MGMT);
1113 CMD(tdls_oper, TDLS_OPER);
1114 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001115 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1116 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001117 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001118 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001119 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1120 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001121 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1122 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001123 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001124 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001125 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001126
Kalle Valo4745fc02011-11-17 19:06:10 +02001127#ifdef CONFIG_NL80211_TESTMODE
1128 CMD(testmode_cmd, TESTMODE);
1129#endif
1130
Johannes Berg8fdc6212009-03-14 09:34:01 +01001131#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001132
Johannes Berg6829c872009-07-02 09:13:27 +02001133 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001134 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001135 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1136 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001137 }
1138
Johannes Berg6829c872009-07-02 09:13:27 +02001139 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001140 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001141 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1142 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001143 }
1144
Johannes Berg8fdc6212009-03-14 09:34:01 +01001145 nla_nest_end(msg, nl_cmds);
1146
Johannes Berg7c4ef712011-11-18 15:33:48 +01001147 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001148 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1149 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1150 dev->wiphy.max_remain_on_channel_duration))
1151 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001152
David S. Miller9360ffd2012-03-29 04:41:26 -04001153 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1154 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1155 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001156
Johannes Berg2e161f72010-08-12 15:38:38 +02001157 if (mgmt_stypes) {
1158 u16 stypes;
1159 struct nlattr *nl_ftypes, *nl_ifs;
1160 enum nl80211_iftype ift;
1161
1162 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1163 if (!nl_ifs)
1164 goto nla_put_failure;
1165
1166 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1167 nl_ftypes = nla_nest_start(msg, ift);
1168 if (!nl_ftypes)
1169 goto nla_put_failure;
1170 i = 0;
1171 stypes = mgmt_stypes[ift].tx;
1172 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001173 if ((stypes & 1) &&
1174 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1175 (i << 4) | IEEE80211_FTYPE_MGMT))
1176 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001177 stypes >>= 1;
1178 i++;
1179 }
1180 nla_nest_end(msg, nl_ftypes);
1181 }
1182
Johannes Berg74b70a42010-08-24 12:15:53 +02001183 nla_nest_end(msg, nl_ifs);
1184
Johannes Berg2e161f72010-08-12 15:38:38 +02001185 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1186 if (!nl_ifs)
1187 goto nla_put_failure;
1188
1189 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1190 nl_ftypes = nla_nest_start(msg, ift);
1191 if (!nl_ftypes)
1192 goto nla_put_failure;
1193 i = 0;
1194 stypes = mgmt_stypes[ift].rx;
1195 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001196 if ((stypes & 1) &&
1197 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1198 (i << 4) | IEEE80211_FTYPE_MGMT))
1199 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001200 stypes >>= 1;
1201 i++;
1202 }
1203 nla_nest_end(msg, nl_ftypes);
1204 }
1205 nla_nest_end(msg, nl_ifs);
1206 }
1207
Johannes Bergdfb89c52012-06-27 09:23:48 +02001208#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001209 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1210 struct nlattr *nl_wowlan;
1211
1212 nl_wowlan = nla_nest_start(msg,
1213 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1214 if (!nl_wowlan)
1215 goto nla_put_failure;
1216
David S. Miller9360ffd2012-03-29 04:41:26 -04001217 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1218 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1219 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1220 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1221 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1222 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1223 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1224 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1225 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1226 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1227 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1228 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1229 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1230 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1231 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1232 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1233 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001234 if (dev->wiphy.wowlan.n_patterns) {
1235 struct nl80211_wowlan_pattern_support pat = {
1236 .max_patterns = dev->wiphy.wowlan.n_patterns,
1237 .min_pattern_len =
1238 dev->wiphy.wowlan.pattern_min_len,
1239 .max_pattern_len =
1240 dev->wiphy.wowlan.pattern_max_len,
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08001241 .max_pkt_offset =
1242 dev->wiphy.wowlan.max_pkt_offset,
Johannes Bergff1b6e62011-05-04 15:37:28 +02001243 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001244 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1245 sizeof(pat), &pat))
1246 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001247 }
1248
1249 nla_nest_end(msg, nl_wowlan);
1250 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001251#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001252
Johannes Berg7527a782011-05-13 10:58:57 +02001253 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1254 dev->wiphy.software_iftypes))
1255 goto nla_put_failure;
1256
1257 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1258 goto nla_put_failure;
1259
David S. Miller9360ffd2012-03-29 04:41:26 -04001260 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1261 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1262 dev->wiphy.ap_sme_capa))
1263 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001264
David S. Miller9360ffd2012-03-29 04:41:26 -04001265 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1266 dev->wiphy.features))
1267 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001268
David S. Miller9360ffd2012-03-29 04:41:26 -04001269 if (dev->wiphy.ht_capa_mod_mask &&
1270 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1271 sizeof(*dev->wiphy.ht_capa_mod_mask),
1272 dev->wiphy.ht_capa_mod_mask))
1273 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001274
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05301275 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1276 dev->wiphy.max_acl_mac_addrs &&
1277 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1278 dev->wiphy.max_acl_mac_addrs))
1279 goto nla_put_failure;
1280
Johannes Berg55682962007-09-20 13:09:35 -04001281 return genlmsg_end(msg, hdr);
1282
1283 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001284 genlmsg_cancel(msg, hdr);
1285 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001286}
1287
1288static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1289{
1290 int idx = 0;
1291 int start = cb->args[0];
1292 struct cfg80211_registered_device *dev;
1293
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001294 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001295 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001296 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1297 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001298 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001299 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001300 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001301 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001302 dev) < 0) {
1303 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001304 break;
Julius Volzb4637272008-07-08 14:02:19 +02001305 }
Johannes Berg55682962007-09-20 13:09:35 -04001306 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001307 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001308
1309 cb->args[0] = idx;
1310
1311 return skb->len;
1312}
1313
1314static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1315{
1316 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001317 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001318
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001319 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001320 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001321 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001322
Eric W. Biederman15e47302012-09-07 20:12:54 +00001323 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001324 nlmsg_free(msg);
1325 return -ENOBUFS;
1326 }
Johannes Berg55682962007-09-20 13:09:35 -04001327
Johannes Berg134e6372009-07-10 09:51:34 +00001328 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001329}
1330
Jouni Malinen31888482008-10-30 16:59:24 +02001331static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1332 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1333 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1334 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1335 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1336 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1337};
1338
1339static int parse_txq_params(struct nlattr *tb[],
1340 struct ieee80211_txq_params *txq_params)
1341{
Johannes Berga3304b02012-03-28 11:04:24 +02001342 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001343 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1344 !tb[NL80211_TXQ_ATTR_AIFS])
1345 return -EINVAL;
1346
Johannes Berga3304b02012-03-28 11:04:24 +02001347 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001348 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1349 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1350 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1351 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1352
Johannes Berga3304b02012-03-28 11:04:24 +02001353 if (txq_params->ac >= NL80211_NUM_ACS)
1354 return -EINVAL;
1355
Jouni Malinen31888482008-10-30 16:59:24 +02001356 return 0;
1357}
1358
Johannes Bergf444de02010-05-05 15:25:02 +02001359static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1360{
1361 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001362 * You can only set the channel explicitly for WDS interfaces,
1363 * all others have their channel managed via their respective
1364 * "establish a connection" command (connect, join, ...)
1365 *
1366 * For AP/GO and mesh mode, the channel can be set with the
1367 * channel userspace API, but is only stored and passed to the
1368 * low-level driver when the AP starts or the mesh is joined.
1369 * This is for backward compatibility, userspace can also give
1370 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001371 *
1372 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001373 * whatever else is going on, so they have their own special
1374 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001375 */
1376 return !wdev ||
1377 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001378 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001379 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1380 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001381}
1382
Johannes Berg683b6d32012-11-08 21:25:48 +01001383static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1384 struct genl_info *info,
1385 struct cfg80211_chan_def *chandef)
1386{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301387 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001388
1389 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1390 return -EINVAL;
1391
1392 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1393
1394 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001395 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1396 chandef->center_freq1 = control_freq;
1397 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001398
1399 /* Primary channel not allowed */
1400 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1401 return -EINVAL;
1402
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001403 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1404 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001405
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001406 chantype = nla_get_u32(
1407 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1408
1409 switch (chantype) {
1410 case NL80211_CHAN_NO_HT:
1411 case NL80211_CHAN_HT20:
1412 case NL80211_CHAN_HT40PLUS:
1413 case NL80211_CHAN_HT40MINUS:
1414 cfg80211_chandef_create(chandef, chandef->chan,
1415 chantype);
1416 break;
1417 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001418 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001419 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001420 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1421 chandef->width =
1422 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1423 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1424 chandef->center_freq1 =
1425 nla_get_u32(
1426 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1427 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1428 chandef->center_freq2 =
1429 nla_get_u32(
1430 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1431 }
1432
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001433 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001434 return -EINVAL;
1435
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001436 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1437 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001438 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001439
Johannes Berg683b6d32012-11-08 21:25:48 +01001440 return 0;
1441}
1442
Johannes Bergf444de02010-05-05 15:25:02 +02001443static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1444 struct wireless_dev *wdev,
1445 struct genl_info *info)
1446{
Johannes Berg683b6d32012-11-08 21:25:48 +01001447 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001448 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001449 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1450
1451 if (wdev)
1452 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001453
Johannes Bergf444de02010-05-05 15:25:02 +02001454 if (!nl80211_can_set_dev_channel(wdev))
1455 return -EOPNOTSUPP;
1456
Johannes Berg683b6d32012-11-08 21:25:48 +01001457 result = nl80211_parse_chandef(rdev, info, &chandef);
1458 if (result)
1459 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001460
1461 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001462 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001463 case NL80211_IFTYPE_AP:
1464 case NL80211_IFTYPE_P2P_GO:
1465 if (wdev->beacon_interval) {
1466 result = -EBUSY;
1467 break;
1468 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001469 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001470 result = -EINVAL;
1471 break;
1472 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001473 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001474 result = 0;
1475 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001476 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001477 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001478 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001479 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001480 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001481 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001482 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001483 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001484 }
1485 mutex_unlock(&rdev->devlist_mtx);
1486
1487 return result;
1488}
1489
1490static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1491{
Johannes Berg4c476992010-10-04 21:36:35 +02001492 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1493 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001494
Johannes Berg4c476992010-10-04 21:36:35 +02001495 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001496}
1497
Bill Jordane8347eb2010-10-01 13:54:28 -04001498static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1499{
Johannes Berg43b19952010-10-07 13:10:30 +02001500 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1501 struct net_device *dev = info->user_ptr[1];
1502 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001503 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001504
1505 if (!info->attrs[NL80211_ATTR_MAC])
1506 return -EINVAL;
1507
Johannes Berg43b19952010-10-07 13:10:30 +02001508 if (netif_running(dev))
1509 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001510
Johannes Berg43b19952010-10-07 13:10:30 +02001511 if (!rdev->ops->set_wds_peer)
1512 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001513
Johannes Berg43b19952010-10-07 13:10:30 +02001514 if (wdev->iftype != NL80211_IFTYPE_WDS)
1515 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001516
1517 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001518 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001519}
1520
1521
Johannes Berg55682962007-09-20 13:09:35 -04001522static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1523{
1524 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001525 struct net_device *netdev = NULL;
1526 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001527 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001528 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001529 u32 changed;
1530 u8 retry_short = 0, retry_long = 0;
1531 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001532 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001533
Johannes Bergf444de02010-05-05 15:25:02 +02001534 /*
1535 * Try to find the wiphy and netdev. Normally this
1536 * function shouldn't need the netdev, but this is
1537 * done for backward compatibility -- previously
1538 * setting the channel was done per wiphy, but now
1539 * it is per netdev. Previous userland like hostapd
1540 * also passed a netdev to set_wiphy, so that it is
1541 * possible to let that go to the right netdev!
1542 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001543 mutex_lock(&cfg80211_mutex);
1544
Johannes Bergf444de02010-05-05 15:25:02 +02001545 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1546 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1547
1548 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1549 if (netdev && netdev->ieee80211_ptr) {
1550 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1551 mutex_lock(&rdev->mtx);
1552 } else
1553 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001554 }
1555
Johannes Bergf444de02010-05-05 15:25:02 +02001556 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001557 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1558 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001559 if (IS_ERR(rdev)) {
1560 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001561 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001562 }
1563 wdev = NULL;
1564 netdev = NULL;
1565 result = 0;
1566
1567 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001568 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001569 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001570
1571 /*
1572 * end workaround code, by now the rdev is available
1573 * and locked, and wdev may or may not be NULL.
1574 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001575
1576 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001577 result = cfg80211_dev_rename(
1578 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001579
1580 mutex_unlock(&cfg80211_mutex);
1581
1582 if (result)
1583 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001584
Jouni Malinen31888482008-10-30 16:59:24 +02001585 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1586 struct ieee80211_txq_params txq_params;
1587 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1588
1589 if (!rdev->ops->set_txq_params) {
1590 result = -EOPNOTSUPP;
1591 goto bad_res;
1592 }
1593
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001594 if (!netdev) {
1595 result = -EINVAL;
1596 goto bad_res;
1597 }
1598
Johannes Berg133a3ff2011-11-03 14:50:13 +01001599 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1600 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1601 result = -EINVAL;
1602 goto bad_res;
1603 }
1604
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001605 if (!netif_running(netdev)) {
1606 result = -ENETDOWN;
1607 goto bad_res;
1608 }
1609
Jouni Malinen31888482008-10-30 16:59:24 +02001610 nla_for_each_nested(nl_txq_params,
1611 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1612 rem_txq_params) {
1613 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1614 nla_data(nl_txq_params),
1615 nla_len(nl_txq_params),
1616 txq_params_policy);
1617 result = parse_txq_params(tb, &txq_params);
1618 if (result)
1619 goto bad_res;
1620
Hila Gonene35e4d22012-06-27 17:19:42 +03001621 result = rdev_set_txq_params(rdev, netdev,
1622 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001623 if (result)
1624 goto bad_res;
1625 }
1626 }
1627
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001628 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001629 result = __nl80211_set_channel(rdev,
1630 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1631 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001632 if (result)
1633 goto bad_res;
1634 }
1635
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001636 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001637 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001638 enum nl80211_tx_power_setting type;
1639 int idx, mbm = 0;
1640
Johannes Bergc8442112012-10-24 10:17:18 +02001641 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1642 txp_wdev = NULL;
1643
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001644 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001645 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001646 goto bad_res;
1647 }
1648
1649 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1650 type = nla_get_u32(info->attrs[idx]);
1651
1652 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1653 (type != NL80211_TX_POWER_AUTOMATIC)) {
1654 result = -EINVAL;
1655 goto bad_res;
1656 }
1657
1658 if (type != NL80211_TX_POWER_AUTOMATIC) {
1659 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1660 mbm = nla_get_u32(info->attrs[idx]);
1661 }
1662
Johannes Bergc8442112012-10-24 10:17:18 +02001663 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001664 if (result)
1665 goto bad_res;
1666 }
1667
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001668 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1669 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1670 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001671 if ((!rdev->wiphy.available_antennas_tx &&
1672 !rdev->wiphy.available_antennas_rx) ||
1673 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001674 result = -EOPNOTSUPP;
1675 goto bad_res;
1676 }
1677
1678 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1679 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1680
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001681 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001682 * available antenna masks, except for the "all" mask */
1683 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1684 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001685 result = -EINVAL;
1686 goto bad_res;
1687 }
1688
Bruno Randolf7f531e02010-12-16 11:30:22 +09001689 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1690 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001691
Hila Gonene35e4d22012-06-27 17:19:42 +03001692 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001693 if (result)
1694 goto bad_res;
1695 }
1696
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001697 changed = 0;
1698
1699 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1700 retry_short = nla_get_u8(
1701 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1702 if (retry_short == 0) {
1703 result = -EINVAL;
1704 goto bad_res;
1705 }
1706 changed |= WIPHY_PARAM_RETRY_SHORT;
1707 }
1708
1709 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1710 retry_long = nla_get_u8(
1711 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1712 if (retry_long == 0) {
1713 result = -EINVAL;
1714 goto bad_res;
1715 }
1716 changed |= WIPHY_PARAM_RETRY_LONG;
1717 }
1718
1719 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1720 frag_threshold = nla_get_u32(
1721 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1722 if (frag_threshold < 256) {
1723 result = -EINVAL;
1724 goto bad_res;
1725 }
1726 if (frag_threshold != (u32) -1) {
1727 /*
1728 * Fragments (apart from the last one) are required to
1729 * have even length. Make the fragmentation code
1730 * simpler by stripping LSB should someone try to use
1731 * odd threshold value.
1732 */
1733 frag_threshold &= ~0x1;
1734 }
1735 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1736 }
1737
1738 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1739 rts_threshold = nla_get_u32(
1740 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1741 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1742 }
1743
Lukáš Turek81077e82009-12-21 22:50:47 +01001744 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1745 coverage_class = nla_get_u8(
1746 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1747 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1748 }
1749
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001750 if (changed) {
1751 u8 old_retry_short, old_retry_long;
1752 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001753 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001754
1755 if (!rdev->ops->set_wiphy_params) {
1756 result = -EOPNOTSUPP;
1757 goto bad_res;
1758 }
1759
1760 old_retry_short = rdev->wiphy.retry_short;
1761 old_retry_long = rdev->wiphy.retry_long;
1762 old_frag_threshold = rdev->wiphy.frag_threshold;
1763 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001764 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001765
1766 if (changed & WIPHY_PARAM_RETRY_SHORT)
1767 rdev->wiphy.retry_short = retry_short;
1768 if (changed & WIPHY_PARAM_RETRY_LONG)
1769 rdev->wiphy.retry_long = retry_long;
1770 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1771 rdev->wiphy.frag_threshold = frag_threshold;
1772 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1773 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001774 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1775 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001776
Hila Gonene35e4d22012-06-27 17:19:42 +03001777 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001778 if (result) {
1779 rdev->wiphy.retry_short = old_retry_short;
1780 rdev->wiphy.retry_long = old_retry_long;
1781 rdev->wiphy.frag_threshold = old_frag_threshold;
1782 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001783 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001784 }
1785 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001786
Johannes Berg306d6112008-12-08 12:39:04 +01001787 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001788 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001789 if (netdev)
1790 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001791 return result;
1792}
1793
Johannes Berg71bbc992012-06-15 15:30:18 +02001794static inline u64 wdev_id(struct wireless_dev *wdev)
1795{
1796 return (u64)wdev->identifier |
1797 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1798}
Johannes Berg55682962007-09-20 13:09:35 -04001799
Johannes Berg683b6d32012-11-08 21:25:48 +01001800static int nl80211_send_chandef(struct sk_buff *msg,
1801 struct cfg80211_chan_def *chandef)
1802{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001803 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001804
Johannes Berg683b6d32012-11-08 21:25:48 +01001805 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1806 chandef->chan->center_freq))
1807 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001808 switch (chandef->width) {
1809 case NL80211_CHAN_WIDTH_20_NOHT:
1810 case NL80211_CHAN_WIDTH_20:
1811 case NL80211_CHAN_WIDTH_40:
1812 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1813 cfg80211_get_chandef_type(chandef)))
1814 return -ENOBUFS;
1815 break;
1816 default:
1817 break;
1818 }
1819 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1820 return -ENOBUFS;
1821 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1822 return -ENOBUFS;
1823 if (chandef->center_freq2 &&
1824 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001825 return -ENOBUFS;
1826 return 0;
1827}
1828
Eric W. Biederman15e47302012-09-07 20:12:54 +00001829static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001830 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001831 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001832{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001833 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001834 void *hdr;
1835
Eric W. Biederman15e47302012-09-07 20:12:54 +00001836 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001837 if (!hdr)
1838 return -1;
1839
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001840 if (dev &&
1841 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001842 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001843 goto nla_put_failure;
1844
1845 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1846 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001847 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001848 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001849 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1850 rdev->devlist_generation ^
1851 (cfg80211_rdev_list_generation << 2)))
1852 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001853
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001854 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001855 int ret;
1856 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001857
Johannes Berg683b6d32012-11-08 21:25:48 +01001858 ret = rdev_get_channel(rdev, wdev, &chandef);
1859 if (ret == 0) {
1860 if (nl80211_send_chandef(msg, &chandef))
1861 goto nla_put_failure;
1862 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001863 }
1864
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001865 if (wdev->ssid_len) {
1866 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1867 goto nla_put_failure;
1868 }
1869
Johannes Berg55682962007-09-20 13:09:35 -04001870 return genlmsg_end(msg, hdr);
1871
1872 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001873 genlmsg_cancel(msg, hdr);
1874 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001875}
1876
1877static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1878{
1879 int wp_idx = 0;
1880 int if_idx = 0;
1881 int wp_start = cb->args[0];
1882 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001883 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001884 struct wireless_dev *wdev;
1885
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001886 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001887 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1888 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001889 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001890 if (wp_idx < wp_start) {
1891 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001892 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001893 }
Johannes Berg55682962007-09-20 13:09:35 -04001894 if_idx = 0;
1895
Johannes Bergf5ea9122009-08-07 16:17:38 +02001896 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001897 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001898 if (if_idx < if_start) {
1899 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001900 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001901 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001902 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001903 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001904 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001905 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001906 goto out;
1907 }
1908 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001909 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001910 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001911
1912 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001913 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001914 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001915 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001916
1917 cb->args[0] = wp_idx;
1918 cb->args[1] = if_idx;
1919
1920 return skb->len;
1921}
1922
1923static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1924{
1925 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001926 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001927 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001928
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001929 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001930 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001931 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001932
Eric W. Biederman15e47302012-09-07 20:12:54 +00001933 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001934 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001935 nlmsg_free(msg);
1936 return -ENOBUFS;
1937 }
Johannes Berg55682962007-09-20 13:09:35 -04001938
Johannes Berg134e6372009-07-10 09:51:34 +00001939 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001940}
1941
Michael Wu66f7ac52008-01-31 19:48:22 +01001942static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1943 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1944 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1945 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1946 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1947 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1948};
1949
1950static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1951{
1952 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1953 int flag;
1954
1955 *mntrflags = 0;
1956
1957 if (!nla)
1958 return -EINVAL;
1959
1960 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1961 nla, mntr_flags_policy))
1962 return -EINVAL;
1963
1964 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1965 if (flags[flag])
1966 *mntrflags |= (1<<flag);
1967
1968 return 0;
1969}
1970
Johannes Berg9bc383d2009-11-19 11:55:19 +01001971static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001972 struct net_device *netdev, u8 use_4addr,
1973 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001974{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001975 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001976 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001977 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001978 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001979 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001980
1981 switch (iftype) {
1982 case NL80211_IFTYPE_AP_VLAN:
1983 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1984 return 0;
1985 break;
1986 case NL80211_IFTYPE_STATION:
1987 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1988 return 0;
1989 break;
1990 default:
1991 break;
1992 }
1993
1994 return -EOPNOTSUPP;
1995}
1996
Johannes Berg55682962007-09-20 13:09:35 -04001997static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1998{
Johannes Berg4c476992010-10-04 21:36:35 +02001999 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002000 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002001 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002002 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002003 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002004 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002005 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002006
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002007 memset(&params, 0, sizeof(params));
2008
Johannes Berg04a773a2009-04-19 21:24:32 +02002009 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002010
Johannes Berg723b0382008-09-16 20:22:09 +02002011 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002012 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002013 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002014 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002015 if (ntype > NL80211_IFTYPE_MAX)
2016 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002017 }
2018
Johannes Berg92ffe052008-09-16 20:39:36 +02002019 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002020 struct wireless_dev *wdev = dev->ieee80211_ptr;
2021
Johannes Berg4c476992010-10-04 21:36:35 +02002022 if (ntype != NL80211_IFTYPE_MESH_POINT)
2023 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002024 if (netif_running(dev))
2025 return -EBUSY;
2026
2027 wdev_lock(wdev);
2028 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2029 IEEE80211_MAX_MESH_ID_LEN);
2030 wdev->mesh_id_up_len =
2031 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2032 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2033 wdev->mesh_id_up_len);
2034 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002035 }
2036
Felix Fietkau8b787642009-11-10 18:53:10 +01002037 if (info->attrs[NL80211_ATTR_4ADDR]) {
2038 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2039 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002040 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002041 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002042 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002043 } else {
2044 params.use_4addr = -1;
2045 }
2046
Johannes Berg92ffe052008-09-16 20:39:36 +02002047 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002048 if (ntype != NL80211_IFTYPE_MONITOR)
2049 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002050 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2051 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002052 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002053 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002054
2055 flags = &_flags;
2056 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002057 }
Johannes Berg3b858752009-03-12 09:55:09 +01002058
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002059 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002060 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002061 else
2062 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002063
Johannes Berg9bc383d2009-11-19 11:55:19 +01002064 if (!err && params.use_4addr != -1)
2065 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2066
Johannes Berg55682962007-09-20 13:09:35 -04002067 return err;
2068}
2069
2070static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2071{
Johannes Berg4c476992010-10-04 21:36:35 +02002072 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002073 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002074 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002075 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002076 int err;
2077 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002078 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002079
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002080 memset(&params, 0, sizeof(params));
2081
Johannes Berg55682962007-09-20 13:09:35 -04002082 if (!info->attrs[NL80211_ATTR_IFNAME])
2083 return -EINVAL;
2084
2085 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2086 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2087 if (type > NL80211_IFTYPE_MAX)
2088 return -EINVAL;
2089 }
2090
Johannes Berg79c97e92009-07-07 03:56:12 +02002091 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002092 !(rdev->wiphy.interface_modes & (1 << type)))
2093 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002094
Arend van Spriel1c18f142013-01-08 10:17:27 +01002095 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2096 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2097 ETH_ALEN);
2098 if (!is_valid_ether_addr(params.macaddr))
2099 return -EADDRNOTAVAIL;
2100 }
2101
Johannes Berg9bc383d2009-11-19 11:55:19 +01002102 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002103 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002104 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002105 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002106 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002107 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002108
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002109 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2110 if (!msg)
2111 return -ENOMEM;
2112
Michael Wu66f7ac52008-01-31 19:48:22 +01002113 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2114 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2115 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002116 wdev = rdev_add_virtual_intf(rdev,
2117 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2118 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002119 if (IS_ERR(wdev)) {
2120 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002121 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002122 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002123
Johannes Berg98104fde2012-06-16 00:19:54 +02002124 switch (type) {
2125 case NL80211_IFTYPE_MESH_POINT:
2126 if (!info->attrs[NL80211_ATTR_MESH_ID])
2127 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002128 wdev_lock(wdev);
2129 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2130 IEEE80211_MAX_MESH_ID_LEN);
2131 wdev->mesh_id_up_len =
2132 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2133 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2134 wdev->mesh_id_up_len);
2135 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002136 break;
2137 case NL80211_IFTYPE_P2P_DEVICE:
2138 /*
2139 * P2P Device doesn't have a netdev, so doesn't go
2140 * through the netdev notifier and must be added here
2141 */
2142 mutex_init(&wdev->mtx);
2143 INIT_LIST_HEAD(&wdev->event_list);
2144 spin_lock_init(&wdev->event_lock);
2145 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2146 spin_lock_init(&wdev->mgmt_registrations_lock);
2147
2148 mutex_lock(&rdev->devlist_mtx);
2149 wdev->identifier = ++rdev->wdev_id;
2150 list_add_rcu(&wdev->list, &rdev->wdev_list);
2151 rdev->devlist_generation++;
2152 mutex_unlock(&rdev->devlist_mtx);
2153 break;
2154 default:
2155 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002156 }
2157
Eric W. Biederman15e47302012-09-07 20:12:54 +00002158 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002159 rdev, wdev) < 0) {
2160 nlmsg_free(msg);
2161 return -ENOBUFS;
2162 }
2163
2164 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002165}
2166
2167static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2168{
Johannes Berg4c476992010-10-04 21:36:35 +02002169 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002170 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002171
Johannes Berg4c476992010-10-04 21:36:35 +02002172 if (!rdev->ops->del_virtual_intf)
2173 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002174
Johannes Berg84efbb82012-06-16 00:00:26 +02002175 /*
2176 * If we remove a wireless device without a netdev then clear
2177 * user_ptr[1] so that nl80211_post_doit won't dereference it
2178 * to check if it needs to do dev_put(). Otherwise it crashes
2179 * since the wdev has been freed, unlike with a netdev where
2180 * we need the dev_put() for the netdev to really be freed.
2181 */
2182 if (!wdev->netdev)
2183 info->user_ptr[1] = NULL;
2184
Hila Gonene35e4d22012-06-27 17:19:42 +03002185 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002186}
2187
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002188static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2189{
2190 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2191 struct net_device *dev = info->user_ptr[1];
2192 u16 noack_map;
2193
2194 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2195 return -EINVAL;
2196
2197 if (!rdev->ops->set_noack_map)
2198 return -EOPNOTSUPP;
2199
2200 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2201
Hila Gonene35e4d22012-06-27 17:19:42 +03002202 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002203}
2204
Johannes Berg41ade002007-12-19 02:03:29 +01002205struct get_key_cookie {
2206 struct sk_buff *msg;
2207 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002208 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002209};
2210
2211static void get_key_callback(void *c, struct key_params *params)
2212{
Johannes Bergb9454e82009-07-08 13:29:08 +02002213 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002214 struct get_key_cookie *cookie = c;
2215
David S. Miller9360ffd2012-03-29 04:41:26 -04002216 if ((params->key &&
2217 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2218 params->key_len, params->key)) ||
2219 (params->seq &&
2220 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2221 params->seq_len, params->seq)) ||
2222 (params->cipher &&
2223 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2224 params->cipher)))
2225 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002226
Johannes Bergb9454e82009-07-08 13:29:08 +02002227 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2228 if (!key)
2229 goto nla_put_failure;
2230
David S. Miller9360ffd2012-03-29 04:41:26 -04002231 if ((params->key &&
2232 nla_put(cookie->msg, NL80211_KEY_DATA,
2233 params->key_len, params->key)) ||
2234 (params->seq &&
2235 nla_put(cookie->msg, NL80211_KEY_SEQ,
2236 params->seq_len, params->seq)) ||
2237 (params->cipher &&
2238 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2239 params->cipher)))
2240 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002241
David S. Miller9360ffd2012-03-29 04:41:26 -04002242 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2243 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002244
2245 nla_nest_end(cookie->msg, key);
2246
Johannes Berg41ade002007-12-19 02:03:29 +01002247 return;
2248 nla_put_failure:
2249 cookie->error = 1;
2250}
2251
2252static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2253{
Johannes Berg4c476992010-10-04 21:36:35 +02002254 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002255 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002256 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002257 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002258 const u8 *mac_addr = NULL;
2259 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002260 struct get_key_cookie cookie = {
2261 .error = 0,
2262 };
2263 void *hdr;
2264 struct sk_buff *msg;
2265
2266 if (info->attrs[NL80211_ATTR_KEY_IDX])
2267 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2268
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002269 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002270 return -EINVAL;
2271
2272 if (info->attrs[NL80211_ATTR_MAC])
2273 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2274
Johannes Berge31b8212010-10-05 19:39:30 +02002275 pairwise = !!mac_addr;
2276 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2277 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2278 if (kt >= NUM_NL80211_KEYTYPES)
2279 return -EINVAL;
2280 if (kt != NL80211_KEYTYPE_GROUP &&
2281 kt != NL80211_KEYTYPE_PAIRWISE)
2282 return -EINVAL;
2283 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2284 }
2285
Johannes Berg4c476992010-10-04 21:36:35 +02002286 if (!rdev->ops->get_key)
2287 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002288
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002289 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002290 if (!msg)
2291 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002292
Eric W. Biederman15e47302012-09-07 20:12:54 +00002293 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002294 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002295 if (IS_ERR(hdr))
2296 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002297
2298 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002299 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002300
David S. Miller9360ffd2012-03-29 04:41:26 -04002301 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2302 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2303 goto nla_put_failure;
2304 if (mac_addr &&
2305 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2306 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002307
Johannes Berge31b8212010-10-05 19:39:30 +02002308 if (pairwise && mac_addr &&
2309 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2310 return -ENOENT;
2311
Hila Gonene35e4d22012-06-27 17:19:42 +03002312 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2313 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002314
2315 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002316 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002317
2318 if (cookie.error)
2319 goto nla_put_failure;
2320
2321 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002322 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002323
2324 nla_put_failure:
2325 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002326 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002327 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002328 return err;
2329}
2330
2331static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2332{
Johannes Berg4c476992010-10-04 21:36:35 +02002333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002334 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002335 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002336 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002337
Johannes Bergb9454e82009-07-08 13:29:08 +02002338 err = nl80211_parse_key(info, &key);
2339 if (err)
2340 return err;
2341
2342 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002343 return -EINVAL;
2344
Johannes Bergb9454e82009-07-08 13:29:08 +02002345 /* only support setting default key */
2346 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002347 return -EINVAL;
2348
Johannes Bergfffd0932009-07-08 14:22:54 +02002349 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002350
2351 if (key.def) {
2352 if (!rdev->ops->set_default_key) {
2353 err = -EOPNOTSUPP;
2354 goto out;
2355 }
2356
2357 err = nl80211_key_allowed(dev->ieee80211_ptr);
2358 if (err)
2359 goto out;
2360
Hila Gonene35e4d22012-06-27 17:19:42 +03002361 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002362 key.def_uni, key.def_multi);
2363
2364 if (err)
2365 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002366
Johannes Berg3d23e342009-09-29 23:27:28 +02002367#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002368 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002369#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002370 } else {
2371 if (key.def_uni || !key.def_multi) {
2372 err = -EINVAL;
2373 goto out;
2374 }
2375
2376 if (!rdev->ops->set_default_mgmt_key) {
2377 err = -EOPNOTSUPP;
2378 goto out;
2379 }
2380
2381 err = nl80211_key_allowed(dev->ieee80211_ptr);
2382 if (err)
2383 goto out;
2384
Hila Gonene35e4d22012-06-27 17:19:42 +03002385 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002386 if (err)
2387 goto out;
2388
2389#ifdef CONFIG_CFG80211_WEXT
2390 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2391#endif
2392 }
2393
2394 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002395 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002396
Johannes Berg41ade002007-12-19 02:03:29 +01002397 return err;
2398}
2399
2400static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2401{
Johannes Berg4c476992010-10-04 21:36:35 +02002402 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002403 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002404 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002405 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002406 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002407
Johannes Bergb9454e82009-07-08 13:29:08 +02002408 err = nl80211_parse_key(info, &key);
2409 if (err)
2410 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002411
Johannes Bergb9454e82009-07-08 13:29:08 +02002412 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002413 return -EINVAL;
2414
Johannes Berg41ade002007-12-19 02:03:29 +01002415 if (info->attrs[NL80211_ATTR_MAC])
2416 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2417
Johannes Berge31b8212010-10-05 19:39:30 +02002418 if (key.type == -1) {
2419 if (mac_addr)
2420 key.type = NL80211_KEYTYPE_PAIRWISE;
2421 else
2422 key.type = NL80211_KEYTYPE_GROUP;
2423 }
2424
2425 /* for now */
2426 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2427 key.type != NL80211_KEYTYPE_GROUP)
2428 return -EINVAL;
2429
Johannes Berg4c476992010-10-04 21:36:35 +02002430 if (!rdev->ops->add_key)
2431 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002432
Johannes Berge31b8212010-10-05 19:39:30 +02002433 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2434 key.type == NL80211_KEYTYPE_PAIRWISE,
2435 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002436 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002437
2438 wdev_lock(dev->ieee80211_ptr);
2439 err = nl80211_key_allowed(dev->ieee80211_ptr);
2440 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002441 err = rdev_add_key(rdev, dev, key.idx,
2442 key.type == NL80211_KEYTYPE_PAIRWISE,
2443 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002444 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002445
Johannes Berg41ade002007-12-19 02:03:29 +01002446 return err;
2447}
2448
2449static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2450{
Johannes Berg4c476992010-10-04 21:36:35 +02002451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002452 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002453 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002454 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002455 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002456
Johannes Bergb9454e82009-07-08 13:29:08 +02002457 err = nl80211_parse_key(info, &key);
2458 if (err)
2459 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002460
2461 if (info->attrs[NL80211_ATTR_MAC])
2462 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2463
Johannes Berge31b8212010-10-05 19:39:30 +02002464 if (key.type == -1) {
2465 if (mac_addr)
2466 key.type = NL80211_KEYTYPE_PAIRWISE;
2467 else
2468 key.type = NL80211_KEYTYPE_GROUP;
2469 }
2470
2471 /* for now */
2472 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2473 key.type != NL80211_KEYTYPE_GROUP)
2474 return -EINVAL;
2475
Johannes Berg4c476992010-10-04 21:36:35 +02002476 if (!rdev->ops->del_key)
2477 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002478
Johannes Bergfffd0932009-07-08 14:22:54 +02002479 wdev_lock(dev->ieee80211_ptr);
2480 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002481
2482 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2483 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2484 err = -ENOENT;
2485
Johannes Bergfffd0932009-07-08 14:22:54 +02002486 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002487 err = rdev_del_key(rdev, dev, key.idx,
2488 key.type == NL80211_KEYTYPE_PAIRWISE,
2489 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002490
Johannes Berg3d23e342009-09-29 23:27:28 +02002491#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002492 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002493 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002494 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002495 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002496 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2497 }
2498#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002499 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002500
Johannes Berg41ade002007-12-19 02:03:29 +01002501 return err;
2502}
2503
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302504/* This function returns an error or the number of nested attributes */
2505static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2506{
2507 struct nlattr *attr;
2508 int n_entries = 0, tmp;
2509
2510 nla_for_each_nested(attr, nl_attr, tmp) {
2511 if (nla_len(attr) != ETH_ALEN)
2512 return -EINVAL;
2513
2514 n_entries++;
2515 }
2516
2517 return n_entries;
2518}
2519
2520/*
2521 * This function parses ACL information and allocates memory for ACL data.
2522 * On successful return, the calling function is responsible to free the
2523 * ACL buffer returned by this function.
2524 */
2525static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2526 struct genl_info *info)
2527{
2528 enum nl80211_acl_policy acl_policy;
2529 struct nlattr *attr;
2530 struct cfg80211_acl_data *acl;
2531 int i = 0, n_entries, tmp;
2532
2533 if (!wiphy->max_acl_mac_addrs)
2534 return ERR_PTR(-EOPNOTSUPP);
2535
2536 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2537 return ERR_PTR(-EINVAL);
2538
2539 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2540 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2541 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2542 return ERR_PTR(-EINVAL);
2543
2544 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2545 return ERR_PTR(-EINVAL);
2546
2547 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2548 if (n_entries < 0)
2549 return ERR_PTR(n_entries);
2550
2551 if (n_entries > wiphy->max_acl_mac_addrs)
2552 return ERR_PTR(-ENOTSUPP);
2553
2554 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2555 GFP_KERNEL);
2556 if (!acl)
2557 return ERR_PTR(-ENOMEM);
2558
2559 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2560 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2561 i++;
2562 }
2563
2564 acl->n_acl_entries = n_entries;
2565 acl->acl_policy = acl_policy;
2566
2567 return acl;
2568}
2569
2570static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2571{
2572 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2573 struct net_device *dev = info->user_ptr[1];
2574 struct cfg80211_acl_data *acl;
2575 int err;
2576
2577 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2578 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2579 return -EOPNOTSUPP;
2580
2581 if (!dev->ieee80211_ptr->beacon_interval)
2582 return -EINVAL;
2583
2584 acl = parse_acl_data(&rdev->wiphy, info);
2585 if (IS_ERR(acl))
2586 return PTR_ERR(acl);
2587
2588 err = rdev_set_mac_acl(rdev, dev, acl);
2589
2590 kfree(acl);
2591
2592 return err;
2593}
2594
Johannes Berg88600202012-02-13 15:17:18 +01002595static int nl80211_parse_beacon(struct genl_info *info,
2596 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002597{
Johannes Berg88600202012-02-13 15:17:18 +01002598 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002599
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002600 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2601 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2602 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2603 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002604 return -EINVAL;
2605
Johannes Berg88600202012-02-13 15:17:18 +01002606 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002607
Johannes Berged1b6cc2007-12-19 02:03:32 +01002608 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002609 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2610 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2611 if (!bcn->head_len)
2612 return -EINVAL;
2613 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002614 }
2615
2616 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002617 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2618 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002619 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002620 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002621 }
2622
Johannes Berg4c476992010-10-04 21:36:35 +02002623 if (!haveinfo)
2624 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002625
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002626 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002627 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2628 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002629 }
2630
2631 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002632 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002633 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002634 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002635 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2636 }
2637
2638 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002639 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002640 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002641 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002642 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2643 }
2644
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002645 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002646 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002647 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002648 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002649 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2650 }
2651
Johannes Berg88600202012-02-13 15:17:18 +01002652 return 0;
2653}
2654
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002655static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2656 struct cfg80211_ap_settings *params)
2657{
2658 struct wireless_dev *wdev;
2659 bool ret = false;
2660
2661 mutex_lock(&rdev->devlist_mtx);
2662
Johannes Berg89a54e42012-06-15 14:33:17 +02002663 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002664 if (wdev->iftype != NL80211_IFTYPE_AP &&
2665 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2666 continue;
2667
Johannes Berg683b6d32012-11-08 21:25:48 +01002668 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002669 continue;
2670
Johannes Berg683b6d32012-11-08 21:25:48 +01002671 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002672 ret = true;
2673 break;
2674 }
2675
2676 mutex_unlock(&rdev->devlist_mtx);
2677
2678 return ret;
2679}
2680
Jouni Malinene39e5b52012-09-30 19:29:39 +03002681static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2682 enum nl80211_auth_type auth_type,
2683 enum nl80211_commands cmd)
2684{
2685 if (auth_type > NL80211_AUTHTYPE_MAX)
2686 return false;
2687
2688 switch (cmd) {
2689 case NL80211_CMD_AUTHENTICATE:
2690 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2691 auth_type == NL80211_AUTHTYPE_SAE)
2692 return false;
2693 return true;
2694 case NL80211_CMD_CONNECT:
2695 case NL80211_CMD_START_AP:
2696 /* SAE not supported yet */
2697 if (auth_type == NL80211_AUTHTYPE_SAE)
2698 return false;
2699 return true;
2700 default:
2701 return false;
2702 }
2703}
2704
Johannes Berg88600202012-02-13 15:17:18 +01002705static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2706{
2707 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2708 struct net_device *dev = info->user_ptr[1];
2709 struct wireless_dev *wdev = dev->ieee80211_ptr;
2710 struct cfg80211_ap_settings params;
2711 int err;
2712
2713 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2714 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2715 return -EOPNOTSUPP;
2716
2717 if (!rdev->ops->start_ap)
2718 return -EOPNOTSUPP;
2719
2720 if (wdev->beacon_interval)
2721 return -EALREADY;
2722
2723 memset(&params, 0, sizeof(params));
2724
2725 /* these are required for START_AP */
2726 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2727 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2728 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2729 return -EINVAL;
2730
2731 err = nl80211_parse_beacon(info, &params.beacon);
2732 if (err)
2733 return err;
2734
2735 params.beacon_interval =
2736 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2737 params.dtim_period =
2738 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2739
2740 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2741 if (err)
2742 return err;
2743
2744 /*
2745 * In theory, some of these attributes should be required here
2746 * but since they were not used when the command was originally
2747 * added, keep them optional for old user space programs to let
2748 * them continue to work with drivers that do not need the
2749 * additional information -- drivers must check!
2750 */
2751 if (info->attrs[NL80211_ATTR_SSID]) {
2752 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2753 params.ssid_len =
2754 nla_len(info->attrs[NL80211_ATTR_SSID]);
2755 if (params.ssid_len == 0 ||
2756 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2757 return -EINVAL;
2758 }
2759
2760 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2761 params.hidden_ssid = nla_get_u32(
2762 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2763 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2764 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2765 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2766 return -EINVAL;
2767 }
2768
2769 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2770
2771 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2772 params.auth_type = nla_get_u32(
2773 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002774 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2775 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002776 return -EINVAL;
2777 } else
2778 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2779
2780 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2781 NL80211_MAX_NR_CIPHER_SUITES);
2782 if (err)
2783 return err;
2784
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302785 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2786 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2787 return -EOPNOTSUPP;
2788 params.inactivity_timeout = nla_get_u16(
2789 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2790 }
2791
Johannes Berg53cabad2012-11-14 15:17:28 +01002792 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2793 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2794 return -EINVAL;
2795 params.p2p_ctwindow =
2796 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2797 if (params.p2p_ctwindow > 127)
2798 return -EINVAL;
2799 if (params.p2p_ctwindow != 0 &&
2800 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2801 return -EINVAL;
2802 }
2803
2804 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2805 u8 tmp;
2806
2807 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2808 return -EINVAL;
2809 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2810 if (tmp > 1)
2811 return -EINVAL;
2812 params.p2p_opp_ps = tmp;
2813 if (params.p2p_opp_ps != 0 &&
2814 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2815 return -EINVAL;
2816 }
2817
Johannes Bergaa430da2012-05-16 23:50:18 +02002818 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002819 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2820 if (err)
2821 return err;
2822 } else if (wdev->preset_chandef.chan) {
2823 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002824 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002825 return -EINVAL;
2826
Johannes Berg683b6d32012-11-08 21:25:48 +01002827 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002828 return -EINVAL;
2829
Michal Kaziore4e32452012-06-29 12:47:08 +02002830 mutex_lock(&rdev->devlist_mtx);
Johannes Berg683b6d32012-11-08 21:25:48 +01002831 err = cfg80211_can_use_chan(rdev, wdev, params.chandef.chan,
Michal Kaziore4e32452012-06-29 12:47:08 +02002832 CHAN_MODE_SHARED);
2833 mutex_unlock(&rdev->devlist_mtx);
2834
2835 if (err)
2836 return err;
2837
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302838 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
2839 params.acl = parse_acl_data(&rdev->wiphy, info);
2840 if (IS_ERR(params.acl))
2841 return PTR_ERR(params.acl);
2842 }
2843
Hila Gonene35e4d22012-06-27 17:19:42 +03002844 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002845 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002846 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002847 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002848 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002849 wdev->ssid_len = params.ssid_len;
2850 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002851 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302852
2853 kfree(params.acl);
2854
Johannes Berg56d18932011-05-09 18:41:15 +02002855 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002856}
2857
Johannes Berg88600202012-02-13 15:17:18 +01002858static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2859{
2860 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2861 struct net_device *dev = info->user_ptr[1];
2862 struct wireless_dev *wdev = dev->ieee80211_ptr;
2863 struct cfg80211_beacon_data params;
2864 int err;
2865
2866 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2867 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2868 return -EOPNOTSUPP;
2869
2870 if (!rdev->ops->change_beacon)
2871 return -EOPNOTSUPP;
2872
2873 if (!wdev->beacon_interval)
2874 return -EINVAL;
2875
2876 err = nl80211_parse_beacon(info, &params);
2877 if (err)
2878 return err;
2879
Hila Gonene35e4d22012-06-27 17:19:42 +03002880 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002881}
2882
2883static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002884{
Johannes Berg4c476992010-10-04 21:36:35 +02002885 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2886 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002887
Michal Kazior60771782012-06-29 12:46:56 +02002888 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002889}
2890
Johannes Berg5727ef12007-12-19 02:03:34 +01002891static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2892 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2893 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2894 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002895 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002896 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002897 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002898};
2899
Johannes Bergeccb8e82009-05-11 21:57:56 +03002900static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002901 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002902 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002903{
2904 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002905 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002906 int flag;
2907
Johannes Bergeccb8e82009-05-11 21:57:56 +03002908 /*
2909 * Try parsing the new attribute first so userspace
2910 * can specify both for older kernels.
2911 */
2912 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2913 if (nla) {
2914 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002915
Johannes Bergeccb8e82009-05-11 21:57:56 +03002916 sta_flags = nla_data(nla);
2917 params->sta_flags_mask = sta_flags->mask;
2918 params->sta_flags_set = sta_flags->set;
2919 if ((params->sta_flags_mask |
2920 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2921 return -EINVAL;
2922 return 0;
2923 }
2924
2925 /* if present, parse the old attribute */
2926
2927 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002928 if (!nla)
2929 return 0;
2930
2931 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2932 nla, sta_flags_policy))
2933 return -EINVAL;
2934
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002935 /*
2936 * Only allow certain flags for interface types so that
2937 * other attributes are silently ignored. Remember that
2938 * this is backward compatibility code with old userspace
2939 * and shouldn't be hit in other cases anyway.
2940 */
2941 switch (iftype) {
2942 case NL80211_IFTYPE_AP:
2943 case NL80211_IFTYPE_AP_VLAN:
2944 case NL80211_IFTYPE_P2P_GO:
2945 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2946 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2947 BIT(NL80211_STA_FLAG_WME) |
2948 BIT(NL80211_STA_FLAG_MFP);
2949 break;
2950 case NL80211_IFTYPE_P2P_CLIENT:
2951 case NL80211_IFTYPE_STATION:
2952 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2953 BIT(NL80211_STA_FLAG_TDLS_PEER);
2954 break;
2955 case NL80211_IFTYPE_MESH_POINT:
2956 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2957 BIT(NL80211_STA_FLAG_MFP) |
2958 BIT(NL80211_STA_FLAG_AUTHORIZED);
2959 default:
2960 return -EINVAL;
2961 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002962
Johannes Berg3383b5a2012-05-10 20:14:43 +02002963 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2964 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002965 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002966
Johannes Berg3383b5a2012-05-10 20:14:43 +02002967 /* no longer support new API additions in old API */
2968 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2969 return -EINVAL;
2970 }
2971 }
2972
Johannes Berg5727ef12007-12-19 02:03:34 +01002973 return 0;
2974}
2975
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002976static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2977 int attr)
2978{
2979 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002980 u32 bitrate;
2981 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002982
2983 rate = nla_nest_start(msg, attr);
2984 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002985 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002986
2987 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2988 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002989 /* report 16-bit bitrate only if we can */
2990 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002991 if (bitrate > 0 &&
2992 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
2993 return false;
2994 if (bitrate_compat > 0 &&
2995 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
2996 return false;
2997
2998 if (info->flags & RATE_INFO_FLAGS_MCS) {
2999 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3000 return false;
3001 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3002 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3003 return false;
3004 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3005 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3006 return false;
3007 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3008 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3009 return false;
3010 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3011 return false;
3012 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3013 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3014 return false;
3015 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3016 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3017 return false;
3018 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3019 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3020 return false;
3021 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3022 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3023 return false;
3024 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3025 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3026 return false;
3027 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003028
3029 nla_nest_end(msg, rate);
3030 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003031}
3032
Eric W. Biederman15e47302012-09-07 20:12:54 +00003033static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003034 int flags,
3035 struct cfg80211_registered_device *rdev,
3036 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003037 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003038{
3039 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003040 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003041
Eric W. Biederman15e47302012-09-07 20:12:54 +00003042 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003043 if (!hdr)
3044 return -1;
3045
David S. Miller9360ffd2012-03-29 04:41:26 -04003046 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3047 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3048 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3049 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003050
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003051 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3052 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003053 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003054 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3055 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3056 sinfo->connected_time))
3057 goto nla_put_failure;
3058 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3059 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3060 sinfo->inactive_time))
3061 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003062 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3063 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003064 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003065 (u32)sinfo->rx_bytes))
3066 goto nla_put_failure;
3067 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3068 NL80211_STA_INFO_TX_BYTES64)) &&
3069 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3070 (u32)sinfo->tx_bytes))
3071 goto nla_put_failure;
3072 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3073 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003074 sinfo->rx_bytes))
3075 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003076 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3077 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003078 sinfo->tx_bytes))
3079 goto nla_put_failure;
3080 if ((sinfo->filled & STATION_INFO_LLID) &&
3081 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3082 goto nla_put_failure;
3083 if ((sinfo->filled & STATION_INFO_PLID) &&
3084 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3085 goto nla_put_failure;
3086 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3087 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3088 sinfo->plink_state))
3089 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003090 switch (rdev->wiphy.signal_type) {
3091 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003092 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3093 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3094 sinfo->signal))
3095 goto nla_put_failure;
3096 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3097 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3098 sinfo->signal_avg))
3099 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003100 break;
3101 default:
3102 break;
3103 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003104 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003105 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3106 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003107 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003108 }
3109 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3110 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3111 NL80211_STA_INFO_RX_BITRATE))
3112 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003113 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003114 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3115 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3116 sinfo->rx_packets))
3117 goto nla_put_failure;
3118 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3119 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3120 sinfo->tx_packets))
3121 goto nla_put_failure;
3122 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3123 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3124 sinfo->tx_retries))
3125 goto nla_put_failure;
3126 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3127 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3128 sinfo->tx_failed))
3129 goto nla_put_failure;
3130 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3131 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3132 sinfo->beacon_loss_count))
3133 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003134 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3135 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3136 sinfo->local_pm))
3137 goto nla_put_failure;
3138 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3139 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3140 sinfo->peer_pm))
3141 goto nla_put_failure;
3142 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3143 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3144 sinfo->nonpeer_pm))
3145 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003146 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3147 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3148 if (!bss_param)
3149 goto nla_put_failure;
3150
David S. Miller9360ffd2012-03-29 04:41:26 -04003151 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3152 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3153 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3154 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3155 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3156 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3157 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3158 sinfo->bss_param.dtim_period) ||
3159 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3160 sinfo->bss_param.beacon_interval))
3161 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003162
3163 nla_nest_end(msg, bss_param);
3164 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003165 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3166 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3167 sizeof(struct nl80211_sta_flag_update),
3168 &sinfo->sta_flags))
3169 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003170 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3171 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3172 sinfo->t_offset))
3173 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003174 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003175
David S. Miller9360ffd2012-03-29 04:41:26 -04003176 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3177 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3178 sinfo->assoc_req_ies))
3179 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003180
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003181 return genlmsg_end(msg, hdr);
3182
3183 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003184 genlmsg_cancel(msg, hdr);
3185 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003186}
3187
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003188static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003189 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003190{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003191 struct station_info sinfo;
3192 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003193 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003194 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003195 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003196 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003197
Johannes Berg67748892010-10-04 21:14:06 +02003198 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3199 if (err)
3200 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003201
3202 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003203 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003204 goto out_err;
3205 }
3206
Johannes Bergbba95fe2008-07-29 13:22:51 +02003207 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003208 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003209 err = rdev_dump_station(dev, netdev, sta_idx,
3210 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003211 if (err == -ENOENT)
3212 break;
3213 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003214 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003215
3216 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003217 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003218 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003219 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003220 &sinfo) < 0)
3221 goto out;
3222
3223 sta_idx++;
3224 }
3225
3226
3227 out:
3228 cb->args[1] = sta_idx;
3229 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003230 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003231 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003232
3233 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003234}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003235
Johannes Berg5727ef12007-12-19 02:03:34 +01003236static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3237{
Johannes Berg4c476992010-10-04 21:36:35 +02003238 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3239 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003240 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003241 struct sk_buff *msg;
3242 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003243 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003244
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003245 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003246
3247 if (!info->attrs[NL80211_ATTR_MAC])
3248 return -EINVAL;
3249
3250 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3251
Johannes Berg4c476992010-10-04 21:36:35 +02003252 if (!rdev->ops->get_station)
3253 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003254
Hila Gonene35e4d22012-06-27 17:19:42 +03003255 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003256 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003257 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003258
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003259 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003260 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003261 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003262
Eric W. Biederman15e47302012-09-07 20:12:54 +00003263 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003264 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003265 nlmsg_free(msg);
3266 return -ENOBUFS;
3267 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003268
Johannes Berg4c476992010-10-04 21:36:35 +02003269 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003270}
3271
3272/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003273 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003274 */
Johannes Berg80b99892011-11-18 16:23:01 +01003275static struct net_device *get_vlan(struct genl_info *info,
3276 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003277{
Johannes Berg463d0182009-07-14 00:33:35 +02003278 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003279 struct net_device *v;
3280 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003281
Johannes Berg80b99892011-11-18 16:23:01 +01003282 if (!vlanattr)
3283 return NULL;
3284
3285 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3286 if (!v)
3287 return ERR_PTR(-ENODEV);
3288
3289 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3290 ret = -EINVAL;
3291 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003292 }
Johannes Berg80b99892011-11-18 16:23:01 +01003293
3294 if (!netif_running(v)) {
3295 ret = -ENETDOWN;
3296 goto error;
3297 }
3298
3299 return v;
3300 error:
3301 dev_put(v);
3302 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003303}
3304
3305static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3306{
Johannes Berg4c476992010-10-04 21:36:35 +02003307 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003308 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003309 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003310 struct station_parameters params;
3311 u8 *mac_addr = NULL;
3312
3313 memset(&params, 0, sizeof(params));
3314
3315 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003316 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003317
3318 if (info->attrs[NL80211_ATTR_STA_AID])
3319 return -EINVAL;
3320
3321 if (!info->attrs[NL80211_ATTR_MAC])
3322 return -EINVAL;
3323
3324 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3325
3326 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3327 params.supported_rates =
3328 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3329 params.supported_rates_len =
3330 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3331 }
3332
Johannes Bergba23d202012-12-27 17:32:09 +01003333 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL] ||
3334 info->attrs[NL80211_ATTR_HT_CAPABILITY])
3335 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003336
Johannes Bergbdd90d52011-12-14 12:20:27 +01003337 if (!rdev->ops->change_station)
3338 return -EOPNOTSUPP;
3339
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003340 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003341 return -EINVAL;
3342
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003343 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3344 params.plink_action =
3345 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3346
Javier Cardona9c3990a2011-05-03 16:57:11 -07003347 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3348 params.plink_state =
3349 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3350
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003351 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3352 enum nl80211_mesh_power_mode pm = nla_get_u32(
3353 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3354
3355 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3356 pm > NL80211_MESH_POWER_MAX)
3357 return -EINVAL;
3358
3359 params.local_pm = pm;
3360 }
3361
Johannes Berga97f4422009-06-18 17:23:43 +02003362 switch (dev->ieee80211_ptr->iftype) {
3363 case NL80211_IFTYPE_AP:
3364 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003365 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003366 /* disallow mesh-specific things */
3367 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003368 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003369 if (params.local_pm)
3370 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003371
3372 /* TDLS can't be set, ... */
3373 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3374 return -EINVAL;
3375 /*
3376 * ... but don't bother the driver with it. This works around
3377 * a hostapd/wpa_supplicant issue -- it always includes the
3378 * TLDS_PEER flag in the mask even for AP mode.
3379 */
3380 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3381
3382 /* accept only the listed bits */
3383 if (params.sta_flags_mask &
3384 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003385 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3386 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003387 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3388 BIT(NL80211_STA_FLAG_WME) |
3389 BIT(NL80211_STA_FLAG_MFP)))
3390 return -EINVAL;
3391
Johannes Bergd582cff2012-10-26 17:53:44 +02003392 /* but authenticated/associated only if driver handles it */
3393 if (!(rdev->wiphy.features &
3394 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3395 params.sta_flags_mask &
3396 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3397 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3398 return -EINVAL;
3399
Johannes Bergba23d202012-12-27 17:32:09 +01003400 /* reject other things that can't change */
3401 if (params.supported_rates)
3402 return -EINVAL;
3403
Johannes Bergbdd90d52011-12-14 12:20:27 +01003404 /* must be last in here for error handling */
3405 params.vlan = get_vlan(info, rdev);
3406 if (IS_ERR(params.vlan))
3407 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003408 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003409 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003410 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003411 /*
3412 * Don't allow userspace to change the TDLS_PEER flag,
3413 * but silently ignore attempts to change it since we
3414 * don't have state here to verify that it doesn't try
3415 * to change the flag.
3416 */
3417 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003418 /* fall through */
3419 case NL80211_IFTYPE_ADHOC:
3420 /* disallow things sta doesn't support */
3421 if (params.plink_action)
3422 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003423 if (params.local_pm)
3424 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003425 /* reject any changes other than AUTHORIZED */
3426 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3427 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003428 break;
3429 case NL80211_IFTYPE_MESH_POINT:
3430 /* disallow things mesh doesn't support */
3431 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003432 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003433 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003434 return -EINVAL;
3435 /*
3436 * No special handling for TDLS here -- the userspace
3437 * mesh code doesn't have this bug.
3438 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003439 if (params.sta_flags_mask &
3440 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003441 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003442 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003443 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003444 break;
3445 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003446 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003447 }
3448
Johannes Bergbdd90d52011-12-14 12:20:27 +01003449 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003450
Hila Gonene35e4d22012-06-27 17:19:42 +03003451 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003452
Johannes Berg5727ef12007-12-19 02:03:34 +01003453 if (params.vlan)
3454 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003455
Johannes Berg5727ef12007-12-19 02:03:34 +01003456 return err;
3457}
3458
Eliad Pellerc75786c2011-08-23 14:37:46 +03003459static struct nla_policy
3460nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3461 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3462 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3463};
3464
Johannes Berg5727ef12007-12-19 02:03:34 +01003465static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3466{
Johannes Berg4c476992010-10-04 21:36:35 +02003467 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003468 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003469 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003470 struct station_parameters params;
3471 u8 *mac_addr = NULL;
3472
3473 memset(&params, 0, sizeof(params));
3474
3475 if (!info->attrs[NL80211_ATTR_MAC])
3476 return -EINVAL;
3477
Johannes Berg5727ef12007-12-19 02:03:34 +01003478 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3479 return -EINVAL;
3480
3481 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3482 return -EINVAL;
3483
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003484 if (!info->attrs[NL80211_ATTR_STA_AID])
3485 return -EINVAL;
3486
Johannes Berg5727ef12007-12-19 02:03:34 +01003487 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3488 params.supported_rates =
3489 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3490 params.supported_rates_len =
3491 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3492 params.listen_interval =
3493 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003494
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003495 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3496 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3497 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003498
Jouni Malinen36aedc92008-08-25 11:58:58 +03003499 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3500 params.ht_capa =
3501 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003502
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003503 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3504 params.vht_capa =
3505 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3506
Javier Cardona96b78df2011-04-07 15:08:33 -07003507 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3508 params.plink_action =
3509 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3510
Johannes Bergbdd90d52011-12-14 12:20:27 +01003511 if (!rdev->ops->add_station)
3512 return -EOPNOTSUPP;
3513
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003514 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003515 return -EINVAL;
3516
Johannes Bergbdd90d52011-12-14 12:20:27 +01003517 switch (dev->ieee80211_ptr->iftype) {
3518 case NL80211_IFTYPE_AP:
3519 case NL80211_IFTYPE_AP_VLAN:
3520 case NL80211_IFTYPE_P2P_GO:
3521 /* parse WME attributes if sta is WME capable */
3522 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3523 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3524 info->attrs[NL80211_ATTR_STA_WME]) {
3525 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3526 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003527
Johannes Bergbdd90d52011-12-14 12:20:27 +01003528 nla = info->attrs[NL80211_ATTR_STA_WME];
3529 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3530 nl80211_sta_wme_policy);
3531 if (err)
3532 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003533
Johannes Bergbdd90d52011-12-14 12:20:27 +01003534 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3535 params.uapsd_queues =
3536 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3537 if (params.uapsd_queues &
3538 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3539 return -EINVAL;
3540
3541 if (tb[NL80211_STA_WME_MAX_SP])
3542 params.max_sp =
3543 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3544
3545 if (params.max_sp &
3546 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3547 return -EINVAL;
3548
3549 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3550 }
3551 /* TDLS peers cannot be added */
3552 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003553 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003554 /* but don't bother the driver with it */
3555 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003556
Johannes Bergd582cff2012-10-26 17:53:44 +02003557 /* allow authenticated/associated only if driver handles it */
3558 if (!(rdev->wiphy.features &
3559 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3560 params.sta_flags_mask &
3561 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3562 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3563 return -EINVAL;
3564
Johannes Bergbdd90d52011-12-14 12:20:27 +01003565 /* must be last in here for error handling */
3566 params.vlan = get_vlan(info, rdev);
3567 if (IS_ERR(params.vlan))
3568 return PTR_ERR(params.vlan);
3569 break;
3570 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergd582cff2012-10-26 17:53:44 +02003571 /* associated is disallowed */
3572 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3573 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003574 /* TDLS peers cannot be added */
3575 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003576 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003577 break;
3578 case NL80211_IFTYPE_STATION:
Johannes Bergd582cff2012-10-26 17:53:44 +02003579 /* associated is disallowed */
3580 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3581 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003582 /* Only TDLS peers can be added */
3583 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3584 return -EINVAL;
3585 /* Can only add if TDLS ... */
3586 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3587 return -EOPNOTSUPP;
3588 /* ... with external setup is supported */
3589 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3590 return -EOPNOTSUPP;
3591 break;
3592 default:
3593 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003594 }
3595
Johannes Bergbdd90d52011-12-14 12:20:27 +01003596 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003597
Hila Gonene35e4d22012-06-27 17:19:42 +03003598 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003599
Johannes Berg5727ef12007-12-19 02:03:34 +01003600 if (params.vlan)
3601 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003602 return err;
3603}
3604
3605static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3606{
Johannes Berg4c476992010-10-04 21:36:35 +02003607 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3608 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003609 u8 *mac_addr = NULL;
3610
3611 if (info->attrs[NL80211_ATTR_MAC])
3612 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3613
Johannes Berge80cf852009-05-11 14:43:13 +02003614 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003615 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003616 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003617 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3618 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003619
Johannes Berg4c476992010-10-04 21:36:35 +02003620 if (!rdev->ops->del_station)
3621 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003622
Hila Gonene35e4d22012-06-27 17:19:42 +03003623 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003624}
3625
Eric W. Biederman15e47302012-09-07 20:12:54 +00003626static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003627 int flags, struct net_device *dev,
3628 u8 *dst, u8 *next_hop,
3629 struct mpath_info *pinfo)
3630{
3631 void *hdr;
3632 struct nlattr *pinfoattr;
3633
Eric W. Biederman15e47302012-09-07 20:12:54 +00003634 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003635 if (!hdr)
3636 return -1;
3637
David S. Miller9360ffd2012-03-29 04:41:26 -04003638 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3639 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3640 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3641 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3642 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003643
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003644 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3645 if (!pinfoattr)
3646 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003647 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3648 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3649 pinfo->frame_qlen))
3650 goto nla_put_failure;
3651 if (((pinfo->filled & MPATH_INFO_SN) &&
3652 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3653 ((pinfo->filled & MPATH_INFO_METRIC) &&
3654 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3655 pinfo->metric)) ||
3656 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3657 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3658 pinfo->exptime)) ||
3659 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3660 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3661 pinfo->flags)) ||
3662 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3663 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3664 pinfo->discovery_timeout)) ||
3665 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3666 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3667 pinfo->discovery_retries)))
3668 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003669
3670 nla_nest_end(msg, pinfoattr);
3671
3672 return genlmsg_end(msg, hdr);
3673
3674 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003675 genlmsg_cancel(msg, hdr);
3676 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003677}
3678
3679static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003680 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003681{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003682 struct mpath_info pinfo;
3683 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003684 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003685 u8 dst[ETH_ALEN];
3686 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003687 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003688 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003689
Johannes Berg67748892010-10-04 21:14:06 +02003690 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3691 if (err)
3692 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003693
3694 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003695 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003696 goto out_err;
3697 }
3698
Jouni Malineneec60b02009-03-20 21:21:19 +02003699 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3700 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003701 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003702 }
3703
Johannes Bergbba95fe2008-07-29 13:22:51 +02003704 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003705 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3706 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003707 if (err == -ENOENT)
3708 break;
3709 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003710 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003711
Eric W. Biederman15e47302012-09-07 20:12:54 +00003712 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003713 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3714 netdev, dst, next_hop,
3715 &pinfo) < 0)
3716 goto out;
3717
3718 path_idx++;
3719 }
3720
3721
3722 out:
3723 cb->args[1] = path_idx;
3724 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003725 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003726 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003727 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003728}
3729
3730static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3731{
Johannes Berg4c476992010-10-04 21:36:35 +02003732 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003733 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003734 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003735 struct mpath_info pinfo;
3736 struct sk_buff *msg;
3737 u8 *dst = NULL;
3738 u8 next_hop[ETH_ALEN];
3739
3740 memset(&pinfo, 0, sizeof(pinfo));
3741
3742 if (!info->attrs[NL80211_ATTR_MAC])
3743 return -EINVAL;
3744
3745 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3746
Johannes Berg4c476992010-10-04 21:36:35 +02003747 if (!rdev->ops->get_mpath)
3748 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003749
Johannes Berg4c476992010-10-04 21:36:35 +02003750 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3751 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003752
Hila Gonene35e4d22012-06-27 17:19:42 +03003753 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003754 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003755 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003756
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003757 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003758 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003759 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003760
Eric W. Biederman15e47302012-09-07 20:12:54 +00003761 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003762 dev, dst, next_hop, &pinfo) < 0) {
3763 nlmsg_free(msg);
3764 return -ENOBUFS;
3765 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003766
Johannes Berg4c476992010-10-04 21:36:35 +02003767 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003768}
3769
3770static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3771{
Johannes Berg4c476992010-10-04 21:36:35 +02003772 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3773 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003774 u8 *dst = NULL;
3775 u8 *next_hop = NULL;
3776
3777 if (!info->attrs[NL80211_ATTR_MAC])
3778 return -EINVAL;
3779
3780 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3781 return -EINVAL;
3782
3783 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3784 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3785
Johannes Berg4c476992010-10-04 21:36:35 +02003786 if (!rdev->ops->change_mpath)
3787 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003788
Johannes Berg4c476992010-10-04 21:36:35 +02003789 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3790 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003791
Hila Gonene35e4d22012-06-27 17:19:42 +03003792 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003793}
Johannes Berg4c476992010-10-04 21:36:35 +02003794
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003795static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3796{
Johannes Berg4c476992010-10-04 21:36:35 +02003797 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3798 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003799 u8 *dst = NULL;
3800 u8 *next_hop = NULL;
3801
3802 if (!info->attrs[NL80211_ATTR_MAC])
3803 return -EINVAL;
3804
3805 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3806 return -EINVAL;
3807
3808 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3809 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3810
Johannes Berg4c476992010-10-04 21:36:35 +02003811 if (!rdev->ops->add_mpath)
3812 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003813
Johannes Berg4c476992010-10-04 21:36:35 +02003814 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3815 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003816
Hila Gonene35e4d22012-06-27 17:19:42 +03003817 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003818}
3819
3820static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3821{
Johannes Berg4c476992010-10-04 21:36:35 +02003822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3823 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003824 u8 *dst = NULL;
3825
3826 if (info->attrs[NL80211_ATTR_MAC])
3827 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3828
Johannes Berg4c476992010-10-04 21:36:35 +02003829 if (!rdev->ops->del_mpath)
3830 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003831
Hila Gonene35e4d22012-06-27 17:19:42 +03003832 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003833}
3834
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003835static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3836{
Johannes Berg4c476992010-10-04 21:36:35 +02003837 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3838 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003839 struct bss_parameters params;
3840
3841 memset(&params, 0, sizeof(params));
3842 /* default to not changing parameters */
3843 params.use_cts_prot = -1;
3844 params.use_short_preamble = -1;
3845 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003846 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003847 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01003848 params.p2p_ctwindow = -1;
3849 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003850
3851 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3852 params.use_cts_prot =
3853 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3854 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3855 params.use_short_preamble =
3856 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3857 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3858 params.use_short_slot_time =
3859 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003860 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3861 params.basic_rates =
3862 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3863 params.basic_rates_len =
3864 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3865 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003866 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3867 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003868 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3869 params.ht_opmode =
3870 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003871
Johannes Berg53cabad2012-11-14 15:17:28 +01003872 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3873 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3874 return -EINVAL;
3875 params.p2p_ctwindow =
3876 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3877 if (params.p2p_ctwindow < 0)
3878 return -EINVAL;
3879 if (params.p2p_ctwindow != 0 &&
3880 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3881 return -EINVAL;
3882 }
3883
3884 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3885 u8 tmp;
3886
3887 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3888 return -EINVAL;
3889 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3890 if (tmp > 1)
3891 return -EINVAL;
3892 params.p2p_opp_ps = tmp;
3893 if (params.p2p_opp_ps &&
3894 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3895 return -EINVAL;
3896 }
3897
Johannes Berg4c476992010-10-04 21:36:35 +02003898 if (!rdev->ops->change_bss)
3899 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003900
Johannes Berg074ac8d2010-09-16 14:58:22 +02003901 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003902 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3903 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003904
Hila Gonene35e4d22012-06-27 17:19:42 +03003905 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003906}
3907
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003908static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003909 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3910 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3911 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3912 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3913 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3914 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3915};
3916
3917static int parse_reg_rule(struct nlattr *tb[],
3918 struct ieee80211_reg_rule *reg_rule)
3919{
3920 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3921 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3922
3923 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3924 return -EINVAL;
3925 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3926 return -EINVAL;
3927 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3928 return -EINVAL;
3929 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3930 return -EINVAL;
3931 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3932 return -EINVAL;
3933
3934 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3935
3936 freq_range->start_freq_khz =
3937 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3938 freq_range->end_freq_khz =
3939 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3940 freq_range->max_bandwidth_khz =
3941 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3942
3943 power_rule->max_eirp =
3944 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3945
3946 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3947 power_rule->max_antenna_gain =
3948 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3949
3950 return 0;
3951}
3952
3953static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3954{
3955 int r;
3956 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003957 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003958
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003959 /*
3960 * You should only get this when cfg80211 hasn't yet initialized
3961 * completely when built-in to the kernel right between the time
3962 * window between nl80211_init() and regulatory_init(), if that is
3963 * even possible.
3964 */
Johannes Berg458f4f92012-12-06 15:47:38 +01003965 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003966 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003967
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003968 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3969 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003970
3971 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3972
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003973 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3974 user_reg_hint_type =
3975 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3976 else
3977 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3978
3979 switch (user_reg_hint_type) {
3980 case NL80211_USER_REG_HINT_USER:
3981 case NL80211_USER_REG_HINT_CELL_BASE:
3982 break;
3983 default:
3984 return -EINVAL;
3985 }
3986
3987 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003988
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003989 return r;
3990}
3991
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003992static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003993 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003994{
Johannes Berg4c476992010-10-04 21:36:35 +02003995 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003996 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003997 struct wireless_dev *wdev = dev->ieee80211_ptr;
3998 struct mesh_config cur_params;
3999 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004000 void *hdr;
4001 struct nlattr *pinfoattr;
4002 struct sk_buff *msg;
4003
Johannes Berg29cbe682010-12-03 09:20:44 +01004004 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4005 return -EOPNOTSUPP;
4006
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004007 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004008 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004009
Johannes Berg29cbe682010-12-03 09:20:44 +01004010 wdev_lock(wdev);
4011 /* If not connected, get default parameters */
4012 if (!wdev->mesh_id_len)
4013 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4014 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004015 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004016 wdev_unlock(wdev);
4017
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004018 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004019 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004020
4021 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004022 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004023 if (!msg)
4024 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004025 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004026 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004027 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004028 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004029 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004030 if (!pinfoattr)
4031 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004032 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4033 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4034 cur_params.dot11MeshRetryTimeout) ||
4035 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4036 cur_params.dot11MeshConfirmTimeout) ||
4037 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4038 cur_params.dot11MeshHoldingTimeout) ||
4039 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4040 cur_params.dot11MeshMaxPeerLinks) ||
4041 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4042 cur_params.dot11MeshMaxRetries) ||
4043 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4044 cur_params.dot11MeshTTL) ||
4045 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4046 cur_params.element_ttl) ||
4047 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4048 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004049 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4050 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004051 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4052 cur_params.dot11MeshHWMPmaxPREQretries) ||
4053 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4054 cur_params.path_refresh_time) ||
4055 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4056 cur_params.min_discovery_timeout) ||
4057 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4058 cur_params.dot11MeshHWMPactivePathTimeout) ||
4059 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4060 cur_params.dot11MeshHWMPpreqMinInterval) ||
4061 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4062 cur_params.dot11MeshHWMPperrMinInterval) ||
4063 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4064 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4065 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4066 cur_params.dot11MeshHWMPRootMode) ||
4067 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4068 cur_params.dot11MeshHWMPRannInterval) ||
4069 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4070 cur_params.dot11MeshGateAnnouncementProtocol) ||
4071 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4072 cur_params.dot11MeshForwarding) ||
4073 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004074 cur_params.rssi_threshold) ||
4075 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004076 cur_params.ht_opmode) ||
4077 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4078 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4079 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004080 cur_params.dot11MeshHWMProotInterval) ||
4081 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004082 cur_params.dot11MeshHWMPconfirmationInterval) ||
4083 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4084 cur_params.power_mode) ||
4085 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4086 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004087 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004088 nla_nest_end(msg, pinfoattr);
4089 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004090 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004091
Johannes Berg3b858752009-03-12 09:55:09 +01004092 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004093 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004094 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004095 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004096 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004097}
4098
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004099static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004100 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4101 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4102 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4103 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4104 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4105 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004106 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004107 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004108 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004109 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4110 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4111 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4112 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4113 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004114 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004115 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004116 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004117 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004118 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004119 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004120 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4121 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004122 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4123 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004124 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004125 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4126 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004127};
4128
Javier Cardonac80d5452010-12-16 17:37:49 -08004129static const struct nla_policy
4130 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004131 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004132 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4133 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004134 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004135 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004136 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004137 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004138};
4139
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004140static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004141 struct mesh_config *cfg,
4142 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004143{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004144 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004145 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004146
Marco Porschea54fba2013-01-07 16:04:48 +01004147#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4148do { \
4149 if (tb[attr]) { \
4150 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4151 return -EINVAL; \
4152 cfg->param = fn(tb[attr]); \
4153 mask |= (1 << (attr - 1)); \
4154 } \
4155} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004156
4157
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004158 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004159 return -EINVAL;
4160 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004161 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004162 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004163 return -EINVAL;
4164
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004165 /* This makes sure that there aren't more than 32 mesh config
4166 * parameters (otherwise our bitfield scheme would not work.) */
4167 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4168
4169 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004170 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004171 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4172 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004173 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004174 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4175 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004176 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004177 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4178 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004179 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004180 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4181 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004182 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004183 mask, NL80211_MESHCONF_MAX_RETRIES,
4184 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004185 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004186 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004187 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004188 mask, NL80211_MESHCONF_ELEMENT_TTL,
4189 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004190 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004191 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4192 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004193 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4194 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004195 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4196 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004197 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004198 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4199 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004200 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004201 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4202 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004203 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004204 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4205 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004206 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4207 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004208 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4209 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004210 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004211 1, 65535, mask,
4212 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004213 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004214 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004215 1, 65535, mask,
4216 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004217 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004218 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004219 dot11MeshHWMPnetDiameterTraversalTime,
4220 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004221 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4222 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004223 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4224 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4225 nla_get_u8);
4226 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4227 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004228 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004229 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004230 dot11MeshGateAnnouncementProtocol, 0, 1,
4231 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004232 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004233 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004234 mask, NL80211_MESHCONF_FORWARDING,
4235 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004236 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004237 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4238 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004239 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004240 mask, NL80211_MESHCONF_HT_OPMODE,
4241 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004242 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004243 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004244 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4245 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004246 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004247 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4248 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004249 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004250 dot11MeshHWMPconfirmationInterval,
4251 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004252 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4253 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004254 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4255 NL80211_MESH_POWER_ACTIVE,
4256 NL80211_MESH_POWER_MAX,
4257 mask, NL80211_MESHCONF_POWER_MODE,
4258 nla_get_u32);
4259 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4260 0, 65535, mask,
4261 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004262 if (mask_out)
4263 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004264
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004265 return 0;
4266
4267#undef FILL_IN_MESH_PARAM_IF_SET
4268}
4269
Javier Cardonac80d5452010-12-16 17:37:49 -08004270static int nl80211_parse_mesh_setup(struct genl_info *info,
4271 struct mesh_setup *setup)
4272{
4273 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4274
4275 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4276 return -EINVAL;
4277 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4278 info->attrs[NL80211_ATTR_MESH_SETUP],
4279 nl80211_mesh_setup_params_policy))
4280 return -EINVAL;
4281
Javier Cardonad299a1f2012-03-31 11:31:33 -07004282 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4283 setup->sync_method =
4284 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4285 IEEE80211_SYNC_METHOD_VENDOR :
4286 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4287
Javier Cardonac80d5452010-12-16 17:37:49 -08004288 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4289 setup->path_sel_proto =
4290 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4291 IEEE80211_PATH_PROTOCOL_VENDOR :
4292 IEEE80211_PATH_PROTOCOL_HWMP;
4293
4294 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4295 setup->path_metric =
4296 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4297 IEEE80211_PATH_METRIC_VENDOR :
4298 IEEE80211_PATH_METRIC_AIRTIME;
4299
Javier Cardona581a8b02011-04-07 15:08:27 -07004300
4301 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004302 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004303 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004304 if (!is_valid_ie_attr(ieattr))
4305 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004306 setup->ie = nla_data(ieattr);
4307 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004308 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004309 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4310 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004311
4312 return 0;
4313}
4314
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004315static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004316 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004317{
4318 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4319 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004320 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004321 struct mesh_config cfg;
4322 u32 mask;
4323 int err;
4324
Johannes Berg29cbe682010-12-03 09:20:44 +01004325 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4326 return -EOPNOTSUPP;
4327
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004328 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004329 return -EOPNOTSUPP;
4330
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004331 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004332 if (err)
4333 return err;
4334
Johannes Berg29cbe682010-12-03 09:20:44 +01004335 wdev_lock(wdev);
4336 if (!wdev->mesh_id_len)
4337 err = -ENOLINK;
4338
4339 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004340 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004341
4342 wdev_unlock(wdev);
4343
4344 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004345}
4346
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004347static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4348{
Johannes Berg458f4f92012-12-06 15:47:38 +01004349 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004350 struct sk_buff *msg;
4351 void *hdr = NULL;
4352 struct nlattr *nl_reg_rules;
4353 unsigned int i;
4354 int err = -EINVAL;
4355
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004356 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004357
4358 if (!cfg80211_regdomain)
4359 goto out;
4360
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004361 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004362 if (!msg) {
4363 err = -ENOBUFS;
4364 goto out;
4365 }
4366
Eric W. Biederman15e47302012-09-07 20:12:54 +00004367 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004368 NL80211_CMD_GET_REG);
4369 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004370 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004371
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004372 if (reg_last_request_cell_base() &&
4373 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4374 NL80211_USER_REG_HINT_CELL_BASE))
4375 goto nla_put_failure;
4376
Johannes Berg458f4f92012-12-06 15:47:38 +01004377 rcu_read_lock();
4378 regdom = rcu_dereference(cfg80211_regdomain);
4379
4380 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4381 (regdom->dfs_region &&
4382 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4383 goto nla_put_failure_rcu;
4384
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004385 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4386 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004387 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004388
Johannes Berg458f4f92012-12-06 15:47:38 +01004389 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004390 struct nlattr *nl_reg_rule;
4391 const struct ieee80211_reg_rule *reg_rule;
4392 const struct ieee80211_freq_range *freq_range;
4393 const struct ieee80211_power_rule *power_rule;
4394
Johannes Berg458f4f92012-12-06 15:47:38 +01004395 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004396 freq_range = &reg_rule->freq_range;
4397 power_rule = &reg_rule->power_rule;
4398
4399 nl_reg_rule = nla_nest_start(msg, i);
4400 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004401 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004402
David S. Miller9360ffd2012-03-29 04:41:26 -04004403 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4404 reg_rule->flags) ||
4405 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4406 freq_range->start_freq_khz) ||
4407 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4408 freq_range->end_freq_khz) ||
4409 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4410 freq_range->max_bandwidth_khz) ||
4411 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4412 power_rule->max_antenna_gain) ||
4413 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4414 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004415 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004416
4417 nla_nest_end(msg, nl_reg_rule);
4418 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004419 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004420
4421 nla_nest_end(msg, nl_reg_rules);
4422
4423 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004424 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004425 goto out;
4426
Johannes Berg458f4f92012-12-06 15:47:38 +01004427nla_put_failure_rcu:
4428 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004429nla_put_failure:
4430 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004431put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004432 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004433 err = -EMSGSIZE;
4434out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004435 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004436 return err;
4437}
4438
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004439static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4440{
4441 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4442 struct nlattr *nl_reg_rule;
4443 char *alpha2 = NULL;
4444 int rem_reg_rules = 0, r = 0;
4445 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004446 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004447 struct ieee80211_regdomain *rd = NULL;
4448
4449 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4450 return -EINVAL;
4451
4452 if (!info->attrs[NL80211_ATTR_REG_RULES])
4453 return -EINVAL;
4454
4455 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4456
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004457 if (info->attrs[NL80211_ATTR_DFS_REGION])
4458 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4459
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004460 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004461 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004462 num_rules++;
4463 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004464 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004465 }
4466
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004467 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004468 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004469
4470 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004471 if (!rd)
4472 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004473
4474 rd->n_reg_rules = num_rules;
4475 rd->alpha2[0] = alpha2[0];
4476 rd->alpha2[1] = alpha2[1];
4477
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004478 /*
4479 * Disable DFS master mode if the DFS region was
4480 * not supported or known on this kernel.
4481 */
4482 if (reg_supported_dfs_region(dfs_region))
4483 rd->dfs_region = dfs_region;
4484
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004485 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004486 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004487 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004488 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4489 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004490 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4491 if (r)
4492 goto bad_reg;
4493
4494 rule_idx++;
4495
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004496 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4497 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004498 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004499 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004500 }
4501
Johannes Berg6913b492012-12-04 00:48:59 +01004502 mutex_lock(&cfg80211_mutex);
4503
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004504 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004505 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004506 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004507 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004508
Johannes Bergd2372b32008-10-24 20:32:20 +02004509 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004510 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004511 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004512}
4513
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004514static int validate_scan_freqs(struct nlattr *freqs)
4515{
4516 struct nlattr *attr1, *attr2;
4517 int n_channels = 0, tmp1, tmp2;
4518
4519 nla_for_each_nested(attr1, freqs, tmp1) {
4520 n_channels++;
4521 /*
4522 * Some hardware has a limited channel list for
4523 * scanning, and it is pretty much nonsensical
4524 * to scan for a channel twice, so disallow that
4525 * and don't require drivers to check that the
4526 * channel list they get isn't longer than what
4527 * they can scan, as long as they can scan all
4528 * the channels they registered at once.
4529 */
4530 nla_for_each_nested(attr2, freqs, tmp2)
4531 if (attr1 != attr2 &&
4532 nla_get_u32(attr1) == nla_get_u32(attr2))
4533 return 0;
4534 }
4535
4536 return n_channels;
4537}
4538
Johannes Berg2a519312009-02-10 21:25:55 +01004539static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4540{
Johannes Berg4c476992010-10-04 21:36:35 +02004541 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004542 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004543 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004544 struct nlattr *attr;
4545 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004546 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004547 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004548
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004549 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4550 return -EINVAL;
4551
Johannes Berg79c97e92009-07-07 03:56:12 +02004552 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004553
Johannes Berg4c476992010-10-04 21:36:35 +02004554 if (!rdev->ops->scan)
4555 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004556
Johannes Berg4c476992010-10-04 21:36:35 +02004557 if (rdev->scan_req)
4558 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004559
4560 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004561 n_channels = validate_scan_freqs(
4562 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004563 if (!n_channels)
4564 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004565 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004566 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004567 n_channels = 0;
4568
Johannes Berg2a519312009-02-10 21:25:55 +01004569 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4570 if (wiphy->bands[band])
4571 n_channels += wiphy->bands[band]->n_channels;
4572 }
4573
4574 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4575 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4576 n_ssids++;
4577
Johannes Berg4c476992010-10-04 21:36:35 +02004578 if (n_ssids > wiphy->max_scan_ssids)
4579 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004580
Jouni Malinen70692ad2009-02-16 19:39:13 +02004581 if (info->attrs[NL80211_ATTR_IE])
4582 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4583 else
4584 ie_len = 0;
4585
Johannes Berg4c476992010-10-04 21:36:35 +02004586 if (ie_len > wiphy->max_scan_ie_len)
4587 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004588
Johannes Berg2a519312009-02-10 21:25:55 +01004589 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004590 + sizeof(*request->ssids) * n_ssids
4591 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004592 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004593 if (!request)
4594 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004595
Johannes Berg2a519312009-02-10 21:25:55 +01004596 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004597 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004598 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004599 if (ie_len) {
4600 if (request->ssids)
4601 request->ie = (void *)(request->ssids + n_ssids);
4602 else
4603 request->ie = (void *)(request->channels + n_channels);
4604 }
Johannes Berg2a519312009-02-10 21:25:55 +01004605
Johannes Berg584991d2009-11-02 13:32:03 +01004606 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004607 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4608 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004609 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004610 struct ieee80211_channel *chan;
4611
4612 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4613
4614 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004615 err = -EINVAL;
4616 goto out_free;
4617 }
Johannes Berg584991d2009-11-02 13:32:03 +01004618
4619 /* ignore disabled channels */
4620 if (chan->flags & IEEE80211_CHAN_DISABLED)
4621 continue;
4622
4623 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004624 i++;
4625 }
4626 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004627 enum ieee80211_band band;
4628
Johannes Berg2a519312009-02-10 21:25:55 +01004629 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004630 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4631 int j;
4632 if (!wiphy->bands[band])
4633 continue;
4634 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004635 struct ieee80211_channel *chan;
4636
4637 chan = &wiphy->bands[band]->channels[j];
4638
4639 if (chan->flags & IEEE80211_CHAN_DISABLED)
4640 continue;
4641
4642 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004643 i++;
4644 }
4645 }
4646 }
4647
Johannes Berg584991d2009-11-02 13:32:03 +01004648 if (!i) {
4649 err = -EINVAL;
4650 goto out_free;
4651 }
4652
4653 request->n_channels = i;
4654
Johannes Berg2a519312009-02-10 21:25:55 +01004655 i = 0;
4656 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4657 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004658 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004659 err = -EINVAL;
4660 goto out_free;
4661 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004662 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004663 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004664 i++;
4665 }
4666 }
4667
Jouni Malinen70692ad2009-02-16 19:39:13 +02004668 if (info->attrs[NL80211_ATTR_IE]) {
4669 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004670 memcpy((void *)request->ie,
4671 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004672 request->ie_len);
4673 }
4674
Johannes Berg34850ab2011-07-18 18:08:35 +02004675 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004676 if (wiphy->bands[i])
4677 request->rates[i] =
4678 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004679
4680 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4681 nla_for_each_nested(attr,
4682 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4683 tmp) {
4684 enum ieee80211_band band = nla_type(attr);
4685
Dan Carpenter84404622011-07-29 11:52:18 +03004686 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004687 err = -EINVAL;
4688 goto out_free;
4689 }
4690 err = ieee80211_get_ratemask(wiphy->bands[band],
4691 nla_data(attr),
4692 nla_len(attr),
4693 &request->rates[band]);
4694 if (err)
4695 goto out_free;
4696 }
4697 }
4698
Sam Leffler46856bb2012-10-11 21:03:32 -07004699 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004700 request->flags = nla_get_u32(
4701 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004702 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4703 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4704 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4705 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004706 err = -EOPNOTSUPP;
4707 goto out_free;
4708 }
4709 }
Sam Lefflered4737712012-10-11 21:03:31 -07004710
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304711 request->no_cck =
4712 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4713
Johannes Bergfd014282012-06-18 19:17:03 +02004714 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004715 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004716 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004717
Johannes Berg79c97e92009-07-07 03:56:12 +02004718 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004719 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004720
Johannes Berg463d0182009-07-14 00:33:35 +02004721 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004722 nl80211_send_scan_start(rdev, wdev);
4723 if (wdev->netdev)
4724 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004725 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004726 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004727 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004728 kfree(request);
4729 }
Johannes Berg3b858752009-03-12 09:55:09 +01004730
Johannes Berg2a519312009-02-10 21:25:55 +01004731 return err;
4732}
4733
Luciano Coelho807f8a82011-05-11 17:09:35 +03004734static int nl80211_start_sched_scan(struct sk_buff *skb,
4735 struct genl_info *info)
4736{
4737 struct cfg80211_sched_scan_request *request;
4738 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4739 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004740 struct nlattr *attr;
4741 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004742 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004743 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004744 enum ieee80211_band band;
4745 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004746 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004747
4748 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4749 !rdev->ops->sched_scan_start)
4750 return -EOPNOTSUPP;
4751
4752 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4753 return -EINVAL;
4754
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004755 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4756 return -EINVAL;
4757
4758 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4759 if (interval == 0)
4760 return -EINVAL;
4761
Luciano Coelho807f8a82011-05-11 17:09:35 +03004762 wiphy = &rdev->wiphy;
4763
4764 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4765 n_channels = validate_scan_freqs(
4766 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4767 if (!n_channels)
4768 return -EINVAL;
4769 } else {
4770 n_channels = 0;
4771
4772 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4773 if (wiphy->bands[band])
4774 n_channels += wiphy->bands[band]->n_channels;
4775 }
4776
4777 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4778 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4779 tmp)
4780 n_ssids++;
4781
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004782 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004783 return -EINVAL;
4784
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004785 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4786 nla_for_each_nested(attr,
4787 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4788 tmp)
4789 n_match_sets++;
4790
4791 if (n_match_sets > wiphy->max_match_sets)
4792 return -EINVAL;
4793
Luciano Coelho807f8a82011-05-11 17:09:35 +03004794 if (info->attrs[NL80211_ATTR_IE])
4795 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4796 else
4797 ie_len = 0;
4798
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004799 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004800 return -EINVAL;
4801
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004802 mutex_lock(&rdev->sched_scan_mtx);
4803
4804 if (rdev->sched_scan_req) {
4805 err = -EINPROGRESS;
4806 goto out;
4807 }
4808
Luciano Coelho807f8a82011-05-11 17:09:35 +03004809 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004810 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004811 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004812 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004813 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004814 if (!request) {
4815 err = -ENOMEM;
4816 goto out;
4817 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004818
4819 if (n_ssids)
4820 request->ssids = (void *)&request->channels[n_channels];
4821 request->n_ssids = n_ssids;
4822 if (ie_len) {
4823 if (request->ssids)
4824 request->ie = (void *)(request->ssids + n_ssids);
4825 else
4826 request->ie = (void *)(request->channels + n_channels);
4827 }
4828
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004829 if (n_match_sets) {
4830 if (request->ie)
4831 request->match_sets = (void *)(request->ie + ie_len);
4832 else if (request->ssids)
4833 request->match_sets =
4834 (void *)(request->ssids + n_ssids);
4835 else
4836 request->match_sets =
4837 (void *)(request->channels + n_channels);
4838 }
4839 request->n_match_sets = n_match_sets;
4840
Luciano Coelho807f8a82011-05-11 17:09:35 +03004841 i = 0;
4842 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4843 /* user specified, bail out if channel not found */
4844 nla_for_each_nested(attr,
4845 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4846 tmp) {
4847 struct ieee80211_channel *chan;
4848
4849 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4850
4851 if (!chan) {
4852 err = -EINVAL;
4853 goto out_free;
4854 }
4855
4856 /* ignore disabled channels */
4857 if (chan->flags & IEEE80211_CHAN_DISABLED)
4858 continue;
4859
4860 request->channels[i] = chan;
4861 i++;
4862 }
4863 } else {
4864 /* all channels */
4865 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4866 int j;
4867 if (!wiphy->bands[band])
4868 continue;
4869 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4870 struct ieee80211_channel *chan;
4871
4872 chan = &wiphy->bands[band]->channels[j];
4873
4874 if (chan->flags & IEEE80211_CHAN_DISABLED)
4875 continue;
4876
4877 request->channels[i] = chan;
4878 i++;
4879 }
4880 }
4881 }
4882
4883 if (!i) {
4884 err = -EINVAL;
4885 goto out_free;
4886 }
4887
4888 request->n_channels = i;
4889
4890 i = 0;
4891 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4892 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4893 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004894 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004895 err = -EINVAL;
4896 goto out_free;
4897 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004898 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004899 memcpy(request->ssids[i].ssid, nla_data(attr),
4900 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004901 i++;
4902 }
4903 }
4904
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004905 i = 0;
4906 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4907 nla_for_each_nested(attr,
4908 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4909 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004910 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004911
4912 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4913 nla_data(attr), nla_len(attr),
4914 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004915 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004916 if (ssid) {
4917 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4918 err = -EINVAL;
4919 goto out_free;
4920 }
4921 memcpy(request->match_sets[i].ssid.ssid,
4922 nla_data(ssid), nla_len(ssid));
4923 request->match_sets[i].ssid.ssid_len =
4924 nla_len(ssid);
4925 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004926 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4927 if (rssi)
4928 request->rssi_thold = nla_get_u32(rssi);
4929 else
4930 request->rssi_thold =
4931 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004932 i++;
4933 }
4934 }
4935
Luciano Coelho807f8a82011-05-11 17:09:35 +03004936 if (info->attrs[NL80211_ATTR_IE]) {
4937 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4938 memcpy((void *)request->ie,
4939 nla_data(info->attrs[NL80211_ATTR_IE]),
4940 request->ie_len);
4941 }
4942
Sam Leffler46856bb2012-10-11 21:03:32 -07004943 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004944 request->flags = nla_get_u32(
4945 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004946 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4947 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4948 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4949 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004950 err = -EOPNOTSUPP;
4951 goto out_free;
4952 }
4953 }
Sam Lefflered4737712012-10-11 21:03:31 -07004954
Luciano Coelho807f8a82011-05-11 17:09:35 +03004955 request->dev = dev;
4956 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004957 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004958 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004959
Hila Gonene35e4d22012-06-27 17:19:42 +03004960 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004961 if (!err) {
4962 rdev->sched_scan_req = request;
4963 nl80211_send_sched_scan(rdev, dev,
4964 NL80211_CMD_START_SCHED_SCAN);
4965 goto out;
4966 }
4967
4968out_free:
4969 kfree(request);
4970out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004971 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004972 return err;
4973}
4974
4975static int nl80211_stop_sched_scan(struct sk_buff *skb,
4976 struct genl_info *info)
4977{
4978 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004979 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004980
4981 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4982 !rdev->ops->sched_scan_stop)
4983 return -EOPNOTSUPP;
4984
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004985 mutex_lock(&rdev->sched_scan_mtx);
4986 err = __cfg80211_stop_sched_scan(rdev, false);
4987 mutex_unlock(&rdev->sched_scan_mtx);
4988
4989 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004990}
4991
Johannes Berg9720bb32011-06-21 09:45:33 +02004992static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4993 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004994 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004995 struct wireless_dev *wdev,
4996 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004997{
Johannes Berg48ab9052009-07-10 18:42:31 +02004998 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01004999 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005000 void *hdr;
5001 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005002 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005003
5004 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005005
Eric W. Biederman15e47302012-09-07 20:12:54 +00005006 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005007 NL80211_CMD_NEW_SCAN_RESULTS);
5008 if (!hdr)
5009 return -1;
5010
Johannes Berg9720bb32011-06-21 09:45:33 +02005011 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5012
David S. Miller9360ffd2012-03-29 04:41:26 -04005013 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5014 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5015 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005016
5017 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5018 if (!bss)
5019 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005020 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005021 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005022 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005023
5024 rcu_read_lock();
5025 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005026 if (ies) {
5027 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5028 goto fail_unlock_rcu;
5029 tsf = true;
5030 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5031 ies->len, ies->data))
5032 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005033 }
5034 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005035 if (ies) {
5036 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5037 goto fail_unlock_rcu;
5038 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5039 ies->len, ies->data))
5040 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005041 }
5042 rcu_read_unlock();
5043
David S. Miller9360ffd2012-03-29 04:41:26 -04005044 if (res->beacon_interval &&
5045 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5046 goto nla_put_failure;
5047 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5048 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5049 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5050 jiffies_to_msecs(jiffies - intbss->ts)))
5051 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005052
Johannes Berg77965c92009-02-18 18:45:06 +01005053 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005054 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005055 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5056 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005057 break;
5058 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005059 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5060 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005061 break;
5062 default:
5063 break;
5064 }
5065
Johannes Berg48ab9052009-07-10 18:42:31 +02005066 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005067 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005068 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005069 if (intbss == wdev->current_bss &&
5070 nla_put_u32(msg, NL80211_BSS_STATUS,
5071 NL80211_BSS_STATUS_ASSOCIATED))
5072 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005073 break;
5074 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005075 if (intbss == wdev->current_bss &&
5076 nla_put_u32(msg, NL80211_BSS_STATUS,
5077 NL80211_BSS_STATUS_IBSS_JOINED))
5078 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005079 break;
5080 default:
5081 break;
5082 }
5083
Johannes Berg2a519312009-02-10 21:25:55 +01005084 nla_nest_end(msg, bss);
5085
5086 return genlmsg_end(msg, hdr);
5087
Johannes Berg8cef2c92013-02-05 16:54:31 +01005088 fail_unlock_rcu:
5089 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005090 nla_put_failure:
5091 genlmsg_cancel(msg, hdr);
5092 return -EMSGSIZE;
5093}
5094
5095static int nl80211_dump_scan(struct sk_buff *skb,
5096 struct netlink_callback *cb)
5097{
Johannes Berg48ab9052009-07-10 18:42:31 +02005098 struct cfg80211_registered_device *rdev;
5099 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005100 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005101 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005102 int start = cb->args[1], idx = 0;
5103 int err;
5104
Johannes Berg67748892010-10-04 21:14:06 +02005105 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5106 if (err)
5107 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005108
Johannes Berg48ab9052009-07-10 18:42:31 +02005109 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005110
Johannes Berg48ab9052009-07-10 18:42:31 +02005111 wdev_lock(wdev);
5112 spin_lock_bh(&rdev->bss_lock);
5113 cfg80211_bss_expire(rdev);
5114
Johannes Berg9720bb32011-06-21 09:45:33 +02005115 cb->seq = rdev->bss_generation;
5116
Johannes Berg48ab9052009-07-10 18:42:31 +02005117 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005118 if (++idx <= start)
5119 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005120 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005121 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005122 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005123 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005124 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005125 }
5126 }
5127
Johannes Berg48ab9052009-07-10 18:42:31 +02005128 spin_unlock_bh(&rdev->bss_lock);
5129 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005130
5131 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005132 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005133
Johannes Berg67748892010-10-04 21:14:06 +02005134 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005135}
5136
Eric W. Biederman15e47302012-09-07 20:12:54 +00005137static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005138 int flags, struct net_device *dev,
5139 struct survey_info *survey)
5140{
5141 void *hdr;
5142 struct nlattr *infoattr;
5143
Eric W. Biederman15e47302012-09-07 20:12:54 +00005144 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005145 NL80211_CMD_NEW_SURVEY_RESULTS);
5146 if (!hdr)
5147 return -ENOMEM;
5148
David S. Miller9360ffd2012-03-29 04:41:26 -04005149 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5150 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005151
5152 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5153 if (!infoattr)
5154 goto nla_put_failure;
5155
David S. Miller9360ffd2012-03-29 04:41:26 -04005156 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5157 survey->channel->center_freq))
5158 goto nla_put_failure;
5159
5160 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5161 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5162 goto nla_put_failure;
5163 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5164 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5165 goto nla_put_failure;
5166 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5167 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5168 survey->channel_time))
5169 goto nla_put_failure;
5170 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5171 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5172 survey->channel_time_busy))
5173 goto nla_put_failure;
5174 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5175 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5176 survey->channel_time_ext_busy))
5177 goto nla_put_failure;
5178 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5179 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5180 survey->channel_time_rx))
5181 goto nla_put_failure;
5182 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5183 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5184 survey->channel_time_tx))
5185 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005186
5187 nla_nest_end(msg, infoattr);
5188
5189 return genlmsg_end(msg, hdr);
5190
5191 nla_put_failure:
5192 genlmsg_cancel(msg, hdr);
5193 return -EMSGSIZE;
5194}
5195
5196static int nl80211_dump_survey(struct sk_buff *skb,
5197 struct netlink_callback *cb)
5198{
5199 struct survey_info survey;
5200 struct cfg80211_registered_device *dev;
5201 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005202 int survey_idx = cb->args[1];
5203 int res;
5204
Johannes Berg67748892010-10-04 21:14:06 +02005205 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5206 if (res)
5207 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005208
5209 if (!dev->ops->dump_survey) {
5210 res = -EOPNOTSUPP;
5211 goto out_err;
5212 }
5213
5214 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005215 struct ieee80211_channel *chan;
5216
Hila Gonene35e4d22012-06-27 17:19:42 +03005217 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005218 if (res == -ENOENT)
5219 break;
5220 if (res)
5221 goto out_err;
5222
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005223 /* Survey without a channel doesn't make sense */
5224 if (!survey.channel) {
5225 res = -EINVAL;
5226 goto out;
5227 }
5228
5229 chan = ieee80211_get_channel(&dev->wiphy,
5230 survey.channel->center_freq);
5231 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5232 survey_idx++;
5233 continue;
5234 }
5235
Holger Schurig61fa7132009-11-11 12:25:40 +01005236 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005237 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005238 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5239 netdev,
5240 &survey) < 0)
5241 goto out;
5242 survey_idx++;
5243 }
5244
5245 out:
5246 cb->args[1] = survey_idx;
5247 res = skb->len;
5248 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005249 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005250 return res;
5251}
5252
Samuel Ortizb23aa672009-07-01 21:26:54 +02005253static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5254{
5255 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5256 NL80211_WPA_VERSION_2));
5257}
5258
Jouni Malinen636a5d32009-03-19 13:39:22 +02005259static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5260{
Johannes Berg4c476992010-10-04 21:36:35 +02005261 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5262 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005263 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005264 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5265 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005266 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005267 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005268 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005269
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005270 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5271 return -EINVAL;
5272
5273 if (!info->attrs[NL80211_ATTR_MAC])
5274 return -EINVAL;
5275
Jouni Malinen17780922009-03-27 20:52:47 +02005276 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5277 return -EINVAL;
5278
Johannes Berg19957bb2009-07-02 17:20:43 +02005279 if (!info->attrs[NL80211_ATTR_SSID])
5280 return -EINVAL;
5281
5282 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5283 return -EINVAL;
5284
Johannes Bergfffd0932009-07-08 14:22:54 +02005285 err = nl80211_parse_key(info, &key);
5286 if (err)
5287 return err;
5288
5289 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005290 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5291 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005292 if (!key.p.key || !key.p.key_len)
5293 return -EINVAL;
5294 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5295 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5296 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5297 key.p.key_len != WLAN_KEY_LEN_WEP104))
5298 return -EINVAL;
5299 if (key.idx > 4)
5300 return -EINVAL;
5301 } else {
5302 key.p.key_len = 0;
5303 key.p.key = NULL;
5304 }
5305
Johannes Bergafea0b72010-08-10 09:46:42 +02005306 if (key.idx >= 0) {
5307 int i;
5308 bool ok = false;
5309 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5310 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5311 ok = true;
5312 break;
5313 }
5314 }
Johannes Berg4c476992010-10-04 21:36:35 +02005315 if (!ok)
5316 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005317 }
5318
Johannes Berg4c476992010-10-04 21:36:35 +02005319 if (!rdev->ops->auth)
5320 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005321
Johannes Berg074ac8d2010-09-16 14:58:22 +02005322 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005323 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5324 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005325
Johannes Berg19957bb2009-07-02 17:20:43 +02005326 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005327 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005328 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005329 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5330 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005331
Johannes Berg19957bb2009-07-02 17:20:43 +02005332 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5333 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5334
5335 if (info->attrs[NL80211_ATTR_IE]) {
5336 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5337 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5338 }
5339
5340 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005341 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005342 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005343
Jouni Malinene39e5b52012-09-30 19:29:39 +03005344 if (auth_type == NL80211_AUTHTYPE_SAE &&
5345 !info->attrs[NL80211_ATTR_SAE_DATA])
5346 return -EINVAL;
5347
5348 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5349 if (auth_type != NL80211_AUTHTYPE_SAE)
5350 return -EINVAL;
5351 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5352 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5353 /* need to include at least Auth Transaction and Status Code */
5354 if (sae_data_len < 4)
5355 return -EINVAL;
5356 }
5357
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005358 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5359
Johannes Berg95de8172012-01-20 13:55:25 +01005360 /*
5361 * Since we no longer track auth state, ignore
5362 * requests to only change local state.
5363 */
5364 if (local_state_change)
5365 return 0;
5366
Johannes Berg4c476992010-10-04 21:36:35 +02005367 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5368 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005369 key.p.key, key.p.key_len, key.idx,
5370 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005371}
5372
Johannes Bergc0692b82010-08-27 14:26:53 +03005373static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5374 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005375 struct cfg80211_crypto_settings *settings,
5376 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005377{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005378 memset(settings, 0, sizeof(*settings));
5379
Samuel Ortizb23aa672009-07-01 21:26:54 +02005380 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5381
Johannes Bergc0692b82010-08-27 14:26:53 +03005382 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5383 u16 proto;
5384 proto = nla_get_u16(
5385 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5386 settings->control_port_ethertype = cpu_to_be16(proto);
5387 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5388 proto != ETH_P_PAE)
5389 return -EINVAL;
5390 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5391 settings->control_port_no_encrypt = true;
5392 } else
5393 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5394
Samuel Ortizb23aa672009-07-01 21:26:54 +02005395 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5396 void *data;
5397 int len, i;
5398
5399 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5400 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5401 settings->n_ciphers_pairwise = len / sizeof(u32);
5402
5403 if (len % sizeof(u32))
5404 return -EINVAL;
5405
Johannes Berg3dc27d22009-07-02 21:36:37 +02005406 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005407 return -EINVAL;
5408
5409 memcpy(settings->ciphers_pairwise, data, len);
5410
5411 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005412 if (!cfg80211_supported_cipher_suite(
5413 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005414 settings->ciphers_pairwise[i]))
5415 return -EINVAL;
5416 }
5417
5418 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5419 settings->cipher_group =
5420 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005421 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5422 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005423 return -EINVAL;
5424 }
5425
5426 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5427 settings->wpa_versions =
5428 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5429 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5430 return -EINVAL;
5431 }
5432
5433 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5434 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005435 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005436
5437 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5438 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5439 settings->n_akm_suites = len / sizeof(u32);
5440
5441 if (len % sizeof(u32))
5442 return -EINVAL;
5443
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005444 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5445 return -EINVAL;
5446
Samuel Ortizb23aa672009-07-01 21:26:54 +02005447 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005448 }
5449
5450 return 0;
5451}
5452
Jouni Malinen636a5d32009-03-19 13:39:22 +02005453static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5454{
Johannes Berg4c476992010-10-04 21:36:35 +02005455 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5456 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005457 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005458 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005459 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005460 int err, ssid_len, ie_len = 0;
5461 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005462 u32 flags = 0;
5463 struct ieee80211_ht_cap *ht_capa = NULL;
5464 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005465
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005466 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5467 return -EINVAL;
5468
5469 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005470 !info->attrs[NL80211_ATTR_SSID] ||
5471 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005472 return -EINVAL;
5473
Johannes Berg4c476992010-10-04 21:36:35 +02005474 if (!rdev->ops->assoc)
5475 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005476
Johannes Berg074ac8d2010-09-16 14:58:22 +02005477 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005478 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5479 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005480
Johannes Berg19957bb2009-07-02 17:20:43 +02005481 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005482
Johannes Berg19957bb2009-07-02 17:20:43 +02005483 chan = ieee80211_get_channel(&rdev->wiphy,
5484 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005485 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5486 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005487
Johannes Berg19957bb2009-07-02 17:20:43 +02005488 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5489 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005490
5491 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005492 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5493 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005494 }
5495
Jouni Malinendc6382c2009-05-06 22:09:37 +03005496 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005497 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005498 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005499 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005500 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005501 else if (mfp != NL80211_MFP_NO)
5502 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005503 }
5504
Johannes Berg3e5d7642009-07-07 14:37:26 +02005505 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5506 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5507
Ben Greear7e7c8922011-11-18 11:31:59 -08005508 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5509 flags |= ASSOC_REQ_DISABLE_HT;
5510
5511 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5512 ht_capa_mask =
5513 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5514
5515 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5516 if (!ht_capa_mask)
5517 return -EINVAL;
5518 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5519 }
5520
Johannes Bergc0692b82010-08-27 14:26:53 +03005521 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005522 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005523 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5524 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005525 &crypto, flags, ht_capa,
5526 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005527
Jouni Malinen636a5d32009-03-19 13:39:22 +02005528 return err;
5529}
5530
5531static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5532{
Johannes Berg4c476992010-10-04 21:36:35 +02005533 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5534 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005535 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005536 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005537 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005538 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005539
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005540 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5541 return -EINVAL;
5542
5543 if (!info->attrs[NL80211_ATTR_MAC])
5544 return -EINVAL;
5545
5546 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5547 return -EINVAL;
5548
Johannes Berg4c476992010-10-04 21:36:35 +02005549 if (!rdev->ops->deauth)
5550 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005551
Johannes Berg074ac8d2010-09-16 14:58:22 +02005552 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005553 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5554 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005555
Johannes Berg19957bb2009-07-02 17:20:43 +02005556 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005557
Johannes Berg19957bb2009-07-02 17:20:43 +02005558 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5559 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005560 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005561 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005562 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005563
5564 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005565 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5566 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005567 }
5568
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005569 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5570
Johannes Berg4c476992010-10-04 21:36:35 +02005571 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5572 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005573}
5574
5575static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5576{
Johannes Berg4c476992010-10-04 21:36:35 +02005577 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5578 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005579 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005580 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005581 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005582 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005583
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005584 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5585 return -EINVAL;
5586
5587 if (!info->attrs[NL80211_ATTR_MAC])
5588 return -EINVAL;
5589
5590 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5591 return -EINVAL;
5592
Johannes Berg4c476992010-10-04 21:36:35 +02005593 if (!rdev->ops->disassoc)
5594 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005595
Johannes Berg074ac8d2010-09-16 14:58:22 +02005596 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005597 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5598 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005599
Johannes Berg19957bb2009-07-02 17:20:43 +02005600 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005601
Johannes Berg19957bb2009-07-02 17:20:43 +02005602 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5603 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005604 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005605 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005606 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005607
5608 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005609 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5610 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005611 }
5612
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005613 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5614
Johannes Berg4c476992010-10-04 21:36:35 +02005615 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5616 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005617}
5618
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005619static bool
5620nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5621 int mcast_rate[IEEE80211_NUM_BANDS],
5622 int rateval)
5623{
5624 struct wiphy *wiphy = &rdev->wiphy;
5625 bool found = false;
5626 int band, i;
5627
5628 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5629 struct ieee80211_supported_band *sband;
5630
5631 sband = wiphy->bands[band];
5632 if (!sband)
5633 continue;
5634
5635 for (i = 0; i < sband->n_bitrates; i++) {
5636 if (sband->bitrates[i].bitrate == rateval) {
5637 mcast_rate[band] = i + 1;
5638 found = true;
5639 break;
5640 }
5641 }
5642 }
5643
5644 return found;
5645}
5646
Johannes Berg04a773a2009-04-19 21:24:32 +02005647static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5648{
Johannes Berg4c476992010-10-04 21:36:35 +02005649 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5650 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005651 struct cfg80211_ibss_params ibss;
5652 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005653 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005654 int err;
5655
Johannes Berg8e30bc52009-04-22 17:45:38 +02005656 memset(&ibss, 0, sizeof(ibss));
5657
Johannes Berg04a773a2009-04-19 21:24:32 +02005658 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5659 return -EINVAL;
5660
Johannes Berg683b6d32012-11-08 21:25:48 +01005661 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005662 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5663 return -EINVAL;
5664
Johannes Berg8e30bc52009-04-22 17:45:38 +02005665 ibss.beacon_interval = 100;
5666
5667 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5668 ibss.beacon_interval =
5669 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5670 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5671 return -EINVAL;
5672 }
5673
Johannes Berg4c476992010-10-04 21:36:35 +02005674 if (!rdev->ops->join_ibss)
5675 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005676
Johannes Berg4c476992010-10-04 21:36:35 +02005677 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5678 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005679
Johannes Berg79c97e92009-07-07 03:56:12 +02005680 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005681
Johannes Berg39193492011-09-16 13:45:25 +02005682 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005683 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005684
5685 if (!is_valid_ether_addr(ibss.bssid))
5686 return -EINVAL;
5687 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005688 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5689 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5690
5691 if (info->attrs[NL80211_ATTR_IE]) {
5692 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5693 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5694 }
5695
Johannes Berg683b6d32012-11-08 21:25:48 +01005696 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5697 if (err)
5698 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005699
Johannes Berg683b6d32012-11-08 21:25:48 +01005700 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005701 return -EINVAL;
5702
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005703 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5704 return -EINVAL;
5705 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5706 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005707 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005708
Johannes Berg04a773a2009-04-19 21:24:32 +02005709 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005710 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005711
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005712 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5713 u8 *rates =
5714 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5715 int n_rates =
5716 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5717 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005718 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005719
Johannes Berg34850ab2011-07-18 18:08:35 +02005720 err = ieee80211_get_ratemask(sband, rates, n_rates,
5721 &ibss.basic_rates);
5722 if (err)
5723 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005724 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005725
5726 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5727 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5728 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5729 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005730
Johannes Berg4c476992010-10-04 21:36:35 +02005731 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305732 bool no_ht = false;
5733
Johannes Berg4c476992010-10-04 21:36:35 +02005734 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305735 info->attrs[NL80211_ATTR_KEYS],
5736 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005737 if (IS_ERR(connkeys))
5738 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305739
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005740 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5741 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305742 kfree(connkeys);
5743 return -EINVAL;
5744 }
Johannes Berg4c476992010-10-04 21:36:35 +02005745 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005746
Antonio Quartulli267335d2012-01-31 20:25:47 +01005747 ibss.control_port =
5748 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5749
Johannes Berg4c476992010-10-04 21:36:35 +02005750 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005751 if (err)
5752 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005753 return err;
5754}
5755
5756static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5757{
Johannes Berg4c476992010-10-04 21:36:35 +02005758 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5759 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005760
Johannes Berg4c476992010-10-04 21:36:35 +02005761 if (!rdev->ops->leave_ibss)
5762 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005763
Johannes Berg4c476992010-10-04 21:36:35 +02005764 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5765 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005766
Johannes Berg4c476992010-10-04 21:36:35 +02005767 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005768}
5769
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005770static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5771{
5772 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5773 struct net_device *dev = info->user_ptr[1];
5774 int mcast_rate[IEEE80211_NUM_BANDS];
5775 u32 nla_rate;
5776 int err;
5777
5778 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5779 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5780 return -EOPNOTSUPP;
5781
5782 if (!rdev->ops->set_mcast_rate)
5783 return -EOPNOTSUPP;
5784
5785 memset(mcast_rate, 0, sizeof(mcast_rate));
5786
5787 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5788 return -EINVAL;
5789
5790 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5791 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5792 return -EINVAL;
5793
5794 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5795
5796 return err;
5797}
5798
5799
Johannes Bergaff89a92009-07-01 21:26:51 +02005800#ifdef CONFIG_NL80211_TESTMODE
5801static struct genl_multicast_group nl80211_testmode_mcgrp = {
5802 .name = "testmode",
5803};
5804
5805static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5806{
Johannes Berg4c476992010-10-04 21:36:35 +02005807 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005808 int err;
5809
5810 if (!info->attrs[NL80211_ATTR_TESTDATA])
5811 return -EINVAL;
5812
Johannes Bergaff89a92009-07-01 21:26:51 +02005813 err = -EOPNOTSUPP;
5814 if (rdev->ops->testmode_cmd) {
5815 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005816 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005817 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5818 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5819 rdev->testmode_info = NULL;
5820 }
5821
Johannes Bergaff89a92009-07-01 21:26:51 +02005822 return err;
5823}
5824
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005825static int nl80211_testmode_dump(struct sk_buff *skb,
5826 struct netlink_callback *cb)
5827{
Johannes Berg00918d32011-12-13 17:22:05 +01005828 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005829 int err;
5830 long phy_idx;
5831 void *data = NULL;
5832 int data_len = 0;
5833
5834 if (cb->args[0]) {
5835 /*
5836 * 0 is a valid index, but not valid for args[0],
5837 * so we need to offset by 1.
5838 */
5839 phy_idx = cb->args[0] - 1;
5840 } else {
5841 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5842 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5843 nl80211_policy);
5844 if (err)
5845 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005846
Johannes Berg2bd7e352012-06-15 14:23:16 +02005847 mutex_lock(&cfg80211_mutex);
5848 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5849 nl80211_fam.attrbuf);
5850 if (IS_ERR(rdev)) {
5851 mutex_unlock(&cfg80211_mutex);
5852 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005853 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005854 phy_idx = rdev->wiphy_idx;
5855 rdev = NULL;
5856 mutex_unlock(&cfg80211_mutex);
5857
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005858 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5859 cb->args[1] =
5860 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5861 }
5862
5863 if (cb->args[1]) {
5864 data = nla_data((void *)cb->args[1]);
5865 data_len = nla_len((void *)cb->args[1]);
5866 }
5867
5868 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005869 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5870 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005871 mutex_unlock(&cfg80211_mutex);
5872 return -ENOENT;
5873 }
Johannes Berg00918d32011-12-13 17:22:05 +01005874 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005875 mutex_unlock(&cfg80211_mutex);
5876
Johannes Berg00918d32011-12-13 17:22:05 +01005877 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005878 err = -EOPNOTSUPP;
5879 goto out_err;
5880 }
5881
5882 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005883 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005884 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5885 NL80211_CMD_TESTMODE);
5886 struct nlattr *tmdata;
5887
David S. Miller9360ffd2012-03-29 04:41:26 -04005888 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005889 genlmsg_cancel(skb, hdr);
5890 break;
5891 }
5892
5893 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5894 if (!tmdata) {
5895 genlmsg_cancel(skb, hdr);
5896 break;
5897 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005898 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005899 nla_nest_end(skb, tmdata);
5900
5901 if (err == -ENOBUFS || err == -ENOENT) {
5902 genlmsg_cancel(skb, hdr);
5903 break;
5904 } else if (err) {
5905 genlmsg_cancel(skb, hdr);
5906 goto out_err;
5907 }
5908
5909 genlmsg_end(skb, hdr);
5910 }
5911
5912 err = skb->len;
5913 /* see above */
5914 cb->args[0] = phy_idx + 1;
5915 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005916 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005917 return err;
5918}
5919
Johannes Bergaff89a92009-07-01 21:26:51 +02005920static struct sk_buff *
5921__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005922 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005923{
5924 struct sk_buff *skb;
5925 void *hdr;
5926 struct nlattr *data;
5927
5928 skb = nlmsg_new(approxlen + 100, gfp);
5929 if (!skb)
5930 return NULL;
5931
Eric W. Biederman15e47302012-09-07 20:12:54 +00005932 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005933 if (!hdr) {
5934 kfree_skb(skb);
5935 return NULL;
5936 }
5937
David S. Miller9360ffd2012-03-29 04:41:26 -04005938 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5939 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005940 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5941
5942 ((void **)skb->cb)[0] = rdev;
5943 ((void **)skb->cb)[1] = hdr;
5944 ((void **)skb->cb)[2] = data;
5945
5946 return skb;
5947
5948 nla_put_failure:
5949 kfree_skb(skb);
5950 return NULL;
5951}
5952
5953struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5954 int approxlen)
5955{
5956 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5957
5958 if (WARN_ON(!rdev->testmode_info))
5959 return NULL;
5960
5961 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005962 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005963 rdev->testmode_info->snd_seq,
5964 GFP_KERNEL);
5965}
5966EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5967
5968int cfg80211_testmode_reply(struct sk_buff *skb)
5969{
5970 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5971 void *hdr = ((void **)skb->cb)[1];
5972 struct nlattr *data = ((void **)skb->cb)[2];
5973
5974 if (WARN_ON(!rdev->testmode_info)) {
5975 kfree_skb(skb);
5976 return -EINVAL;
5977 }
5978
5979 nla_nest_end(skb, data);
5980 genlmsg_end(skb, hdr);
5981 return genlmsg_reply(skb, rdev->testmode_info);
5982}
5983EXPORT_SYMBOL(cfg80211_testmode_reply);
5984
5985struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5986 int approxlen, gfp_t gfp)
5987{
5988 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5989
5990 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5991}
5992EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5993
5994void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5995{
5996 void *hdr = ((void **)skb->cb)[1];
5997 struct nlattr *data = ((void **)skb->cb)[2];
5998
5999 nla_nest_end(skb, data);
6000 genlmsg_end(skb, hdr);
6001 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6002}
6003EXPORT_SYMBOL(cfg80211_testmode_event);
6004#endif
6005
Samuel Ortizb23aa672009-07-01 21:26:54 +02006006static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6007{
Johannes Berg4c476992010-10-04 21:36:35 +02006008 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6009 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006010 struct cfg80211_connect_params connect;
6011 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006012 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006013 int err;
6014
6015 memset(&connect, 0, sizeof(connect));
6016
6017 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6018 return -EINVAL;
6019
6020 if (!info->attrs[NL80211_ATTR_SSID] ||
6021 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6022 return -EINVAL;
6023
6024 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6025 connect.auth_type =
6026 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006027 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6028 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006029 return -EINVAL;
6030 } else
6031 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6032
6033 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6034
Johannes Bergc0692b82010-08-27 14:26:53 +03006035 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006036 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006037 if (err)
6038 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006039
Johannes Berg074ac8d2010-09-16 14:58:22 +02006040 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006041 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6042 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006043
Johannes Berg79c97e92009-07-07 03:56:12 +02006044 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006045
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306046 connect.bg_scan_period = -1;
6047 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6048 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6049 connect.bg_scan_period =
6050 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6051 }
6052
Samuel Ortizb23aa672009-07-01 21:26:54 +02006053 if (info->attrs[NL80211_ATTR_MAC])
6054 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6055 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6056 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6057
6058 if (info->attrs[NL80211_ATTR_IE]) {
6059 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6060 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6061 }
6062
Jouni Malinencee00a92013-01-15 17:15:57 +02006063 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6064 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6065 if (connect.mfp != NL80211_MFP_REQUIRED &&
6066 connect.mfp != NL80211_MFP_NO)
6067 return -EINVAL;
6068 } else {
6069 connect.mfp = NL80211_MFP_NO;
6070 }
6071
Samuel Ortizb23aa672009-07-01 21:26:54 +02006072 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6073 connect.channel =
6074 ieee80211_get_channel(wiphy,
6075 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6076 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006077 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6078 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006079 }
6080
Johannes Bergfffd0932009-07-08 14:22:54 +02006081 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6082 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306083 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006084 if (IS_ERR(connkeys))
6085 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006086 }
6087
Ben Greear7e7c8922011-11-18 11:31:59 -08006088 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6089 connect.flags |= ASSOC_REQ_DISABLE_HT;
6090
6091 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6092 memcpy(&connect.ht_capa_mask,
6093 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6094 sizeof(connect.ht_capa_mask));
6095
6096 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006097 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6098 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006099 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006100 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006101 memcpy(&connect.ht_capa,
6102 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6103 sizeof(connect.ht_capa));
6104 }
6105
Johannes Bergfffd0932009-07-08 14:22:54 +02006106 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006107 if (err)
6108 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006109 return err;
6110}
6111
6112static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6113{
Johannes Berg4c476992010-10-04 21:36:35 +02006114 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6115 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006116 u16 reason;
6117
6118 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6119 reason = WLAN_REASON_DEAUTH_LEAVING;
6120 else
6121 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6122
6123 if (reason == 0)
6124 return -EINVAL;
6125
Johannes Berg074ac8d2010-09-16 14:58:22 +02006126 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006127 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6128 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006129
Johannes Berg4c476992010-10-04 21:36:35 +02006130 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006131}
6132
Johannes Berg463d0182009-07-14 00:33:35 +02006133static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6134{
Johannes Berg4c476992010-10-04 21:36:35 +02006135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006136 struct net *net;
6137 int err;
6138 u32 pid;
6139
6140 if (!info->attrs[NL80211_ATTR_PID])
6141 return -EINVAL;
6142
6143 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6144
Johannes Berg463d0182009-07-14 00:33:35 +02006145 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006146 if (IS_ERR(net))
6147 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006148
6149 err = 0;
6150
6151 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006152 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6153 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006154
Johannes Berg463d0182009-07-14 00:33:35 +02006155 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006156 return err;
6157}
6158
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006159static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6160{
Johannes Berg4c476992010-10-04 21:36:35 +02006161 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006162 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6163 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006164 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006165 struct cfg80211_pmksa pmksa;
6166
6167 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6168
6169 if (!info->attrs[NL80211_ATTR_MAC])
6170 return -EINVAL;
6171
6172 if (!info->attrs[NL80211_ATTR_PMKID])
6173 return -EINVAL;
6174
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006175 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6176 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6177
Johannes Berg074ac8d2010-09-16 14:58:22 +02006178 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006179 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6180 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006181
6182 switch (info->genlhdr->cmd) {
6183 case NL80211_CMD_SET_PMKSA:
6184 rdev_ops = rdev->ops->set_pmksa;
6185 break;
6186 case NL80211_CMD_DEL_PMKSA:
6187 rdev_ops = rdev->ops->del_pmksa;
6188 break;
6189 default:
6190 WARN_ON(1);
6191 break;
6192 }
6193
Johannes Berg4c476992010-10-04 21:36:35 +02006194 if (!rdev_ops)
6195 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006196
Johannes Berg4c476992010-10-04 21:36:35 +02006197 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006198}
6199
6200static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6201{
Johannes Berg4c476992010-10-04 21:36:35 +02006202 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6203 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006204
Johannes Berg074ac8d2010-09-16 14:58:22 +02006205 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006206 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6207 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006208
Johannes Berg4c476992010-10-04 21:36:35 +02006209 if (!rdev->ops->flush_pmksa)
6210 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006211
Hila Gonene35e4d22012-06-27 17:19:42 +03006212 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006213}
6214
Arik Nemtsov109086c2011-09-28 14:12:50 +03006215static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6216{
6217 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6218 struct net_device *dev = info->user_ptr[1];
6219 u8 action_code, dialog_token;
6220 u16 status_code;
6221 u8 *peer;
6222
6223 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6224 !rdev->ops->tdls_mgmt)
6225 return -EOPNOTSUPP;
6226
6227 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6228 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6229 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6230 !info->attrs[NL80211_ATTR_IE] ||
6231 !info->attrs[NL80211_ATTR_MAC])
6232 return -EINVAL;
6233
6234 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6235 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6236 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6237 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6238
Hila Gonene35e4d22012-06-27 17:19:42 +03006239 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6240 dialog_token, status_code,
6241 nla_data(info->attrs[NL80211_ATTR_IE]),
6242 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006243}
6244
6245static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6246{
6247 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6248 struct net_device *dev = info->user_ptr[1];
6249 enum nl80211_tdls_operation operation;
6250 u8 *peer;
6251
6252 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6253 !rdev->ops->tdls_oper)
6254 return -EOPNOTSUPP;
6255
6256 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6257 !info->attrs[NL80211_ATTR_MAC])
6258 return -EINVAL;
6259
6260 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6261 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6262
Hila Gonene35e4d22012-06-27 17:19:42 +03006263 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006264}
6265
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006266static int nl80211_remain_on_channel(struct sk_buff *skb,
6267 struct genl_info *info)
6268{
Johannes Berg4c476992010-10-04 21:36:35 +02006269 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006270 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006271 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006272 struct sk_buff *msg;
6273 void *hdr;
6274 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006275 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006276 int err;
6277
6278 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6279 !info->attrs[NL80211_ATTR_DURATION])
6280 return -EINVAL;
6281
6282 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6283
Johannes Berg7c4ef712011-11-18 15:33:48 +01006284 if (!rdev->ops->remain_on_channel ||
6285 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006286 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006287
Johannes Bergebf348f2012-06-01 12:50:54 +02006288 /*
6289 * We should be on that channel for at least a minimum amount of
6290 * time (10ms) but no longer than the driver supports.
6291 */
6292 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6293 duration > rdev->wiphy.max_remain_on_channel_duration)
6294 return -EINVAL;
6295
Johannes Berg683b6d32012-11-08 21:25:48 +01006296 err = nl80211_parse_chandef(rdev, info, &chandef);
6297 if (err)
6298 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006299
6300 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006301 if (!msg)
6302 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006303
Eric W. Biederman15e47302012-09-07 20:12:54 +00006304 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006305 NL80211_CMD_REMAIN_ON_CHANNEL);
6306
6307 if (IS_ERR(hdr)) {
6308 err = PTR_ERR(hdr);
6309 goto free_msg;
6310 }
6311
Johannes Berg683b6d32012-11-08 21:25:48 +01006312 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6313 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006314
6315 if (err)
6316 goto free_msg;
6317
David S. Miller9360ffd2012-03-29 04:41:26 -04006318 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6319 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006320
6321 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006322
6323 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006324
6325 nla_put_failure:
6326 err = -ENOBUFS;
6327 free_msg:
6328 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006329 return err;
6330}
6331
6332static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6333 struct genl_info *info)
6334{
Johannes Berg4c476992010-10-04 21:36:35 +02006335 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006336 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006337 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006338
6339 if (!info->attrs[NL80211_ATTR_COOKIE])
6340 return -EINVAL;
6341
Johannes Berg4c476992010-10-04 21:36:35 +02006342 if (!rdev->ops->cancel_remain_on_channel)
6343 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006344
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006345 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6346
Hila Gonene35e4d22012-06-27 17:19:42 +03006347 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006348}
6349
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006350static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6351 u8 *rates, u8 rates_len)
6352{
6353 u8 i;
6354 u32 mask = 0;
6355
6356 for (i = 0; i < rates_len; i++) {
6357 int rate = (rates[i] & 0x7f) * 5;
6358 int ridx;
6359 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6360 struct ieee80211_rate *srate =
6361 &sband->bitrates[ridx];
6362 if (rate == srate->bitrate) {
6363 mask |= 1 << ridx;
6364 break;
6365 }
6366 }
6367 if (ridx == sband->n_bitrates)
6368 return 0; /* rate not found */
6369 }
6370
6371 return mask;
6372}
6373
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006374static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6375 u8 *rates, u8 rates_len,
6376 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6377{
6378 u8 i;
6379
6380 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6381
6382 for (i = 0; i < rates_len; i++) {
6383 int ridx, rbit;
6384
6385 ridx = rates[i] / 8;
6386 rbit = BIT(rates[i] % 8);
6387
6388 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006389 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006390 return false;
6391
6392 /* check availability */
6393 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6394 mcs[ridx] |= rbit;
6395 else
6396 return false;
6397 }
6398
6399 return true;
6400}
6401
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006402static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006403 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6404 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006405 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6406 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006407};
6408
6409static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6410 struct genl_info *info)
6411{
6412 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006413 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006414 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006415 int rem, i;
6416 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006417 struct nlattr *tx_rates;
6418 struct ieee80211_supported_band *sband;
6419
6420 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6421 return -EINVAL;
6422
Johannes Berg4c476992010-10-04 21:36:35 +02006423 if (!rdev->ops->set_bitrate_mask)
6424 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006425
6426 memset(&mask, 0, sizeof(mask));
6427 /* Default to all rates enabled */
6428 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6429 sband = rdev->wiphy.bands[i];
6430 mask.control[i].legacy =
6431 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006432 if (sband)
6433 memcpy(mask.control[i].mcs,
6434 sband->ht_cap.mcs.rx_mask,
6435 sizeof(mask.control[i].mcs));
6436 else
6437 memset(mask.control[i].mcs, 0,
6438 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006439 }
6440
6441 /*
6442 * The nested attribute uses enum nl80211_band as the index. This maps
6443 * directly to the enum ieee80211_band values used in cfg80211.
6444 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006445 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006446 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6447 {
6448 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006449 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6450 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006451 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006452 if (sband == NULL)
6453 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006454 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6455 nla_len(tx_rates), nl80211_txattr_policy);
6456 if (tb[NL80211_TXRATE_LEGACY]) {
6457 mask.control[band].legacy = rateset_to_mask(
6458 sband,
6459 nla_data(tb[NL80211_TXRATE_LEGACY]),
6460 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306461 if ((mask.control[band].legacy == 0) &&
6462 nla_len(tb[NL80211_TXRATE_LEGACY]))
6463 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006464 }
6465 if (tb[NL80211_TXRATE_MCS]) {
6466 if (!ht_rateset_to_mask(
6467 sband,
6468 nla_data(tb[NL80211_TXRATE_MCS]),
6469 nla_len(tb[NL80211_TXRATE_MCS]),
6470 mask.control[band].mcs))
6471 return -EINVAL;
6472 }
6473
6474 if (mask.control[band].legacy == 0) {
6475 /* don't allow empty legacy rates if HT
6476 * is not even supported. */
6477 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6478 return -EINVAL;
6479
6480 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6481 if (mask.control[band].mcs[i])
6482 break;
6483
6484 /* legacy and mcs rates may not be both empty */
6485 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006486 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006487 }
6488 }
6489
Hila Gonene35e4d22012-06-27 17:19:42 +03006490 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006491}
6492
Johannes Berg2e161f72010-08-12 15:38:38 +02006493static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006494{
Johannes Berg4c476992010-10-04 21:36:35 +02006495 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006496 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006497 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006498
6499 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6500 return -EINVAL;
6501
Johannes Berg2e161f72010-08-12 15:38:38 +02006502 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6503 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006504
Johannes Berg71bbc992012-06-15 15:30:18 +02006505 switch (wdev->iftype) {
6506 case NL80211_IFTYPE_STATION:
6507 case NL80211_IFTYPE_ADHOC:
6508 case NL80211_IFTYPE_P2P_CLIENT:
6509 case NL80211_IFTYPE_AP:
6510 case NL80211_IFTYPE_AP_VLAN:
6511 case NL80211_IFTYPE_MESH_POINT:
6512 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006513 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006514 break;
6515 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006516 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006517 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006518
6519 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006520 if (!rdev->ops->mgmt_tx)
6521 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006522
Eric W. Biederman15e47302012-09-07 20:12:54 +00006523 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006524 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6525 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006526}
6527
Johannes Berg2e161f72010-08-12 15:38:38 +02006528static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006529{
Johannes Berg4c476992010-10-04 21:36:35 +02006530 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006531 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006532 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006533 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006534 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006535 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006536 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006537 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006538 bool offchan, no_cck, dont_wait_for_ack;
6539
6540 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006541
Johannes Berg683b6d32012-11-08 21:25:48 +01006542 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006543 return -EINVAL;
6544
Johannes Berg4c476992010-10-04 21:36:35 +02006545 if (!rdev->ops->mgmt_tx)
6546 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006547
Johannes Berg71bbc992012-06-15 15:30:18 +02006548 switch (wdev->iftype) {
6549 case NL80211_IFTYPE_STATION:
6550 case NL80211_IFTYPE_ADHOC:
6551 case NL80211_IFTYPE_P2P_CLIENT:
6552 case NL80211_IFTYPE_AP:
6553 case NL80211_IFTYPE_AP_VLAN:
6554 case NL80211_IFTYPE_MESH_POINT:
6555 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006556 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006557 break;
6558 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006559 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006560 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006561
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006562 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006563 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006564 return -EINVAL;
6565 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006566
6567 /*
6568 * We should wait on the channel for at least a minimum amount
6569 * of time (10ms) but no longer than the driver supports.
6570 */
6571 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6572 wait > rdev->wiphy.max_remain_on_channel_duration)
6573 return -EINVAL;
6574
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006575 }
6576
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006577 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6578
Johannes Berg7c4ef712011-11-18 15:33:48 +01006579 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6580 return -EINVAL;
6581
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306582 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6583
Johannes Berg683b6d32012-11-08 21:25:48 +01006584 err = nl80211_parse_chandef(rdev, info, &chandef);
6585 if (err)
6586 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006587
Johannes Berge247bd902011-11-04 11:18:21 +01006588 if (!dont_wait_for_ack) {
6589 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6590 if (!msg)
6591 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006592
Eric W. Biederman15e47302012-09-07 20:12:54 +00006593 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006594 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006595
Johannes Berge247bd902011-11-04 11:18:21 +01006596 if (IS_ERR(hdr)) {
6597 err = PTR_ERR(hdr);
6598 goto free_msg;
6599 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006600 }
Johannes Berge247bd902011-11-04 11:18:21 +01006601
Johannes Berg683b6d32012-11-08 21:25:48 +01006602 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006603 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6604 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006605 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006606 if (err)
6607 goto free_msg;
6608
Johannes Berge247bd902011-11-04 11:18:21 +01006609 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006610 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6611 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006612
Johannes Berge247bd902011-11-04 11:18:21 +01006613 genlmsg_end(msg, hdr);
6614 return genlmsg_reply(msg, info);
6615 }
6616
6617 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006618
6619 nla_put_failure:
6620 err = -ENOBUFS;
6621 free_msg:
6622 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006623 return err;
6624}
6625
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006626static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6627{
6628 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006629 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006630 u64 cookie;
6631
6632 if (!info->attrs[NL80211_ATTR_COOKIE])
6633 return -EINVAL;
6634
6635 if (!rdev->ops->mgmt_tx_cancel_wait)
6636 return -EOPNOTSUPP;
6637
Johannes Berg71bbc992012-06-15 15:30:18 +02006638 switch (wdev->iftype) {
6639 case NL80211_IFTYPE_STATION:
6640 case NL80211_IFTYPE_ADHOC:
6641 case NL80211_IFTYPE_P2P_CLIENT:
6642 case NL80211_IFTYPE_AP:
6643 case NL80211_IFTYPE_AP_VLAN:
6644 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006645 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006646 break;
6647 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006648 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006649 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006650
6651 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6652
Hila Gonene35e4d22012-06-27 17:19:42 +03006653 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006654}
6655
Kalle Valoffb9eb32010-02-17 17:58:10 +02006656static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6657{
Johannes Berg4c476992010-10-04 21:36:35 +02006658 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006659 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006660 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006661 u8 ps_state;
6662 bool state;
6663 int err;
6664
Johannes Berg4c476992010-10-04 21:36:35 +02006665 if (!info->attrs[NL80211_ATTR_PS_STATE])
6666 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006667
6668 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6669
Johannes Berg4c476992010-10-04 21:36:35 +02006670 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6671 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006672
6673 wdev = dev->ieee80211_ptr;
6674
Johannes Berg4c476992010-10-04 21:36:35 +02006675 if (!rdev->ops->set_power_mgmt)
6676 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006677
6678 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6679
6680 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006681 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006682
Hila Gonene35e4d22012-06-27 17:19:42 +03006683 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006684 if (!err)
6685 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006686 return err;
6687}
6688
6689static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6690{
Johannes Berg4c476992010-10-04 21:36:35 +02006691 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006692 enum nl80211_ps_state ps_state;
6693 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006694 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006695 struct sk_buff *msg;
6696 void *hdr;
6697 int err;
6698
Kalle Valoffb9eb32010-02-17 17:58:10 +02006699 wdev = dev->ieee80211_ptr;
6700
Johannes Berg4c476992010-10-04 21:36:35 +02006701 if (!rdev->ops->set_power_mgmt)
6702 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006703
6704 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006705 if (!msg)
6706 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006707
Eric W. Biederman15e47302012-09-07 20:12:54 +00006708 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006709 NL80211_CMD_GET_POWER_SAVE);
6710 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006711 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006712 goto free_msg;
6713 }
6714
6715 if (wdev->ps)
6716 ps_state = NL80211_PS_ENABLED;
6717 else
6718 ps_state = NL80211_PS_DISABLED;
6719
David S. Miller9360ffd2012-03-29 04:41:26 -04006720 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6721 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006722
6723 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006724 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006725
Johannes Berg4c476992010-10-04 21:36:35 +02006726 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006727 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006728 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006729 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006730 return err;
6731}
6732
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006733static struct nla_policy
6734nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6735 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6736 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6737 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006738 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6739 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6740 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006741};
6742
Thomas Pedersen84f10702012-07-12 16:17:33 -07006743static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006744 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006745{
6746 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6747 struct wireless_dev *wdev;
6748 struct net_device *dev = info->user_ptr[1];
6749
Johannes Bergd9d8b012012-11-26 12:51:52 +01006750 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006751 return -EINVAL;
6752
6753 wdev = dev->ieee80211_ptr;
6754
6755 if (!rdev->ops->set_cqm_txe_config)
6756 return -EOPNOTSUPP;
6757
6758 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6759 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6760 return -EOPNOTSUPP;
6761
Hila Gonene35e4d22012-06-27 17:19:42 +03006762 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006763}
6764
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006765static int nl80211_set_cqm_rssi(struct genl_info *info,
6766 s32 threshold, u32 hysteresis)
6767{
Johannes Berg4c476992010-10-04 21:36:35 +02006768 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006769 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006770 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006771
6772 if (threshold > 0)
6773 return -EINVAL;
6774
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006775 wdev = dev->ieee80211_ptr;
6776
Johannes Berg4c476992010-10-04 21:36:35 +02006777 if (!rdev->ops->set_cqm_rssi_config)
6778 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006779
Johannes Berg074ac8d2010-09-16 14:58:22 +02006780 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006781 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6782 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006783
Hila Gonene35e4d22012-06-27 17:19:42 +03006784 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006785}
6786
6787static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6788{
6789 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6790 struct nlattr *cqm;
6791 int err;
6792
6793 cqm = info->attrs[NL80211_ATTR_CQM];
6794 if (!cqm) {
6795 err = -EINVAL;
6796 goto out;
6797 }
6798
6799 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6800 nl80211_attr_cqm_policy);
6801 if (err)
6802 goto out;
6803
6804 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6805 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6806 s32 threshold;
6807 u32 hysteresis;
6808 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6809 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6810 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006811 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6812 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6813 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6814 u32 rate, pkts, intvl;
6815 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6816 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6817 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6818 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006819 } else
6820 err = -EINVAL;
6821
6822out:
6823 return err;
6824}
6825
Johannes Berg29cbe682010-12-03 09:20:44 +01006826static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6827{
6828 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6829 struct net_device *dev = info->user_ptr[1];
6830 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006831 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006832 int err;
6833
6834 /* start with default */
6835 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006836 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006837
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006838 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006839 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006840 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006841 if (err)
6842 return err;
6843 }
6844
6845 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6846 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6847 return -EINVAL;
6848
Javier Cardonac80d5452010-12-16 17:37:49 -08006849 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6850 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6851
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006852 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6853 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6854 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6855 return -EINVAL;
6856
Marco Porsch9bdbf042013-01-07 16:04:51 +01006857 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6858 setup.beacon_interval =
6859 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6860 if (setup.beacon_interval < 10 ||
6861 setup.beacon_interval > 10000)
6862 return -EINVAL;
6863 }
6864
6865 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
6866 setup.dtim_period =
6867 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
6868 if (setup.dtim_period < 1 || setup.dtim_period > 100)
6869 return -EINVAL;
6870 }
6871
Javier Cardonac80d5452010-12-16 17:37:49 -08006872 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6873 /* parse additional setup parameters if given */
6874 err = nl80211_parse_mesh_setup(info, &setup);
6875 if (err)
6876 return err;
6877 }
6878
Johannes Bergcc1d2802012-05-16 23:50:20 +02006879 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01006880 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
6881 if (err)
6882 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006883 } else {
6884 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01006885 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006886 }
6887
Javier Cardonac80d5452010-12-16 17:37:49 -08006888 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006889}
6890
6891static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6892{
6893 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6894 struct net_device *dev = info->user_ptr[1];
6895
6896 return cfg80211_leave_mesh(rdev, dev);
6897}
6898
Johannes Bergdfb89c52012-06-27 09:23:48 +02006899#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08006900static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
6901 struct cfg80211_registered_device *rdev)
6902{
6903 struct nlattr *nl_pats, *nl_pat;
6904 int i, pat_len;
6905
6906 if (!rdev->wowlan->n_patterns)
6907 return 0;
6908
6909 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
6910 if (!nl_pats)
6911 return -ENOBUFS;
6912
6913 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6914 nl_pat = nla_nest_start(msg, i + 1);
6915 if (!nl_pat)
6916 return -ENOBUFS;
6917 pat_len = rdev->wowlan->patterns[i].pattern_len;
6918 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6919 DIV_ROUND_UP(pat_len, 8),
6920 rdev->wowlan->patterns[i].mask) ||
6921 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6922 pat_len, rdev->wowlan->patterns[i].pattern) ||
6923 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
6924 rdev->wowlan->patterns[i].pkt_offset))
6925 return -ENOBUFS;
6926 nla_nest_end(msg, nl_pat);
6927 }
6928 nla_nest_end(msg, nl_pats);
6929
6930 return 0;
6931}
6932
Johannes Bergff1b6e62011-05-04 15:37:28 +02006933static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6934{
6935 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6936 struct sk_buff *msg;
6937 void *hdr;
6938
6939 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6940 return -EOPNOTSUPP;
6941
6942 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6943 if (!msg)
6944 return -ENOMEM;
6945
Eric W. Biederman15e47302012-09-07 20:12:54 +00006946 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006947 NL80211_CMD_GET_WOWLAN);
6948 if (!hdr)
6949 goto nla_put_failure;
6950
6951 if (rdev->wowlan) {
6952 struct nlattr *nl_wowlan;
6953
6954 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6955 if (!nl_wowlan)
6956 goto nla_put_failure;
6957
David S. Miller9360ffd2012-03-29 04:41:26 -04006958 if ((rdev->wowlan->any &&
6959 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6960 (rdev->wowlan->disconnect &&
6961 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6962 (rdev->wowlan->magic_pkt &&
6963 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6964 (rdev->wowlan->gtk_rekey_failure &&
6965 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6966 (rdev->wowlan->eap_identity_req &&
6967 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6968 (rdev->wowlan->four_way_handshake &&
6969 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6970 (rdev->wowlan->rfkill_release &&
6971 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6972 goto nla_put_failure;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08006973 if (nl80211_send_wowlan_patterns(msg, rdev))
6974 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006975 nla_nest_end(msg, nl_wowlan);
6976 }
6977
6978 genlmsg_end(msg, hdr);
6979 return genlmsg_reply(msg, info);
6980
6981nla_put_failure:
6982 nlmsg_free(msg);
6983 return -ENOBUFS;
6984}
6985
6986static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6987{
6988 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6989 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006990 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006991 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006992 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6993 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006994 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006995
6996 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6997 return -EOPNOTSUPP;
6998
Johannes Bergae33bd82012-07-12 16:25:02 +02006999 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7000 cfg80211_rdev_free_wowlan(rdev);
7001 rdev->wowlan = NULL;
7002 goto set_wakeup;
7003 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007004
7005 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7006 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7007 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7008 nl80211_wowlan_policy);
7009 if (err)
7010 return err;
7011
7012 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7013 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7014 return -EINVAL;
7015 new_triggers.any = true;
7016 }
7017
7018 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7019 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7020 return -EINVAL;
7021 new_triggers.disconnect = true;
7022 }
7023
7024 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7025 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7026 return -EINVAL;
7027 new_triggers.magic_pkt = true;
7028 }
7029
Johannes Berg77dbbb12011-07-13 10:48:55 +02007030 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7031 return -EINVAL;
7032
7033 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7034 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7035 return -EINVAL;
7036 new_triggers.gtk_rekey_failure = true;
7037 }
7038
7039 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7040 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7041 return -EINVAL;
7042 new_triggers.eap_identity_req = true;
7043 }
7044
7045 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7046 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7047 return -EINVAL;
7048 new_triggers.four_way_handshake = true;
7049 }
7050
7051 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7052 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7053 return -EINVAL;
7054 new_triggers.rfkill_release = true;
7055 }
7056
Johannes Bergff1b6e62011-05-04 15:37:28 +02007057 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7058 struct nlattr *pat;
7059 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007060 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007061 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7062
7063 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7064 rem)
7065 n_patterns++;
7066 if (n_patterns > wowlan->n_patterns)
7067 return -EINVAL;
7068
7069 new_triggers.patterns = kcalloc(n_patterns,
7070 sizeof(new_triggers.patterns[0]),
7071 GFP_KERNEL);
7072 if (!new_triggers.patterns)
7073 return -ENOMEM;
7074
7075 new_triggers.n_patterns = n_patterns;
7076 i = 0;
7077
7078 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7079 rem) {
7080 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7081 nla_data(pat), nla_len(pat), NULL);
7082 err = -EINVAL;
7083 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7084 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7085 goto error;
7086 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7087 mask_len = DIV_ROUND_UP(pat_len, 8);
7088 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7089 mask_len)
7090 goto error;
7091 if (pat_len > wowlan->pattern_max_len ||
7092 pat_len < wowlan->pattern_min_len)
7093 goto error;
7094
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007095 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7096 pkt_offset = 0;
7097 else
7098 pkt_offset = nla_get_u32(
7099 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7100 if (pkt_offset > wowlan->max_pkt_offset)
7101 goto error;
7102 new_triggers.patterns[i].pkt_offset = pkt_offset;
7103
Johannes Bergff1b6e62011-05-04 15:37:28 +02007104 new_triggers.patterns[i].mask =
7105 kmalloc(mask_len + pat_len, GFP_KERNEL);
7106 if (!new_triggers.patterns[i].mask) {
7107 err = -ENOMEM;
7108 goto error;
7109 }
7110 new_triggers.patterns[i].pattern =
7111 new_triggers.patterns[i].mask + mask_len;
7112 memcpy(new_triggers.patterns[i].mask,
7113 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7114 mask_len);
7115 new_triggers.patterns[i].pattern_len = pat_len;
7116 memcpy(new_triggers.patterns[i].pattern,
7117 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7118 pat_len);
7119 i++;
7120 }
7121 }
7122
Johannes Bergae33bd82012-07-12 16:25:02 +02007123 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7124 if (!ntrig) {
7125 err = -ENOMEM;
7126 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007127 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007128 cfg80211_rdev_free_wowlan(rdev);
7129 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007130
Johannes Bergae33bd82012-07-12 16:25:02 +02007131 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007132 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007133 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007134
Johannes Bergff1b6e62011-05-04 15:37:28 +02007135 return 0;
7136 error:
7137 for (i = 0; i < new_triggers.n_patterns; i++)
7138 kfree(new_triggers.patterns[i].mask);
7139 kfree(new_triggers.patterns);
7140 return err;
7141}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007142#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007143
Johannes Berge5497d72011-07-05 16:35:40 +02007144static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7145{
7146 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7147 struct net_device *dev = info->user_ptr[1];
7148 struct wireless_dev *wdev = dev->ieee80211_ptr;
7149 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7150 struct cfg80211_gtk_rekey_data rekey_data;
7151 int err;
7152
7153 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7154 return -EINVAL;
7155
7156 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7157 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7158 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7159 nl80211_rekey_policy);
7160 if (err)
7161 return err;
7162
7163 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7164 return -ERANGE;
7165 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7166 return -ERANGE;
7167 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7168 return -ERANGE;
7169
7170 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7171 NL80211_KEK_LEN);
7172 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7173 NL80211_KCK_LEN);
7174 memcpy(rekey_data.replay_ctr,
7175 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7176 NL80211_REPLAY_CTR_LEN);
7177
7178 wdev_lock(wdev);
7179 if (!wdev->current_bss) {
7180 err = -ENOTCONN;
7181 goto out;
7182 }
7183
7184 if (!rdev->ops->set_rekey_data) {
7185 err = -EOPNOTSUPP;
7186 goto out;
7187 }
7188
Hila Gonene35e4d22012-06-27 17:19:42 +03007189 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007190 out:
7191 wdev_unlock(wdev);
7192 return err;
7193}
7194
Johannes Berg28946da2011-11-04 11:18:12 +01007195static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7196 struct genl_info *info)
7197{
7198 struct net_device *dev = info->user_ptr[1];
7199 struct wireless_dev *wdev = dev->ieee80211_ptr;
7200
7201 if (wdev->iftype != NL80211_IFTYPE_AP &&
7202 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7203 return -EINVAL;
7204
Eric W. Biederman15e47302012-09-07 20:12:54 +00007205 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007206 return -EBUSY;
7207
Eric W. Biederman15e47302012-09-07 20:12:54 +00007208 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007209 return 0;
7210}
7211
Johannes Berg7f6cf312011-11-04 11:18:15 +01007212static int nl80211_probe_client(struct sk_buff *skb,
7213 struct genl_info *info)
7214{
7215 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7216 struct net_device *dev = info->user_ptr[1];
7217 struct wireless_dev *wdev = dev->ieee80211_ptr;
7218 struct sk_buff *msg;
7219 void *hdr;
7220 const u8 *addr;
7221 u64 cookie;
7222 int err;
7223
7224 if (wdev->iftype != NL80211_IFTYPE_AP &&
7225 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7226 return -EOPNOTSUPP;
7227
7228 if (!info->attrs[NL80211_ATTR_MAC])
7229 return -EINVAL;
7230
7231 if (!rdev->ops->probe_client)
7232 return -EOPNOTSUPP;
7233
7234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7235 if (!msg)
7236 return -ENOMEM;
7237
Eric W. Biederman15e47302012-09-07 20:12:54 +00007238 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007239 NL80211_CMD_PROBE_CLIENT);
7240
7241 if (IS_ERR(hdr)) {
7242 err = PTR_ERR(hdr);
7243 goto free_msg;
7244 }
7245
7246 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7247
Hila Gonene35e4d22012-06-27 17:19:42 +03007248 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007249 if (err)
7250 goto free_msg;
7251
David S. Miller9360ffd2012-03-29 04:41:26 -04007252 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7253 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007254
7255 genlmsg_end(msg, hdr);
7256
7257 return genlmsg_reply(msg, info);
7258
7259 nla_put_failure:
7260 err = -ENOBUFS;
7261 free_msg:
7262 nlmsg_free(msg);
7263 return err;
7264}
7265
Johannes Berg5e760232011-11-04 11:18:17 +01007266static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7267{
7268 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007269 struct cfg80211_beacon_registration *reg, *nreg;
7270 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007271
7272 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7273 return -EOPNOTSUPP;
7274
Ben Greear37c73b52012-10-26 14:49:25 -07007275 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7276 if (!nreg)
7277 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007278
Ben Greear37c73b52012-10-26 14:49:25 -07007279 /* First, check if already registered. */
7280 spin_lock_bh(&rdev->beacon_registrations_lock);
7281 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7282 if (reg->nlportid == info->snd_portid) {
7283 rv = -EALREADY;
7284 goto out_err;
7285 }
7286 }
7287 /* Add it to the list */
7288 nreg->nlportid = info->snd_portid;
7289 list_add(&nreg->list, &rdev->beacon_registrations);
7290
7291 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007292
7293 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007294out_err:
7295 spin_unlock_bh(&rdev->beacon_registrations_lock);
7296 kfree(nreg);
7297 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007298}
7299
Johannes Berg98104fde2012-06-16 00:19:54 +02007300static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7301{
7302 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7303 struct wireless_dev *wdev = info->user_ptr[1];
7304 int err;
7305
7306 if (!rdev->ops->start_p2p_device)
7307 return -EOPNOTSUPP;
7308
7309 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7310 return -EOPNOTSUPP;
7311
7312 if (wdev->p2p_started)
7313 return 0;
7314
7315 mutex_lock(&rdev->devlist_mtx);
7316 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7317 mutex_unlock(&rdev->devlist_mtx);
7318 if (err)
7319 return err;
7320
Johannes Bergeeb126e2012-10-23 15:16:50 +02007321 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007322 if (err)
7323 return err;
7324
7325 wdev->p2p_started = true;
7326 mutex_lock(&rdev->devlist_mtx);
7327 rdev->opencount++;
7328 mutex_unlock(&rdev->devlist_mtx);
7329
7330 return 0;
7331}
7332
7333static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7334{
7335 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7336 struct wireless_dev *wdev = info->user_ptr[1];
7337
7338 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7339 return -EOPNOTSUPP;
7340
7341 if (!rdev->ops->stop_p2p_device)
7342 return -EOPNOTSUPP;
7343
7344 if (!wdev->p2p_started)
7345 return 0;
7346
Johannes Bergeeb126e2012-10-23 15:16:50 +02007347 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007348 wdev->p2p_started = false;
7349
7350 mutex_lock(&rdev->devlist_mtx);
7351 rdev->opencount--;
7352 mutex_unlock(&rdev->devlist_mtx);
7353
7354 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7355 rdev->scan_req->aborted = true;
7356 ___cfg80211_scan_done(rdev, true);
7357 }
7358
7359 return 0;
7360}
7361
Johannes Berg4c476992010-10-04 21:36:35 +02007362#define NL80211_FLAG_NEED_WIPHY 0x01
7363#define NL80211_FLAG_NEED_NETDEV 0x02
7364#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007365#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7366#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7367 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007368#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007369/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007370#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7371 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007372
7373static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7374 struct genl_info *info)
7375{
7376 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007377 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007378 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007379 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7380
7381 if (rtnl)
7382 rtnl_lock();
7383
7384 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007385 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007386 if (IS_ERR(rdev)) {
7387 if (rtnl)
7388 rtnl_unlock();
7389 return PTR_ERR(rdev);
7390 }
7391 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007392 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7393 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007394 mutex_lock(&cfg80211_mutex);
7395 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7396 info->attrs);
7397 if (IS_ERR(wdev)) {
7398 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007399 if (rtnl)
7400 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007401 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007402 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007403
Johannes Berg89a54e42012-06-15 14:33:17 +02007404 dev = wdev->netdev;
7405 rdev = wiphy_to_dev(wdev->wiphy);
7406
Johannes Berg1bf614e2012-06-15 15:23:36 +02007407 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7408 if (!dev) {
7409 mutex_unlock(&cfg80211_mutex);
7410 if (rtnl)
7411 rtnl_unlock();
7412 return -EINVAL;
7413 }
7414
7415 info->user_ptr[1] = dev;
7416 } else {
7417 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007418 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007419
Johannes Berg1bf614e2012-06-15 15:23:36 +02007420 if (dev) {
7421 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7422 !netif_running(dev)) {
7423 mutex_unlock(&cfg80211_mutex);
7424 if (rtnl)
7425 rtnl_unlock();
7426 return -ENETDOWN;
7427 }
7428
7429 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007430 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7431 if (!wdev->p2p_started) {
7432 mutex_unlock(&cfg80211_mutex);
7433 if (rtnl)
7434 rtnl_unlock();
7435 return -ENETDOWN;
7436 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007437 }
7438
Johannes Berg89a54e42012-06-15 14:33:17 +02007439 cfg80211_lock_rdev(rdev);
7440
7441 mutex_unlock(&cfg80211_mutex);
7442
Johannes Berg4c476992010-10-04 21:36:35 +02007443 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007444 }
7445
7446 return 0;
7447}
7448
7449static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7450 struct genl_info *info)
7451{
7452 if (info->user_ptr[0])
7453 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007454 if (info->user_ptr[1]) {
7455 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7456 struct wireless_dev *wdev = info->user_ptr[1];
7457
7458 if (wdev->netdev)
7459 dev_put(wdev->netdev);
7460 } else {
7461 dev_put(info->user_ptr[1]);
7462 }
7463 }
Johannes Berg4c476992010-10-04 21:36:35 +02007464 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7465 rtnl_unlock();
7466}
7467
Johannes Berg55682962007-09-20 13:09:35 -04007468static struct genl_ops nl80211_ops[] = {
7469 {
7470 .cmd = NL80211_CMD_GET_WIPHY,
7471 .doit = nl80211_get_wiphy,
7472 .dumpit = nl80211_dump_wiphy,
7473 .policy = nl80211_policy,
7474 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007475 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007476 },
7477 {
7478 .cmd = NL80211_CMD_SET_WIPHY,
7479 .doit = nl80211_set_wiphy,
7480 .policy = nl80211_policy,
7481 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007482 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007483 },
7484 {
7485 .cmd = NL80211_CMD_GET_INTERFACE,
7486 .doit = nl80211_get_interface,
7487 .dumpit = nl80211_dump_interface,
7488 .policy = nl80211_policy,
7489 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007490 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007491 },
7492 {
7493 .cmd = NL80211_CMD_SET_INTERFACE,
7494 .doit = nl80211_set_interface,
7495 .policy = nl80211_policy,
7496 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007497 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7498 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007499 },
7500 {
7501 .cmd = NL80211_CMD_NEW_INTERFACE,
7502 .doit = nl80211_new_interface,
7503 .policy = nl80211_policy,
7504 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007505 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7506 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007507 },
7508 {
7509 .cmd = NL80211_CMD_DEL_INTERFACE,
7510 .doit = nl80211_del_interface,
7511 .policy = nl80211_policy,
7512 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007513 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007514 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007515 },
Johannes Berg41ade002007-12-19 02:03:29 +01007516 {
7517 .cmd = NL80211_CMD_GET_KEY,
7518 .doit = nl80211_get_key,
7519 .policy = nl80211_policy,
7520 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007521 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007522 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007523 },
7524 {
7525 .cmd = NL80211_CMD_SET_KEY,
7526 .doit = nl80211_set_key,
7527 .policy = nl80211_policy,
7528 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007529 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007530 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007531 },
7532 {
7533 .cmd = NL80211_CMD_NEW_KEY,
7534 .doit = nl80211_new_key,
7535 .policy = nl80211_policy,
7536 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007537 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007538 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007539 },
7540 {
7541 .cmd = NL80211_CMD_DEL_KEY,
7542 .doit = nl80211_del_key,
7543 .policy = nl80211_policy,
7544 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007545 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007546 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007547 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007548 {
7549 .cmd = NL80211_CMD_SET_BEACON,
7550 .policy = nl80211_policy,
7551 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007552 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007553 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007554 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007555 },
7556 {
Johannes Berg88600202012-02-13 15:17:18 +01007557 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007558 .policy = nl80211_policy,
7559 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007560 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007561 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007562 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007563 },
7564 {
Johannes Berg88600202012-02-13 15:17:18 +01007565 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007566 .policy = nl80211_policy,
7567 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007568 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007569 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007570 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007571 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007572 {
7573 .cmd = NL80211_CMD_GET_STATION,
7574 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007575 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007576 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007577 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7578 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007579 },
7580 {
7581 .cmd = NL80211_CMD_SET_STATION,
7582 .doit = nl80211_set_station,
7583 .policy = nl80211_policy,
7584 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007585 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007586 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007587 },
7588 {
7589 .cmd = NL80211_CMD_NEW_STATION,
7590 .doit = nl80211_new_station,
7591 .policy = nl80211_policy,
7592 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007593 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007594 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007595 },
7596 {
7597 .cmd = NL80211_CMD_DEL_STATION,
7598 .doit = nl80211_del_station,
7599 .policy = nl80211_policy,
7600 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007601 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007602 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007603 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007604 {
7605 .cmd = NL80211_CMD_GET_MPATH,
7606 .doit = nl80211_get_mpath,
7607 .dumpit = nl80211_dump_mpath,
7608 .policy = nl80211_policy,
7609 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007610 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007611 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007612 },
7613 {
7614 .cmd = NL80211_CMD_SET_MPATH,
7615 .doit = nl80211_set_mpath,
7616 .policy = nl80211_policy,
7617 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007618 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007619 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007620 },
7621 {
7622 .cmd = NL80211_CMD_NEW_MPATH,
7623 .doit = nl80211_new_mpath,
7624 .policy = nl80211_policy,
7625 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007626 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007627 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007628 },
7629 {
7630 .cmd = NL80211_CMD_DEL_MPATH,
7631 .doit = nl80211_del_mpath,
7632 .policy = nl80211_policy,
7633 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007634 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007635 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007636 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007637 {
7638 .cmd = NL80211_CMD_SET_BSS,
7639 .doit = nl80211_set_bss,
7640 .policy = nl80211_policy,
7641 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007642 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007643 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007644 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007645 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007646 .cmd = NL80211_CMD_GET_REG,
7647 .doit = nl80211_get_reg,
7648 .policy = nl80211_policy,
7649 /* can be retrieved by unprivileged users */
7650 },
7651 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007652 .cmd = NL80211_CMD_SET_REG,
7653 .doit = nl80211_set_reg,
7654 .policy = nl80211_policy,
7655 .flags = GENL_ADMIN_PERM,
7656 },
7657 {
7658 .cmd = NL80211_CMD_REQ_SET_REG,
7659 .doit = nl80211_req_set_reg,
7660 .policy = nl80211_policy,
7661 .flags = GENL_ADMIN_PERM,
7662 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007663 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007664 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7665 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007666 .policy = nl80211_policy,
7667 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007668 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007669 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007670 },
7671 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007672 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7673 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007674 .policy = nl80211_policy,
7675 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007676 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007677 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007678 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007679 {
Johannes Berg2a519312009-02-10 21:25:55 +01007680 .cmd = NL80211_CMD_TRIGGER_SCAN,
7681 .doit = nl80211_trigger_scan,
7682 .policy = nl80211_policy,
7683 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007684 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007685 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007686 },
7687 {
7688 .cmd = NL80211_CMD_GET_SCAN,
7689 .policy = nl80211_policy,
7690 .dumpit = nl80211_dump_scan,
7691 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007692 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007693 .cmd = NL80211_CMD_START_SCHED_SCAN,
7694 .doit = nl80211_start_sched_scan,
7695 .policy = nl80211_policy,
7696 .flags = GENL_ADMIN_PERM,
7697 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7698 NL80211_FLAG_NEED_RTNL,
7699 },
7700 {
7701 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7702 .doit = nl80211_stop_sched_scan,
7703 .policy = nl80211_policy,
7704 .flags = GENL_ADMIN_PERM,
7705 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7706 NL80211_FLAG_NEED_RTNL,
7707 },
7708 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007709 .cmd = NL80211_CMD_AUTHENTICATE,
7710 .doit = nl80211_authenticate,
7711 .policy = nl80211_policy,
7712 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007713 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007714 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007715 },
7716 {
7717 .cmd = NL80211_CMD_ASSOCIATE,
7718 .doit = nl80211_associate,
7719 .policy = nl80211_policy,
7720 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007721 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007722 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007723 },
7724 {
7725 .cmd = NL80211_CMD_DEAUTHENTICATE,
7726 .doit = nl80211_deauthenticate,
7727 .policy = nl80211_policy,
7728 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007729 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007730 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007731 },
7732 {
7733 .cmd = NL80211_CMD_DISASSOCIATE,
7734 .doit = nl80211_disassociate,
7735 .policy = nl80211_policy,
7736 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007737 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007738 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007739 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007740 {
7741 .cmd = NL80211_CMD_JOIN_IBSS,
7742 .doit = nl80211_join_ibss,
7743 .policy = nl80211_policy,
7744 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007745 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007746 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007747 },
7748 {
7749 .cmd = NL80211_CMD_LEAVE_IBSS,
7750 .doit = nl80211_leave_ibss,
7751 .policy = nl80211_policy,
7752 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007753 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007754 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007755 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007756#ifdef CONFIG_NL80211_TESTMODE
7757 {
7758 .cmd = NL80211_CMD_TESTMODE,
7759 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007760 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007761 .policy = nl80211_policy,
7762 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007763 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7764 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007765 },
7766#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007767 {
7768 .cmd = NL80211_CMD_CONNECT,
7769 .doit = nl80211_connect,
7770 .policy = nl80211_policy,
7771 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007772 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007773 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007774 },
7775 {
7776 .cmd = NL80211_CMD_DISCONNECT,
7777 .doit = nl80211_disconnect,
7778 .policy = nl80211_policy,
7779 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007780 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007781 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007782 },
Johannes Berg463d0182009-07-14 00:33:35 +02007783 {
7784 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7785 .doit = nl80211_wiphy_netns,
7786 .policy = nl80211_policy,
7787 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007788 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7789 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007790 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007791 {
7792 .cmd = NL80211_CMD_GET_SURVEY,
7793 .policy = nl80211_policy,
7794 .dumpit = nl80211_dump_survey,
7795 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007796 {
7797 .cmd = NL80211_CMD_SET_PMKSA,
7798 .doit = nl80211_setdel_pmksa,
7799 .policy = nl80211_policy,
7800 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007801 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007802 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007803 },
7804 {
7805 .cmd = NL80211_CMD_DEL_PMKSA,
7806 .doit = nl80211_setdel_pmksa,
7807 .policy = nl80211_policy,
7808 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007809 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007810 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007811 },
7812 {
7813 .cmd = NL80211_CMD_FLUSH_PMKSA,
7814 .doit = nl80211_flush_pmksa,
7815 .policy = nl80211_policy,
7816 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007817 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007818 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007819 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007820 {
7821 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7822 .doit = nl80211_remain_on_channel,
7823 .policy = nl80211_policy,
7824 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007825 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007826 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007827 },
7828 {
7829 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7830 .doit = nl80211_cancel_remain_on_channel,
7831 .policy = nl80211_policy,
7832 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007833 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007834 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007835 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007836 {
7837 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7838 .doit = nl80211_set_tx_bitrate_mask,
7839 .policy = nl80211_policy,
7840 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007841 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7842 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007843 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007844 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007845 .cmd = NL80211_CMD_REGISTER_FRAME,
7846 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007847 .policy = nl80211_policy,
7848 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007849 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007850 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007851 },
7852 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007853 .cmd = NL80211_CMD_FRAME,
7854 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007855 .policy = nl80211_policy,
7856 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007857 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007858 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007859 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007860 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007861 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7862 .doit = nl80211_tx_mgmt_cancel_wait,
7863 .policy = nl80211_policy,
7864 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007865 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007866 NL80211_FLAG_NEED_RTNL,
7867 },
7868 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007869 .cmd = NL80211_CMD_SET_POWER_SAVE,
7870 .doit = nl80211_set_power_save,
7871 .policy = nl80211_policy,
7872 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007873 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7874 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007875 },
7876 {
7877 .cmd = NL80211_CMD_GET_POWER_SAVE,
7878 .doit = nl80211_get_power_save,
7879 .policy = nl80211_policy,
7880 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007881 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7882 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007883 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007884 {
7885 .cmd = NL80211_CMD_SET_CQM,
7886 .doit = nl80211_set_cqm,
7887 .policy = nl80211_policy,
7888 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007889 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7890 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007891 },
Johannes Bergf444de02010-05-05 15:25:02 +02007892 {
7893 .cmd = NL80211_CMD_SET_CHANNEL,
7894 .doit = nl80211_set_channel,
7895 .policy = nl80211_policy,
7896 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007897 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7898 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007899 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007900 {
7901 .cmd = NL80211_CMD_SET_WDS_PEER,
7902 .doit = nl80211_set_wds_peer,
7903 .policy = nl80211_policy,
7904 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007905 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7906 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007907 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007908 {
7909 .cmd = NL80211_CMD_JOIN_MESH,
7910 .doit = nl80211_join_mesh,
7911 .policy = nl80211_policy,
7912 .flags = GENL_ADMIN_PERM,
7913 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7914 NL80211_FLAG_NEED_RTNL,
7915 },
7916 {
7917 .cmd = NL80211_CMD_LEAVE_MESH,
7918 .doit = nl80211_leave_mesh,
7919 .policy = nl80211_policy,
7920 .flags = GENL_ADMIN_PERM,
7921 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7922 NL80211_FLAG_NEED_RTNL,
7923 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007924#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007925 {
7926 .cmd = NL80211_CMD_GET_WOWLAN,
7927 .doit = nl80211_get_wowlan,
7928 .policy = nl80211_policy,
7929 /* can be retrieved by unprivileged users */
7930 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7931 NL80211_FLAG_NEED_RTNL,
7932 },
7933 {
7934 .cmd = NL80211_CMD_SET_WOWLAN,
7935 .doit = nl80211_set_wowlan,
7936 .policy = nl80211_policy,
7937 .flags = GENL_ADMIN_PERM,
7938 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7939 NL80211_FLAG_NEED_RTNL,
7940 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007941#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007942 {
7943 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7944 .doit = nl80211_set_rekey_data,
7945 .policy = nl80211_policy,
7946 .flags = GENL_ADMIN_PERM,
7947 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7948 NL80211_FLAG_NEED_RTNL,
7949 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007950 {
7951 .cmd = NL80211_CMD_TDLS_MGMT,
7952 .doit = nl80211_tdls_mgmt,
7953 .policy = nl80211_policy,
7954 .flags = GENL_ADMIN_PERM,
7955 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7956 NL80211_FLAG_NEED_RTNL,
7957 },
7958 {
7959 .cmd = NL80211_CMD_TDLS_OPER,
7960 .doit = nl80211_tdls_oper,
7961 .policy = nl80211_policy,
7962 .flags = GENL_ADMIN_PERM,
7963 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7964 NL80211_FLAG_NEED_RTNL,
7965 },
Johannes Berg28946da2011-11-04 11:18:12 +01007966 {
7967 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7968 .doit = nl80211_register_unexpected_frame,
7969 .policy = nl80211_policy,
7970 .flags = GENL_ADMIN_PERM,
7971 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7972 NL80211_FLAG_NEED_RTNL,
7973 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007974 {
7975 .cmd = NL80211_CMD_PROBE_CLIENT,
7976 .doit = nl80211_probe_client,
7977 .policy = nl80211_policy,
7978 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007979 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007980 NL80211_FLAG_NEED_RTNL,
7981 },
Johannes Berg5e760232011-11-04 11:18:17 +01007982 {
7983 .cmd = NL80211_CMD_REGISTER_BEACONS,
7984 .doit = nl80211_register_beacons,
7985 .policy = nl80211_policy,
7986 .flags = GENL_ADMIN_PERM,
7987 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7988 NL80211_FLAG_NEED_RTNL,
7989 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007990 {
7991 .cmd = NL80211_CMD_SET_NOACK_MAP,
7992 .doit = nl80211_set_noack_map,
7993 .policy = nl80211_policy,
7994 .flags = GENL_ADMIN_PERM,
7995 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7996 NL80211_FLAG_NEED_RTNL,
7997 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007998 {
7999 .cmd = NL80211_CMD_START_P2P_DEVICE,
8000 .doit = nl80211_start_p2p_device,
8001 .policy = nl80211_policy,
8002 .flags = GENL_ADMIN_PERM,
8003 .internal_flags = NL80211_FLAG_NEED_WDEV |
8004 NL80211_FLAG_NEED_RTNL,
8005 },
8006 {
8007 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8008 .doit = nl80211_stop_p2p_device,
8009 .policy = nl80211_policy,
8010 .flags = GENL_ADMIN_PERM,
8011 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8012 NL80211_FLAG_NEED_RTNL,
8013 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008014 {
8015 .cmd = NL80211_CMD_SET_MCAST_RATE,
8016 .doit = nl80211_set_mcast_rate,
8017 .policy = nl80211_policy,
8018 .flags = GENL_ADMIN_PERM,
8019 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8020 NL80211_FLAG_NEED_RTNL,
8021 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308022 {
8023 .cmd = NL80211_CMD_SET_MAC_ACL,
8024 .doit = nl80211_set_mac_acl,
8025 .policy = nl80211_policy,
8026 .flags = GENL_ADMIN_PERM,
8027 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8028 NL80211_FLAG_NEED_RTNL,
8029 },
Johannes Berg55682962007-09-20 13:09:35 -04008030};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008031
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008032static struct genl_multicast_group nl80211_mlme_mcgrp = {
8033 .name = "mlme",
8034};
Johannes Berg55682962007-09-20 13:09:35 -04008035
8036/* multicast groups */
8037static struct genl_multicast_group nl80211_config_mcgrp = {
8038 .name = "config",
8039};
Johannes Berg2a519312009-02-10 21:25:55 +01008040static struct genl_multicast_group nl80211_scan_mcgrp = {
8041 .name = "scan",
8042};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008043static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8044 .name = "regulatory",
8045};
Johannes Berg55682962007-09-20 13:09:35 -04008046
8047/* notification functions */
8048
8049void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8050{
8051 struct sk_buff *msg;
8052
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008053 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008054 if (!msg)
8055 return;
8056
8057 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8058 nlmsg_free(msg);
8059 return;
8060 }
8061
Johannes Berg463d0182009-07-14 00:33:35 +02008062 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8063 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008064}
8065
Johannes Berg362a4152009-05-24 16:43:15 +02008066static int nl80211_add_scan_req(struct sk_buff *msg,
8067 struct cfg80211_registered_device *rdev)
8068{
8069 struct cfg80211_scan_request *req = rdev->scan_req;
8070 struct nlattr *nest;
8071 int i;
8072
Johannes Berg667503dd2009-07-07 03:56:11 +02008073 ASSERT_RDEV_LOCK(rdev);
8074
Johannes Berg362a4152009-05-24 16:43:15 +02008075 if (WARN_ON(!req))
8076 return 0;
8077
8078 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8079 if (!nest)
8080 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008081 for (i = 0; i < req->n_ssids; i++) {
8082 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8083 goto nla_put_failure;
8084 }
Johannes Berg362a4152009-05-24 16:43:15 +02008085 nla_nest_end(msg, nest);
8086
8087 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8088 if (!nest)
8089 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008090 for (i = 0; i < req->n_channels; i++) {
8091 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8092 goto nla_put_failure;
8093 }
Johannes Berg362a4152009-05-24 16:43:15 +02008094 nla_nest_end(msg, nest);
8095
David S. Miller9360ffd2012-03-29 04:41:26 -04008096 if (req->ie &&
8097 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8098 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008099
Sam Lefflered4737712012-10-11 21:03:31 -07008100 if (req->flags)
8101 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8102
Johannes Berg362a4152009-05-24 16:43:15 +02008103 return 0;
8104 nla_put_failure:
8105 return -ENOBUFS;
8106}
8107
Johannes Berga538e2d2009-06-16 19:56:42 +02008108static int nl80211_send_scan_msg(struct sk_buff *msg,
8109 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008110 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008111 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008112 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008113{
8114 void *hdr;
8115
Eric W. Biederman15e47302012-09-07 20:12:54 +00008116 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008117 if (!hdr)
8118 return -1;
8119
David S. Miller9360ffd2012-03-29 04:41:26 -04008120 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008121 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8122 wdev->netdev->ifindex)) ||
8123 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008124 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008125
Johannes Berg362a4152009-05-24 16:43:15 +02008126 /* ignore errors and send incomplete event anyway */
8127 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008128
8129 return genlmsg_end(msg, hdr);
8130
8131 nla_put_failure:
8132 genlmsg_cancel(msg, hdr);
8133 return -EMSGSIZE;
8134}
8135
Luciano Coelho807f8a82011-05-11 17:09:35 +03008136static int
8137nl80211_send_sched_scan_msg(struct sk_buff *msg,
8138 struct cfg80211_registered_device *rdev,
8139 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008140 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008141{
8142 void *hdr;
8143
Eric W. Biederman15e47302012-09-07 20:12:54 +00008144 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008145 if (!hdr)
8146 return -1;
8147
David S. Miller9360ffd2012-03-29 04:41:26 -04008148 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8149 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8150 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008151
8152 return genlmsg_end(msg, hdr);
8153
8154 nla_put_failure:
8155 genlmsg_cancel(msg, hdr);
8156 return -EMSGSIZE;
8157}
8158
Johannes Berga538e2d2009-06-16 19:56:42 +02008159void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008160 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008161{
8162 struct sk_buff *msg;
8163
Thomas Graf58050fc2012-06-28 03:57:45 +00008164 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008165 if (!msg)
8166 return;
8167
Johannes Bergfd014282012-06-18 19:17:03 +02008168 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008169 NL80211_CMD_TRIGGER_SCAN) < 0) {
8170 nlmsg_free(msg);
8171 return;
8172 }
8173
Johannes Berg463d0182009-07-14 00:33:35 +02008174 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8175 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008176}
8177
Johannes Berg2a519312009-02-10 21:25:55 +01008178void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008179 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008180{
8181 struct sk_buff *msg;
8182
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008183 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008184 if (!msg)
8185 return;
8186
Johannes Bergfd014282012-06-18 19:17:03 +02008187 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008188 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008189 nlmsg_free(msg);
8190 return;
8191 }
8192
Johannes Berg463d0182009-07-14 00:33:35 +02008193 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8194 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008195}
8196
8197void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008198 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008199{
8200 struct sk_buff *msg;
8201
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008202 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008203 if (!msg)
8204 return;
8205
Johannes Bergfd014282012-06-18 19:17:03 +02008206 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008207 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008208 nlmsg_free(msg);
8209 return;
8210 }
8211
Johannes Berg463d0182009-07-14 00:33:35 +02008212 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8213 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008214}
8215
Luciano Coelho807f8a82011-05-11 17:09:35 +03008216void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8217 struct net_device *netdev)
8218{
8219 struct sk_buff *msg;
8220
8221 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8222 if (!msg)
8223 return;
8224
8225 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8226 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8227 nlmsg_free(msg);
8228 return;
8229 }
8230
8231 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8232 nl80211_scan_mcgrp.id, GFP_KERNEL);
8233}
8234
8235void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8236 struct net_device *netdev, u32 cmd)
8237{
8238 struct sk_buff *msg;
8239
Thomas Graf58050fc2012-06-28 03:57:45 +00008240 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008241 if (!msg)
8242 return;
8243
8244 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8245 nlmsg_free(msg);
8246 return;
8247 }
8248
8249 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8250 nl80211_scan_mcgrp.id, GFP_KERNEL);
8251}
8252
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008253/*
8254 * This can happen on global regulatory changes or device specific settings
8255 * based on custom world regulatory domains.
8256 */
8257void nl80211_send_reg_change_event(struct regulatory_request *request)
8258{
8259 struct sk_buff *msg;
8260 void *hdr;
8261
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008262 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008263 if (!msg)
8264 return;
8265
8266 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8267 if (!hdr) {
8268 nlmsg_free(msg);
8269 return;
8270 }
8271
8272 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008273 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8274 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008275
David S. Miller9360ffd2012-03-29 04:41:26 -04008276 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8277 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8278 NL80211_REGDOM_TYPE_WORLD))
8279 goto nla_put_failure;
8280 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8281 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8282 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8283 goto nla_put_failure;
8284 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8285 request->intersect) {
8286 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8287 NL80211_REGDOM_TYPE_INTERSECTION))
8288 goto nla_put_failure;
8289 } else {
8290 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8291 NL80211_REGDOM_TYPE_COUNTRY) ||
8292 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8293 request->alpha2))
8294 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008295 }
8296
Johannes Bergf4173762012-12-03 18:23:37 +01008297 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008298 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8299 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008300
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008301 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008302
Johannes Bergbc43b282009-07-25 10:54:13 +02008303 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008304 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008305 GFP_ATOMIC);
8306 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008307
8308 return;
8309
8310nla_put_failure:
8311 genlmsg_cancel(msg, hdr);
8312 nlmsg_free(msg);
8313}
8314
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008315static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8316 struct net_device *netdev,
8317 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008318 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008319{
8320 struct sk_buff *msg;
8321 void *hdr;
8322
Johannes Berge6d6e342009-07-01 21:26:47 +02008323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008324 if (!msg)
8325 return;
8326
8327 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8328 if (!hdr) {
8329 nlmsg_free(msg);
8330 return;
8331 }
8332
David S. Miller9360ffd2012-03-29 04:41:26 -04008333 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8334 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8335 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8336 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008337
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008338 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008339
Johannes Berg463d0182009-07-14 00:33:35 +02008340 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8341 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008342 return;
8343
8344 nla_put_failure:
8345 genlmsg_cancel(msg, hdr);
8346 nlmsg_free(msg);
8347}
8348
8349void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008350 struct net_device *netdev, const u8 *buf,
8351 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008352{
8353 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008354 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008355}
8356
8357void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8358 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008359 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008360{
Johannes Berge6d6e342009-07-01 21:26:47 +02008361 nl80211_send_mlme_event(rdev, netdev, buf, len,
8362 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008363}
8364
Jouni Malinen53b46b82009-03-27 20:53:56 +02008365void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008366 struct net_device *netdev, const u8 *buf,
8367 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008368{
8369 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008370 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008371}
8372
Jouni Malinen53b46b82009-03-27 20:53:56 +02008373void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8374 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008375 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008376{
8377 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008378 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008379}
8380
Jouni Malinencf4e5942010-12-16 00:52:40 +02008381void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8382 struct net_device *netdev, const u8 *buf,
8383 size_t len, gfp_t gfp)
8384{
8385 nl80211_send_mlme_event(rdev, netdev, buf, len,
8386 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8387}
8388
8389void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8390 struct net_device *netdev, const u8 *buf,
8391 size_t len, gfp_t gfp)
8392{
8393 nl80211_send_mlme_event(rdev, netdev, buf, len,
8394 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8395}
8396
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008397static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8398 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008399 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008400{
8401 struct sk_buff *msg;
8402 void *hdr;
8403
Johannes Berge6d6e342009-07-01 21:26:47 +02008404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008405 if (!msg)
8406 return;
8407
8408 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8409 if (!hdr) {
8410 nlmsg_free(msg);
8411 return;
8412 }
8413
David S. Miller9360ffd2012-03-29 04:41:26 -04008414 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8415 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8416 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8417 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8418 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008419
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008420 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008421
Johannes Berg463d0182009-07-14 00:33:35 +02008422 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8423 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008424 return;
8425
8426 nla_put_failure:
8427 genlmsg_cancel(msg, hdr);
8428 nlmsg_free(msg);
8429}
8430
8431void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008432 struct net_device *netdev, const u8 *addr,
8433 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008434{
8435 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008436 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008437}
8438
8439void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008440 struct net_device *netdev, const u8 *addr,
8441 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008442{
Johannes Berge6d6e342009-07-01 21:26:47 +02008443 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8444 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008445}
8446
Samuel Ortizb23aa672009-07-01 21:26:54 +02008447void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8448 struct net_device *netdev, const u8 *bssid,
8449 const u8 *req_ie, size_t req_ie_len,
8450 const u8 *resp_ie, size_t resp_ie_len,
8451 u16 status, gfp_t gfp)
8452{
8453 struct sk_buff *msg;
8454 void *hdr;
8455
Thomas Graf58050fc2012-06-28 03:57:45 +00008456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008457 if (!msg)
8458 return;
8459
8460 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8461 if (!hdr) {
8462 nlmsg_free(msg);
8463 return;
8464 }
8465
David S. Miller9360ffd2012-03-29 04:41:26 -04008466 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8467 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8468 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8469 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8470 (req_ie &&
8471 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8472 (resp_ie &&
8473 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8474 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008475
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008476 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008477
Johannes Berg463d0182009-07-14 00:33:35 +02008478 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8479 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008480 return;
8481
8482 nla_put_failure:
8483 genlmsg_cancel(msg, hdr);
8484 nlmsg_free(msg);
8485
8486}
8487
8488void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8489 struct net_device *netdev, const u8 *bssid,
8490 const u8 *req_ie, size_t req_ie_len,
8491 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8492{
8493 struct sk_buff *msg;
8494 void *hdr;
8495
Thomas Graf58050fc2012-06-28 03:57:45 +00008496 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008497 if (!msg)
8498 return;
8499
8500 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8501 if (!hdr) {
8502 nlmsg_free(msg);
8503 return;
8504 }
8505
David S. Miller9360ffd2012-03-29 04:41:26 -04008506 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8507 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8508 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8509 (req_ie &&
8510 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8511 (resp_ie &&
8512 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8513 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008514
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008515 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008516
Johannes Berg463d0182009-07-14 00:33:35 +02008517 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8518 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008519 return;
8520
8521 nla_put_failure:
8522 genlmsg_cancel(msg, hdr);
8523 nlmsg_free(msg);
8524
8525}
8526
8527void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8528 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008529 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008530{
8531 struct sk_buff *msg;
8532 void *hdr;
8533
Thomas Graf58050fc2012-06-28 03:57:45 +00008534 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008535 if (!msg)
8536 return;
8537
8538 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8539 if (!hdr) {
8540 nlmsg_free(msg);
8541 return;
8542 }
8543
David S. Miller9360ffd2012-03-29 04:41:26 -04008544 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8545 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8546 (from_ap && reason &&
8547 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8548 (from_ap &&
8549 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8550 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8551 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008552
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008553 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008554
Johannes Berg463d0182009-07-14 00:33:35 +02008555 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8556 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008557 return;
8558
8559 nla_put_failure:
8560 genlmsg_cancel(msg, hdr);
8561 nlmsg_free(msg);
8562
8563}
8564
Johannes Berg04a773a2009-04-19 21:24:32 +02008565void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8566 struct net_device *netdev, const u8 *bssid,
8567 gfp_t gfp)
8568{
8569 struct sk_buff *msg;
8570 void *hdr;
8571
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008572 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008573 if (!msg)
8574 return;
8575
8576 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8577 if (!hdr) {
8578 nlmsg_free(msg);
8579 return;
8580 }
8581
David S. Miller9360ffd2012-03-29 04:41:26 -04008582 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8583 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8584 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8585 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008586
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008587 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008588
Johannes Berg463d0182009-07-14 00:33:35 +02008589 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8590 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008591 return;
8592
8593 nla_put_failure:
8594 genlmsg_cancel(msg, hdr);
8595 nlmsg_free(msg);
8596}
8597
Javier Cardonac93b5e72011-04-07 15:08:34 -07008598void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8599 struct net_device *netdev,
8600 const u8 *macaddr, const u8* ie, u8 ie_len,
8601 gfp_t gfp)
8602{
8603 struct sk_buff *msg;
8604 void *hdr;
8605
8606 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8607 if (!msg)
8608 return;
8609
8610 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8611 if (!hdr) {
8612 nlmsg_free(msg);
8613 return;
8614 }
8615
David S. Miller9360ffd2012-03-29 04:41:26 -04008616 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8617 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8618 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8619 (ie_len && ie &&
8620 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8621 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008622
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008623 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008624
8625 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8626 nl80211_mlme_mcgrp.id, gfp);
8627 return;
8628
8629 nla_put_failure:
8630 genlmsg_cancel(msg, hdr);
8631 nlmsg_free(msg);
8632}
8633
Jouni Malinena3b8b052009-03-27 21:59:49 +02008634void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8635 struct net_device *netdev, const u8 *addr,
8636 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008637 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008638{
8639 struct sk_buff *msg;
8640 void *hdr;
8641
Johannes Berge6d6e342009-07-01 21:26:47 +02008642 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008643 if (!msg)
8644 return;
8645
8646 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8647 if (!hdr) {
8648 nlmsg_free(msg);
8649 return;
8650 }
8651
David S. Miller9360ffd2012-03-29 04:41:26 -04008652 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8653 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8654 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8655 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8656 (key_id != -1 &&
8657 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8658 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8659 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008660
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008661 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008662
Johannes Berg463d0182009-07-14 00:33:35 +02008663 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8664 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008665 return;
8666
8667 nla_put_failure:
8668 genlmsg_cancel(msg, hdr);
8669 nlmsg_free(msg);
8670}
8671
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008672void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8673 struct ieee80211_channel *channel_before,
8674 struct ieee80211_channel *channel_after)
8675{
8676 struct sk_buff *msg;
8677 void *hdr;
8678 struct nlattr *nl_freq;
8679
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008680 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008681 if (!msg)
8682 return;
8683
8684 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8685 if (!hdr) {
8686 nlmsg_free(msg);
8687 return;
8688 }
8689
8690 /*
8691 * Since we are applying the beacon hint to a wiphy we know its
8692 * wiphy_idx is valid
8693 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008694 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8695 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008696
8697 /* Before */
8698 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8699 if (!nl_freq)
8700 goto nla_put_failure;
8701 if (nl80211_msg_put_channel(msg, channel_before))
8702 goto nla_put_failure;
8703 nla_nest_end(msg, nl_freq);
8704
8705 /* After */
8706 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8707 if (!nl_freq)
8708 goto nla_put_failure;
8709 if (nl80211_msg_put_channel(msg, channel_after))
8710 goto nla_put_failure;
8711 nla_nest_end(msg, nl_freq);
8712
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008713 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008714
Johannes Berg463d0182009-07-14 00:33:35 +02008715 rcu_read_lock();
8716 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8717 GFP_ATOMIC);
8718 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008719
8720 return;
8721
8722nla_put_failure:
8723 genlmsg_cancel(msg, hdr);
8724 nlmsg_free(msg);
8725}
8726
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008727static void nl80211_send_remain_on_chan_event(
8728 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008729 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008730 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008731 unsigned int duration, gfp_t gfp)
8732{
8733 struct sk_buff *msg;
8734 void *hdr;
8735
8736 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8737 if (!msg)
8738 return;
8739
8740 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8741 if (!hdr) {
8742 nlmsg_free(msg);
8743 return;
8744 }
8745
David S. Miller9360ffd2012-03-29 04:41:26 -04008746 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008747 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8748 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008749 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008750 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01008751 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
8752 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008753 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8754 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008755
David S. Miller9360ffd2012-03-29 04:41:26 -04008756 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8757 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8758 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008759
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008760 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008761
8762 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8763 nl80211_mlme_mcgrp.id, gfp);
8764 return;
8765
8766 nla_put_failure:
8767 genlmsg_cancel(msg, hdr);
8768 nlmsg_free(msg);
8769}
8770
8771void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008772 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008773 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008774 unsigned int duration, gfp_t gfp)
8775{
8776 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008777 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01008778 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008779}
8780
8781void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008782 struct cfg80211_registered_device *rdev,
8783 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01008784 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008785{
8786 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01008787 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008788}
8789
Johannes Berg98b62182009-12-23 13:15:44 +01008790void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8791 struct net_device *dev, const u8 *mac_addr,
8792 struct station_info *sinfo, gfp_t gfp)
8793{
8794 struct sk_buff *msg;
8795
Thomas Graf58050fc2012-06-28 03:57:45 +00008796 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008797 if (!msg)
8798 return;
8799
John W. Linville66266b32012-03-15 13:25:41 -04008800 if (nl80211_send_station(msg, 0, 0, 0,
8801 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008802 nlmsg_free(msg);
8803 return;
8804 }
8805
8806 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8807 nl80211_mlme_mcgrp.id, gfp);
8808}
8809
Jouni Malinenec15e682011-03-23 15:29:52 +02008810void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8811 struct net_device *dev, const u8 *mac_addr,
8812 gfp_t gfp)
8813{
8814 struct sk_buff *msg;
8815 void *hdr;
8816
Thomas Graf58050fc2012-06-28 03:57:45 +00008817 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008818 if (!msg)
8819 return;
8820
8821 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8822 if (!hdr) {
8823 nlmsg_free(msg);
8824 return;
8825 }
8826
David S. Miller9360ffd2012-03-29 04:41:26 -04008827 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8828 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8829 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008830
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008831 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008832
8833 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8834 nl80211_mlme_mcgrp.id, gfp);
8835 return;
8836
8837 nla_put_failure:
8838 genlmsg_cancel(msg, hdr);
8839 nlmsg_free(msg);
8840}
8841
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308842void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8843 struct net_device *dev, const u8 *mac_addr,
8844 enum nl80211_connect_failed_reason reason,
8845 gfp_t gfp)
8846{
8847 struct sk_buff *msg;
8848 void *hdr;
8849
8850 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8851 if (!msg)
8852 return;
8853
8854 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8855 if (!hdr) {
8856 nlmsg_free(msg);
8857 return;
8858 }
8859
8860 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8861 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8862 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8863 goto nla_put_failure;
8864
8865 genlmsg_end(msg, hdr);
8866
8867 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8868 nl80211_mlme_mcgrp.id, gfp);
8869 return;
8870
8871 nla_put_failure:
8872 genlmsg_cancel(msg, hdr);
8873 nlmsg_free(msg);
8874}
8875
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008876static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8877 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008878{
8879 struct wireless_dev *wdev = dev->ieee80211_ptr;
8880 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8881 struct sk_buff *msg;
8882 void *hdr;
8883 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008884 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008885
Eric W. Biederman15e47302012-09-07 20:12:54 +00008886 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008887 return false;
8888
8889 msg = nlmsg_new(100, gfp);
8890 if (!msg)
8891 return true;
8892
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008893 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008894 if (!hdr) {
8895 nlmsg_free(msg);
8896 return true;
8897 }
8898
David S. Miller9360ffd2012-03-29 04:41:26 -04008899 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8900 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8901 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8902 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008903
8904 err = genlmsg_end(msg, hdr);
8905 if (err < 0) {
8906 nlmsg_free(msg);
8907 return true;
8908 }
8909
Eric W. Biederman15e47302012-09-07 20:12:54 +00008910 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008911 return true;
8912
8913 nla_put_failure:
8914 genlmsg_cancel(msg, hdr);
8915 nlmsg_free(msg);
8916 return true;
8917}
8918
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008919bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8920{
8921 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8922 addr, gfp);
8923}
8924
8925bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8926 const u8 *addr, gfp_t gfp)
8927{
8928 return __nl80211_unexpected_frame(dev,
8929 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8930 addr, gfp);
8931}
8932
Johannes Berg2e161f72010-08-12 15:38:38 +02008933int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008934 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008935 int freq, int sig_dbm,
8936 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008937{
Johannes Berg71bbc992012-06-15 15:30:18 +02008938 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008939 struct sk_buff *msg;
8940 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008941
8942 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8943 if (!msg)
8944 return -ENOMEM;
8945
Johannes Berg2e161f72010-08-12 15:38:38 +02008946 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008947 if (!hdr) {
8948 nlmsg_free(msg);
8949 return -ENOMEM;
8950 }
8951
David S. Miller9360ffd2012-03-29 04:41:26 -04008952 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008953 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8954 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008955 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8956 (sig_dbm &&
8957 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8958 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8959 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008960
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008961 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008962
Eric W. Biederman15e47302012-09-07 20:12:54 +00008963 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008964
8965 nla_put_failure:
8966 genlmsg_cancel(msg, hdr);
8967 nlmsg_free(msg);
8968 return -ENOBUFS;
8969}
8970
Johannes Berg2e161f72010-08-12 15:38:38 +02008971void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008972 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008973 const u8 *buf, size_t len, bool ack,
8974 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008975{
Johannes Berg71bbc992012-06-15 15:30:18 +02008976 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008977 struct sk_buff *msg;
8978 void *hdr;
8979
8980 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8981 if (!msg)
8982 return;
8983
Johannes Berg2e161f72010-08-12 15:38:38 +02008984 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008985 if (!hdr) {
8986 nlmsg_free(msg);
8987 return;
8988 }
8989
David S. Miller9360ffd2012-03-29 04:41:26 -04008990 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008991 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8992 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008993 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8994 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8995 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8996 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008997
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008998 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008999
9000 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9001 return;
9002
9003 nla_put_failure:
9004 genlmsg_cancel(msg, hdr);
9005 nlmsg_free(msg);
9006}
9007
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009008void
9009nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9010 struct net_device *netdev,
9011 enum nl80211_cqm_rssi_threshold_event rssi_event,
9012 gfp_t gfp)
9013{
9014 struct sk_buff *msg;
9015 struct nlattr *pinfoattr;
9016 void *hdr;
9017
Thomas Graf58050fc2012-06-28 03:57:45 +00009018 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009019 if (!msg)
9020 return;
9021
9022 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9023 if (!hdr) {
9024 nlmsg_free(msg);
9025 return;
9026 }
9027
David S. Miller9360ffd2012-03-29 04:41:26 -04009028 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9029 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9030 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009031
9032 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9033 if (!pinfoattr)
9034 goto nla_put_failure;
9035
David S. Miller9360ffd2012-03-29 04:41:26 -04009036 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9037 rssi_event))
9038 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009039
9040 nla_nest_end(msg, pinfoattr);
9041
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009042 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009043
9044 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9045 nl80211_mlme_mcgrp.id, gfp);
9046 return;
9047
9048 nla_put_failure:
9049 genlmsg_cancel(msg, hdr);
9050 nlmsg_free(msg);
9051}
9052
Johannes Berge5497d72011-07-05 16:35:40 +02009053void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9054 struct net_device *netdev, const u8 *bssid,
9055 const u8 *replay_ctr, gfp_t gfp)
9056{
9057 struct sk_buff *msg;
9058 struct nlattr *rekey_attr;
9059 void *hdr;
9060
Thomas Graf58050fc2012-06-28 03:57:45 +00009061 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009062 if (!msg)
9063 return;
9064
9065 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9066 if (!hdr) {
9067 nlmsg_free(msg);
9068 return;
9069 }
9070
David S. Miller9360ffd2012-03-29 04:41:26 -04009071 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9072 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9073 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9074 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009075
9076 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9077 if (!rekey_attr)
9078 goto nla_put_failure;
9079
David S. Miller9360ffd2012-03-29 04:41:26 -04009080 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9081 NL80211_REPLAY_CTR_LEN, replay_ctr))
9082 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009083
9084 nla_nest_end(msg, rekey_attr);
9085
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009086 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009087
9088 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9089 nl80211_mlme_mcgrp.id, gfp);
9090 return;
9091
9092 nla_put_failure:
9093 genlmsg_cancel(msg, hdr);
9094 nlmsg_free(msg);
9095}
9096
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009097void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9098 struct net_device *netdev, int index,
9099 const u8 *bssid, bool preauth, gfp_t gfp)
9100{
9101 struct sk_buff *msg;
9102 struct nlattr *attr;
9103 void *hdr;
9104
Thomas Graf58050fc2012-06-28 03:57:45 +00009105 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009106 if (!msg)
9107 return;
9108
9109 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9110 if (!hdr) {
9111 nlmsg_free(msg);
9112 return;
9113 }
9114
David S. Miller9360ffd2012-03-29 04:41:26 -04009115 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9116 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9117 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009118
9119 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9120 if (!attr)
9121 goto nla_put_failure;
9122
David S. Miller9360ffd2012-03-29 04:41:26 -04009123 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9124 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9125 (preauth &&
9126 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9127 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009128
9129 nla_nest_end(msg, attr);
9130
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009131 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009132
9133 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9134 nl80211_mlme_mcgrp.id, gfp);
9135 return;
9136
9137 nla_put_failure:
9138 genlmsg_cancel(msg, hdr);
9139 nlmsg_free(msg);
9140}
9141
Thomas Pedersen53145262012-04-06 13:35:47 -07009142void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009143 struct net_device *netdev,
9144 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009145{
9146 struct sk_buff *msg;
9147 void *hdr;
9148
Thomas Graf58050fc2012-06-28 03:57:45 +00009149 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009150 if (!msg)
9151 return;
9152
9153 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9154 if (!hdr) {
9155 nlmsg_free(msg);
9156 return;
9157 }
9158
Johannes Berg683b6d32012-11-08 21:25:48 +01009159 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9160 goto nla_put_failure;
9161
9162 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009163 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009164
9165 genlmsg_end(msg, hdr);
9166
9167 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9168 nl80211_mlme_mcgrp.id, gfp);
9169 return;
9170
9171 nla_put_failure:
9172 genlmsg_cancel(msg, hdr);
9173 nlmsg_free(msg);
9174}
9175
Johannes Bergc063dbf2010-11-24 08:10:05 +01009176void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009177nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9178 struct net_device *netdev, const u8 *peer,
9179 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9180{
9181 struct sk_buff *msg;
9182 struct nlattr *pinfoattr;
9183 void *hdr;
9184
9185 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9186 if (!msg)
9187 return;
9188
9189 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9190 if (!hdr) {
9191 nlmsg_free(msg);
9192 return;
9193 }
9194
9195 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9196 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9197 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9198 goto nla_put_failure;
9199
9200 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9201 if (!pinfoattr)
9202 goto nla_put_failure;
9203
9204 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9205 goto nla_put_failure;
9206
9207 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9208 goto nla_put_failure;
9209
9210 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9211 goto nla_put_failure;
9212
9213 nla_nest_end(msg, pinfoattr);
9214
9215 genlmsg_end(msg, hdr);
9216
9217 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9218 nl80211_mlme_mcgrp.id, gfp);
9219 return;
9220
9221 nla_put_failure:
9222 genlmsg_cancel(msg, hdr);
9223 nlmsg_free(msg);
9224}
9225
9226void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009227nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9228 struct net_device *netdev, const u8 *peer,
9229 u32 num_packets, gfp_t gfp)
9230{
9231 struct sk_buff *msg;
9232 struct nlattr *pinfoattr;
9233 void *hdr;
9234
Thomas Graf58050fc2012-06-28 03:57:45 +00009235 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009236 if (!msg)
9237 return;
9238
9239 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9240 if (!hdr) {
9241 nlmsg_free(msg);
9242 return;
9243 }
9244
David S. Miller9360ffd2012-03-29 04:41:26 -04009245 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9246 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9247 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9248 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009249
9250 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9251 if (!pinfoattr)
9252 goto nla_put_failure;
9253
David S. Miller9360ffd2012-03-29 04:41:26 -04009254 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9255 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009256
9257 nla_nest_end(msg, pinfoattr);
9258
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009259 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009260
9261 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9262 nl80211_mlme_mcgrp.id, gfp);
9263 return;
9264
9265 nla_put_failure:
9266 genlmsg_cancel(msg, hdr);
9267 nlmsg_free(msg);
9268}
9269
Johannes Berg7f6cf312011-11-04 11:18:15 +01009270void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9271 u64 cookie, bool acked, gfp_t gfp)
9272{
9273 struct wireless_dev *wdev = dev->ieee80211_ptr;
9274 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9275 struct sk_buff *msg;
9276 void *hdr;
9277 int err;
9278
Beni Lev4ee3e062012-08-27 12:49:39 +03009279 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9280
Thomas Graf58050fc2012-06-28 03:57:45 +00009281 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009282
Johannes Berg7f6cf312011-11-04 11:18:15 +01009283 if (!msg)
9284 return;
9285
9286 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9287 if (!hdr) {
9288 nlmsg_free(msg);
9289 return;
9290 }
9291
David S. Miller9360ffd2012-03-29 04:41:26 -04009292 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9293 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9294 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9295 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9296 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9297 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009298
9299 err = genlmsg_end(msg, hdr);
9300 if (err < 0) {
9301 nlmsg_free(msg);
9302 return;
9303 }
9304
9305 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9306 nl80211_mlme_mcgrp.id, gfp);
9307 return;
9308
9309 nla_put_failure:
9310 genlmsg_cancel(msg, hdr);
9311 nlmsg_free(msg);
9312}
9313EXPORT_SYMBOL(cfg80211_probe_status);
9314
Johannes Berg5e760232011-11-04 11:18:17 +01009315void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9316 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009317 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009318{
9319 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9320 struct sk_buff *msg;
9321 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009322 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009323
Beni Lev4ee3e062012-08-27 12:49:39 +03009324 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9325
Ben Greear37c73b52012-10-26 14:49:25 -07009326 spin_lock_bh(&rdev->beacon_registrations_lock);
9327 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9328 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9329 if (!msg) {
9330 spin_unlock_bh(&rdev->beacon_registrations_lock);
9331 return;
9332 }
Johannes Berg5e760232011-11-04 11:18:17 +01009333
Ben Greear37c73b52012-10-26 14:49:25 -07009334 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9335 if (!hdr)
9336 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009337
Ben Greear37c73b52012-10-26 14:49:25 -07009338 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9339 (freq &&
9340 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9341 (sig_dbm &&
9342 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9343 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9344 goto nla_put_failure;
9345
9346 genlmsg_end(msg, hdr);
9347
9348 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009349 }
Ben Greear37c73b52012-10-26 14:49:25 -07009350 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009351 return;
9352
9353 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009354 spin_unlock_bh(&rdev->beacon_registrations_lock);
9355 if (hdr)
9356 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009357 nlmsg_free(msg);
9358}
9359EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9360
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009361#ifdef CONFIG_PM
9362void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9363 struct cfg80211_wowlan_wakeup *wakeup,
9364 gfp_t gfp)
9365{
9366 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9367 struct sk_buff *msg;
9368 void *hdr;
9369 int err, size = 200;
9370
9371 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9372
9373 if (wakeup)
9374 size += wakeup->packet_present_len;
9375
9376 msg = nlmsg_new(size, gfp);
9377 if (!msg)
9378 return;
9379
9380 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9381 if (!hdr)
9382 goto free_msg;
9383
9384 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9385 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9386 goto free_msg;
9387
9388 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9389 wdev->netdev->ifindex))
9390 goto free_msg;
9391
9392 if (wakeup) {
9393 struct nlattr *reasons;
9394
9395 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9396
9397 if (wakeup->disconnect &&
9398 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9399 goto free_msg;
9400 if (wakeup->magic_pkt &&
9401 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9402 goto free_msg;
9403 if (wakeup->gtk_rekey_failure &&
9404 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9405 goto free_msg;
9406 if (wakeup->eap_identity_req &&
9407 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9408 goto free_msg;
9409 if (wakeup->four_way_handshake &&
9410 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9411 goto free_msg;
9412 if (wakeup->rfkill_release &&
9413 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9414 goto free_msg;
9415
9416 if (wakeup->pattern_idx >= 0 &&
9417 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9418 wakeup->pattern_idx))
9419 goto free_msg;
9420
9421 if (wakeup->packet) {
9422 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9423 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9424
9425 if (!wakeup->packet_80211) {
9426 pkt_attr =
9427 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9428 len_attr =
9429 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9430 }
9431
9432 if (wakeup->packet_len &&
9433 nla_put_u32(msg, len_attr, wakeup->packet_len))
9434 goto free_msg;
9435
9436 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9437 wakeup->packet))
9438 goto free_msg;
9439 }
9440
9441 nla_nest_end(msg, reasons);
9442 }
9443
9444 err = genlmsg_end(msg, hdr);
9445 if (err < 0)
9446 goto free_msg;
9447
9448 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9449 nl80211_mlme_mcgrp.id, gfp);
9450 return;
9451
9452 free_msg:
9453 nlmsg_free(msg);
9454}
9455EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9456#endif
9457
Jouni Malinen3475b092012-11-16 22:49:57 +02009458void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9459 enum nl80211_tdls_operation oper,
9460 u16 reason_code, gfp_t gfp)
9461{
9462 struct wireless_dev *wdev = dev->ieee80211_ptr;
9463 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9464 struct sk_buff *msg;
9465 void *hdr;
9466 int err;
9467
9468 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9469 reason_code);
9470
9471 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9472 if (!msg)
9473 return;
9474
9475 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9476 if (!hdr) {
9477 nlmsg_free(msg);
9478 return;
9479 }
9480
9481 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9482 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9483 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9484 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9485 (reason_code > 0 &&
9486 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9487 goto nla_put_failure;
9488
9489 err = genlmsg_end(msg, hdr);
9490 if (err < 0) {
9491 nlmsg_free(msg);
9492 return;
9493 }
9494
9495 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9496 nl80211_mlme_mcgrp.id, gfp);
9497 return;
9498
9499 nla_put_failure:
9500 genlmsg_cancel(msg, hdr);
9501 nlmsg_free(msg);
9502}
9503EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9504
Jouni Malinen026331c2010-02-15 12:53:10 +02009505static int nl80211_netlink_notify(struct notifier_block * nb,
9506 unsigned long state,
9507 void *_notify)
9508{
9509 struct netlink_notify *notify = _notify;
9510 struct cfg80211_registered_device *rdev;
9511 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009512 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009513
9514 if (state != NETLINK_URELEASE)
9515 return NOTIFY_DONE;
9516
9517 rcu_read_lock();
9518
Johannes Berg5e760232011-11-04 11:18:17 +01009519 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009520 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009521 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009522
9523 spin_lock_bh(&rdev->beacon_registrations_lock);
9524 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9525 list) {
9526 if (reg->nlportid == notify->portid) {
9527 list_del(&reg->list);
9528 kfree(reg);
9529 break;
9530 }
9531 }
9532 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009533 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009534
9535 rcu_read_unlock();
9536
9537 return NOTIFY_DONE;
9538}
9539
9540static struct notifier_block nl80211_netlink_notifier = {
9541 .notifier_call = nl80211_netlink_notify,
9542};
9543
Johannes Berg55682962007-09-20 13:09:35 -04009544/* initialisation/exit functions */
9545
9546int nl80211_init(void)
9547{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009548 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009549
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009550 err = genl_register_family_with_ops(&nl80211_fam,
9551 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009552 if (err)
9553 return err;
9554
Johannes Berg55682962007-09-20 13:09:35 -04009555 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9556 if (err)
9557 goto err_out;
9558
Johannes Berg2a519312009-02-10 21:25:55 +01009559 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9560 if (err)
9561 goto err_out;
9562
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009563 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9564 if (err)
9565 goto err_out;
9566
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009567 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9568 if (err)
9569 goto err_out;
9570
Johannes Bergaff89a92009-07-01 21:26:51 +02009571#ifdef CONFIG_NL80211_TESTMODE
9572 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9573 if (err)
9574 goto err_out;
9575#endif
9576
Jouni Malinen026331c2010-02-15 12:53:10 +02009577 err = netlink_register_notifier(&nl80211_netlink_notifier);
9578 if (err)
9579 goto err_out;
9580
Johannes Berg55682962007-09-20 13:09:35 -04009581 return 0;
9582 err_out:
9583 genl_unregister_family(&nl80211_fam);
9584 return err;
9585}
9586
9587void nl80211_exit(void)
9588{
Jouni Malinen026331c2010-02-15 12:53:10 +02009589 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009590 genl_unregister_family(&nl80211_fam);
9591}