blob: 93bc63eae0761cfcb98d6134d03d214412eb2906 [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,
1241 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001242 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1243 sizeof(pat), &pat))
1244 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001245 }
1246
1247 nla_nest_end(msg, nl_wowlan);
1248 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001249#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001250
Johannes Berg7527a782011-05-13 10:58:57 +02001251 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1252 dev->wiphy.software_iftypes))
1253 goto nla_put_failure;
1254
1255 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1256 goto nla_put_failure;
1257
David S. Miller9360ffd2012-03-29 04:41:26 -04001258 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1259 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1260 dev->wiphy.ap_sme_capa))
1261 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001262
David S. Miller9360ffd2012-03-29 04:41:26 -04001263 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1264 dev->wiphy.features))
1265 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001266
David S. Miller9360ffd2012-03-29 04:41:26 -04001267 if (dev->wiphy.ht_capa_mod_mask &&
1268 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1269 sizeof(*dev->wiphy.ht_capa_mod_mask),
1270 dev->wiphy.ht_capa_mod_mask))
1271 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001272
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05301273 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1274 dev->wiphy.max_acl_mac_addrs &&
1275 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1276 dev->wiphy.max_acl_mac_addrs))
1277 goto nla_put_failure;
1278
Johannes Berg55682962007-09-20 13:09:35 -04001279 return genlmsg_end(msg, hdr);
1280
1281 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001282 genlmsg_cancel(msg, hdr);
1283 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001284}
1285
1286static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1287{
1288 int idx = 0;
1289 int start = cb->args[0];
1290 struct cfg80211_registered_device *dev;
1291
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001292 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001293 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001294 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1295 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001296 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001297 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001298 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001299 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001300 dev) < 0) {
1301 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001302 break;
Julius Volzb4637272008-07-08 14:02:19 +02001303 }
Johannes Berg55682962007-09-20 13:09:35 -04001304 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001305 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001306
1307 cb->args[0] = idx;
1308
1309 return skb->len;
1310}
1311
1312static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1313{
1314 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001315 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001316
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001317 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001318 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001319 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001320
Eric W. Biederman15e47302012-09-07 20:12:54 +00001321 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001322 nlmsg_free(msg);
1323 return -ENOBUFS;
1324 }
Johannes Berg55682962007-09-20 13:09:35 -04001325
Johannes Berg134e6372009-07-10 09:51:34 +00001326 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001327}
1328
Jouni Malinen31888482008-10-30 16:59:24 +02001329static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1330 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1331 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1332 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1333 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1334 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1335};
1336
1337static int parse_txq_params(struct nlattr *tb[],
1338 struct ieee80211_txq_params *txq_params)
1339{
Johannes Berga3304b02012-03-28 11:04:24 +02001340 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001341 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1342 !tb[NL80211_TXQ_ATTR_AIFS])
1343 return -EINVAL;
1344
Johannes Berga3304b02012-03-28 11:04:24 +02001345 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001346 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1347 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1348 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1349 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1350
Johannes Berga3304b02012-03-28 11:04:24 +02001351 if (txq_params->ac >= NL80211_NUM_ACS)
1352 return -EINVAL;
1353
Jouni Malinen31888482008-10-30 16:59:24 +02001354 return 0;
1355}
1356
Johannes Bergf444de02010-05-05 15:25:02 +02001357static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1358{
1359 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001360 * You can only set the channel explicitly for WDS interfaces,
1361 * all others have their channel managed via their respective
1362 * "establish a connection" command (connect, join, ...)
1363 *
1364 * For AP/GO and mesh mode, the channel can be set with the
1365 * channel userspace API, but is only stored and passed to the
1366 * low-level driver when the AP starts or the mesh is joined.
1367 * This is for backward compatibility, userspace can also give
1368 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001369 *
1370 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001371 * whatever else is going on, so they have their own special
1372 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001373 */
1374 return !wdev ||
1375 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001376 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001377 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1378 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001379}
1380
Johannes Berg683b6d32012-11-08 21:25:48 +01001381static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1382 struct genl_info *info,
1383 struct cfg80211_chan_def *chandef)
1384{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301385 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001386
1387 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1388 return -EINVAL;
1389
1390 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1391
1392 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001393 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1394 chandef->center_freq1 = control_freq;
1395 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001396
1397 /* Primary channel not allowed */
1398 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1399 return -EINVAL;
1400
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001401 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1402 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001403
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001404 chantype = nla_get_u32(
1405 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1406
1407 switch (chantype) {
1408 case NL80211_CHAN_NO_HT:
1409 case NL80211_CHAN_HT20:
1410 case NL80211_CHAN_HT40PLUS:
1411 case NL80211_CHAN_HT40MINUS:
1412 cfg80211_chandef_create(chandef, chandef->chan,
1413 chantype);
1414 break;
1415 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001416 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001417 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001418 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1419 chandef->width =
1420 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1421 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1422 chandef->center_freq1 =
1423 nla_get_u32(
1424 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1425 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1426 chandef->center_freq2 =
1427 nla_get_u32(
1428 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1429 }
1430
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001431 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001432 return -EINVAL;
1433
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001434 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1435 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001436 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001437
Johannes Berg683b6d32012-11-08 21:25:48 +01001438 return 0;
1439}
1440
Johannes Bergf444de02010-05-05 15:25:02 +02001441static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1442 struct wireless_dev *wdev,
1443 struct genl_info *info)
1444{
Johannes Berg683b6d32012-11-08 21:25:48 +01001445 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001446 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001447 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1448
1449 if (wdev)
1450 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001451
Johannes Bergf444de02010-05-05 15:25:02 +02001452 if (!nl80211_can_set_dev_channel(wdev))
1453 return -EOPNOTSUPP;
1454
Johannes Berg683b6d32012-11-08 21:25:48 +01001455 result = nl80211_parse_chandef(rdev, info, &chandef);
1456 if (result)
1457 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001458
1459 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001460 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001461 case NL80211_IFTYPE_AP:
1462 case NL80211_IFTYPE_P2P_GO:
1463 if (wdev->beacon_interval) {
1464 result = -EBUSY;
1465 break;
1466 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001467 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001468 result = -EINVAL;
1469 break;
1470 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001471 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001472 result = 0;
1473 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001474 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001475 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001476 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001477 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001478 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001479 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001480 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001481 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001482 }
1483 mutex_unlock(&rdev->devlist_mtx);
1484
1485 return result;
1486}
1487
1488static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1489{
Johannes Berg4c476992010-10-04 21:36:35 +02001490 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1491 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001492
Johannes Berg4c476992010-10-04 21:36:35 +02001493 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001494}
1495
Bill Jordane8347eb2010-10-01 13:54:28 -04001496static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1497{
Johannes Berg43b19952010-10-07 13:10:30 +02001498 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1499 struct net_device *dev = info->user_ptr[1];
1500 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001501 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001502
1503 if (!info->attrs[NL80211_ATTR_MAC])
1504 return -EINVAL;
1505
Johannes Berg43b19952010-10-07 13:10:30 +02001506 if (netif_running(dev))
1507 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001508
Johannes Berg43b19952010-10-07 13:10:30 +02001509 if (!rdev->ops->set_wds_peer)
1510 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001511
Johannes Berg43b19952010-10-07 13:10:30 +02001512 if (wdev->iftype != NL80211_IFTYPE_WDS)
1513 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001514
1515 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001516 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001517}
1518
1519
Johannes Berg55682962007-09-20 13:09:35 -04001520static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1521{
1522 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001523 struct net_device *netdev = NULL;
1524 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001525 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001526 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001527 u32 changed;
1528 u8 retry_short = 0, retry_long = 0;
1529 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001530 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001531
Johannes Bergf444de02010-05-05 15:25:02 +02001532 /*
1533 * Try to find the wiphy and netdev. Normally this
1534 * function shouldn't need the netdev, but this is
1535 * done for backward compatibility -- previously
1536 * setting the channel was done per wiphy, but now
1537 * it is per netdev. Previous userland like hostapd
1538 * also passed a netdev to set_wiphy, so that it is
1539 * possible to let that go to the right netdev!
1540 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001541 mutex_lock(&cfg80211_mutex);
1542
Johannes Bergf444de02010-05-05 15:25:02 +02001543 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1544 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1545
1546 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1547 if (netdev && netdev->ieee80211_ptr) {
1548 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1549 mutex_lock(&rdev->mtx);
1550 } else
1551 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001552 }
1553
Johannes Bergf444de02010-05-05 15:25:02 +02001554 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001555 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1556 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001557 if (IS_ERR(rdev)) {
1558 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001559 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001560 }
1561 wdev = NULL;
1562 netdev = NULL;
1563 result = 0;
1564
1565 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001566 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001567 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001568
1569 /*
1570 * end workaround code, by now the rdev is available
1571 * and locked, and wdev may or may not be NULL.
1572 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001573
1574 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001575 result = cfg80211_dev_rename(
1576 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001577
1578 mutex_unlock(&cfg80211_mutex);
1579
1580 if (result)
1581 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001582
Jouni Malinen31888482008-10-30 16:59:24 +02001583 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1584 struct ieee80211_txq_params txq_params;
1585 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1586
1587 if (!rdev->ops->set_txq_params) {
1588 result = -EOPNOTSUPP;
1589 goto bad_res;
1590 }
1591
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001592 if (!netdev) {
1593 result = -EINVAL;
1594 goto bad_res;
1595 }
1596
Johannes Berg133a3ff2011-11-03 14:50:13 +01001597 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1598 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1599 result = -EINVAL;
1600 goto bad_res;
1601 }
1602
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001603 if (!netif_running(netdev)) {
1604 result = -ENETDOWN;
1605 goto bad_res;
1606 }
1607
Jouni Malinen31888482008-10-30 16:59:24 +02001608 nla_for_each_nested(nl_txq_params,
1609 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1610 rem_txq_params) {
1611 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1612 nla_data(nl_txq_params),
1613 nla_len(nl_txq_params),
1614 txq_params_policy);
1615 result = parse_txq_params(tb, &txq_params);
1616 if (result)
1617 goto bad_res;
1618
Hila Gonene35e4d22012-06-27 17:19:42 +03001619 result = rdev_set_txq_params(rdev, netdev,
1620 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001621 if (result)
1622 goto bad_res;
1623 }
1624 }
1625
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001626 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001627 result = __nl80211_set_channel(rdev,
1628 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1629 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001630 if (result)
1631 goto bad_res;
1632 }
1633
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001634 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001635 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001636 enum nl80211_tx_power_setting type;
1637 int idx, mbm = 0;
1638
Johannes Bergc8442112012-10-24 10:17:18 +02001639 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1640 txp_wdev = NULL;
1641
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001642 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001643 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001644 goto bad_res;
1645 }
1646
1647 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1648 type = nla_get_u32(info->attrs[idx]);
1649
1650 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1651 (type != NL80211_TX_POWER_AUTOMATIC)) {
1652 result = -EINVAL;
1653 goto bad_res;
1654 }
1655
1656 if (type != NL80211_TX_POWER_AUTOMATIC) {
1657 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1658 mbm = nla_get_u32(info->attrs[idx]);
1659 }
1660
Johannes Bergc8442112012-10-24 10:17:18 +02001661 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001662 if (result)
1663 goto bad_res;
1664 }
1665
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001666 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1667 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1668 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001669 if ((!rdev->wiphy.available_antennas_tx &&
1670 !rdev->wiphy.available_antennas_rx) ||
1671 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001672 result = -EOPNOTSUPP;
1673 goto bad_res;
1674 }
1675
1676 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1677 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1678
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001679 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001680 * available antenna masks, except for the "all" mask */
1681 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1682 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001683 result = -EINVAL;
1684 goto bad_res;
1685 }
1686
Bruno Randolf7f531e02010-12-16 11:30:22 +09001687 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1688 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001689
Hila Gonene35e4d22012-06-27 17:19:42 +03001690 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001691 if (result)
1692 goto bad_res;
1693 }
1694
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001695 changed = 0;
1696
1697 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1698 retry_short = nla_get_u8(
1699 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1700 if (retry_short == 0) {
1701 result = -EINVAL;
1702 goto bad_res;
1703 }
1704 changed |= WIPHY_PARAM_RETRY_SHORT;
1705 }
1706
1707 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1708 retry_long = nla_get_u8(
1709 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1710 if (retry_long == 0) {
1711 result = -EINVAL;
1712 goto bad_res;
1713 }
1714 changed |= WIPHY_PARAM_RETRY_LONG;
1715 }
1716
1717 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1718 frag_threshold = nla_get_u32(
1719 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1720 if (frag_threshold < 256) {
1721 result = -EINVAL;
1722 goto bad_res;
1723 }
1724 if (frag_threshold != (u32) -1) {
1725 /*
1726 * Fragments (apart from the last one) are required to
1727 * have even length. Make the fragmentation code
1728 * simpler by stripping LSB should someone try to use
1729 * odd threshold value.
1730 */
1731 frag_threshold &= ~0x1;
1732 }
1733 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1734 }
1735
1736 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1737 rts_threshold = nla_get_u32(
1738 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1739 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1740 }
1741
Lukáš Turek81077e82009-12-21 22:50:47 +01001742 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1743 coverage_class = nla_get_u8(
1744 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1745 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1746 }
1747
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001748 if (changed) {
1749 u8 old_retry_short, old_retry_long;
1750 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001751 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001752
1753 if (!rdev->ops->set_wiphy_params) {
1754 result = -EOPNOTSUPP;
1755 goto bad_res;
1756 }
1757
1758 old_retry_short = rdev->wiphy.retry_short;
1759 old_retry_long = rdev->wiphy.retry_long;
1760 old_frag_threshold = rdev->wiphy.frag_threshold;
1761 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001762 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001763
1764 if (changed & WIPHY_PARAM_RETRY_SHORT)
1765 rdev->wiphy.retry_short = retry_short;
1766 if (changed & WIPHY_PARAM_RETRY_LONG)
1767 rdev->wiphy.retry_long = retry_long;
1768 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1769 rdev->wiphy.frag_threshold = frag_threshold;
1770 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1771 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001772 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1773 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001774
Hila Gonene35e4d22012-06-27 17:19:42 +03001775 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001776 if (result) {
1777 rdev->wiphy.retry_short = old_retry_short;
1778 rdev->wiphy.retry_long = old_retry_long;
1779 rdev->wiphy.frag_threshold = old_frag_threshold;
1780 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001781 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001782 }
1783 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001784
Johannes Berg306d6112008-12-08 12:39:04 +01001785 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001786 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001787 if (netdev)
1788 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001789 return result;
1790}
1791
Johannes Berg71bbc992012-06-15 15:30:18 +02001792static inline u64 wdev_id(struct wireless_dev *wdev)
1793{
1794 return (u64)wdev->identifier |
1795 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1796}
Johannes Berg55682962007-09-20 13:09:35 -04001797
Johannes Berg683b6d32012-11-08 21:25:48 +01001798static int nl80211_send_chandef(struct sk_buff *msg,
1799 struct cfg80211_chan_def *chandef)
1800{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001801 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001802
Johannes Berg683b6d32012-11-08 21:25:48 +01001803 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1804 chandef->chan->center_freq))
1805 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001806 switch (chandef->width) {
1807 case NL80211_CHAN_WIDTH_20_NOHT:
1808 case NL80211_CHAN_WIDTH_20:
1809 case NL80211_CHAN_WIDTH_40:
1810 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1811 cfg80211_get_chandef_type(chandef)))
1812 return -ENOBUFS;
1813 break;
1814 default:
1815 break;
1816 }
1817 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1818 return -ENOBUFS;
1819 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1820 return -ENOBUFS;
1821 if (chandef->center_freq2 &&
1822 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001823 return -ENOBUFS;
1824 return 0;
1825}
1826
Eric W. Biederman15e47302012-09-07 20:12:54 +00001827static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001828 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001829 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001830{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001831 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001832 void *hdr;
1833
Eric W. Biederman15e47302012-09-07 20:12:54 +00001834 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001835 if (!hdr)
1836 return -1;
1837
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001838 if (dev &&
1839 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001840 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001841 goto nla_put_failure;
1842
1843 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1844 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001845 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001846 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001847 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1848 rdev->devlist_generation ^
1849 (cfg80211_rdev_list_generation << 2)))
1850 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001851
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001852 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001853 int ret;
1854 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001855
Johannes Berg683b6d32012-11-08 21:25:48 +01001856 ret = rdev_get_channel(rdev, wdev, &chandef);
1857 if (ret == 0) {
1858 if (nl80211_send_chandef(msg, &chandef))
1859 goto nla_put_failure;
1860 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001861 }
1862
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001863 if (wdev->ssid_len) {
1864 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1865 goto nla_put_failure;
1866 }
1867
Johannes Berg55682962007-09-20 13:09:35 -04001868 return genlmsg_end(msg, hdr);
1869
1870 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001871 genlmsg_cancel(msg, hdr);
1872 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001873}
1874
1875static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1876{
1877 int wp_idx = 0;
1878 int if_idx = 0;
1879 int wp_start = cb->args[0];
1880 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001881 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001882 struct wireless_dev *wdev;
1883
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001884 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001885 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1886 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001887 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001888 if (wp_idx < wp_start) {
1889 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001890 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001891 }
Johannes Berg55682962007-09-20 13:09:35 -04001892 if_idx = 0;
1893
Johannes Bergf5ea9122009-08-07 16:17:38 +02001894 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001895 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001896 if (if_idx < if_start) {
1897 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001898 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001899 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001900 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001901 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001902 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001903 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001904 goto out;
1905 }
1906 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001907 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001908 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001909
1910 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001911 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001912 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001913 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001914
1915 cb->args[0] = wp_idx;
1916 cb->args[1] = if_idx;
1917
1918 return skb->len;
1919}
1920
1921static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1922{
1923 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001924 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001925 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001926
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001927 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001928 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001929 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001930
Eric W. Biederman15e47302012-09-07 20:12:54 +00001931 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001932 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001933 nlmsg_free(msg);
1934 return -ENOBUFS;
1935 }
Johannes Berg55682962007-09-20 13:09:35 -04001936
Johannes Berg134e6372009-07-10 09:51:34 +00001937 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001938}
1939
Michael Wu66f7ac52008-01-31 19:48:22 +01001940static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1941 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1942 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1943 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1944 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1945 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1946};
1947
1948static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1949{
1950 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1951 int flag;
1952
1953 *mntrflags = 0;
1954
1955 if (!nla)
1956 return -EINVAL;
1957
1958 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1959 nla, mntr_flags_policy))
1960 return -EINVAL;
1961
1962 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1963 if (flags[flag])
1964 *mntrflags |= (1<<flag);
1965
1966 return 0;
1967}
1968
Johannes Berg9bc383d2009-11-19 11:55:19 +01001969static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001970 struct net_device *netdev, u8 use_4addr,
1971 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001972{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001973 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001974 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001975 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001976 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001977 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001978
1979 switch (iftype) {
1980 case NL80211_IFTYPE_AP_VLAN:
1981 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1982 return 0;
1983 break;
1984 case NL80211_IFTYPE_STATION:
1985 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1986 return 0;
1987 break;
1988 default:
1989 break;
1990 }
1991
1992 return -EOPNOTSUPP;
1993}
1994
Johannes Berg55682962007-09-20 13:09:35 -04001995static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1996{
Johannes Berg4c476992010-10-04 21:36:35 +02001997 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001998 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001999 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002000 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002001 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002002 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002003 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002004
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002005 memset(&params, 0, sizeof(params));
2006
Johannes Berg04a773a2009-04-19 21:24:32 +02002007 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002008
Johannes Berg723b0382008-09-16 20:22:09 +02002009 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002010 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002011 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002012 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002013 if (ntype > NL80211_IFTYPE_MAX)
2014 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002015 }
2016
Johannes Berg92ffe052008-09-16 20:39:36 +02002017 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002018 struct wireless_dev *wdev = dev->ieee80211_ptr;
2019
Johannes Berg4c476992010-10-04 21:36:35 +02002020 if (ntype != NL80211_IFTYPE_MESH_POINT)
2021 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002022 if (netif_running(dev))
2023 return -EBUSY;
2024
2025 wdev_lock(wdev);
2026 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2027 IEEE80211_MAX_MESH_ID_LEN);
2028 wdev->mesh_id_up_len =
2029 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2030 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2031 wdev->mesh_id_up_len);
2032 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002033 }
2034
Felix Fietkau8b787642009-11-10 18:53:10 +01002035 if (info->attrs[NL80211_ATTR_4ADDR]) {
2036 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2037 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002038 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002039 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002040 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002041 } else {
2042 params.use_4addr = -1;
2043 }
2044
Johannes Berg92ffe052008-09-16 20:39:36 +02002045 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002046 if (ntype != NL80211_IFTYPE_MONITOR)
2047 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002048 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2049 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002050 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002051 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002052
2053 flags = &_flags;
2054 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002055 }
Johannes Berg3b858752009-03-12 09:55:09 +01002056
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002057 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002058 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002059 else
2060 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002061
Johannes Berg9bc383d2009-11-19 11:55:19 +01002062 if (!err && params.use_4addr != -1)
2063 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2064
Johannes Berg55682962007-09-20 13:09:35 -04002065 return err;
2066}
2067
2068static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2069{
Johannes Berg4c476992010-10-04 21:36:35 +02002070 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002071 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002072 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002073 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002074 int err;
2075 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002076 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002077
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002078 memset(&params, 0, sizeof(params));
2079
Johannes Berg55682962007-09-20 13:09:35 -04002080 if (!info->attrs[NL80211_ATTR_IFNAME])
2081 return -EINVAL;
2082
2083 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2084 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2085 if (type > NL80211_IFTYPE_MAX)
2086 return -EINVAL;
2087 }
2088
Johannes Berg79c97e92009-07-07 03:56:12 +02002089 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002090 !(rdev->wiphy.interface_modes & (1 << type)))
2091 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002092
Arend van Spriel1c18f142013-01-08 10:17:27 +01002093 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2094 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2095 ETH_ALEN);
2096 if (!is_valid_ether_addr(params.macaddr))
2097 return -EADDRNOTAVAIL;
2098 }
2099
Johannes Berg9bc383d2009-11-19 11:55:19 +01002100 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002101 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002102 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002103 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002104 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002105 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002106
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002107 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2108 if (!msg)
2109 return -ENOMEM;
2110
Michael Wu66f7ac52008-01-31 19:48:22 +01002111 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2112 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2113 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002114 wdev = rdev_add_virtual_intf(rdev,
2115 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2116 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002117 if (IS_ERR(wdev)) {
2118 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002119 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002120 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002121
Johannes Berg98104fde2012-06-16 00:19:54 +02002122 switch (type) {
2123 case NL80211_IFTYPE_MESH_POINT:
2124 if (!info->attrs[NL80211_ATTR_MESH_ID])
2125 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002126 wdev_lock(wdev);
2127 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2128 IEEE80211_MAX_MESH_ID_LEN);
2129 wdev->mesh_id_up_len =
2130 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2131 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2132 wdev->mesh_id_up_len);
2133 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002134 break;
2135 case NL80211_IFTYPE_P2P_DEVICE:
2136 /*
2137 * P2P Device doesn't have a netdev, so doesn't go
2138 * through the netdev notifier and must be added here
2139 */
2140 mutex_init(&wdev->mtx);
2141 INIT_LIST_HEAD(&wdev->event_list);
2142 spin_lock_init(&wdev->event_lock);
2143 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2144 spin_lock_init(&wdev->mgmt_registrations_lock);
2145
2146 mutex_lock(&rdev->devlist_mtx);
2147 wdev->identifier = ++rdev->wdev_id;
2148 list_add_rcu(&wdev->list, &rdev->wdev_list);
2149 rdev->devlist_generation++;
2150 mutex_unlock(&rdev->devlist_mtx);
2151 break;
2152 default:
2153 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002154 }
2155
Eric W. Biederman15e47302012-09-07 20:12:54 +00002156 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002157 rdev, wdev) < 0) {
2158 nlmsg_free(msg);
2159 return -ENOBUFS;
2160 }
2161
2162 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002163}
2164
2165static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2166{
Johannes Berg4c476992010-10-04 21:36:35 +02002167 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002168 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002169
Johannes Berg4c476992010-10-04 21:36:35 +02002170 if (!rdev->ops->del_virtual_intf)
2171 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002172
Johannes Berg84efbb82012-06-16 00:00:26 +02002173 /*
2174 * If we remove a wireless device without a netdev then clear
2175 * user_ptr[1] so that nl80211_post_doit won't dereference it
2176 * to check if it needs to do dev_put(). Otherwise it crashes
2177 * since the wdev has been freed, unlike with a netdev where
2178 * we need the dev_put() for the netdev to really be freed.
2179 */
2180 if (!wdev->netdev)
2181 info->user_ptr[1] = NULL;
2182
Hila Gonene35e4d22012-06-27 17:19:42 +03002183 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002184}
2185
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002186static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2187{
2188 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2189 struct net_device *dev = info->user_ptr[1];
2190 u16 noack_map;
2191
2192 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2193 return -EINVAL;
2194
2195 if (!rdev->ops->set_noack_map)
2196 return -EOPNOTSUPP;
2197
2198 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2199
Hila Gonene35e4d22012-06-27 17:19:42 +03002200 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002201}
2202
Johannes Berg41ade002007-12-19 02:03:29 +01002203struct get_key_cookie {
2204 struct sk_buff *msg;
2205 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002206 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002207};
2208
2209static void get_key_callback(void *c, struct key_params *params)
2210{
Johannes Bergb9454e82009-07-08 13:29:08 +02002211 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002212 struct get_key_cookie *cookie = c;
2213
David S. Miller9360ffd2012-03-29 04:41:26 -04002214 if ((params->key &&
2215 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2216 params->key_len, params->key)) ||
2217 (params->seq &&
2218 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2219 params->seq_len, params->seq)) ||
2220 (params->cipher &&
2221 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2222 params->cipher)))
2223 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002224
Johannes Bergb9454e82009-07-08 13:29:08 +02002225 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2226 if (!key)
2227 goto nla_put_failure;
2228
David S. Miller9360ffd2012-03-29 04:41:26 -04002229 if ((params->key &&
2230 nla_put(cookie->msg, NL80211_KEY_DATA,
2231 params->key_len, params->key)) ||
2232 (params->seq &&
2233 nla_put(cookie->msg, NL80211_KEY_SEQ,
2234 params->seq_len, params->seq)) ||
2235 (params->cipher &&
2236 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2237 params->cipher)))
2238 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002239
David S. Miller9360ffd2012-03-29 04:41:26 -04002240 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2241 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002242
2243 nla_nest_end(cookie->msg, key);
2244
Johannes Berg41ade002007-12-19 02:03:29 +01002245 return;
2246 nla_put_failure:
2247 cookie->error = 1;
2248}
2249
2250static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2251{
Johannes Berg4c476992010-10-04 21:36:35 +02002252 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002253 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002254 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002255 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002256 const u8 *mac_addr = NULL;
2257 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002258 struct get_key_cookie cookie = {
2259 .error = 0,
2260 };
2261 void *hdr;
2262 struct sk_buff *msg;
2263
2264 if (info->attrs[NL80211_ATTR_KEY_IDX])
2265 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2266
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002267 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002268 return -EINVAL;
2269
2270 if (info->attrs[NL80211_ATTR_MAC])
2271 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2272
Johannes Berge31b8212010-10-05 19:39:30 +02002273 pairwise = !!mac_addr;
2274 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2275 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2276 if (kt >= NUM_NL80211_KEYTYPES)
2277 return -EINVAL;
2278 if (kt != NL80211_KEYTYPE_GROUP &&
2279 kt != NL80211_KEYTYPE_PAIRWISE)
2280 return -EINVAL;
2281 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2282 }
2283
Johannes Berg4c476992010-10-04 21:36:35 +02002284 if (!rdev->ops->get_key)
2285 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002286
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002288 if (!msg)
2289 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002290
Eric W. Biederman15e47302012-09-07 20:12:54 +00002291 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002292 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002293 if (IS_ERR(hdr))
2294 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002295
2296 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002297 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002298
David S. Miller9360ffd2012-03-29 04:41:26 -04002299 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2300 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2301 goto nla_put_failure;
2302 if (mac_addr &&
2303 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2304 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002305
Johannes Berge31b8212010-10-05 19:39:30 +02002306 if (pairwise && mac_addr &&
2307 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2308 return -ENOENT;
2309
Hila Gonene35e4d22012-06-27 17:19:42 +03002310 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2311 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002312
2313 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002314 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002315
2316 if (cookie.error)
2317 goto nla_put_failure;
2318
2319 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002320 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002321
2322 nla_put_failure:
2323 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002324 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002325 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002326 return err;
2327}
2328
2329static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2330{
Johannes Berg4c476992010-10-04 21:36:35 +02002331 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002332 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002333 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002334 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002335
Johannes Bergb9454e82009-07-08 13:29:08 +02002336 err = nl80211_parse_key(info, &key);
2337 if (err)
2338 return err;
2339
2340 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002341 return -EINVAL;
2342
Johannes Bergb9454e82009-07-08 13:29:08 +02002343 /* only support setting default key */
2344 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002345 return -EINVAL;
2346
Johannes Bergfffd0932009-07-08 14:22:54 +02002347 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002348
2349 if (key.def) {
2350 if (!rdev->ops->set_default_key) {
2351 err = -EOPNOTSUPP;
2352 goto out;
2353 }
2354
2355 err = nl80211_key_allowed(dev->ieee80211_ptr);
2356 if (err)
2357 goto out;
2358
Hila Gonene35e4d22012-06-27 17:19:42 +03002359 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002360 key.def_uni, key.def_multi);
2361
2362 if (err)
2363 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002364
Johannes Berg3d23e342009-09-29 23:27:28 +02002365#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002366 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002367#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002368 } else {
2369 if (key.def_uni || !key.def_multi) {
2370 err = -EINVAL;
2371 goto out;
2372 }
2373
2374 if (!rdev->ops->set_default_mgmt_key) {
2375 err = -EOPNOTSUPP;
2376 goto out;
2377 }
2378
2379 err = nl80211_key_allowed(dev->ieee80211_ptr);
2380 if (err)
2381 goto out;
2382
Hila Gonene35e4d22012-06-27 17:19:42 +03002383 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002384 if (err)
2385 goto out;
2386
2387#ifdef CONFIG_CFG80211_WEXT
2388 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2389#endif
2390 }
2391
2392 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002393 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002394
Johannes Berg41ade002007-12-19 02:03:29 +01002395 return err;
2396}
2397
2398static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2399{
Johannes Berg4c476992010-10-04 21:36:35 +02002400 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002401 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002402 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002403 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002404 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002405
Johannes Bergb9454e82009-07-08 13:29:08 +02002406 err = nl80211_parse_key(info, &key);
2407 if (err)
2408 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002409
Johannes Bergb9454e82009-07-08 13:29:08 +02002410 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002411 return -EINVAL;
2412
Johannes Berg41ade002007-12-19 02:03:29 +01002413 if (info->attrs[NL80211_ATTR_MAC])
2414 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2415
Johannes Berge31b8212010-10-05 19:39:30 +02002416 if (key.type == -1) {
2417 if (mac_addr)
2418 key.type = NL80211_KEYTYPE_PAIRWISE;
2419 else
2420 key.type = NL80211_KEYTYPE_GROUP;
2421 }
2422
2423 /* for now */
2424 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2425 key.type != NL80211_KEYTYPE_GROUP)
2426 return -EINVAL;
2427
Johannes Berg4c476992010-10-04 21:36:35 +02002428 if (!rdev->ops->add_key)
2429 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002430
Johannes Berge31b8212010-10-05 19:39:30 +02002431 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2432 key.type == NL80211_KEYTYPE_PAIRWISE,
2433 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002434 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002435
2436 wdev_lock(dev->ieee80211_ptr);
2437 err = nl80211_key_allowed(dev->ieee80211_ptr);
2438 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002439 err = rdev_add_key(rdev, dev, key.idx,
2440 key.type == NL80211_KEYTYPE_PAIRWISE,
2441 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002442 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002443
Johannes Berg41ade002007-12-19 02:03:29 +01002444 return err;
2445}
2446
2447static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2448{
Johannes Berg4c476992010-10-04 21:36:35 +02002449 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002450 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002451 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002452 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002453 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002454
Johannes Bergb9454e82009-07-08 13:29:08 +02002455 err = nl80211_parse_key(info, &key);
2456 if (err)
2457 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002458
2459 if (info->attrs[NL80211_ATTR_MAC])
2460 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2461
Johannes Berge31b8212010-10-05 19:39:30 +02002462 if (key.type == -1) {
2463 if (mac_addr)
2464 key.type = NL80211_KEYTYPE_PAIRWISE;
2465 else
2466 key.type = NL80211_KEYTYPE_GROUP;
2467 }
2468
2469 /* for now */
2470 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2471 key.type != NL80211_KEYTYPE_GROUP)
2472 return -EINVAL;
2473
Johannes Berg4c476992010-10-04 21:36:35 +02002474 if (!rdev->ops->del_key)
2475 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002476
Johannes Bergfffd0932009-07-08 14:22:54 +02002477 wdev_lock(dev->ieee80211_ptr);
2478 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002479
2480 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2481 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2482 err = -ENOENT;
2483
Johannes Bergfffd0932009-07-08 14:22:54 +02002484 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002485 err = rdev_del_key(rdev, dev, key.idx,
2486 key.type == NL80211_KEYTYPE_PAIRWISE,
2487 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002488
Johannes Berg3d23e342009-09-29 23:27:28 +02002489#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002490 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002491 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002492 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002493 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002494 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2495 }
2496#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002497 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002498
Johannes Berg41ade002007-12-19 02:03:29 +01002499 return err;
2500}
2501
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302502/* This function returns an error or the number of nested attributes */
2503static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2504{
2505 struct nlattr *attr;
2506 int n_entries = 0, tmp;
2507
2508 nla_for_each_nested(attr, nl_attr, tmp) {
2509 if (nla_len(attr) != ETH_ALEN)
2510 return -EINVAL;
2511
2512 n_entries++;
2513 }
2514
2515 return n_entries;
2516}
2517
2518/*
2519 * This function parses ACL information and allocates memory for ACL data.
2520 * On successful return, the calling function is responsible to free the
2521 * ACL buffer returned by this function.
2522 */
2523static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2524 struct genl_info *info)
2525{
2526 enum nl80211_acl_policy acl_policy;
2527 struct nlattr *attr;
2528 struct cfg80211_acl_data *acl;
2529 int i = 0, n_entries, tmp;
2530
2531 if (!wiphy->max_acl_mac_addrs)
2532 return ERR_PTR(-EOPNOTSUPP);
2533
2534 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2535 return ERR_PTR(-EINVAL);
2536
2537 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2538 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2539 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2540 return ERR_PTR(-EINVAL);
2541
2542 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2543 return ERR_PTR(-EINVAL);
2544
2545 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2546 if (n_entries < 0)
2547 return ERR_PTR(n_entries);
2548
2549 if (n_entries > wiphy->max_acl_mac_addrs)
2550 return ERR_PTR(-ENOTSUPP);
2551
2552 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2553 GFP_KERNEL);
2554 if (!acl)
2555 return ERR_PTR(-ENOMEM);
2556
2557 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2558 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2559 i++;
2560 }
2561
2562 acl->n_acl_entries = n_entries;
2563 acl->acl_policy = acl_policy;
2564
2565 return acl;
2566}
2567
2568static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2569{
2570 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2571 struct net_device *dev = info->user_ptr[1];
2572 struct cfg80211_acl_data *acl;
2573 int err;
2574
2575 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2576 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2577 return -EOPNOTSUPP;
2578
2579 if (!dev->ieee80211_ptr->beacon_interval)
2580 return -EINVAL;
2581
2582 acl = parse_acl_data(&rdev->wiphy, info);
2583 if (IS_ERR(acl))
2584 return PTR_ERR(acl);
2585
2586 err = rdev_set_mac_acl(rdev, dev, acl);
2587
2588 kfree(acl);
2589
2590 return err;
2591}
2592
Johannes Berg88600202012-02-13 15:17:18 +01002593static int nl80211_parse_beacon(struct genl_info *info,
2594 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002595{
Johannes Berg88600202012-02-13 15:17:18 +01002596 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002597
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002598 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2599 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2600 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2601 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002602 return -EINVAL;
2603
Johannes Berg88600202012-02-13 15:17:18 +01002604 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002605
Johannes Berged1b6cc2007-12-19 02:03:32 +01002606 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002607 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2608 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2609 if (!bcn->head_len)
2610 return -EINVAL;
2611 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002612 }
2613
2614 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002615 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2616 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002617 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002618 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002619 }
2620
Johannes Berg4c476992010-10-04 21:36:35 +02002621 if (!haveinfo)
2622 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002623
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002624 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002625 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2626 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002627 }
2628
2629 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002630 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002631 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002632 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002633 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2634 }
2635
2636 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002637 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002638 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002639 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002640 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2641 }
2642
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002643 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002644 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002645 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002646 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002647 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2648 }
2649
Johannes Berg88600202012-02-13 15:17:18 +01002650 return 0;
2651}
2652
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002653static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2654 struct cfg80211_ap_settings *params)
2655{
2656 struct wireless_dev *wdev;
2657 bool ret = false;
2658
2659 mutex_lock(&rdev->devlist_mtx);
2660
Johannes Berg89a54e42012-06-15 14:33:17 +02002661 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002662 if (wdev->iftype != NL80211_IFTYPE_AP &&
2663 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2664 continue;
2665
Johannes Berg683b6d32012-11-08 21:25:48 +01002666 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002667 continue;
2668
Johannes Berg683b6d32012-11-08 21:25:48 +01002669 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002670 ret = true;
2671 break;
2672 }
2673
2674 mutex_unlock(&rdev->devlist_mtx);
2675
2676 return ret;
2677}
2678
Jouni Malinene39e5b52012-09-30 19:29:39 +03002679static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2680 enum nl80211_auth_type auth_type,
2681 enum nl80211_commands cmd)
2682{
2683 if (auth_type > NL80211_AUTHTYPE_MAX)
2684 return false;
2685
2686 switch (cmd) {
2687 case NL80211_CMD_AUTHENTICATE:
2688 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2689 auth_type == NL80211_AUTHTYPE_SAE)
2690 return false;
2691 return true;
2692 case NL80211_CMD_CONNECT:
2693 case NL80211_CMD_START_AP:
2694 /* SAE not supported yet */
2695 if (auth_type == NL80211_AUTHTYPE_SAE)
2696 return false;
2697 return true;
2698 default:
2699 return false;
2700 }
2701}
2702
Johannes Berg88600202012-02-13 15:17:18 +01002703static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2704{
2705 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2706 struct net_device *dev = info->user_ptr[1];
2707 struct wireless_dev *wdev = dev->ieee80211_ptr;
2708 struct cfg80211_ap_settings params;
2709 int err;
2710
2711 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2712 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2713 return -EOPNOTSUPP;
2714
2715 if (!rdev->ops->start_ap)
2716 return -EOPNOTSUPP;
2717
2718 if (wdev->beacon_interval)
2719 return -EALREADY;
2720
2721 memset(&params, 0, sizeof(params));
2722
2723 /* these are required for START_AP */
2724 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2725 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2726 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2727 return -EINVAL;
2728
2729 err = nl80211_parse_beacon(info, &params.beacon);
2730 if (err)
2731 return err;
2732
2733 params.beacon_interval =
2734 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2735 params.dtim_period =
2736 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2737
2738 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2739 if (err)
2740 return err;
2741
2742 /*
2743 * In theory, some of these attributes should be required here
2744 * but since they were not used when the command was originally
2745 * added, keep them optional for old user space programs to let
2746 * them continue to work with drivers that do not need the
2747 * additional information -- drivers must check!
2748 */
2749 if (info->attrs[NL80211_ATTR_SSID]) {
2750 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2751 params.ssid_len =
2752 nla_len(info->attrs[NL80211_ATTR_SSID]);
2753 if (params.ssid_len == 0 ||
2754 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2755 return -EINVAL;
2756 }
2757
2758 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2759 params.hidden_ssid = nla_get_u32(
2760 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2761 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2762 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2763 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2764 return -EINVAL;
2765 }
2766
2767 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2768
2769 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2770 params.auth_type = nla_get_u32(
2771 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002772 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2773 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002774 return -EINVAL;
2775 } else
2776 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2777
2778 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2779 NL80211_MAX_NR_CIPHER_SUITES);
2780 if (err)
2781 return err;
2782
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302783 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2784 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2785 return -EOPNOTSUPP;
2786 params.inactivity_timeout = nla_get_u16(
2787 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2788 }
2789
Johannes Berg53cabad2012-11-14 15:17:28 +01002790 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2791 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2792 return -EINVAL;
2793 params.p2p_ctwindow =
2794 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2795 if (params.p2p_ctwindow > 127)
2796 return -EINVAL;
2797 if (params.p2p_ctwindow != 0 &&
2798 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2799 return -EINVAL;
2800 }
2801
2802 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2803 u8 tmp;
2804
2805 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2806 return -EINVAL;
2807 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2808 if (tmp > 1)
2809 return -EINVAL;
2810 params.p2p_opp_ps = tmp;
2811 if (params.p2p_opp_ps != 0 &&
2812 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2813 return -EINVAL;
2814 }
2815
Johannes Bergaa430da2012-05-16 23:50:18 +02002816 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002817 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2818 if (err)
2819 return err;
2820 } else if (wdev->preset_chandef.chan) {
2821 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002822 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002823 return -EINVAL;
2824
Johannes Berg683b6d32012-11-08 21:25:48 +01002825 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002826 return -EINVAL;
2827
Michal Kaziore4e32452012-06-29 12:47:08 +02002828 mutex_lock(&rdev->devlist_mtx);
Johannes Berg683b6d32012-11-08 21:25:48 +01002829 err = cfg80211_can_use_chan(rdev, wdev, params.chandef.chan,
Michal Kaziore4e32452012-06-29 12:47:08 +02002830 CHAN_MODE_SHARED);
2831 mutex_unlock(&rdev->devlist_mtx);
2832
2833 if (err)
2834 return err;
2835
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302836 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
2837 params.acl = parse_acl_data(&rdev->wiphy, info);
2838 if (IS_ERR(params.acl))
2839 return PTR_ERR(params.acl);
2840 }
2841
Hila Gonene35e4d22012-06-27 17:19:42 +03002842 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002843 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002844 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002845 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002846 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002847 wdev->ssid_len = params.ssid_len;
2848 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002849 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302850
2851 kfree(params.acl);
2852
Johannes Berg56d18932011-05-09 18:41:15 +02002853 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002854}
2855
Johannes Berg88600202012-02-13 15:17:18 +01002856static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2857{
2858 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2859 struct net_device *dev = info->user_ptr[1];
2860 struct wireless_dev *wdev = dev->ieee80211_ptr;
2861 struct cfg80211_beacon_data params;
2862 int err;
2863
2864 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2865 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2866 return -EOPNOTSUPP;
2867
2868 if (!rdev->ops->change_beacon)
2869 return -EOPNOTSUPP;
2870
2871 if (!wdev->beacon_interval)
2872 return -EINVAL;
2873
2874 err = nl80211_parse_beacon(info, &params);
2875 if (err)
2876 return err;
2877
Hila Gonene35e4d22012-06-27 17:19:42 +03002878 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002879}
2880
2881static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002882{
Johannes Berg4c476992010-10-04 21:36:35 +02002883 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2884 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002885
Michal Kazior60771782012-06-29 12:46:56 +02002886 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002887}
2888
Johannes Berg5727ef12007-12-19 02:03:34 +01002889static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2890 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2891 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2892 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002893 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002894 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002895 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002896};
2897
Johannes Bergeccb8e82009-05-11 21:57:56 +03002898static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002899 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002900 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002901{
2902 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002903 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002904 int flag;
2905
Johannes Bergeccb8e82009-05-11 21:57:56 +03002906 /*
2907 * Try parsing the new attribute first so userspace
2908 * can specify both for older kernels.
2909 */
2910 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2911 if (nla) {
2912 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002913
Johannes Bergeccb8e82009-05-11 21:57:56 +03002914 sta_flags = nla_data(nla);
2915 params->sta_flags_mask = sta_flags->mask;
2916 params->sta_flags_set = sta_flags->set;
2917 if ((params->sta_flags_mask |
2918 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2919 return -EINVAL;
2920 return 0;
2921 }
2922
2923 /* if present, parse the old attribute */
2924
2925 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002926 if (!nla)
2927 return 0;
2928
2929 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2930 nla, sta_flags_policy))
2931 return -EINVAL;
2932
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002933 /*
2934 * Only allow certain flags for interface types so that
2935 * other attributes are silently ignored. Remember that
2936 * this is backward compatibility code with old userspace
2937 * and shouldn't be hit in other cases anyway.
2938 */
2939 switch (iftype) {
2940 case NL80211_IFTYPE_AP:
2941 case NL80211_IFTYPE_AP_VLAN:
2942 case NL80211_IFTYPE_P2P_GO:
2943 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2944 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2945 BIT(NL80211_STA_FLAG_WME) |
2946 BIT(NL80211_STA_FLAG_MFP);
2947 break;
2948 case NL80211_IFTYPE_P2P_CLIENT:
2949 case NL80211_IFTYPE_STATION:
2950 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2951 BIT(NL80211_STA_FLAG_TDLS_PEER);
2952 break;
2953 case NL80211_IFTYPE_MESH_POINT:
2954 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2955 BIT(NL80211_STA_FLAG_MFP) |
2956 BIT(NL80211_STA_FLAG_AUTHORIZED);
2957 default:
2958 return -EINVAL;
2959 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002960
Johannes Berg3383b5a2012-05-10 20:14:43 +02002961 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2962 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002963 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002964
Johannes Berg3383b5a2012-05-10 20:14:43 +02002965 /* no longer support new API additions in old API */
2966 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2967 return -EINVAL;
2968 }
2969 }
2970
Johannes Berg5727ef12007-12-19 02:03:34 +01002971 return 0;
2972}
2973
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002974static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2975 int attr)
2976{
2977 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002978 u32 bitrate;
2979 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002980
2981 rate = nla_nest_start(msg, attr);
2982 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002983 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002984
2985 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2986 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002987 /* report 16-bit bitrate only if we can */
2988 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002989 if (bitrate > 0 &&
2990 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
2991 return false;
2992 if (bitrate_compat > 0 &&
2993 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
2994 return false;
2995
2996 if (info->flags & RATE_INFO_FLAGS_MCS) {
2997 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
2998 return false;
2999 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3000 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3001 return false;
3002 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3003 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3004 return false;
3005 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3006 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3007 return false;
3008 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3009 return false;
3010 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3011 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3012 return false;
3013 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3014 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3015 return false;
3016 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3017 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3018 return false;
3019 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3020 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3021 return false;
3022 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3023 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3024 return false;
3025 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003026
3027 nla_nest_end(msg, rate);
3028 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003029}
3030
Eric W. Biederman15e47302012-09-07 20:12:54 +00003031static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003032 int flags,
3033 struct cfg80211_registered_device *rdev,
3034 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003035 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003036{
3037 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003038 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003039
Eric W. Biederman15e47302012-09-07 20:12:54 +00003040 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003041 if (!hdr)
3042 return -1;
3043
David S. Miller9360ffd2012-03-29 04:41:26 -04003044 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3045 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3046 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3047 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003048
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003049 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3050 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003051 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003052 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3053 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3054 sinfo->connected_time))
3055 goto nla_put_failure;
3056 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3057 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3058 sinfo->inactive_time))
3059 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003060 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3061 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003062 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003063 (u32)sinfo->rx_bytes))
3064 goto nla_put_failure;
3065 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3066 NL80211_STA_INFO_TX_BYTES64)) &&
3067 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3068 (u32)sinfo->tx_bytes))
3069 goto nla_put_failure;
3070 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3071 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003072 sinfo->rx_bytes))
3073 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003074 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3075 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003076 sinfo->tx_bytes))
3077 goto nla_put_failure;
3078 if ((sinfo->filled & STATION_INFO_LLID) &&
3079 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3080 goto nla_put_failure;
3081 if ((sinfo->filled & STATION_INFO_PLID) &&
3082 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3083 goto nla_put_failure;
3084 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3085 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3086 sinfo->plink_state))
3087 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003088 switch (rdev->wiphy.signal_type) {
3089 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003090 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3091 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3092 sinfo->signal))
3093 goto nla_put_failure;
3094 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3095 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3096 sinfo->signal_avg))
3097 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003098 break;
3099 default:
3100 break;
3101 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003102 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003103 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3104 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003105 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003106 }
3107 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3108 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3109 NL80211_STA_INFO_RX_BITRATE))
3110 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003111 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003112 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3113 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3114 sinfo->rx_packets))
3115 goto nla_put_failure;
3116 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3117 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3118 sinfo->tx_packets))
3119 goto nla_put_failure;
3120 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3121 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3122 sinfo->tx_retries))
3123 goto nla_put_failure;
3124 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3125 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3126 sinfo->tx_failed))
3127 goto nla_put_failure;
3128 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3129 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3130 sinfo->beacon_loss_count))
3131 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003132 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3133 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3134 sinfo->local_pm))
3135 goto nla_put_failure;
3136 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3137 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3138 sinfo->peer_pm))
3139 goto nla_put_failure;
3140 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3141 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3142 sinfo->nonpeer_pm))
3143 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003144 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3145 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3146 if (!bss_param)
3147 goto nla_put_failure;
3148
David S. Miller9360ffd2012-03-29 04:41:26 -04003149 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3150 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3151 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3152 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3153 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3154 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3155 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3156 sinfo->bss_param.dtim_period) ||
3157 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3158 sinfo->bss_param.beacon_interval))
3159 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003160
3161 nla_nest_end(msg, bss_param);
3162 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003163 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3164 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3165 sizeof(struct nl80211_sta_flag_update),
3166 &sinfo->sta_flags))
3167 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003168 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3169 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3170 sinfo->t_offset))
3171 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003172 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003173
David S. Miller9360ffd2012-03-29 04:41:26 -04003174 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3175 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3176 sinfo->assoc_req_ies))
3177 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003178
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003179 return genlmsg_end(msg, hdr);
3180
3181 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003182 genlmsg_cancel(msg, hdr);
3183 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003184}
3185
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003186static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003187 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003188{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003189 struct station_info sinfo;
3190 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003191 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003192 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003193 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003194 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003195
Johannes Berg67748892010-10-04 21:14:06 +02003196 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3197 if (err)
3198 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003199
3200 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003201 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003202 goto out_err;
3203 }
3204
Johannes Bergbba95fe2008-07-29 13:22:51 +02003205 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003206 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003207 err = rdev_dump_station(dev, netdev, sta_idx,
3208 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003209 if (err == -ENOENT)
3210 break;
3211 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003212 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003213
3214 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003215 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003216 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003217 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003218 &sinfo) < 0)
3219 goto out;
3220
3221 sta_idx++;
3222 }
3223
3224
3225 out:
3226 cb->args[1] = sta_idx;
3227 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003228 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003229 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003230
3231 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003232}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003233
Johannes Berg5727ef12007-12-19 02:03:34 +01003234static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3235{
Johannes Berg4c476992010-10-04 21:36:35 +02003236 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3237 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003238 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003239 struct sk_buff *msg;
3240 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003241 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003242
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003243 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003244
3245 if (!info->attrs[NL80211_ATTR_MAC])
3246 return -EINVAL;
3247
3248 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3249
Johannes Berg4c476992010-10-04 21:36:35 +02003250 if (!rdev->ops->get_station)
3251 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003252
Hila Gonene35e4d22012-06-27 17:19:42 +03003253 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003254 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003255 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003256
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003257 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003258 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003259 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003260
Eric W. Biederman15e47302012-09-07 20:12:54 +00003261 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003262 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003263 nlmsg_free(msg);
3264 return -ENOBUFS;
3265 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003266
Johannes Berg4c476992010-10-04 21:36:35 +02003267 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003268}
3269
3270/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003271 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003272 */
Johannes Berg80b99892011-11-18 16:23:01 +01003273static struct net_device *get_vlan(struct genl_info *info,
3274 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003275{
Johannes Berg463d0182009-07-14 00:33:35 +02003276 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003277 struct net_device *v;
3278 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003279
Johannes Berg80b99892011-11-18 16:23:01 +01003280 if (!vlanattr)
3281 return NULL;
3282
3283 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3284 if (!v)
3285 return ERR_PTR(-ENODEV);
3286
3287 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3288 ret = -EINVAL;
3289 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003290 }
Johannes Berg80b99892011-11-18 16:23:01 +01003291
3292 if (!netif_running(v)) {
3293 ret = -ENETDOWN;
3294 goto error;
3295 }
3296
3297 return v;
3298 error:
3299 dev_put(v);
3300 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003301}
3302
3303static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3304{
Johannes Berg4c476992010-10-04 21:36:35 +02003305 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003306 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003307 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003308 struct station_parameters params;
3309 u8 *mac_addr = NULL;
3310
3311 memset(&params, 0, sizeof(params));
3312
3313 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003314 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003315
3316 if (info->attrs[NL80211_ATTR_STA_AID])
3317 return -EINVAL;
3318
3319 if (!info->attrs[NL80211_ATTR_MAC])
3320 return -EINVAL;
3321
3322 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3323
3324 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3325 params.supported_rates =
3326 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3327 params.supported_rates_len =
3328 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3329 }
3330
Johannes Bergba23d202012-12-27 17:32:09 +01003331 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL] ||
3332 info->attrs[NL80211_ATTR_HT_CAPABILITY])
3333 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003334
Johannes Bergbdd90d52011-12-14 12:20:27 +01003335 if (!rdev->ops->change_station)
3336 return -EOPNOTSUPP;
3337
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003338 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003339 return -EINVAL;
3340
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003341 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3342 params.plink_action =
3343 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3344
Javier Cardona9c3990a2011-05-03 16:57:11 -07003345 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3346 params.plink_state =
3347 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3348
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003349 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3350 enum nl80211_mesh_power_mode pm = nla_get_u32(
3351 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3352
3353 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3354 pm > NL80211_MESH_POWER_MAX)
3355 return -EINVAL;
3356
3357 params.local_pm = pm;
3358 }
3359
Johannes Berga97f4422009-06-18 17:23:43 +02003360 switch (dev->ieee80211_ptr->iftype) {
3361 case NL80211_IFTYPE_AP:
3362 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003363 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003364 /* disallow mesh-specific things */
3365 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003366 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003367 if (params.local_pm)
3368 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003369
3370 /* TDLS can't be set, ... */
3371 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3372 return -EINVAL;
3373 /*
3374 * ... but don't bother the driver with it. This works around
3375 * a hostapd/wpa_supplicant issue -- it always includes the
3376 * TLDS_PEER flag in the mask even for AP mode.
3377 */
3378 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3379
3380 /* accept only the listed bits */
3381 if (params.sta_flags_mask &
3382 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003383 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3384 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003385 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3386 BIT(NL80211_STA_FLAG_WME) |
3387 BIT(NL80211_STA_FLAG_MFP)))
3388 return -EINVAL;
3389
Johannes Bergd582cff2012-10-26 17:53:44 +02003390 /* but authenticated/associated only if driver handles it */
3391 if (!(rdev->wiphy.features &
3392 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3393 params.sta_flags_mask &
3394 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3395 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3396 return -EINVAL;
3397
Johannes Bergba23d202012-12-27 17:32:09 +01003398 /* reject other things that can't change */
3399 if (params.supported_rates)
3400 return -EINVAL;
3401
Johannes Bergbdd90d52011-12-14 12:20:27 +01003402 /* must be last in here for error handling */
3403 params.vlan = get_vlan(info, rdev);
3404 if (IS_ERR(params.vlan))
3405 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003406 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003407 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003408 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003409 /*
3410 * Don't allow userspace to change the TDLS_PEER flag,
3411 * but silently ignore attempts to change it since we
3412 * don't have state here to verify that it doesn't try
3413 * to change the flag.
3414 */
3415 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003416 /* fall through */
3417 case NL80211_IFTYPE_ADHOC:
3418 /* disallow things sta doesn't support */
3419 if (params.plink_action)
3420 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003421 if (params.local_pm)
3422 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003423 /* reject any changes other than AUTHORIZED */
3424 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3425 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003426 break;
3427 case NL80211_IFTYPE_MESH_POINT:
3428 /* disallow things mesh doesn't support */
3429 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003430 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003431 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003432 return -EINVAL;
3433 /*
3434 * No special handling for TDLS here -- the userspace
3435 * mesh code doesn't have this bug.
3436 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003437 if (params.sta_flags_mask &
3438 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003439 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003440 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003441 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003442 break;
3443 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003444 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003445 }
3446
Johannes Bergbdd90d52011-12-14 12:20:27 +01003447 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003448
Hila Gonene35e4d22012-06-27 17:19:42 +03003449 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003450
Johannes Berg5727ef12007-12-19 02:03:34 +01003451 if (params.vlan)
3452 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003453
Johannes Berg5727ef12007-12-19 02:03:34 +01003454 return err;
3455}
3456
Eliad Pellerc75786c2011-08-23 14:37:46 +03003457static struct nla_policy
3458nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3459 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3460 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3461};
3462
Johannes Berg5727ef12007-12-19 02:03:34 +01003463static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3464{
Johannes Berg4c476992010-10-04 21:36:35 +02003465 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003466 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003467 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003468 struct station_parameters params;
3469 u8 *mac_addr = NULL;
3470
3471 memset(&params, 0, sizeof(params));
3472
3473 if (!info->attrs[NL80211_ATTR_MAC])
3474 return -EINVAL;
3475
Johannes Berg5727ef12007-12-19 02:03:34 +01003476 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3477 return -EINVAL;
3478
3479 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3480 return -EINVAL;
3481
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003482 if (!info->attrs[NL80211_ATTR_STA_AID])
3483 return -EINVAL;
3484
Johannes Berg5727ef12007-12-19 02:03:34 +01003485 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3486 params.supported_rates =
3487 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3488 params.supported_rates_len =
3489 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3490 params.listen_interval =
3491 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003492
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003493 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3494 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3495 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003496
Jouni Malinen36aedc92008-08-25 11:58:58 +03003497 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3498 params.ht_capa =
3499 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003500
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003501 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3502 params.vht_capa =
3503 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3504
Javier Cardona96b78df2011-04-07 15:08:33 -07003505 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3506 params.plink_action =
3507 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3508
Johannes Bergbdd90d52011-12-14 12:20:27 +01003509 if (!rdev->ops->add_station)
3510 return -EOPNOTSUPP;
3511
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003512 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003513 return -EINVAL;
3514
Johannes Bergbdd90d52011-12-14 12:20:27 +01003515 switch (dev->ieee80211_ptr->iftype) {
3516 case NL80211_IFTYPE_AP:
3517 case NL80211_IFTYPE_AP_VLAN:
3518 case NL80211_IFTYPE_P2P_GO:
3519 /* parse WME attributes if sta is WME capable */
3520 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3521 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3522 info->attrs[NL80211_ATTR_STA_WME]) {
3523 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3524 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003525
Johannes Bergbdd90d52011-12-14 12:20:27 +01003526 nla = info->attrs[NL80211_ATTR_STA_WME];
3527 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3528 nl80211_sta_wme_policy);
3529 if (err)
3530 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003531
Johannes Bergbdd90d52011-12-14 12:20:27 +01003532 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3533 params.uapsd_queues =
3534 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3535 if (params.uapsd_queues &
3536 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3537 return -EINVAL;
3538
3539 if (tb[NL80211_STA_WME_MAX_SP])
3540 params.max_sp =
3541 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3542
3543 if (params.max_sp &
3544 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3545 return -EINVAL;
3546
3547 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3548 }
3549 /* TDLS peers cannot be added */
3550 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003551 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003552 /* but don't bother the driver with it */
3553 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003554
Johannes Bergd582cff2012-10-26 17:53:44 +02003555 /* allow authenticated/associated only if driver handles it */
3556 if (!(rdev->wiphy.features &
3557 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3558 params.sta_flags_mask &
3559 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3560 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3561 return -EINVAL;
3562
Johannes Bergbdd90d52011-12-14 12:20:27 +01003563 /* must be last in here for error handling */
3564 params.vlan = get_vlan(info, rdev);
3565 if (IS_ERR(params.vlan))
3566 return PTR_ERR(params.vlan);
3567 break;
3568 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergd582cff2012-10-26 17:53:44 +02003569 /* associated is disallowed */
3570 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3571 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003572 /* TDLS peers cannot be added */
3573 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003574 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003575 break;
3576 case NL80211_IFTYPE_STATION:
Johannes Bergd582cff2012-10-26 17:53:44 +02003577 /* associated is disallowed */
3578 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3579 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003580 /* Only TDLS peers can be added */
3581 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3582 return -EINVAL;
3583 /* Can only add if TDLS ... */
3584 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3585 return -EOPNOTSUPP;
3586 /* ... with external setup is supported */
3587 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3588 return -EOPNOTSUPP;
3589 break;
3590 default:
3591 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003592 }
3593
Johannes Bergbdd90d52011-12-14 12:20:27 +01003594 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003595
Hila Gonene35e4d22012-06-27 17:19:42 +03003596 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003597
Johannes Berg5727ef12007-12-19 02:03:34 +01003598 if (params.vlan)
3599 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003600 return err;
3601}
3602
3603static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3604{
Johannes Berg4c476992010-10-04 21:36:35 +02003605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3606 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003607 u8 *mac_addr = NULL;
3608
3609 if (info->attrs[NL80211_ATTR_MAC])
3610 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3611
Johannes Berge80cf852009-05-11 14:43:13 +02003612 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003613 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003614 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003615 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3616 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003617
Johannes Berg4c476992010-10-04 21:36:35 +02003618 if (!rdev->ops->del_station)
3619 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003620
Hila Gonene35e4d22012-06-27 17:19:42 +03003621 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003622}
3623
Eric W. Biederman15e47302012-09-07 20:12:54 +00003624static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003625 int flags, struct net_device *dev,
3626 u8 *dst, u8 *next_hop,
3627 struct mpath_info *pinfo)
3628{
3629 void *hdr;
3630 struct nlattr *pinfoattr;
3631
Eric W. Biederman15e47302012-09-07 20:12:54 +00003632 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003633 if (!hdr)
3634 return -1;
3635
David S. Miller9360ffd2012-03-29 04:41:26 -04003636 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3637 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3638 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3639 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3640 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003641
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003642 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3643 if (!pinfoattr)
3644 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003645 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3646 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3647 pinfo->frame_qlen))
3648 goto nla_put_failure;
3649 if (((pinfo->filled & MPATH_INFO_SN) &&
3650 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3651 ((pinfo->filled & MPATH_INFO_METRIC) &&
3652 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3653 pinfo->metric)) ||
3654 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3655 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3656 pinfo->exptime)) ||
3657 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3658 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3659 pinfo->flags)) ||
3660 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3661 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3662 pinfo->discovery_timeout)) ||
3663 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3664 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3665 pinfo->discovery_retries)))
3666 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003667
3668 nla_nest_end(msg, pinfoattr);
3669
3670 return genlmsg_end(msg, hdr);
3671
3672 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003673 genlmsg_cancel(msg, hdr);
3674 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003675}
3676
3677static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003678 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003679{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003680 struct mpath_info pinfo;
3681 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003682 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003683 u8 dst[ETH_ALEN];
3684 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003685 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003686 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003687
Johannes Berg67748892010-10-04 21:14:06 +02003688 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3689 if (err)
3690 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003691
3692 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003693 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003694 goto out_err;
3695 }
3696
Jouni Malineneec60b02009-03-20 21:21:19 +02003697 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3698 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003699 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003700 }
3701
Johannes Bergbba95fe2008-07-29 13:22:51 +02003702 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003703 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3704 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003705 if (err == -ENOENT)
3706 break;
3707 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003708 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003709
Eric W. Biederman15e47302012-09-07 20:12:54 +00003710 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003711 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3712 netdev, dst, next_hop,
3713 &pinfo) < 0)
3714 goto out;
3715
3716 path_idx++;
3717 }
3718
3719
3720 out:
3721 cb->args[1] = path_idx;
3722 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003723 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003724 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003725 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003726}
3727
3728static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3729{
Johannes Berg4c476992010-10-04 21:36:35 +02003730 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003731 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003732 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003733 struct mpath_info pinfo;
3734 struct sk_buff *msg;
3735 u8 *dst = NULL;
3736 u8 next_hop[ETH_ALEN];
3737
3738 memset(&pinfo, 0, sizeof(pinfo));
3739
3740 if (!info->attrs[NL80211_ATTR_MAC])
3741 return -EINVAL;
3742
3743 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3744
Johannes Berg4c476992010-10-04 21:36:35 +02003745 if (!rdev->ops->get_mpath)
3746 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003747
Johannes Berg4c476992010-10-04 21:36:35 +02003748 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3749 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003750
Hila Gonene35e4d22012-06-27 17:19:42 +03003751 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003752 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003753 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003754
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003755 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003756 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003757 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003758
Eric W. Biederman15e47302012-09-07 20:12:54 +00003759 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003760 dev, dst, next_hop, &pinfo) < 0) {
3761 nlmsg_free(msg);
3762 return -ENOBUFS;
3763 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003764
Johannes Berg4c476992010-10-04 21:36:35 +02003765 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003766}
3767
3768static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3769{
Johannes Berg4c476992010-10-04 21:36:35 +02003770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3771 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003772 u8 *dst = NULL;
3773 u8 *next_hop = NULL;
3774
3775 if (!info->attrs[NL80211_ATTR_MAC])
3776 return -EINVAL;
3777
3778 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3779 return -EINVAL;
3780
3781 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3782 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3783
Johannes Berg4c476992010-10-04 21:36:35 +02003784 if (!rdev->ops->change_mpath)
3785 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003786
Johannes Berg4c476992010-10-04 21:36:35 +02003787 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3788 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003789
Hila Gonene35e4d22012-06-27 17:19:42 +03003790 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003791}
Johannes Berg4c476992010-10-04 21:36:35 +02003792
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003793static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3794{
Johannes Berg4c476992010-10-04 21:36:35 +02003795 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3796 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003797 u8 *dst = NULL;
3798 u8 *next_hop = NULL;
3799
3800 if (!info->attrs[NL80211_ATTR_MAC])
3801 return -EINVAL;
3802
3803 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3804 return -EINVAL;
3805
3806 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3807 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3808
Johannes Berg4c476992010-10-04 21:36:35 +02003809 if (!rdev->ops->add_mpath)
3810 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003811
Johannes Berg4c476992010-10-04 21:36:35 +02003812 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3813 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003814
Hila Gonene35e4d22012-06-27 17:19:42 +03003815 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003816}
3817
3818static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3819{
Johannes Berg4c476992010-10-04 21:36:35 +02003820 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3821 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003822 u8 *dst = NULL;
3823
3824 if (info->attrs[NL80211_ATTR_MAC])
3825 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3826
Johannes Berg4c476992010-10-04 21:36:35 +02003827 if (!rdev->ops->del_mpath)
3828 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003829
Hila Gonene35e4d22012-06-27 17:19:42 +03003830 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003831}
3832
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003833static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3834{
Johannes Berg4c476992010-10-04 21:36:35 +02003835 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3836 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003837 struct bss_parameters params;
3838
3839 memset(&params, 0, sizeof(params));
3840 /* default to not changing parameters */
3841 params.use_cts_prot = -1;
3842 params.use_short_preamble = -1;
3843 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003844 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003845 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01003846 params.p2p_ctwindow = -1;
3847 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003848
3849 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3850 params.use_cts_prot =
3851 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3852 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3853 params.use_short_preamble =
3854 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3855 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3856 params.use_short_slot_time =
3857 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003858 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3859 params.basic_rates =
3860 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3861 params.basic_rates_len =
3862 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3863 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003864 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3865 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003866 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3867 params.ht_opmode =
3868 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003869
Johannes Berg53cabad2012-11-14 15:17:28 +01003870 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3871 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3872 return -EINVAL;
3873 params.p2p_ctwindow =
3874 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3875 if (params.p2p_ctwindow < 0)
3876 return -EINVAL;
3877 if (params.p2p_ctwindow != 0 &&
3878 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3879 return -EINVAL;
3880 }
3881
3882 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3883 u8 tmp;
3884
3885 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3886 return -EINVAL;
3887 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3888 if (tmp > 1)
3889 return -EINVAL;
3890 params.p2p_opp_ps = tmp;
3891 if (params.p2p_opp_ps &&
3892 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3893 return -EINVAL;
3894 }
3895
Johannes Berg4c476992010-10-04 21:36:35 +02003896 if (!rdev->ops->change_bss)
3897 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003898
Johannes Berg074ac8d2010-09-16 14:58:22 +02003899 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003900 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3901 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003902
Hila Gonene35e4d22012-06-27 17:19:42 +03003903 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003904}
3905
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003906static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003907 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3908 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3909 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3910 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3911 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3912 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3913};
3914
3915static int parse_reg_rule(struct nlattr *tb[],
3916 struct ieee80211_reg_rule *reg_rule)
3917{
3918 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3919 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3920
3921 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3922 return -EINVAL;
3923 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3924 return -EINVAL;
3925 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3926 return -EINVAL;
3927 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3928 return -EINVAL;
3929 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3930 return -EINVAL;
3931
3932 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3933
3934 freq_range->start_freq_khz =
3935 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3936 freq_range->end_freq_khz =
3937 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3938 freq_range->max_bandwidth_khz =
3939 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3940
3941 power_rule->max_eirp =
3942 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3943
3944 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3945 power_rule->max_antenna_gain =
3946 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3947
3948 return 0;
3949}
3950
3951static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3952{
3953 int r;
3954 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003955 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003956
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003957 /*
3958 * You should only get this when cfg80211 hasn't yet initialized
3959 * completely when built-in to the kernel right between the time
3960 * window between nl80211_init() and regulatory_init(), if that is
3961 * even possible.
3962 */
Johannes Berg458f4f92012-12-06 15:47:38 +01003963 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003964 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003965
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003966 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3967 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003968
3969 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3970
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003971 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3972 user_reg_hint_type =
3973 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3974 else
3975 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3976
3977 switch (user_reg_hint_type) {
3978 case NL80211_USER_REG_HINT_USER:
3979 case NL80211_USER_REG_HINT_CELL_BASE:
3980 break;
3981 default:
3982 return -EINVAL;
3983 }
3984
3985 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003986
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003987 return r;
3988}
3989
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003990static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003991 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003992{
Johannes Berg4c476992010-10-04 21:36:35 +02003993 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003994 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003995 struct wireless_dev *wdev = dev->ieee80211_ptr;
3996 struct mesh_config cur_params;
3997 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003998 void *hdr;
3999 struct nlattr *pinfoattr;
4000 struct sk_buff *msg;
4001
Johannes Berg29cbe682010-12-03 09:20:44 +01004002 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4003 return -EOPNOTSUPP;
4004
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004005 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004006 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004007
Johannes Berg29cbe682010-12-03 09:20:44 +01004008 wdev_lock(wdev);
4009 /* If not connected, get default parameters */
4010 if (!wdev->mesh_id_len)
4011 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4012 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004013 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004014 wdev_unlock(wdev);
4015
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004016 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004017 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004018
4019 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004020 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004021 if (!msg)
4022 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004023 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004024 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004025 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004026 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004027 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004028 if (!pinfoattr)
4029 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004030 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4031 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4032 cur_params.dot11MeshRetryTimeout) ||
4033 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4034 cur_params.dot11MeshConfirmTimeout) ||
4035 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4036 cur_params.dot11MeshHoldingTimeout) ||
4037 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4038 cur_params.dot11MeshMaxPeerLinks) ||
4039 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4040 cur_params.dot11MeshMaxRetries) ||
4041 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4042 cur_params.dot11MeshTTL) ||
4043 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4044 cur_params.element_ttl) ||
4045 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4046 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004047 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4048 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004049 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4050 cur_params.dot11MeshHWMPmaxPREQretries) ||
4051 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4052 cur_params.path_refresh_time) ||
4053 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4054 cur_params.min_discovery_timeout) ||
4055 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4056 cur_params.dot11MeshHWMPactivePathTimeout) ||
4057 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4058 cur_params.dot11MeshHWMPpreqMinInterval) ||
4059 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4060 cur_params.dot11MeshHWMPperrMinInterval) ||
4061 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4062 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4063 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4064 cur_params.dot11MeshHWMPRootMode) ||
4065 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4066 cur_params.dot11MeshHWMPRannInterval) ||
4067 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4068 cur_params.dot11MeshGateAnnouncementProtocol) ||
4069 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4070 cur_params.dot11MeshForwarding) ||
4071 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004072 cur_params.rssi_threshold) ||
4073 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004074 cur_params.ht_opmode) ||
4075 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4076 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4077 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004078 cur_params.dot11MeshHWMProotInterval) ||
4079 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004080 cur_params.dot11MeshHWMPconfirmationInterval) ||
4081 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4082 cur_params.power_mode) ||
4083 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4084 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004085 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004086 nla_nest_end(msg, pinfoattr);
4087 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004088 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004089
Johannes Berg3b858752009-03-12 09:55:09 +01004090 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004091 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004092 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004093 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004094 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004095}
4096
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004097static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004098 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4099 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4100 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4101 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4102 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4103 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004104 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004105 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004106 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004107 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4108 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4109 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4110 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4111 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004112 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004113 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004114 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004115 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004116 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004117 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004118 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4119 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004120 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4121 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004122 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004123 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4124 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004125};
4126
Javier Cardonac80d5452010-12-16 17:37:49 -08004127static const struct nla_policy
4128 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004129 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004130 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4131 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004132 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004133 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004134 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004135 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004136};
4137
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004138static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004139 struct mesh_config *cfg,
4140 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004141{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004142 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004143 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004144
Marco Porschea54fba2013-01-07 16:04:48 +01004145#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4146do { \
4147 if (tb[attr]) { \
4148 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4149 return -EINVAL; \
4150 cfg->param = fn(tb[attr]); \
4151 mask |= (1 << (attr - 1)); \
4152 } \
4153} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004154
4155
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004156 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004157 return -EINVAL;
4158 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004159 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004160 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004161 return -EINVAL;
4162
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004163 /* This makes sure that there aren't more than 32 mesh config
4164 * parameters (otherwise our bitfield scheme would not work.) */
4165 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4166
4167 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004168 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004169 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4170 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004171 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004172 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4173 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004174 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004175 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4176 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004177 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004178 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4179 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004180 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004181 mask, NL80211_MESHCONF_MAX_RETRIES,
4182 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004183 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004184 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004185 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004186 mask, NL80211_MESHCONF_ELEMENT_TTL,
4187 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004188 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004189 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4190 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004191 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4192 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004193 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4194 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004195 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004196 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4197 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004198 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004199 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4200 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004201 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004202 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4203 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004204 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4205 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004206 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4207 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004208 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004209 1, 65535, mask,
4210 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004211 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004212 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004213 1, 65535, mask,
4214 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004215 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004216 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004217 dot11MeshHWMPnetDiameterTraversalTime,
4218 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004219 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4220 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004221 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4222 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4223 nla_get_u8);
4224 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4225 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004226 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004227 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004228 dot11MeshGateAnnouncementProtocol, 0, 1,
4229 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004230 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004231 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004232 mask, NL80211_MESHCONF_FORWARDING,
4233 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004234 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004235 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4236 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004237 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004238 mask, NL80211_MESHCONF_HT_OPMODE,
4239 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004240 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004241 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004242 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4243 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004244 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004245 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4246 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004247 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004248 dot11MeshHWMPconfirmationInterval,
4249 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004250 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4251 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004252 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4253 NL80211_MESH_POWER_ACTIVE,
4254 NL80211_MESH_POWER_MAX,
4255 mask, NL80211_MESHCONF_POWER_MODE,
4256 nla_get_u32);
4257 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4258 0, 65535, mask,
4259 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004260 if (mask_out)
4261 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004262
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004263 return 0;
4264
4265#undef FILL_IN_MESH_PARAM_IF_SET
4266}
4267
Javier Cardonac80d5452010-12-16 17:37:49 -08004268static int nl80211_parse_mesh_setup(struct genl_info *info,
4269 struct mesh_setup *setup)
4270{
4271 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4272
4273 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4274 return -EINVAL;
4275 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4276 info->attrs[NL80211_ATTR_MESH_SETUP],
4277 nl80211_mesh_setup_params_policy))
4278 return -EINVAL;
4279
Javier Cardonad299a1f2012-03-31 11:31:33 -07004280 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4281 setup->sync_method =
4282 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4283 IEEE80211_SYNC_METHOD_VENDOR :
4284 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4285
Javier Cardonac80d5452010-12-16 17:37:49 -08004286 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4287 setup->path_sel_proto =
4288 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4289 IEEE80211_PATH_PROTOCOL_VENDOR :
4290 IEEE80211_PATH_PROTOCOL_HWMP;
4291
4292 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4293 setup->path_metric =
4294 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4295 IEEE80211_PATH_METRIC_VENDOR :
4296 IEEE80211_PATH_METRIC_AIRTIME;
4297
Javier Cardona581a8b02011-04-07 15:08:27 -07004298
4299 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004300 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004301 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004302 if (!is_valid_ie_attr(ieattr))
4303 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004304 setup->ie = nla_data(ieattr);
4305 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004306 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004307 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4308 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004309
4310 return 0;
4311}
4312
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004313static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004314 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004315{
4316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4317 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004318 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004319 struct mesh_config cfg;
4320 u32 mask;
4321 int err;
4322
Johannes Berg29cbe682010-12-03 09:20:44 +01004323 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4324 return -EOPNOTSUPP;
4325
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004326 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004327 return -EOPNOTSUPP;
4328
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004329 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004330 if (err)
4331 return err;
4332
Johannes Berg29cbe682010-12-03 09:20:44 +01004333 wdev_lock(wdev);
4334 if (!wdev->mesh_id_len)
4335 err = -ENOLINK;
4336
4337 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004338 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004339
4340 wdev_unlock(wdev);
4341
4342 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004343}
4344
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004345static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4346{
Johannes Berg458f4f92012-12-06 15:47:38 +01004347 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004348 struct sk_buff *msg;
4349 void *hdr = NULL;
4350 struct nlattr *nl_reg_rules;
4351 unsigned int i;
4352 int err = -EINVAL;
4353
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004354 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004355
4356 if (!cfg80211_regdomain)
4357 goto out;
4358
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004360 if (!msg) {
4361 err = -ENOBUFS;
4362 goto out;
4363 }
4364
Eric W. Biederman15e47302012-09-07 20:12:54 +00004365 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004366 NL80211_CMD_GET_REG);
4367 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004368 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004369
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004370 if (reg_last_request_cell_base() &&
4371 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4372 NL80211_USER_REG_HINT_CELL_BASE))
4373 goto nla_put_failure;
4374
Johannes Berg458f4f92012-12-06 15:47:38 +01004375 rcu_read_lock();
4376 regdom = rcu_dereference(cfg80211_regdomain);
4377
4378 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4379 (regdom->dfs_region &&
4380 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4381 goto nla_put_failure_rcu;
4382
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004383 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4384 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004385 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004386
Johannes Berg458f4f92012-12-06 15:47:38 +01004387 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004388 struct nlattr *nl_reg_rule;
4389 const struct ieee80211_reg_rule *reg_rule;
4390 const struct ieee80211_freq_range *freq_range;
4391 const struct ieee80211_power_rule *power_rule;
4392
Johannes Berg458f4f92012-12-06 15:47:38 +01004393 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004394 freq_range = &reg_rule->freq_range;
4395 power_rule = &reg_rule->power_rule;
4396
4397 nl_reg_rule = nla_nest_start(msg, i);
4398 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004399 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004400
David S. Miller9360ffd2012-03-29 04:41:26 -04004401 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4402 reg_rule->flags) ||
4403 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4404 freq_range->start_freq_khz) ||
4405 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4406 freq_range->end_freq_khz) ||
4407 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4408 freq_range->max_bandwidth_khz) ||
4409 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4410 power_rule->max_antenna_gain) ||
4411 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4412 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004413 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004414
4415 nla_nest_end(msg, nl_reg_rule);
4416 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004417 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004418
4419 nla_nest_end(msg, nl_reg_rules);
4420
4421 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004422 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004423 goto out;
4424
Johannes Berg458f4f92012-12-06 15:47:38 +01004425nla_put_failure_rcu:
4426 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004427nla_put_failure:
4428 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004429put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004430 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004431 err = -EMSGSIZE;
4432out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004433 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004434 return err;
4435}
4436
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004437static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4438{
4439 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4440 struct nlattr *nl_reg_rule;
4441 char *alpha2 = NULL;
4442 int rem_reg_rules = 0, r = 0;
4443 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004444 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004445 struct ieee80211_regdomain *rd = NULL;
4446
4447 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4448 return -EINVAL;
4449
4450 if (!info->attrs[NL80211_ATTR_REG_RULES])
4451 return -EINVAL;
4452
4453 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4454
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004455 if (info->attrs[NL80211_ATTR_DFS_REGION])
4456 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4457
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004458 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004459 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004460 num_rules++;
4461 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004462 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004463 }
4464
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004465 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004466 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004467
4468 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004469 if (!rd)
4470 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004471
4472 rd->n_reg_rules = num_rules;
4473 rd->alpha2[0] = alpha2[0];
4474 rd->alpha2[1] = alpha2[1];
4475
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004476 /*
4477 * Disable DFS master mode if the DFS region was
4478 * not supported or known on this kernel.
4479 */
4480 if (reg_supported_dfs_region(dfs_region))
4481 rd->dfs_region = dfs_region;
4482
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004483 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004484 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004485 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004486 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4487 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004488 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4489 if (r)
4490 goto bad_reg;
4491
4492 rule_idx++;
4493
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004494 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4495 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004496 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004497 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004498 }
4499
Johannes Berg6913b492012-12-04 00:48:59 +01004500 mutex_lock(&cfg80211_mutex);
4501
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004502 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004503 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004504 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004505 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004506
Johannes Bergd2372b32008-10-24 20:32:20 +02004507 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004508 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004509 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004510}
4511
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004512static int validate_scan_freqs(struct nlattr *freqs)
4513{
4514 struct nlattr *attr1, *attr2;
4515 int n_channels = 0, tmp1, tmp2;
4516
4517 nla_for_each_nested(attr1, freqs, tmp1) {
4518 n_channels++;
4519 /*
4520 * Some hardware has a limited channel list for
4521 * scanning, and it is pretty much nonsensical
4522 * to scan for a channel twice, so disallow that
4523 * and don't require drivers to check that the
4524 * channel list they get isn't longer than what
4525 * they can scan, as long as they can scan all
4526 * the channels they registered at once.
4527 */
4528 nla_for_each_nested(attr2, freqs, tmp2)
4529 if (attr1 != attr2 &&
4530 nla_get_u32(attr1) == nla_get_u32(attr2))
4531 return 0;
4532 }
4533
4534 return n_channels;
4535}
4536
Johannes Berg2a519312009-02-10 21:25:55 +01004537static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4538{
Johannes Berg4c476992010-10-04 21:36:35 +02004539 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004540 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004541 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004542 struct nlattr *attr;
4543 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004544 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004545 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004546
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004547 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4548 return -EINVAL;
4549
Johannes Berg79c97e92009-07-07 03:56:12 +02004550 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004551
Johannes Berg4c476992010-10-04 21:36:35 +02004552 if (!rdev->ops->scan)
4553 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004554
Johannes Berg4c476992010-10-04 21:36:35 +02004555 if (rdev->scan_req)
4556 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004557
4558 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004559 n_channels = validate_scan_freqs(
4560 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004561 if (!n_channels)
4562 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004563 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004564 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004565 n_channels = 0;
4566
Johannes Berg2a519312009-02-10 21:25:55 +01004567 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4568 if (wiphy->bands[band])
4569 n_channels += wiphy->bands[band]->n_channels;
4570 }
4571
4572 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4573 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4574 n_ssids++;
4575
Johannes Berg4c476992010-10-04 21:36:35 +02004576 if (n_ssids > wiphy->max_scan_ssids)
4577 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004578
Jouni Malinen70692ad2009-02-16 19:39:13 +02004579 if (info->attrs[NL80211_ATTR_IE])
4580 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4581 else
4582 ie_len = 0;
4583
Johannes Berg4c476992010-10-04 21:36:35 +02004584 if (ie_len > wiphy->max_scan_ie_len)
4585 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004586
Johannes Berg2a519312009-02-10 21:25:55 +01004587 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004588 + sizeof(*request->ssids) * n_ssids
4589 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004590 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004591 if (!request)
4592 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004593
Johannes Berg2a519312009-02-10 21:25:55 +01004594 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004595 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004596 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004597 if (ie_len) {
4598 if (request->ssids)
4599 request->ie = (void *)(request->ssids + n_ssids);
4600 else
4601 request->ie = (void *)(request->channels + n_channels);
4602 }
Johannes Berg2a519312009-02-10 21:25:55 +01004603
Johannes Berg584991d2009-11-02 13:32:03 +01004604 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004605 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4606 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004607 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004608 struct ieee80211_channel *chan;
4609
4610 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4611
4612 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004613 err = -EINVAL;
4614 goto out_free;
4615 }
Johannes Berg584991d2009-11-02 13:32:03 +01004616
4617 /* ignore disabled channels */
4618 if (chan->flags & IEEE80211_CHAN_DISABLED)
4619 continue;
4620
4621 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004622 i++;
4623 }
4624 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004625 enum ieee80211_band band;
4626
Johannes Berg2a519312009-02-10 21:25:55 +01004627 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004628 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4629 int j;
4630 if (!wiphy->bands[band])
4631 continue;
4632 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004633 struct ieee80211_channel *chan;
4634
4635 chan = &wiphy->bands[band]->channels[j];
4636
4637 if (chan->flags & IEEE80211_CHAN_DISABLED)
4638 continue;
4639
4640 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004641 i++;
4642 }
4643 }
4644 }
4645
Johannes Berg584991d2009-11-02 13:32:03 +01004646 if (!i) {
4647 err = -EINVAL;
4648 goto out_free;
4649 }
4650
4651 request->n_channels = i;
4652
Johannes Berg2a519312009-02-10 21:25:55 +01004653 i = 0;
4654 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4655 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004656 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004657 err = -EINVAL;
4658 goto out_free;
4659 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004660 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004661 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004662 i++;
4663 }
4664 }
4665
Jouni Malinen70692ad2009-02-16 19:39:13 +02004666 if (info->attrs[NL80211_ATTR_IE]) {
4667 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004668 memcpy((void *)request->ie,
4669 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004670 request->ie_len);
4671 }
4672
Johannes Berg34850ab2011-07-18 18:08:35 +02004673 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004674 if (wiphy->bands[i])
4675 request->rates[i] =
4676 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004677
4678 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4679 nla_for_each_nested(attr,
4680 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4681 tmp) {
4682 enum ieee80211_band band = nla_type(attr);
4683
Dan Carpenter84404622011-07-29 11:52:18 +03004684 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004685 err = -EINVAL;
4686 goto out_free;
4687 }
4688 err = ieee80211_get_ratemask(wiphy->bands[band],
4689 nla_data(attr),
4690 nla_len(attr),
4691 &request->rates[band]);
4692 if (err)
4693 goto out_free;
4694 }
4695 }
4696
Sam Leffler46856bb2012-10-11 21:03:32 -07004697 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004698 request->flags = nla_get_u32(
4699 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004700 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4701 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4702 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4703 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004704 err = -EOPNOTSUPP;
4705 goto out_free;
4706 }
4707 }
Sam Lefflered4737712012-10-11 21:03:31 -07004708
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304709 request->no_cck =
4710 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4711
Johannes Bergfd014282012-06-18 19:17:03 +02004712 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004713 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004714 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004715
Johannes Berg79c97e92009-07-07 03:56:12 +02004716 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004717 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004718
Johannes Berg463d0182009-07-14 00:33:35 +02004719 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004720 nl80211_send_scan_start(rdev, wdev);
4721 if (wdev->netdev)
4722 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004723 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004724 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004725 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004726 kfree(request);
4727 }
Johannes Berg3b858752009-03-12 09:55:09 +01004728
Johannes Berg2a519312009-02-10 21:25:55 +01004729 return err;
4730}
4731
Luciano Coelho807f8a82011-05-11 17:09:35 +03004732static int nl80211_start_sched_scan(struct sk_buff *skb,
4733 struct genl_info *info)
4734{
4735 struct cfg80211_sched_scan_request *request;
4736 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4737 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004738 struct nlattr *attr;
4739 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004740 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004741 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004742 enum ieee80211_band band;
4743 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004744 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004745
4746 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4747 !rdev->ops->sched_scan_start)
4748 return -EOPNOTSUPP;
4749
4750 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4751 return -EINVAL;
4752
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004753 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4754 return -EINVAL;
4755
4756 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4757 if (interval == 0)
4758 return -EINVAL;
4759
Luciano Coelho807f8a82011-05-11 17:09:35 +03004760 wiphy = &rdev->wiphy;
4761
4762 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4763 n_channels = validate_scan_freqs(
4764 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4765 if (!n_channels)
4766 return -EINVAL;
4767 } else {
4768 n_channels = 0;
4769
4770 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4771 if (wiphy->bands[band])
4772 n_channels += wiphy->bands[band]->n_channels;
4773 }
4774
4775 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4776 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4777 tmp)
4778 n_ssids++;
4779
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004780 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004781 return -EINVAL;
4782
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004783 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4784 nla_for_each_nested(attr,
4785 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4786 tmp)
4787 n_match_sets++;
4788
4789 if (n_match_sets > wiphy->max_match_sets)
4790 return -EINVAL;
4791
Luciano Coelho807f8a82011-05-11 17:09:35 +03004792 if (info->attrs[NL80211_ATTR_IE])
4793 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4794 else
4795 ie_len = 0;
4796
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004797 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004798 return -EINVAL;
4799
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004800 mutex_lock(&rdev->sched_scan_mtx);
4801
4802 if (rdev->sched_scan_req) {
4803 err = -EINPROGRESS;
4804 goto out;
4805 }
4806
Luciano Coelho807f8a82011-05-11 17:09:35 +03004807 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004808 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004809 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004810 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004811 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004812 if (!request) {
4813 err = -ENOMEM;
4814 goto out;
4815 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004816
4817 if (n_ssids)
4818 request->ssids = (void *)&request->channels[n_channels];
4819 request->n_ssids = n_ssids;
4820 if (ie_len) {
4821 if (request->ssids)
4822 request->ie = (void *)(request->ssids + n_ssids);
4823 else
4824 request->ie = (void *)(request->channels + n_channels);
4825 }
4826
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004827 if (n_match_sets) {
4828 if (request->ie)
4829 request->match_sets = (void *)(request->ie + ie_len);
4830 else if (request->ssids)
4831 request->match_sets =
4832 (void *)(request->ssids + n_ssids);
4833 else
4834 request->match_sets =
4835 (void *)(request->channels + n_channels);
4836 }
4837 request->n_match_sets = n_match_sets;
4838
Luciano Coelho807f8a82011-05-11 17:09:35 +03004839 i = 0;
4840 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4841 /* user specified, bail out if channel not found */
4842 nla_for_each_nested(attr,
4843 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4844 tmp) {
4845 struct ieee80211_channel *chan;
4846
4847 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4848
4849 if (!chan) {
4850 err = -EINVAL;
4851 goto out_free;
4852 }
4853
4854 /* ignore disabled channels */
4855 if (chan->flags & IEEE80211_CHAN_DISABLED)
4856 continue;
4857
4858 request->channels[i] = chan;
4859 i++;
4860 }
4861 } else {
4862 /* all channels */
4863 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4864 int j;
4865 if (!wiphy->bands[band])
4866 continue;
4867 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4868 struct ieee80211_channel *chan;
4869
4870 chan = &wiphy->bands[band]->channels[j];
4871
4872 if (chan->flags & IEEE80211_CHAN_DISABLED)
4873 continue;
4874
4875 request->channels[i] = chan;
4876 i++;
4877 }
4878 }
4879 }
4880
4881 if (!i) {
4882 err = -EINVAL;
4883 goto out_free;
4884 }
4885
4886 request->n_channels = i;
4887
4888 i = 0;
4889 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4890 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4891 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004892 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004893 err = -EINVAL;
4894 goto out_free;
4895 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004896 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004897 memcpy(request->ssids[i].ssid, nla_data(attr),
4898 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004899 i++;
4900 }
4901 }
4902
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004903 i = 0;
4904 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4905 nla_for_each_nested(attr,
4906 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4907 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004908 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004909
4910 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4911 nla_data(attr), nla_len(attr),
4912 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004913 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004914 if (ssid) {
4915 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4916 err = -EINVAL;
4917 goto out_free;
4918 }
4919 memcpy(request->match_sets[i].ssid.ssid,
4920 nla_data(ssid), nla_len(ssid));
4921 request->match_sets[i].ssid.ssid_len =
4922 nla_len(ssid);
4923 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004924 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4925 if (rssi)
4926 request->rssi_thold = nla_get_u32(rssi);
4927 else
4928 request->rssi_thold =
4929 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004930 i++;
4931 }
4932 }
4933
Luciano Coelho807f8a82011-05-11 17:09:35 +03004934 if (info->attrs[NL80211_ATTR_IE]) {
4935 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4936 memcpy((void *)request->ie,
4937 nla_data(info->attrs[NL80211_ATTR_IE]),
4938 request->ie_len);
4939 }
4940
Sam Leffler46856bb2012-10-11 21:03:32 -07004941 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004942 request->flags = nla_get_u32(
4943 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004944 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4945 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4946 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4947 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004948 err = -EOPNOTSUPP;
4949 goto out_free;
4950 }
4951 }
Sam Lefflered4737712012-10-11 21:03:31 -07004952
Luciano Coelho807f8a82011-05-11 17:09:35 +03004953 request->dev = dev;
4954 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004955 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004956 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004957
Hila Gonene35e4d22012-06-27 17:19:42 +03004958 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004959 if (!err) {
4960 rdev->sched_scan_req = request;
4961 nl80211_send_sched_scan(rdev, dev,
4962 NL80211_CMD_START_SCHED_SCAN);
4963 goto out;
4964 }
4965
4966out_free:
4967 kfree(request);
4968out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004969 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004970 return err;
4971}
4972
4973static int nl80211_stop_sched_scan(struct sk_buff *skb,
4974 struct genl_info *info)
4975{
4976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004977 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004978
4979 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4980 !rdev->ops->sched_scan_stop)
4981 return -EOPNOTSUPP;
4982
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004983 mutex_lock(&rdev->sched_scan_mtx);
4984 err = __cfg80211_stop_sched_scan(rdev, false);
4985 mutex_unlock(&rdev->sched_scan_mtx);
4986
4987 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004988}
4989
Johannes Berg9720bb32011-06-21 09:45:33 +02004990static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4991 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004992 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004993 struct wireless_dev *wdev,
4994 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004995{
Johannes Berg48ab9052009-07-10 18:42:31 +02004996 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01004997 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01004998 void *hdr;
4999 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005000 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005001
5002 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005003
Eric W. Biederman15e47302012-09-07 20:12:54 +00005004 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005005 NL80211_CMD_NEW_SCAN_RESULTS);
5006 if (!hdr)
5007 return -1;
5008
Johannes Berg9720bb32011-06-21 09:45:33 +02005009 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5010
David S. Miller9360ffd2012-03-29 04:41:26 -04005011 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5012 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5013 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005014
5015 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5016 if (!bss)
5017 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005018 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005019 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005020 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005021
5022 rcu_read_lock();
5023 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005024 if (ies) {
5025 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5026 goto fail_unlock_rcu;
5027 tsf = true;
5028 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5029 ies->len, ies->data))
5030 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005031 }
5032 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005033 if (ies) {
5034 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5035 goto fail_unlock_rcu;
5036 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5037 ies->len, ies->data))
5038 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005039 }
5040 rcu_read_unlock();
5041
David S. Miller9360ffd2012-03-29 04:41:26 -04005042 if (res->beacon_interval &&
5043 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5044 goto nla_put_failure;
5045 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5046 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5047 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5048 jiffies_to_msecs(jiffies - intbss->ts)))
5049 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005050
Johannes Berg77965c92009-02-18 18:45:06 +01005051 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005052 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005053 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5054 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005055 break;
5056 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005057 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5058 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005059 break;
5060 default:
5061 break;
5062 }
5063
Johannes Berg48ab9052009-07-10 18:42:31 +02005064 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005065 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005066 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005067 if (intbss == wdev->current_bss &&
5068 nla_put_u32(msg, NL80211_BSS_STATUS,
5069 NL80211_BSS_STATUS_ASSOCIATED))
5070 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005071 break;
5072 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005073 if (intbss == wdev->current_bss &&
5074 nla_put_u32(msg, NL80211_BSS_STATUS,
5075 NL80211_BSS_STATUS_IBSS_JOINED))
5076 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005077 break;
5078 default:
5079 break;
5080 }
5081
Johannes Berg2a519312009-02-10 21:25:55 +01005082 nla_nest_end(msg, bss);
5083
5084 return genlmsg_end(msg, hdr);
5085
Johannes Berg8cef2c92013-02-05 16:54:31 +01005086 fail_unlock_rcu:
5087 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005088 nla_put_failure:
5089 genlmsg_cancel(msg, hdr);
5090 return -EMSGSIZE;
5091}
5092
5093static int nl80211_dump_scan(struct sk_buff *skb,
5094 struct netlink_callback *cb)
5095{
Johannes Berg48ab9052009-07-10 18:42:31 +02005096 struct cfg80211_registered_device *rdev;
5097 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005098 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005099 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005100 int start = cb->args[1], idx = 0;
5101 int err;
5102
Johannes Berg67748892010-10-04 21:14:06 +02005103 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5104 if (err)
5105 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005106
Johannes Berg48ab9052009-07-10 18:42:31 +02005107 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005108
Johannes Berg48ab9052009-07-10 18:42:31 +02005109 wdev_lock(wdev);
5110 spin_lock_bh(&rdev->bss_lock);
5111 cfg80211_bss_expire(rdev);
5112
Johannes Berg9720bb32011-06-21 09:45:33 +02005113 cb->seq = rdev->bss_generation;
5114
Johannes Berg48ab9052009-07-10 18:42:31 +02005115 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005116 if (++idx <= start)
5117 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005118 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005119 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005120 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005121 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005122 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005123 }
5124 }
5125
Johannes Berg48ab9052009-07-10 18:42:31 +02005126 spin_unlock_bh(&rdev->bss_lock);
5127 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005128
5129 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005130 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005131
Johannes Berg67748892010-10-04 21:14:06 +02005132 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005133}
5134
Eric W. Biederman15e47302012-09-07 20:12:54 +00005135static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005136 int flags, struct net_device *dev,
5137 struct survey_info *survey)
5138{
5139 void *hdr;
5140 struct nlattr *infoattr;
5141
Eric W. Biederman15e47302012-09-07 20:12:54 +00005142 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005143 NL80211_CMD_NEW_SURVEY_RESULTS);
5144 if (!hdr)
5145 return -ENOMEM;
5146
David S. Miller9360ffd2012-03-29 04:41:26 -04005147 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5148 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005149
5150 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5151 if (!infoattr)
5152 goto nla_put_failure;
5153
David S. Miller9360ffd2012-03-29 04:41:26 -04005154 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5155 survey->channel->center_freq))
5156 goto nla_put_failure;
5157
5158 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5159 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5160 goto nla_put_failure;
5161 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5162 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5163 goto nla_put_failure;
5164 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5165 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5166 survey->channel_time))
5167 goto nla_put_failure;
5168 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5169 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5170 survey->channel_time_busy))
5171 goto nla_put_failure;
5172 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5173 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5174 survey->channel_time_ext_busy))
5175 goto nla_put_failure;
5176 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5177 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5178 survey->channel_time_rx))
5179 goto nla_put_failure;
5180 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5181 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5182 survey->channel_time_tx))
5183 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005184
5185 nla_nest_end(msg, infoattr);
5186
5187 return genlmsg_end(msg, hdr);
5188
5189 nla_put_failure:
5190 genlmsg_cancel(msg, hdr);
5191 return -EMSGSIZE;
5192}
5193
5194static int nl80211_dump_survey(struct sk_buff *skb,
5195 struct netlink_callback *cb)
5196{
5197 struct survey_info survey;
5198 struct cfg80211_registered_device *dev;
5199 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005200 int survey_idx = cb->args[1];
5201 int res;
5202
Johannes Berg67748892010-10-04 21:14:06 +02005203 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5204 if (res)
5205 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005206
5207 if (!dev->ops->dump_survey) {
5208 res = -EOPNOTSUPP;
5209 goto out_err;
5210 }
5211
5212 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005213 struct ieee80211_channel *chan;
5214
Hila Gonene35e4d22012-06-27 17:19:42 +03005215 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005216 if (res == -ENOENT)
5217 break;
5218 if (res)
5219 goto out_err;
5220
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005221 /* Survey without a channel doesn't make sense */
5222 if (!survey.channel) {
5223 res = -EINVAL;
5224 goto out;
5225 }
5226
5227 chan = ieee80211_get_channel(&dev->wiphy,
5228 survey.channel->center_freq);
5229 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5230 survey_idx++;
5231 continue;
5232 }
5233
Holger Schurig61fa7132009-11-11 12:25:40 +01005234 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005235 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005236 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5237 netdev,
5238 &survey) < 0)
5239 goto out;
5240 survey_idx++;
5241 }
5242
5243 out:
5244 cb->args[1] = survey_idx;
5245 res = skb->len;
5246 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005247 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005248 return res;
5249}
5250
Samuel Ortizb23aa672009-07-01 21:26:54 +02005251static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5252{
5253 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5254 NL80211_WPA_VERSION_2));
5255}
5256
Jouni Malinen636a5d32009-03-19 13:39:22 +02005257static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5258{
Johannes Berg4c476992010-10-04 21:36:35 +02005259 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5260 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005261 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005262 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5263 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005264 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005265 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005266 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005267
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005268 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5269 return -EINVAL;
5270
5271 if (!info->attrs[NL80211_ATTR_MAC])
5272 return -EINVAL;
5273
Jouni Malinen17780922009-03-27 20:52:47 +02005274 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5275 return -EINVAL;
5276
Johannes Berg19957bb2009-07-02 17:20:43 +02005277 if (!info->attrs[NL80211_ATTR_SSID])
5278 return -EINVAL;
5279
5280 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5281 return -EINVAL;
5282
Johannes Bergfffd0932009-07-08 14:22:54 +02005283 err = nl80211_parse_key(info, &key);
5284 if (err)
5285 return err;
5286
5287 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005288 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5289 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005290 if (!key.p.key || !key.p.key_len)
5291 return -EINVAL;
5292 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5293 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5294 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5295 key.p.key_len != WLAN_KEY_LEN_WEP104))
5296 return -EINVAL;
5297 if (key.idx > 4)
5298 return -EINVAL;
5299 } else {
5300 key.p.key_len = 0;
5301 key.p.key = NULL;
5302 }
5303
Johannes Bergafea0b72010-08-10 09:46:42 +02005304 if (key.idx >= 0) {
5305 int i;
5306 bool ok = false;
5307 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5308 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5309 ok = true;
5310 break;
5311 }
5312 }
Johannes Berg4c476992010-10-04 21:36:35 +02005313 if (!ok)
5314 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005315 }
5316
Johannes Berg4c476992010-10-04 21:36:35 +02005317 if (!rdev->ops->auth)
5318 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005319
Johannes Berg074ac8d2010-09-16 14:58:22 +02005320 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005321 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5322 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005323
Johannes Berg19957bb2009-07-02 17:20:43 +02005324 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005325 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005326 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005327 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5328 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005329
Johannes Berg19957bb2009-07-02 17:20:43 +02005330 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5331 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5332
5333 if (info->attrs[NL80211_ATTR_IE]) {
5334 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5335 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5336 }
5337
5338 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005339 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005340 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005341
Jouni Malinene39e5b52012-09-30 19:29:39 +03005342 if (auth_type == NL80211_AUTHTYPE_SAE &&
5343 !info->attrs[NL80211_ATTR_SAE_DATA])
5344 return -EINVAL;
5345
5346 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5347 if (auth_type != NL80211_AUTHTYPE_SAE)
5348 return -EINVAL;
5349 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5350 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5351 /* need to include at least Auth Transaction and Status Code */
5352 if (sae_data_len < 4)
5353 return -EINVAL;
5354 }
5355
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005356 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5357
Johannes Berg95de8172012-01-20 13:55:25 +01005358 /*
5359 * Since we no longer track auth state, ignore
5360 * requests to only change local state.
5361 */
5362 if (local_state_change)
5363 return 0;
5364
Johannes Berg4c476992010-10-04 21:36:35 +02005365 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5366 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005367 key.p.key, key.p.key_len, key.idx,
5368 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005369}
5370
Johannes Bergc0692b82010-08-27 14:26:53 +03005371static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5372 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005373 struct cfg80211_crypto_settings *settings,
5374 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005375{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005376 memset(settings, 0, sizeof(*settings));
5377
Samuel Ortizb23aa672009-07-01 21:26:54 +02005378 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5379
Johannes Bergc0692b82010-08-27 14:26:53 +03005380 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5381 u16 proto;
5382 proto = nla_get_u16(
5383 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5384 settings->control_port_ethertype = cpu_to_be16(proto);
5385 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5386 proto != ETH_P_PAE)
5387 return -EINVAL;
5388 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5389 settings->control_port_no_encrypt = true;
5390 } else
5391 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5392
Samuel Ortizb23aa672009-07-01 21:26:54 +02005393 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5394 void *data;
5395 int len, i;
5396
5397 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5398 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5399 settings->n_ciphers_pairwise = len / sizeof(u32);
5400
5401 if (len % sizeof(u32))
5402 return -EINVAL;
5403
Johannes Berg3dc27d22009-07-02 21:36:37 +02005404 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005405 return -EINVAL;
5406
5407 memcpy(settings->ciphers_pairwise, data, len);
5408
5409 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005410 if (!cfg80211_supported_cipher_suite(
5411 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005412 settings->ciphers_pairwise[i]))
5413 return -EINVAL;
5414 }
5415
5416 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5417 settings->cipher_group =
5418 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005419 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5420 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005421 return -EINVAL;
5422 }
5423
5424 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5425 settings->wpa_versions =
5426 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5427 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5428 return -EINVAL;
5429 }
5430
5431 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5432 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005433 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005434
5435 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5436 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5437 settings->n_akm_suites = len / sizeof(u32);
5438
5439 if (len % sizeof(u32))
5440 return -EINVAL;
5441
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005442 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5443 return -EINVAL;
5444
Samuel Ortizb23aa672009-07-01 21:26:54 +02005445 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005446 }
5447
5448 return 0;
5449}
5450
Jouni Malinen636a5d32009-03-19 13:39:22 +02005451static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5452{
Johannes Berg4c476992010-10-04 21:36:35 +02005453 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5454 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005455 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005456 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005457 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005458 int err, ssid_len, ie_len = 0;
5459 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005460 u32 flags = 0;
5461 struct ieee80211_ht_cap *ht_capa = NULL;
5462 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005463
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005464 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5465 return -EINVAL;
5466
5467 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005468 !info->attrs[NL80211_ATTR_SSID] ||
5469 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005470 return -EINVAL;
5471
Johannes Berg4c476992010-10-04 21:36:35 +02005472 if (!rdev->ops->assoc)
5473 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005474
Johannes Berg074ac8d2010-09-16 14:58:22 +02005475 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005476 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5477 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005478
Johannes Berg19957bb2009-07-02 17:20:43 +02005479 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005480
Johannes Berg19957bb2009-07-02 17:20:43 +02005481 chan = ieee80211_get_channel(&rdev->wiphy,
5482 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005483 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5484 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005485
Johannes Berg19957bb2009-07-02 17:20:43 +02005486 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5487 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005488
5489 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005490 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5491 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005492 }
5493
Jouni Malinendc6382c2009-05-06 22:09:37 +03005494 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005495 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005496 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005497 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005498 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005499 else if (mfp != NL80211_MFP_NO)
5500 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005501 }
5502
Johannes Berg3e5d7642009-07-07 14:37:26 +02005503 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5504 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5505
Ben Greear7e7c8922011-11-18 11:31:59 -08005506 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5507 flags |= ASSOC_REQ_DISABLE_HT;
5508
5509 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5510 ht_capa_mask =
5511 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5512
5513 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5514 if (!ht_capa_mask)
5515 return -EINVAL;
5516 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5517 }
5518
Johannes Bergc0692b82010-08-27 14:26:53 +03005519 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005520 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005521 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5522 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005523 &crypto, flags, ht_capa,
5524 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005525
Jouni Malinen636a5d32009-03-19 13:39:22 +02005526 return err;
5527}
5528
5529static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5530{
Johannes Berg4c476992010-10-04 21:36:35 +02005531 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5532 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005533 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005534 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005535 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005536 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005537
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005538 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5539 return -EINVAL;
5540
5541 if (!info->attrs[NL80211_ATTR_MAC])
5542 return -EINVAL;
5543
5544 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5545 return -EINVAL;
5546
Johannes Berg4c476992010-10-04 21:36:35 +02005547 if (!rdev->ops->deauth)
5548 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005549
Johannes Berg074ac8d2010-09-16 14:58:22 +02005550 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005551 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5552 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005553
Johannes Berg19957bb2009-07-02 17:20:43 +02005554 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005555
Johannes Berg19957bb2009-07-02 17:20:43 +02005556 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5557 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005558 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005559 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005560 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005561
5562 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005563 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5564 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005565 }
5566
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005567 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5568
Johannes Berg4c476992010-10-04 21:36:35 +02005569 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5570 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005571}
5572
5573static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5574{
Johannes Berg4c476992010-10-04 21:36:35 +02005575 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5576 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005577 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005578 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005579 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005580 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005581
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005582 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5583 return -EINVAL;
5584
5585 if (!info->attrs[NL80211_ATTR_MAC])
5586 return -EINVAL;
5587
5588 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5589 return -EINVAL;
5590
Johannes Berg4c476992010-10-04 21:36:35 +02005591 if (!rdev->ops->disassoc)
5592 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005593
Johannes Berg074ac8d2010-09-16 14:58:22 +02005594 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005595 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5596 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005597
Johannes Berg19957bb2009-07-02 17:20:43 +02005598 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005599
Johannes Berg19957bb2009-07-02 17:20:43 +02005600 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5601 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005602 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005603 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005604 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005605
5606 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005607 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5608 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005609 }
5610
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005611 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5612
Johannes Berg4c476992010-10-04 21:36:35 +02005613 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5614 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005615}
5616
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005617static bool
5618nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5619 int mcast_rate[IEEE80211_NUM_BANDS],
5620 int rateval)
5621{
5622 struct wiphy *wiphy = &rdev->wiphy;
5623 bool found = false;
5624 int band, i;
5625
5626 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5627 struct ieee80211_supported_band *sband;
5628
5629 sband = wiphy->bands[band];
5630 if (!sband)
5631 continue;
5632
5633 for (i = 0; i < sband->n_bitrates; i++) {
5634 if (sband->bitrates[i].bitrate == rateval) {
5635 mcast_rate[band] = i + 1;
5636 found = true;
5637 break;
5638 }
5639 }
5640 }
5641
5642 return found;
5643}
5644
Johannes Berg04a773a2009-04-19 21:24:32 +02005645static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5646{
Johannes Berg4c476992010-10-04 21:36:35 +02005647 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5648 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005649 struct cfg80211_ibss_params ibss;
5650 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005651 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005652 int err;
5653
Johannes Berg8e30bc52009-04-22 17:45:38 +02005654 memset(&ibss, 0, sizeof(ibss));
5655
Johannes Berg04a773a2009-04-19 21:24:32 +02005656 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5657 return -EINVAL;
5658
Johannes Berg683b6d32012-11-08 21:25:48 +01005659 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005660 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5661 return -EINVAL;
5662
Johannes Berg8e30bc52009-04-22 17:45:38 +02005663 ibss.beacon_interval = 100;
5664
5665 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5666 ibss.beacon_interval =
5667 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5668 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5669 return -EINVAL;
5670 }
5671
Johannes Berg4c476992010-10-04 21:36:35 +02005672 if (!rdev->ops->join_ibss)
5673 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005674
Johannes Berg4c476992010-10-04 21:36:35 +02005675 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5676 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005677
Johannes Berg79c97e92009-07-07 03:56:12 +02005678 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005679
Johannes Berg39193492011-09-16 13:45:25 +02005680 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005681 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005682
5683 if (!is_valid_ether_addr(ibss.bssid))
5684 return -EINVAL;
5685 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005686 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5687 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5688
5689 if (info->attrs[NL80211_ATTR_IE]) {
5690 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5691 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5692 }
5693
Johannes Berg683b6d32012-11-08 21:25:48 +01005694 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5695 if (err)
5696 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005697
Johannes Berg683b6d32012-11-08 21:25:48 +01005698 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005699 return -EINVAL;
5700
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005701 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5702 return -EINVAL;
5703 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5704 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005705 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005706
Johannes Berg04a773a2009-04-19 21:24:32 +02005707 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005708 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005709
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005710 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5711 u8 *rates =
5712 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5713 int n_rates =
5714 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5715 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005716 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005717
Johannes Berg34850ab2011-07-18 18:08:35 +02005718 err = ieee80211_get_ratemask(sband, rates, n_rates,
5719 &ibss.basic_rates);
5720 if (err)
5721 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005722 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005723
5724 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5725 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5726 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5727 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005728
Johannes Berg4c476992010-10-04 21:36:35 +02005729 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305730 bool no_ht = false;
5731
Johannes Berg4c476992010-10-04 21:36:35 +02005732 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305733 info->attrs[NL80211_ATTR_KEYS],
5734 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005735 if (IS_ERR(connkeys))
5736 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305737
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005738 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5739 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305740 kfree(connkeys);
5741 return -EINVAL;
5742 }
Johannes Berg4c476992010-10-04 21:36:35 +02005743 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005744
Antonio Quartulli267335d2012-01-31 20:25:47 +01005745 ibss.control_port =
5746 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5747
Johannes Berg4c476992010-10-04 21:36:35 +02005748 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005749 if (err)
5750 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005751 return err;
5752}
5753
5754static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5755{
Johannes Berg4c476992010-10-04 21:36:35 +02005756 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5757 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005758
Johannes Berg4c476992010-10-04 21:36:35 +02005759 if (!rdev->ops->leave_ibss)
5760 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005761
Johannes Berg4c476992010-10-04 21:36:35 +02005762 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5763 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005764
Johannes Berg4c476992010-10-04 21:36:35 +02005765 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005766}
5767
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005768static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5769{
5770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5771 struct net_device *dev = info->user_ptr[1];
5772 int mcast_rate[IEEE80211_NUM_BANDS];
5773 u32 nla_rate;
5774 int err;
5775
5776 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5777 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5778 return -EOPNOTSUPP;
5779
5780 if (!rdev->ops->set_mcast_rate)
5781 return -EOPNOTSUPP;
5782
5783 memset(mcast_rate, 0, sizeof(mcast_rate));
5784
5785 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5786 return -EINVAL;
5787
5788 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5789 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5790 return -EINVAL;
5791
5792 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5793
5794 return err;
5795}
5796
5797
Johannes Bergaff89a92009-07-01 21:26:51 +02005798#ifdef CONFIG_NL80211_TESTMODE
5799static struct genl_multicast_group nl80211_testmode_mcgrp = {
5800 .name = "testmode",
5801};
5802
5803static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5804{
Johannes Berg4c476992010-10-04 21:36:35 +02005805 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005806 int err;
5807
5808 if (!info->attrs[NL80211_ATTR_TESTDATA])
5809 return -EINVAL;
5810
Johannes Bergaff89a92009-07-01 21:26:51 +02005811 err = -EOPNOTSUPP;
5812 if (rdev->ops->testmode_cmd) {
5813 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005814 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005815 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5816 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5817 rdev->testmode_info = NULL;
5818 }
5819
Johannes Bergaff89a92009-07-01 21:26:51 +02005820 return err;
5821}
5822
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005823static int nl80211_testmode_dump(struct sk_buff *skb,
5824 struct netlink_callback *cb)
5825{
Johannes Berg00918d32011-12-13 17:22:05 +01005826 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005827 int err;
5828 long phy_idx;
5829 void *data = NULL;
5830 int data_len = 0;
5831
5832 if (cb->args[0]) {
5833 /*
5834 * 0 is a valid index, but not valid for args[0],
5835 * so we need to offset by 1.
5836 */
5837 phy_idx = cb->args[0] - 1;
5838 } else {
5839 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5840 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5841 nl80211_policy);
5842 if (err)
5843 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005844
Johannes Berg2bd7e352012-06-15 14:23:16 +02005845 mutex_lock(&cfg80211_mutex);
5846 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5847 nl80211_fam.attrbuf);
5848 if (IS_ERR(rdev)) {
5849 mutex_unlock(&cfg80211_mutex);
5850 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005851 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005852 phy_idx = rdev->wiphy_idx;
5853 rdev = NULL;
5854 mutex_unlock(&cfg80211_mutex);
5855
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005856 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5857 cb->args[1] =
5858 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5859 }
5860
5861 if (cb->args[1]) {
5862 data = nla_data((void *)cb->args[1]);
5863 data_len = nla_len((void *)cb->args[1]);
5864 }
5865
5866 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005867 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5868 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005869 mutex_unlock(&cfg80211_mutex);
5870 return -ENOENT;
5871 }
Johannes Berg00918d32011-12-13 17:22:05 +01005872 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005873 mutex_unlock(&cfg80211_mutex);
5874
Johannes Berg00918d32011-12-13 17:22:05 +01005875 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005876 err = -EOPNOTSUPP;
5877 goto out_err;
5878 }
5879
5880 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005881 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005882 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5883 NL80211_CMD_TESTMODE);
5884 struct nlattr *tmdata;
5885
David S. Miller9360ffd2012-03-29 04:41:26 -04005886 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005887 genlmsg_cancel(skb, hdr);
5888 break;
5889 }
5890
5891 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5892 if (!tmdata) {
5893 genlmsg_cancel(skb, hdr);
5894 break;
5895 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005896 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005897 nla_nest_end(skb, tmdata);
5898
5899 if (err == -ENOBUFS || err == -ENOENT) {
5900 genlmsg_cancel(skb, hdr);
5901 break;
5902 } else if (err) {
5903 genlmsg_cancel(skb, hdr);
5904 goto out_err;
5905 }
5906
5907 genlmsg_end(skb, hdr);
5908 }
5909
5910 err = skb->len;
5911 /* see above */
5912 cb->args[0] = phy_idx + 1;
5913 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005914 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005915 return err;
5916}
5917
Johannes Bergaff89a92009-07-01 21:26:51 +02005918static struct sk_buff *
5919__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005920 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005921{
5922 struct sk_buff *skb;
5923 void *hdr;
5924 struct nlattr *data;
5925
5926 skb = nlmsg_new(approxlen + 100, gfp);
5927 if (!skb)
5928 return NULL;
5929
Eric W. Biederman15e47302012-09-07 20:12:54 +00005930 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005931 if (!hdr) {
5932 kfree_skb(skb);
5933 return NULL;
5934 }
5935
David S. Miller9360ffd2012-03-29 04:41:26 -04005936 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5937 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005938 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5939
5940 ((void **)skb->cb)[0] = rdev;
5941 ((void **)skb->cb)[1] = hdr;
5942 ((void **)skb->cb)[2] = data;
5943
5944 return skb;
5945
5946 nla_put_failure:
5947 kfree_skb(skb);
5948 return NULL;
5949}
5950
5951struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5952 int approxlen)
5953{
5954 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5955
5956 if (WARN_ON(!rdev->testmode_info))
5957 return NULL;
5958
5959 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005960 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005961 rdev->testmode_info->snd_seq,
5962 GFP_KERNEL);
5963}
5964EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5965
5966int cfg80211_testmode_reply(struct sk_buff *skb)
5967{
5968 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5969 void *hdr = ((void **)skb->cb)[1];
5970 struct nlattr *data = ((void **)skb->cb)[2];
5971
5972 if (WARN_ON(!rdev->testmode_info)) {
5973 kfree_skb(skb);
5974 return -EINVAL;
5975 }
5976
5977 nla_nest_end(skb, data);
5978 genlmsg_end(skb, hdr);
5979 return genlmsg_reply(skb, rdev->testmode_info);
5980}
5981EXPORT_SYMBOL(cfg80211_testmode_reply);
5982
5983struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5984 int approxlen, gfp_t gfp)
5985{
5986 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5987
5988 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5989}
5990EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5991
5992void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5993{
5994 void *hdr = ((void **)skb->cb)[1];
5995 struct nlattr *data = ((void **)skb->cb)[2];
5996
5997 nla_nest_end(skb, data);
5998 genlmsg_end(skb, hdr);
5999 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6000}
6001EXPORT_SYMBOL(cfg80211_testmode_event);
6002#endif
6003
Samuel Ortizb23aa672009-07-01 21:26:54 +02006004static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6005{
Johannes Berg4c476992010-10-04 21:36:35 +02006006 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6007 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006008 struct cfg80211_connect_params connect;
6009 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006010 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006011 int err;
6012
6013 memset(&connect, 0, sizeof(connect));
6014
6015 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6016 return -EINVAL;
6017
6018 if (!info->attrs[NL80211_ATTR_SSID] ||
6019 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6020 return -EINVAL;
6021
6022 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6023 connect.auth_type =
6024 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006025 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6026 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006027 return -EINVAL;
6028 } else
6029 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6030
6031 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6032
Johannes Bergc0692b82010-08-27 14:26:53 +03006033 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006034 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006035 if (err)
6036 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006037
Johannes Berg074ac8d2010-09-16 14:58:22 +02006038 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006039 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6040 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006041
Johannes Berg79c97e92009-07-07 03:56:12 +02006042 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006043
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306044 connect.bg_scan_period = -1;
6045 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6046 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6047 connect.bg_scan_period =
6048 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6049 }
6050
Samuel Ortizb23aa672009-07-01 21:26:54 +02006051 if (info->attrs[NL80211_ATTR_MAC])
6052 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6053 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6054 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6055
6056 if (info->attrs[NL80211_ATTR_IE]) {
6057 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6058 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6059 }
6060
Jouni Malinencee00a92013-01-15 17:15:57 +02006061 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6062 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6063 if (connect.mfp != NL80211_MFP_REQUIRED &&
6064 connect.mfp != NL80211_MFP_NO)
6065 return -EINVAL;
6066 } else {
6067 connect.mfp = NL80211_MFP_NO;
6068 }
6069
Samuel Ortizb23aa672009-07-01 21:26:54 +02006070 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6071 connect.channel =
6072 ieee80211_get_channel(wiphy,
6073 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6074 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006075 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6076 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006077 }
6078
Johannes Bergfffd0932009-07-08 14:22:54 +02006079 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6080 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306081 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006082 if (IS_ERR(connkeys))
6083 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006084 }
6085
Ben Greear7e7c8922011-11-18 11:31:59 -08006086 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6087 connect.flags |= ASSOC_REQ_DISABLE_HT;
6088
6089 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6090 memcpy(&connect.ht_capa_mask,
6091 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6092 sizeof(connect.ht_capa_mask));
6093
6094 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006095 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6096 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006097 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006098 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006099 memcpy(&connect.ht_capa,
6100 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6101 sizeof(connect.ht_capa));
6102 }
6103
Johannes Bergfffd0932009-07-08 14:22:54 +02006104 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006105 if (err)
6106 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006107 return err;
6108}
6109
6110static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6111{
Johannes Berg4c476992010-10-04 21:36:35 +02006112 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6113 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006114 u16 reason;
6115
6116 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6117 reason = WLAN_REASON_DEAUTH_LEAVING;
6118 else
6119 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6120
6121 if (reason == 0)
6122 return -EINVAL;
6123
Johannes Berg074ac8d2010-09-16 14:58:22 +02006124 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006125 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6126 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006127
Johannes Berg4c476992010-10-04 21:36:35 +02006128 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006129}
6130
Johannes Berg463d0182009-07-14 00:33:35 +02006131static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6132{
Johannes Berg4c476992010-10-04 21:36:35 +02006133 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006134 struct net *net;
6135 int err;
6136 u32 pid;
6137
6138 if (!info->attrs[NL80211_ATTR_PID])
6139 return -EINVAL;
6140
6141 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6142
Johannes Berg463d0182009-07-14 00:33:35 +02006143 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006144 if (IS_ERR(net))
6145 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006146
6147 err = 0;
6148
6149 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006150 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6151 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006152
Johannes Berg463d0182009-07-14 00:33:35 +02006153 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006154 return err;
6155}
6156
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006157static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6158{
Johannes Berg4c476992010-10-04 21:36:35 +02006159 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006160 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6161 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006162 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006163 struct cfg80211_pmksa pmksa;
6164
6165 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6166
6167 if (!info->attrs[NL80211_ATTR_MAC])
6168 return -EINVAL;
6169
6170 if (!info->attrs[NL80211_ATTR_PMKID])
6171 return -EINVAL;
6172
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006173 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6174 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6175
Johannes Berg074ac8d2010-09-16 14:58:22 +02006176 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006177 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6178 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006179
6180 switch (info->genlhdr->cmd) {
6181 case NL80211_CMD_SET_PMKSA:
6182 rdev_ops = rdev->ops->set_pmksa;
6183 break;
6184 case NL80211_CMD_DEL_PMKSA:
6185 rdev_ops = rdev->ops->del_pmksa;
6186 break;
6187 default:
6188 WARN_ON(1);
6189 break;
6190 }
6191
Johannes Berg4c476992010-10-04 21:36:35 +02006192 if (!rdev_ops)
6193 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006194
Johannes Berg4c476992010-10-04 21:36:35 +02006195 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006196}
6197
6198static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6199{
Johannes Berg4c476992010-10-04 21:36:35 +02006200 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6201 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006202
Johannes Berg074ac8d2010-09-16 14:58:22 +02006203 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006204 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6205 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006206
Johannes Berg4c476992010-10-04 21:36:35 +02006207 if (!rdev->ops->flush_pmksa)
6208 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006209
Hila Gonene35e4d22012-06-27 17:19:42 +03006210 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006211}
6212
Arik Nemtsov109086c2011-09-28 14:12:50 +03006213static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6214{
6215 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6216 struct net_device *dev = info->user_ptr[1];
6217 u8 action_code, dialog_token;
6218 u16 status_code;
6219 u8 *peer;
6220
6221 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6222 !rdev->ops->tdls_mgmt)
6223 return -EOPNOTSUPP;
6224
6225 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6226 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6227 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6228 !info->attrs[NL80211_ATTR_IE] ||
6229 !info->attrs[NL80211_ATTR_MAC])
6230 return -EINVAL;
6231
6232 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6233 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6234 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6235 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6236
Hila Gonene35e4d22012-06-27 17:19:42 +03006237 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6238 dialog_token, status_code,
6239 nla_data(info->attrs[NL80211_ATTR_IE]),
6240 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006241}
6242
6243static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6244{
6245 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6246 struct net_device *dev = info->user_ptr[1];
6247 enum nl80211_tdls_operation operation;
6248 u8 *peer;
6249
6250 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6251 !rdev->ops->tdls_oper)
6252 return -EOPNOTSUPP;
6253
6254 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6255 !info->attrs[NL80211_ATTR_MAC])
6256 return -EINVAL;
6257
6258 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6259 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6260
Hila Gonene35e4d22012-06-27 17:19:42 +03006261 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006262}
6263
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006264static int nl80211_remain_on_channel(struct sk_buff *skb,
6265 struct genl_info *info)
6266{
Johannes Berg4c476992010-10-04 21:36:35 +02006267 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006268 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006269 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006270 struct sk_buff *msg;
6271 void *hdr;
6272 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006273 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006274 int err;
6275
6276 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6277 !info->attrs[NL80211_ATTR_DURATION])
6278 return -EINVAL;
6279
6280 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6281
Johannes Berg7c4ef712011-11-18 15:33:48 +01006282 if (!rdev->ops->remain_on_channel ||
6283 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006284 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006285
Johannes Bergebf348f2012-06-01 12:50:54 +02006286 /*
6287 * We should be on that channel for at least a minimum amount of
6288 * time (10ms) but no longer than the driver supports.
6289 */
6290 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6291 duration > rdev->wiphy.max_remain_on_channel_duration)
6292 return -EINVAL;
6293
Johannes Berg683b6d32012-11-08 21:25:48 +01006294 err = nl80211_parse_chandef(rdev, info, &chandef);
6295 if (err)
6296 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006297
6298 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006299 if (!msg)
6300 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006301
Eric W. Biederman15e47302012-09-07 20:12:54 +00006302 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006303 NL80211_CMD_REMAIN_ON_CHANNEL);
6304
6305 if (IS_ERR(hdr)) {
6306 err = PTR_ERR(hdr);
6307 goto free_msg;
6308 }
6309
Johannes Berg683b6d32012-11-08 21:25:48 +01006310 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6311 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006312
6313 if (err)
6314 goto free_msg;
6315
David S. Miller9360ffd2012-03-29 04:41:26 -04006316 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6317 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006318
6319 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006320
6321 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006322
6323 nla_put_failure:
6324 err = -ENOBUFS;
6325 free_msg:
6326 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006327 return err;
6328}
6329
6330static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6331 struct genl_info *info)
6332{
Johannes Berg4c476992010-10-04 21:36:35 +02006333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006334 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006335 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006336
6337 if (!info->attrs[NL80211_ATTR_COOKIE])
6338 return -EINVAL;
6339
Johannes Berg4c476992010-10-04 21:36:35 +02006340 if (!rdev->ops->cancel_remain_on_channel)
6341 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006342
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006343 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6344
Hila Gonene35e4d22012-06-27 17:19:42 +03006345 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006346}
6347
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006348static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6349 u8 *rates, u8 rates_len)
6350{
6351 u8 i;
6352 u32 mask = 0;
6353
6354 for (i = 0; i < rates_len; i++) {
6355 int rate = (rates[i] & 0x7f) * 5;
6356 int ridx;
6357 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6358 struct ieee80211_rate *srate =
6359 &sband->bitrates[ridx];
6360 if (rate == srate->bitrate) {
6361 mask |= 1 << ridx;
6362 break;
6363 }
6364 }
6365 if (ridx == sband->n_bitrates)
6366 return 0; /* rate not found */
6367 }
6368
6369 return mask;
6370}
6371
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006372static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6373 u8 *rates, u8 rates_len,
6374 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6375{
6376 u8 i;
6377
6378 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6379
6380 for (i = 0; i < rates_len; i++) {
6381 int ridx, rbit;
6382
6383 ridx = rates[i] / 8;
6384 rbit = BIT(rates[i] % 8);
6385
6386 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006387 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006388 return false;
6389
6390 /* check availability */
6391 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6392 mcs[ridx] |= rbit;
6393 else
6394 return false;
6395 }
6396
6397 return true;
6398}
6399
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006400static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006401 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6402 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006403 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6404 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006405};
6406
6407static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6408 struct genl_info *info)
6409{
6410 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006411 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006412 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006413 int rem, i;
6414 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006415 struct nlattr *tx_rates;
6416 struct ieee80211_supported_band *sband;
6417
6418 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6419 return -EINVAL;
6420
Johannes Berg4c476992010-10-04 21:36:35 +02006421 if (!rdev->ops->set_bitrate_mask)
6422 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006423
6424 memset(&mask, 0, sizeof(mask));
6425 /* Default to all rates enabled */
6426 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6427 sband = rdev->wiphy.bands[i];
6428 mask.control[i].legacy =
6429 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006430 if (sband)
6431 memcpy(mask.control[i].mcs,
6432 sband->ht_cap.mcs.rx_mask,
6433 sizeof(mask.control[i].mcs));
6434 else
6435 memset(mask.control[i].mcs, 0,
6436 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006437 }
6438
6439 /*
6440 * The nested attribute uses enum nl80211_band as the index. This maps
6441 * directly to the enum ieee80211_band values used in cfg80211.
6442 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006443 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006444 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6445 {
6446 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006447 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6448 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006449 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006450 if (sband == NULL)
6451 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006452 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6453 nla_len(tx_rates), nl80211_txattr_policy);
6454 if (tb[NL80211_TXRATE_LEGACY]) {
6455 mask.control[band].legacy = rateset_to_mask(
6456 sband,
6457 nla_data(tb[NL80211_TXRATE_LEGACY]),
6458 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306459 if ((mask.control[band].legacy == 0) &&
6460 nla_len(tb[NL80211_TXRATE_LEGACY]))
6461 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006462 }
6463 if (tb[NL80211_TXRATE_MCS]) {
6464 if (!ht_rateset_to_mask(
6465 sband,
6466 nla_data(tb[NL80211_TXRATE_MCS]),
6467 nla_len(tb[NL80211_TXRATE_MCS]),
6468 mask.control[band].mcs))
6469 return -EINVAL;
6470 }
6471
6472 if (mask.control[band].legacy == 0) {
6473 /* don't allow empty legacy rates if HT
6474 * is not even supported. */
6475 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6476 return -EINVAL;
6477
6478 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6479 if (mask.control[band].mcs[i])
6480 break;
6481
6482 /* legacy and mcs rates may not be both empty */
6483 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006484 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006485 }
6486 }
6487
Hila Gonene35e4d22012-06-27 17:19:42 +03006488 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006489}
6490
Johannes Berg2e161f72010-08-12 15:38:38 +02006491static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006492{
Johannes Berg4c476992010-10-04 21:36:35 +02006493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006494 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006495 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006496
6497 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6498 return -EINVAL;
6499
Johannes Berg2e161f72010-08-12 15:38:38 +02006500 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6501 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006502
Johannes Berg71bbc992012-06-15 15:30:18 +02006503 switch (wdev->iftype) {
6504 case NL80211_IFTYPE_STATION:
6505 case NL80211_IFTYPE_ADHOC:
6506 case NL80211_IFTYPE_P2P_CLIENT:
6507 case NL80211_IFTYPE_AP:
6508 case NL80211_IFTYPE_AP_VLAN:
6509 case NL80211_IFTYPE_MESH_POINT:
6510 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006511 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006512 break;
6513 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006514 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006515 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006516
6517 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006518 if (!rdev->ops->mgmt_tx)
6519 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006520
Eric W. Biederman15e47302012-09-07 20:12:54 +00006521 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006522 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6523 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006524}
6525
Johannes Berg2e161f72010-08-12 15:38:38 +02006526static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006527{
Johannes Berg4c476992010-10-04 21:36:35 +02006528 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006529 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006530 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006531 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006532 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006533 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006534 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006535 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006536 bool offchan, no_cck, dont_wait_for_ack;
6537
6538 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006539
Johannes Berg683b6d32012-11-08 21:25:48 +01006540 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006541 return -EINVAL;
6542
Johannes Berg4c476992010-10-04 21:36:35 +02006543 if (!rdev->ops->mgmt_tx)
6544 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006545
Johannes Berg71bbc992012-06-15 15:30:18 +02006546 switch (wdev->iftype) {
6547 case NL80211_IFTYPE_STATION:
6548 case NL80211_IFTYPE_ADHOC:
6549 case NL80211_IFTYPE_P2P_CLIENT:
6550 case NL80211_IFTYPE_AP:
6551 case NL80211_IFTYPE_AP_VLAN:
6552 case NL80211_IFTYPE_MESH_POINT:
6553 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006554 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006555 break;
6556 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006557 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006558 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006559
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006560 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006561 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006562 return -EINVAL;
6563 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006564
6565 /*
6566 * We should wait on the channel for at least a minimum amount
6567 * of time (10ms) but no longer than the driver supports.
6568 */
6569 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6570 wait > rdev->wiphy.max_remain_on_channel_duration)
6571 return -EINVAL;
6572
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006573 }
6574
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006575 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6576
Johannes Berg7c4ef712011-11-18 15:33:48 +01006577 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6578 return -EINVAL;
6579
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306580 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6581
Johannes Berg683b6d32012-11-08 21:25:48 +01006582 err = nl80211_parse_chandef(rdev, info, &chandef);
6583 if (err)
6584 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006585
Johannes Berge247bd902011-11-04 11:18:21 +01006586 if (!dont_wait_for_ack) {
6587 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6588 if (!msg)
6589 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006590
Eric W. Biederman15e47302012-09-07 20:12:54 +00006591 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006592 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006593
Johannes Berge247bd902011-11-04 11:18:21 +01006594 if (IS_ERR(hdr)) {
6595 err = PTR_ERR(hdr);
6596 goto free_msg;
6597 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006598 }
Johannes Berge247bd902011-11-04 11:18:21 +01006599
Johannes Berg683b6d32012-11-08 21:25:48 +01006600 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006601 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6602 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006603 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006604 if (err)
6605 goto free_msg;
6606
Johannes Berge247bd902011-11-04 11:18:21 +01006607 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006608 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6609 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006610
Johannes Berge247bd902011-11-04 11:18:21 +01006611 genlmsg_end(msg, hdr);
6612 return genlmsg_reply(msg, info);
6613 }
6614
6615 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006616
6617 nla_put_failure:
6618 err = -ENOBUFS;
6619 free_msg:
6620 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006621 return err;
6622}
6623
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006624static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6625{
6626 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006627 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006628 u64 cookie;
6629
6630 if (!info->attrs[NL80211_ATTR_COOKIE])
6631 return -EINVAL;
6632
6633 if (!rdev->ops->mgmt_tx_cancel_wait)
6634 return -EOPNOTSUPP;
6635
Johannes Berg71bbc992012-06-15 15:30:18 +02006636 switch (wdev->iftype) {
6637 case NL80211_IFTYPE_STATION:
6638 case NL80211_IFTYPE_ADHOC:
6639 case NL80211_IFTYPE_P2P_CLIENT:
6640 case NL80211_IFTYPE_AP:
6641 case NL80211_IFTYPE_AP_VLAN:
6642 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006643 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006644 break;
6645 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006646 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006647 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006648
6649 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6650
Hila Gonene35e4d22012-06-27 17:19:42 +03006651 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006652}
6653
Kalle Valoffb9eb32010-02-17 17:58:10 +02006654static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6655{
Johannes Berg4c476992010-10-04 21:36:35 +02006656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006657 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006658 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006659 u8 ps_state;
6660 bool state;
6661 int err;
6662
Johannes Berg4c476992010-10-04 21:36:35 +02006663 if (!info->attrs[NL80211_ATTR_PS_STATE])
6664 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006665
6666 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6667
Johannes Berg4c476992010-10-04 21:36:35 +02006668 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6669 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006670
6671 wdev = dev->ieee80211_ptr;
6672
Johannes Berg4c476992010-10-04 21:36:35 +02006673 if (!rdev->ops->set_power_mgmt)
6674 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006675
6676 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6677
6678 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006679 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006680
Hila Gonene35e4d22012-06-27 17:19:42 +03006681 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006682 if (!err)
6683 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006684 return err;
6685}
6686
6687static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6688{
Johannes Berg4c476992010-10-04 21:36:35 +02006689 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006690 enum nl80211_ps_state ps_state;
6691 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006692 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006693 struct sk_buff *msg;
6694 void *hdr;
6695 int err;
6696
Kalle Valoffb9eb32010-02-17 17:58:10 +02006697 wdev = dev->ieee80211_ptr;
6698
Johannes Berg4c476992010-10-04 21:36:35 +02006699 if (!rdev->ops->set_power_mgmt)
6700 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006701
6702 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006703 if (!msg)
6704 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006705
Eric W. Biederman15e47302012-09-07 20:12:54 +00006706 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006707 NL80211_CMD_GET_POWER_SAVE);
6708 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006709 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006710 goto free_msg;
6711 }
6712
6713 if (wdev->ps)
6714 ps_state = NL80211_PS_ENABLED;
6715 else
6716 ps_state = NL80211_PS_DISABLED;
6717
David S. Miller9360ffd2012-03-29 04:41:26 -04006718 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6719 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006720
6721 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006722 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006723
Johannes Berg4c476992010-10-04 21:36:35 +02006724 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006725 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006726 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006727 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006728 return err;
6729}
6730
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006731static struct nla_policy
6732nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6733 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6734 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6735 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006736 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6737 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6738 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006739};
6740
Thomas Pedersen84f10702012-07-12 16:17:33 -07006741static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006742 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006743{
6744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6745 struct wireless_dev *wdev;
6746 struct net_device *dev = info->user_ptr[1];
6747
Johannes Bergd9d8b012012-11-26 12:51:52 +01006748 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006749 return -EINVAL;
6750
6751 wdev = dev->ieee80211_ptr;
6752
6753 if (!rdev->ops->set_cqm_txe_config)
6754 return -EOPNOTSUPP;
6755
6756 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6757 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6758 return -EOPNOTSUPP;
6759
Hila Gonene35e4d22012-06-27 17:19:42 +03006760 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006761}
6762
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006763static int nl80211_set_cqm_rssi(struct genl_info *info,
6764 s32 threshold, u32 hysteresis)
6765{
Johannes Berg4c476992010-10-04 21:36:35 +02006766 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006767 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006768 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006769
6770 if (threshold > 0)
6771 return -EINVAL;
6772
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006773 wdev = dev->ieee80211_ptr;
6774
Johannes Berg4c476992010-10-04 21:36:35 +02006775 if (!rdev->ops->set_cqm_rssi_config)
6776 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006777
Johannes Berg074ac8d2010-09-16 14:58:22 +02006778 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006779 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6780 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006781
Hila Gonene35e4d22012-06-27 17:19:42 +03006782 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006783}
6784
6785static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6786{
6787 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6788 struct nlattr *cqm;
6789 int err;
6790
6791 cqm = info->attrs[NL80211_ATTR_CQM];
6792 if (!cqm) {
6793 err = -EINVAL;
6794 goto out;
6795 }
6796
6797 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6798 nl80211_attr_cqm_policy);
6799 if (err)
6800 goto out;
6801
6802 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6803 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6804 s32 threshold;
6805 u32 hysteresis;
6806 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6807 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6808 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006809 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6810 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6811 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6812 u32 rate, pkts, intvl;
6813 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6814 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6815 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6816 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006817 } else
6818 err = -EINVAL;
6819
6820out:
6821 return err;
6822}
6823
Johannes Berg29cbe682010-12-03 09:20:44 +01006824static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6825{
6826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6827 struct net_device *dev = info->user_ptr[1];
6828 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006829 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006830 int err;
6831
6832 /* start with default */
6833 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006834 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006835
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006836 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006837 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006838 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006839 if (err)
6840 return err;
6841 }
6842
6843 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6844 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6845 return -EINVAL;
6846
Javier Cardonac80d5452010-12-16 17:37:49 -08006847 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6848 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6849
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006850 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6851 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6852 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6853 return -EINVAL;
6854
Marco Porsch9bdbf042013-01-07 16:04:51 +01006855 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6856 setup.beacon_interval =
6857 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6858 if (setup.beacon_interval < 10 ||
6859 setup.beacon_interval > 10000)
6860 return -EINVAL;
6861 }
6862
6863 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
6864 setup.dtim_period =
6865 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
6866 if (setup.dtim_period < 1 || setup.dtim_period > 100)
6867 return -EINVAL;
6868 }
6869
Javier Cardonac80d5452010-12-16 17:37:49 -08006870 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6871 /* parse additional setup parameters if given */
6872 err = nl80211_parse_mesh_setup(info, &setup);
6873 if (err)
6874 return err;
6875 }
6876
Johannes Bergcc1d2802012-05-16 23:50:20 +02006877 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01006878 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
6879 if (err)
6880 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006881 } else {
6882 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01006883 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006884 }
6885
Javier Cardonac80d5452010-12-16 17:37:49 -08006886 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006887}
6888
6889static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6890{
6891 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6892 struct net_device *dev = info->user_ptr[1];
6893
6894 return cfg80211_leave_mesh(rdev, dev);
6895}
6896
Johannes Bergdfb89c52012-06-27 09:23:48 +02006897#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006898static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6899{
6900 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6901 struct sk_buff *msg;
6902 void *hdr;
6903
6904 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6905 return -EOPNOTSUPP;
6906
6907 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6908 if (!msg)
6909 return -ENOMEM;
6910
Eric W. Biederman15e47302012-09-07 20:12:54 +00006911 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006912 NL80211_CMD_GET_WOWLAN);
6913 if (!hdr)
6914 goto nla_put_failure;
6915
6916 if (rdev->wowlan) {
6917 struct nlattr *nl_wowlan;
6918
6919 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6920 if (!nl_wowlan)
6921 goto nla_put_failure;
6922
David S. Miller9360ffd2012-03-29 04:41:26 -04006923 if ((rdev->wowlan->any &&
6924 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6925 (rdev->wowlan->disconnect &&
6926 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6927 (rdev->wowlan->magic_pkt &&
6928 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6929 (rdev->wowlan->gtk_rekey_failure &&
6930 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6931 (rdev->wowlan->eap_identity_req &&
6932 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6933 (rdev->wowlan->four_way_handshake &&
6934 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6935 (rdev->wowlan->rfkill_release &&
6936 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6937 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006938 if (rdev->wowlan->n_patterns) {
6939 struct nlattr *nl_pats, *nl_pat;
6940 int i, pat_len;
6941
6942 nl_pats = nla_nest_start(msg,
6943 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6944 if (!nl_pats)
6945 goto nla_put_failure;
6946
6947 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6948 nl_pat = nla_nest_start(msg, i + 1);
6949 if (!nl_pat)
6950 goto nla_put_failure;
6951 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006952 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6953 DIV_ROUND_UP(pat_len, 8),
6954 rdev->wowlan->patterns[i].mask) ||
6955 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6956 pat_len,
6957 rdev->wowlan->patterns[i].pattern))
6958 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006959 nla_nest_end(msg, nl_pat);
6960 }
6961 nla_nest_end(msg, nl_pats);
6962 }
6963
6964 nla_nest_end(msg, nl_wowlan);
6965 }
6966
6967 genlmsg_end(msg, hdr);
6968 return genlmsg_reply(msg, info);
6969
6970nla_put_failure:
6971 nlmsg_free(msg);
6972 return -ENOBUFS;
6973}
6974
6975static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6976{
6977 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6978 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006979 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006980 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006981 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6982 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006983 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006984
6985 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6986 return -EOPNOTSUPP;
6987
Johannes Bergae33bd82012-07-12 16:25:02 +02006988 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6989 cfg80211_rdev_free_wowlan(rdev);
6990 rdev->wowlan = NULL;
6991 goto set_wakeup;
6992 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006993
6994 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6995 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6996 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6997 nl80211_wowlan_policy);
6998 if (err)
6999 return err;
7000
7001 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7002 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7003 return -EINVAL;
7004 new_triggers.any = true;
7005 }
7006
7007 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7008 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7009 return -EINVAL;
7010 new_triggers.disconnect = true;
7011 }
7012
7013 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7014 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7015 return -EINVAL;
7016 new_triggers.magic_pkt = true;
7017 }
7018
Johannes Berg77dbbb12011-07-13 10:48:55 +02007019 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7020 return -EINVAL;
7021
7022 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7023 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7024 return -EINVAL;
7025 new_triggers.gtk_rekey_failure = true;
7026 }
7027
7028 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7029 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7030 return -EINVAL;
7031 new_triggers.eap_identity_req = true;
7032 }
7033
7034 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7035 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7036 return -EINVAL;
7037 new_triggers.four_way_handshake = true;
7038 }
7039
7040 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7041 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7042 return -EINVAL;
7043 new_triggers.rfkill_release = true;
7044 }
7045
Johannes Bergff1b6e62011-05-04 15:37:28 +02007046 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7047 struct nlattr *pat;
7048 int n_patterns = 0;
7049 int rem, pat_len, mask_len;
7050 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7051
7052 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7053 rem)
7054 n_patterns++;
7055 if (n_patterns > wowlan->n_patterns)
7056 return -EINVAL;
7057
7058 new_triggers.patterns = kcalloc(n_patterns,
7059 sizeof(new_triggers.patterns[0]),
7060 GFP_KERNEL);
7061 if (!new_triggers.patterns)
7062 return -ENOMEM;
7063
7064 new_triggers.n_patterns = n_patterns;
7065 i = 0;
7066
7067 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7068 rem) {
7069 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7070 nla_data(pat), nla_len(pat), NULL);
7071 err = -EINVAL;
7072 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7073 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7074 goto error;
7075 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7076 mask_len = DIV_ROUND_UP(pat_len, 8);
7077 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7078 mask_len)
7079 goto error;
7080 if (pat_len > wowlan->pattern_max_len ||
7081 pat_len < wowlan->pattern_min_len)
7082 goto error;
7083
7084 new_triggers.patterns[i].mask =
7085 kmalloc(mask_len + pat_len, GFP_KERNEL);
7086 if (!new_triggers.patterns[i].mask) {
7087 err = -ENOMEM;
7088 goto error;
7089 }
7090 new_triggers.patterns[i].pattern =
7091 new_triggers.patterns[i].mask + mask_len;
7092 memcpy(new_triggers.patterns[i].mask,
7093 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7094 mask_len);
7095 new_triggers.patterns[i].pattern_len = pat_len;
7096 memcpy(new_triggers.patterns[i].pattern,
7097 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7098 pat_len);
7099 i++;
7100 }
7101 }
7102
Johannes Bergae33bd82012-07-12 16:25:02 +02007103 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7104 if (!ntrig) {
7105 err = -ENOMEM;
7106 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007107 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007108 cfg80211_rdev_free_wowlan(rdev);
7109 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007110
Johannes Bergae33bd82012-07-12 16:25:02 +02007111 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007112 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007113 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007114
Johannes Bergff1b6e62011-05-04 15:37:28 +02007115 return 0;
7116 error:
7117 for (i = 0; i < new_triggers.n_patterns; i++)
7118 kfree(new_triggers.patterns[i].mask);
7119 kfree(new_triggers.patterns);
7120 return err;
7121}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007122#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007123
Johannes Berge5497d72011-07-05 16:35:40 +02007124static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7125{
7126 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7127 struct net_device *dev = info->user_ptr[1];
7128 struct wireless_dev *wdev = dev->ieee80211_ptr;
7129 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7130 struct cfg80211_gtk_rekey_data rekey_data;
7131 int err;
7132
7133 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7134 return -EINVAL;
7135
7136 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7137 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7138 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7139 nl80211_rekey_policy);
7140 if (err)
7141 return err;
7142
7143 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7144 return -ERANGE;
7145 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7146 return -ERANGE;
7147 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7148 return -ERANGE;
7149
7150 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7151 NL80211_KEK_LEN);
7152 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7153 NL80211_KCK_LEN);
7154 memcpy(rekey_data.replay_ctr,
7155 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7156 NL80211_REPLAY_CTR_LEN);
7157
7158 wdev_lock(wdev);
7159 if (!wdev->current_bss) {
7160 err = -ENOTCONN;
7161 goto out;
7162 }
7163
7164 if (!rdev->ops->set_rekey_data) {
7165 err = -EOPNOTSUPP;
7166 goto out;
7167 }
7168
Hila Gonene35e4d22012-06-27 17:19:42 +03007169 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007170 out:
7171 wdev_unlock(wdev);
7172 return err;
7173}
7174
Johannes Berg28946da2011-11-04 11:18:12 +01007175static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7176 struct genl_info *info)
7177{
7178 struct net_device *dev = info->user_ptr[1];
7179 struct wireless_dev *wdev = dev->ieee80211_ptr;
7180
7181 if (wdev->iftype != NL80211_IFTYPE_AP &&
7182 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7183 return -EINVAL;
7184
Eric W. Biederman15e47302012-09-07 20:12:54 +00007185 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007186 return -EBUSY;
7187
Eric W. Biederman15e47302012-09-07 20:12:54 +00007188 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007189 return 0;
7190}
7191
Johannes Berg7f6cf312011-11-04 11:18:15 +01007192static int nl80211_probe_client(struct sk_buff *skb,
7193 struct genl_info *info)
7194{
7195 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7196 struct net_device *dev = info->user_ptr[1];
7197 struct wireless_dev *wdev = dev->ieee80211_ptr;
7198 struct sk_buff *msg;
7199 void *hdr;
7200 const u8 *addr;
7201 u64 cookie;
7202 int err;
7203
7204 if (wdev->iftype != NL80211_IFTYPE_AP &&
7205 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7206 return -EOPNOTSUPP;
7207
7208 if (!info->attrs[NL80211_ATTR_MAC])
7209 return -EINVAL;
7210
7211 if (!rdev->ops->probe_client)
7212 return -EOPNOTSUPP;
7213
7214 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7215 if (!msg)
7216 return -ENOMEM;
7217
Eric W. Biederman15e47302012-09-07 20:12:54 +00007218 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007219 NL80211_CMD_PROBE_CLIENT);
7220
7221 if (IS_ERR(hdr)) {
7222 err = PTR_ERR(hdr);
7223 goto free_msg;
7224 }
7225
7226 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7227
Hila Gonene35e4d22012-06-27 17:19:42 +03007228 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007229 if (err)
7230 goto free_msg;
7231
David S. Miller9360ffd2012-03-29 04:41:26 -04007232 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7233 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007234
7235 genlmsg_end(msg, hdr);
7236
7237 return genlmsg_reply(msg, info);
7238
7239 nla_put_failure:
7240 err = -ENOBUFS;
7241 free_msg:
7242 nlmsg_free(msg);
7243 return err;
7244}
7245
Johannes Berg5e760232011-11-04 11:18:17 +01007246static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7247{
7248 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007249 struct cfg80211_beacon_registration *reg, *nreg;
7250 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007251
7252 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7253 return -EOPNOTSUPP;
7254
Ben Greear37c73b52012-10-26 14:49:25 -07007255 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7256 if (!nreg)
7257 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007258
Ben Greear37c73b52012-10-26 14:49:25 -07007259 /* First, check if already registered. */
7260 spin_lock_bh(&rdev->beacon_registrations_lock);
7261 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7262 if (reg->nlportid == info->snd_portid) {
7263 rv = -EALREADY;
7264 goto out_err;
7265 }
7266 }
7267 /* Add it to the list */
7268 nreg->nlportid = info->snd_portid;
7269 list_add(&nreg->list, &rdev->beacon_registrations);
7270
7271 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007272
7273 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007274out_err:
7275 spin_unlock_bh(&rdev->beacon_registrations_lock);
7276 kfree(nreg);
7277 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007278}
7279
Johannes Berg98104fde2012-06-16 00:19:54 +02007280static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7281{
7282 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7283 struct wireless_dev *wdev = info->user_ptr[1];
7284 int err;
7285
7286 if (!rdev->ops->start_p2p_device)
7287 return -EOPNOTSUPP;
7288
7289 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7290 return -EOPNOTSUPP;
7291
7292 if (wdev->p2p_started)
7293 return 0;
7294
7295 mutex_lock(&rdev->devlist_mtx);
7296 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7297 mutex_unlock(&rdev->devlist_mtx);
7298 if (err)
7299 return err;
7300
Johannes Bergeeb126e2012-10-23 15:16:50 +02007301 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007302 if (err)
7303 return err;
7304
7305 wdev->p2p_started = true;
7306 mutex_lock(&rdev->devlist_mtx);
7307 rdev->opencount++;
7308 mutex_unlock(&rdev->devlist_mtx);
7309
7310 return 0;
7311}
7312
7313static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7314{
7315 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7316 struct wireless_dev *wdev = info->user_ptr[1];
7317
7318 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7319 return -EOPNOTSUPP;
7320
7321 if (!rdev->ops->stop_p2p_device)
7322 return -EOPNOTSUPP;
7323
7324 if (!wdev->p2p_started)
7325 return 0;
7326
Johannes Bergeeb126e2012-10-23 15:16:50 +02007327 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007328 wdev->p2p_started = false;
7329
7330 mutex_lock(&rdev->devlist_mtx);
7331 rdev->opencount--;
7332 mutex_unlock(&rdev->devlist_mtx);
7333
7334 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7335 rdev->scan_req->aborted = true;
7336 ___cfg80211_scan_done(rdev, true);
7337 }
7338
7339 return 0;
7340}
7341
Johannes Berg4c476992010-10-04 21:36:35 +02007342#define NL80211_FLAG_NEED_WIPHY 0x01
7343#define NL80211_FLAG_NEED_NETDEV 0x02
7344#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007345#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7346#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7347 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007348#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007349/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007350#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7351 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007352
7353static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7354 struct genl_info *info)
7355{
7356 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007357 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007358 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007359 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7360
7361 if (rtnl)
7362 rtnl_lock();
7363
7364 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007365 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007366 if (IS_ERR(rdev)) {
7367 if (rtnl)
7368 rtnl_unlock();
7369 return PTR_ERR(rdev);
7370 }
7371 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007372 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7373 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007374 mutex_lock(&cfg80211_mutex);
7375 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7376 info->attrs);
7377 if (IS_ERR(wdev)) {
7378 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007379 if (rtnl)
7380 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007381 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007382 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007383
Johannes Berg89a54e42012-06-15 14:33:17 +02007384 dev = wdev->netdev;
7385 rdev = wiphy_to_dev(wdev->wiphy);
7386
Johannes Berg1bf614e2012-06-15 15:23:36 +02007387 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7388 if (!dev) {
7389 mutex_unlock(&cfg80211_mutex);
7390 if (rtnl)
7391 rtnl_unlock();
7392 return -EINVAL;
7393 }
7394
7395 info->user_ptr[1] = dev;
7396 } else {
7397 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007398 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007399
Johannes Berg1bf614e2012-06-15 15:23:36 +02007400 if (dev) {
7401 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7402 !netif_running(dev)) {
7403 mutex_unlock(&cfg80211_mutex);
7404 if (rtnl)
7405 rtnl_unlock();
7406 return -ENETDOWN;
7407 }
7408
7409 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007410 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7411 if (!wdev->p2p_started) {
7412 mutex_unlock(&cfg80211_mutex);
7413 if (rtnl)
7414 rtnl_unlock();
7415 return -ENETDOWN;
7416 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007417 }
7418
Johannes Berg89a54e42012-06-15 14:33:17 +02007419 cfg80211_lock_rdev(rdev);
7420
7421 mutex_unlock(&cfg80211_mutex);
7422
Johannes Berg4c476992010-10-04 21:36:35 +02007423 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007424 }
7425
7426 return 0;
7427}
7428
7429static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7430 struct genl_info *info)
7431{
7432 if (info->user_ptr[0])
7433 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007434 if (info->user_ptr[1]) {
7435 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7436 struct wireless_dev *wdev = info->user_ptr[1];
7437
7438 if (wdev->netdev)
7439 dev_put(wdev->netdev);
7440 } else {
7441 dev_put(info->user_ptr[1]);
7442 }
7443 }
Johannes Berg4c476992010-10-04 21:36:35 +02007444 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7445 rtnl_unlock();
7446}
7447
Johannes Berg55682962007-09-20 13:09:35 -04007448static struct genl_ops nl80211_ops[] = {
7449 {
7450 .cmd = NL80211_CMD_GET_WIPHY,
7451 .doit = nl80211_get_wiphy,
7452 .dumpit = nl80211_dump_wiphy,
7453 .policy = nl80211_policy,
7454 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007455 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007456 },
7457 {
7458 .cmd = NL80211_CMD_SET_WIPHY,
7459 .doit = nl80211_set_wiphy,
7460 .policy = nl80211_policy,
7461 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007462 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007463 },
7464 {
7465 .cmd = NL80211_CMD_GET_INTERFACE,
7466 .doit = nl80211_get_interface,
7467 .dumpit = nl80211_dump_interface,
7468 .policy = nl80211_policy,
7469 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007470 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007471 },
7472 {
7473 .cmd = NL80211_CMD_SET_INTERFACE,
7474 .doit = nl80211_set_interface,
7475 .policy = nl80211_policy,
7476 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007477 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7478 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007479 },
7480 {
7481 .cmd = NL80211_CMD_NEW_INTERFACE,
7482 .doit = nl80211_new_interface,
7483 .policy = nl80211_policy,
7484 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007485 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7486 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007487 },
7488 {
7489 .cmd = NL80211_CMD_DEL_INTERFACE,
7490 .doit = nl80211_del_interface,
7491 .policy = nl80211_policy,
7492 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007493 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007494 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007495 },
Johannes Berg41ade002007-12-19 02:03:29 +01007496 {
7497 .cmd = NL80211_CMD_GET_KEY,
7498 .doit = nl80211_get_key,
7499 .policy = nl80211_policy,
7500 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007501 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007502 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007503 },
7504 {
7505 .cmd = NL80211_CMD_SET_KEY,
7506 .doit = nl80211_set_key,
7507 .policy = nl80211_policy,
7508 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007509 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007510 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007511 },
7512 {
7513 .cmd = NL80211_CMD_NEW_KEY,
7514 .doit = nl80211_new_key,
7515 .policy = nl80211_policy,
7516 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007517 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007518 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007519 },
7520 {
7521 .cmd = NL80211_CMD_DEL_KEY,
7522 .doit = nl80211_del_key,
7523 .policy = nl80211_policy,
7524 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007525 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007526 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007527 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007528 {
7529 .cmd = NL80211_CMD_SET_BEACON,
7530 .policy = nl80211_policy,
7531 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007532 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007533 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007534 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007535 },
7536 {
Johannes Berg88600202012-02-13 15:17:18 +01007537 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007538 .policy = nl80211_policy,
7539 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007540 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007541 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007542 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007543 },
7544 {
Johannes Berg88600202012-02-13 15:17:18 +01007545 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007546 .policy = nl80211_policy,
7547 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007548 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007549 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007550 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007551 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007552 {
7553 .cmd = NL80211_CMD_GET_STATION,
7554 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007555 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007556 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007557 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7558 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007559 },
7560 {
7561 .cmd = NL80211_CMD_SET_STATION,
7562 .doit = nl80211_set_station,
7563 .policy = nl80211_policy,
7564 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007565 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007566 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007567 },
7568 {
7569 .cmd = NL80211_CMD_NEW_STATION,
7570 .doit = nl80211_new_station,
7571 .policy = nl80211_policy,
7572 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007573 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007574 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007575 },
7576 {
7577 .cmd = NL80211_CMD_DEL_STATION,
7578 .doit = nl80211_del_station,
7579 .policy = nl80211_policy,
7580 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007581 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007582 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007583 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007584 {
7585 .cmd = NL80211_CMD_GET_MPATH,
7586 .doit = nl80211_get_mpath,
7587 .dumpit = nl80211_dump_mpath,
7588 .policy = nl80211_policy,
7589 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007590 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007591 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007592 },
7593 {
7594 .cmd = NL80211_CMD_SET_MPATH,
7595 .doit = nl80211_set_mpath,
7596 .policy = nl80211_policy,
7597 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007598 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007599 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007600 },
7601 {
7602 .cmd = NL80211_CMD_NEW_MPATH,
7603 .doit = nl80211_new_mpath,
7604 .policy = nl80211_policy,
7605 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007606 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007607 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007608 },
7609 {
7610 .cmd = NL80211_CMD_DEL_MPATH,
7611 .doit = nl80211_del_mpath,
7612 .policy = nl80211_policy,
7613 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007614 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007615 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007616 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007617 {
7618 .cmd = NL80211_CMD_SET_BSS,
7619 .doit = nl80211_set_bss,
7620 .policy = nl80211_policy,
7621 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007622 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007623 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007624 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007625 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007626 .cmd = NL80211_CMD_GET_REG,
7627 .doit = nl80211_get_reg,
7628 .policy = nl80211_policy,
7629 /* can be retrieved by unprivileged users */
7630 },
7631 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007632 .cmd = NL80211_CMD_SET_REG,
7633 .doit = nl80211_set_reg,
7634 .policy = nl80211_policy,
7635 .flags = GENL_ADMIN_PERM,
7636 },
7637 {
7638 .cmd = NL80211_CMD_REQ_SET_REG,
7639 .doit = nl80211_req_set_reg,
7640 .policy = nl80211_policy,
7641 .flags = GENL_ADMIN_PERM,
7642 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007643 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007644 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7645 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007646 .policy = nl80211_policy,
7647 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007648 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007649 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007650 },
7651 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007652 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7653 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007654 .policy = nl80211_policy,
7655 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007656 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007657 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007658 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007659 {
Johannes Berg2a519312009-02-10 21:25:55 +01007660 .cmd = NL80211_CMD_TRIGGER_SCAN,
7661 .doit = nl80211_trigger_scan,
7662 .policy = nl80211_policy,
7663 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007664 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007665 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007666 },
7667 {
7668 .cmd = NL80211_CMD_GET_SCAN,
7669 .policy = nl80211_policy,
7670 .dumpit = nl80211_dump_scan,
7671 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007672 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007673 .cmd = NL80211_CMD_START_SCHED_SCAN,
7674 .doit = nl80211_start_sched_scan,
7675 .policy = nl80211_policy,
7676 .flags = GENL_ADMIN_PERM,
7677 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7678 NL80211_FLAG_NEED_RTNL,
7679 },
7680 {
7681 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7682 .doit = nl80211_stop_sched_scan,
7683 .policy = nl80211_policy,
7684 .flags = GENL_ADMIN_PERM,
7685 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7686 NL80211_FLAG_NEED_RTNL,
7687 },
7688 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007689 .cmd = NL80211_CMD_AUTHENTICATE,
7690 .doit = nl80211_authenticate,
7691 .policy = nl80211_policy,
7692 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007693 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007694 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007695 },
7696 {
7697 .cmd = NL80211_CMD_ASSOCIATE,
7698 .doit = nl80211_associate,
7699 .policy = nl80211_policy,
7700 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007701 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007702 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007703 },
7704 {
7705 .cmd = NL80211_CMD_DEAUTHENTICATE,
7706 .doit = nl80211_deauthenticate,
7707 .policy = nl80211_policy,
7708 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007709 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007710 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007711 },
7712 {
7713 .cmd = NL80211_CMD_DISASSOCIATE,
7714 .doit = nl80211_disassociate,
7715 .policy = nl80211_policy,
7716 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007717 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007718 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007719 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007720 {
7721 .cmd = NL80211_CMD_JOIN_IBSS,
7722 .doit = nl80211_join_ibss,
7723 .policy = nl80211_policy,
7724 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007725 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007726 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007727 },
7728 {
7729 .cmd = NL80211_CMD_LEAVE_IBSS,
7730 .doit = nl80211_leave_ibss,
7731 .policy = nl80211_policy,
7732 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007733 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007734 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007735 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007736#ifdef CONFIG_NL80211_TESTMODE
7737 {
7738 .cmd = NL80211_CMD_TESTMODE,
7739 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007740 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007741 .policy = nl80211_policy,
7742 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007743 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7744 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007745 },
7746#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007747 {
7748 .cmd = NL80211_CMD_CONNECT,
7749 .doit = nl80211_connect,
7750 .policy = nl80211_policy,
7751 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007752 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007753 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007754 },
7755 {
7756 .cmd = NL80211_CMD_DISCONNECT,
7757 .doit = nl80211_disconnect,
7758 .policy = nl80211_policy,
7759 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007760 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007761 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007762 },
Johannes Berg463d0182009-07-14 00:33:35 +02007763 {
7764 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7765 .doit = nl80211_wiphy_netns,
7766 .policy = nl80211_policy,
7767 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007768 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7769 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007770 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007771 {
7772 .cmd = NL80211_CMD_GET_SURVEY,
7773 .policy = nl80211_policy,
7774 .dumpit = nl80211_dump_survey,
7775 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007776 {
7777 .cmd = NL80211_CMD_SET_PMKSA,
7778 .doit = nl80211_setdel_pmksa,
7779 .policy = nl80211_policy,
7780 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007781 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007782 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007783 },
7784 {
7785 .cmd = NL80211_CMD_DEL_PMKSA,
7786 .doit = nl80211_setdel_pmksa,
7787 .policy = nl80211_policy,
7788 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007789 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007790 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007791 },
7792 {
7793 .cmd = NL80211_CMD_FLUSH_PMKSA,
7794 .doit = nl80211_flush_pmksa,
7795 .policy = nl80211_policy,
7796 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007797 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007798 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007799 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007800 {
7801 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7802 .doit = nl80211_remain_on_channel,
7803 .policy = nl80211_policy,
7804 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007805 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007806 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007807 },
7808 {
7809 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7810 .doit = nl80211_cancel_remain_on_channel,
7811 .policy = nl80211_policy,
7812 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007813 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007814 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007815 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007816 {
7817 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7818 .doit = nl80211_set_tx_bitrate_mask,
7819 .policy = nl80211_policy,
7820 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007821 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7822 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007823 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007824 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007825 .cmd = NL80211_CMD_REGISTER_FRAME,
7826 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007827 .policy = nl80211_policy,
7828 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007829 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007830 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007831 },
7832 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007833 .cmd = NL80211_CMD_FRAME,
7834 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007835 .policy = nl80211_policy,
7836 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007837 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007838 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007839 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007840 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007841 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7842 .doit = nl80211_tx_mgmt_cancel_wait,
7843 .policy = nl80211_policy,
7844 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007845 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007846 NL80211_FLAG_NEED_RTNL,
7847 },
7848 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007849 .cmd = NL80211_CMD_SET_POWER_SAVE,
7850 .doit = nl80211_set_power_save,
7851 .policy = nl80211_policy,
7852 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007853 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7854 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007855 },
7856 {
7857 .cmd = NL80211_CMD_GET_POWER_SAVE,
7858 .doit = nl80211_get_power_save,
7859 .policy = nl80211_policy,
7860 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007861 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7862 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007863 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007864 {
7865 .cmd = NL80211_CMD_SET_CQM,
7866 .doit = nl80211_set_cqm,
7867 .policy = nl80211_policy,
7868 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007869 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7870 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007871 },
Johannes Bergf444de02010-05-05 15:25:02 +02007872 {
7873 .cmd = NL80211_CMD_SET_CHANNEL,
7874 .doit = nl80211_set_channel,
7875 .policy = nl80211_policy,
7876 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007877 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7878 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007879 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007880 {
7881 .cmd = NL80211_CMD_SET_WDS_PEER,
7882 .doit = nl80211_set_wds_peer,
7883 .policy = nl80211_policy,
7884 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007885 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7886 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007887 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007888 {
7889 .cmd = NL80211_CMD_JOIN_MESH,
7890 .doit = nl80211_join_mesh,
7891 .policy = nl80211_policy,
7892 .flags = GENL_ADMIN_PERM,
7893 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7894 NL80211_FLAG_NEED_RTNL,
7895 },
7896 {
7897 .cmd = NL80211_CMD_LEAVE_MESH,
7898 .doit = nl80211_leave_mesh,
7899 .policy = nl80211_policy,
7900 .flags = GENL_ADMIN_PERM,
7901 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7902 NL80211_FLAG_NEED_RTNL,
7903 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007904#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007905 {
7906 .cmd = NL80211_CMD_GET_WOWLAN,
7907 .doit = nl80211_get_wowlan,
7908 .policy = nl80211_policy,
7909 /* can be retrieved by unprivileged users */
7910 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7911 NL80211_FLAG_NEED_RTNL,
7912 },
7913 {
7914 .cmd = NL80211_CMD_SET_WOWLAN,
7915 .doit = nl80211_set_wowlan,
7916 .policy = nl80211_policy,
7917 .flags = GENL_ADMIN_PERM,
7918 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7919 NL80211_FLAG_NEED_RTNL,
7920 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007921#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007922 {
7923 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7924 .doit = nl80211_set_rekey_data,
7925 .policy = nl80211_policy,
7926 .flags = GENL_ADMIN_PERM,
7927 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7928 NL80211_FLAG_NEED_RTNL,
7929 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007930 {
7931 .cmd = NL80211_CMD_TDLS_MGMT,
7932 .doit = nl80211_tdls_mgmt,
7933 .policy = nl80211_policy,
7934 .flags = GENL_ADMIN_PERM,
7935 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7936 NL80211_FLAG_NEED_RTNL,
7937 },
7938 {
7939 .cmd = NL80211_CMD_TDLS_OPER,
7940 .doit = nl80211_tdls_oper,
7941 .policy = nl80211_policy,
7942 .flags = GENL_ADMIN_PERM,
7943 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7944 NL80211_FLAG_NEED_RTNL,
7945 },
Johannes Berg28946da2011-11-04 11:18:12 +01007946 {
7947 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7948 .doit = nl80211_register_unexpected_frame,
7949 .policy = nl80211_policy,
7950 .flags = GENL_ADMIN_PERM,
7951 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7952 NL80211_FLAG_NEED_RTNL,
7953 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007954 {
7955 .cmd = NL80211_CMD_PROBE_CLIENT,
7956 .doit = nl80211_probe_client,
7957 .policy = nl80211_policy,
7958 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007959 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007960 NL80211_FLAG_NEED_RTNL,
7961 },
Johannes Berg5e760232011-11-04 11:18:17 +01007962 {
7963 .cmd = NL80211_CMD_REGISTER_BEACONS,
7964 .doit = nl80211_register_beacons,
7965 .policy = nl80211_policy,
7966 .flags = GENL_ADMIN_PERM,
7967 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7968 NL80211_FLAG_NEED_RTNL,
7969 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007970 {
7971 .cmd = NL80211_CMD_SET_NOACK_MAP,
7972 .doit = nl80211_set_noack_map,
7973 .policy = nl80211_policy,
7974 .flags = GENL_ADMIN_PERM,
7975 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7976 NL80211_FLAG_NEED_RTNL,
7977 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007978 {
7979 .cmd = NL80211_CMD_START_P2P_DEVICE,
7980 .doit = nl80211_start_p2p_device,
7981 .policy = nl80211_policy,
7982 .flags = GENL_ADMIN_PERM,
7983 .internal_flags = NL80211_FLAG_NEED_WDEV |
7984 NL80211_FLAG_NEED_RTNL,
7985 },
7986 {
7987 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7988 .doit = nl80211_stop_p2p_device,
7989 .policy = nl80211_policy,
7990 .flags = GENL_ADMIN_PERM,
7991 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7992 NL80211_FLAG_NEED_RTNL,
7993 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007994 {
7995 .cmd = NL80211_CMD_SET_MCAST_RATE,
7996 .doit = nl80211_set_mcast_rate,
7997 .policy = nl80211_policy,
7998 .flags = GENL_ADMIN_PERM,
7999 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8000 NL80211_FLAG_NEED_RTNL,
8001 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308002 {
8003 .cmd = NL80211_CMD_SET_MAC_ACL,
8004 .doit = nl80211_set_mac_acl,
8005 .policy = nl80211_policy,
8006 .flags = GENL_ADMIN_PERM,
8007 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8008 NL80211_FLAG_NEED_RTNL,
8009 },
Johannes Berg55682962007-09-20 13:09:35 -04008010};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008011
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008012static struct genl_multicast_group nl80211_mlme_mcgrp = {
8013 .name = "mlme",
8014};
Johannes Berg55682962007-09-20 13:09:35 -04008015
8016/* multicast groups */
8017static struct genl_multicast_group nl80211_config_mcgrp = {
8018 .name = "config",
8019};
Johannes Berg2a519312009-02-10 21:25:55 +01008020static struct genl_multicast_group nl80211_scan_mcgrp = {
8021 .name = "scan",
8022};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008023static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8024 .name = "regulatory",
8025};
Johannes Berg55682962007-09-20 13:09:35 -04008026
8027/* notification functions */
8028
8029void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8030{
8031 struct sk_buff *msg;
8032
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008033 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008034 if (!msg)
8035 return;
8036
8037 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8038 nlmsg_free(msg);
8039 return;
8040 }
8041
Johannes Berg463d0182009-07-14 00:33:35 +02008042 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8043 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008044}
8045
Johannes Berg362a4152009-05-24 16:43:15 +02008046static int nl80211_add_scan_req(struct sk_buff *msg,
8047 struct cfg80211_registered_device *rdev)
8048{
8049 struct cfg80211_scan_request *req = rdev->scan_req;
8050 struct nlattr *nest;
8051 int i;
8052
Johannes Berg667503dd2009-07-07 03:56:11 +02008053 ASSERT_RDEV_LOCK(rdev);
8054
Johannes Berg362a4152009-05-24 16:43:15 +02008055 if (WARN_ON(!req))
8056 return 0;
8057
8058 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8059 if (!nest)
8060 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008061 for (i = 0; i < req->n_ssids; i++) {
8062 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8063 goto nla_put_failure;
8064 }
Johannes Berg362a4152009-05-24 16:43:15 +02008065 nla_nest_end(msg, nest);
8066
8067 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8068 if (!nest)
8069 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008070 for (i = 0; i < req->n_channels; i++) {
8071 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8072 goto nla_put_failure;
8073 }
Johannes Berg362a4152009-05-24 16:43:15 +02008074 nla_nest_end(msg, nest);
8075
David S. Miller9360ffd2012-03-29 04:41:26 -04008076 if (req->ie &&
8077 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8078 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008079
Sam Lefflered4737712012-10-11 21:03:31 -07008080 if (req->flags)
8081 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8082
Johannes Berg362a4152009-05-24 16:43:15 +02008083 return 0;
8084 nla_put_failure:
8085 return -ENOBUFS;
8086}
8087
Johannes Berga538e2d2009-06-16 19:56:42 +02008088static int nl80211_send_scan_msg(struct sk_buff *msg,
8089 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008090 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008091 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008092 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008093{
8094 void *hdr;
8095
Eric W. Biederman15e47302012-09-07 20:12:54 +00008096 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008097 if (!hdr)
8098 return -1;
8099
David S. Miller9360ffd2012-03-29 04:41:26 -04008100 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008101 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8102 wdev->netdev->ifindex)) ||
8103 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008104 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008105
Johannes Berg362a4152009-05-24 16:43:15 +02008106 /* ignore errors and send incomplete event anyway */
8107 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008108
8109 return genlmsg_end(msg, hdr);
8110
8111 nla_put_failure:
8112 genlmsg_cancel(msg, hdr);
8113 return -EMSGSIZE;
8114}
8115
Luciano Coelho807f8a82011-05-11 17:09:35 +03008116static int
8117nl80211_send_sched_scan_msg(struct sk_buff *msg,
8118 struct cfg80211_registered_device *rdev,
8119 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008120 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008121{
8122 void *hdr;
8123
Eric W. Biederman15e47302012-09-07 20:12:54 +00008124 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008125 if (!hdr)
8126 return -1;
8127
David S. Miller9360ffd2012-03-29 04:41:26 -04008128 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8129 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8130 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008131
8132 return genlmsg_end(msg, hdr);
8133
8134 nla_put_failure:
8135 genlmsg_cancel(msg, hdr);
8136 return -EMSGSIZE;
8137}
8138
Johannes Berga538e2d2009-06-16 19:56:42 +02008139void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008140 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008141{
8142 struct sk_buff *msg;
8143
Thomas Graf58050fc2012-06-28 03:57:45 +00008144 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008145 if (!msg)
8146 return;
8147
Johannes Bergfd014282012-06-18 19:17:03 +02008148 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008149 NL80211_CMD_TRIGGER_SCAN) < 0) {
8150 nlmsg_free(msg);
8151 return;
8152 }
8153
Johannes Berg463d0182009-07-14 00:33:35 +02008154 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8155 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008156}
8157
Johannes Berg2a519312009-02-10 21:25:55 +01008158void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008159 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008160{
8161 struct sk_buff *msg;
8162
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008164 if (!msg)
8165 return;
8166
Johannes Bergfd014282012-06-18 19:17:03 +02008167 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008168 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008169 nlmsg_free(msg);
8170 return;
8171 }
8172
Johannes Berg463d0182009-07-14 00:33:35 +02008173 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8174 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008175}
8176
8177void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008178 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008179{
8180 struct sk_buff *msg;
8181
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008182 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008183 if (!msg)
8184 return;
8185
Johannes Bergfd014282012-06-18 19:17:03 +02008186 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008187 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008188 nlmsg_free(msg);
8189 return;
8190 }
8191
Johannes Berg463d0182009-07-14 00:33:35 +02008192 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8193 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008194}
8195
Luciano Coelho807f8a82011-05-11 17:09:35 +03008196void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8197 struct net_device *netdev)
8198{
8199 struct sk_buff *msg;
8200
8201 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8202 if (!msg)
8203 return;
8204
8205 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8206 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8207 nlmsg_free(msg);
8208 return;
8209 }
8210
8211 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8212 nl80211_scan_mcgrp.id, GFP_KERNEL);
8213}
8214
8215void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8216 struct net_device *netdev, u32 cmd)
8217{
8218 struct sk_buff *msg;
8219
Thomas Graf58050fc2012-06-28 03:57:45 +00008220 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008221 if (!msg)
8222 return;
8223
8224 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8225 nlmsg_free(msg);
8226 return;
8227 }
8228
8229 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8230 nl80211_scan_mcgrp.id, GFP_KERNEL);
8231}
8232
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008233/*
8234 * This can happen on global regulatory changes or device specific settings
8235 * based on custom world regulatory domains.
8236 */
8237void nl80211_send_reg_change_event(struct regulatory_request *request)
8238{
8239 struct sk_buff *msg;
8240 void *hdr;
8241
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008242 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008243 if (!msg)
8244 return;
8245
8246 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8247 if (!hdr) {
8248 nlmsg_free(msg);
8249 return;
8250 }
8251
8252 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008253 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8254 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008255
David S. Miller9360ffd2012-03-29 04:41:26 -04008256 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8257 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8258 NL80211_REGDOM_TYPE_WORLD))
8259 goto nla_put_failure;
8260 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8261 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8262 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8263 goto nla_put_failure;
8264 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8265 request->intersect) {
8266 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8267 NL80211_REGDOM_TYPE_INTERSECTION))
8268 goto nla_put_failure;
8269 } else {
8270 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8271 NL80211_REGDOM_TYPE_COUNTRY) ||
8272 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8273 request->alpha2))
8274 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008275 }
8276
Johannes Bergf4173762012-12-03 18:23:37 +01008277 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008278 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8279 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008280
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008281 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008282
Johannes Bergbc43b282009-07-25 10:54:13 +02008283 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008284 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008285 GFP_ATOMIC);
8286 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008287
8288 return;
8289
8290nla_put_failure:
8291 genlmsg_cancel(msg, hdr);
8292 nlmsg_free(msg);
8293}
8294
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008295static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8296 struct net_device *netdev,
8297 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008298 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008299{
8300 struct sk_buff *msg;
8301 void *hdr;
8302
Johannes Berge6d6e342009-07-01 21:26:47 +02008303 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008304 if (!msg)
8305 return;
8306
8307 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8308 if (!hdr) {
8309 nlmsg_free(msg);
8310 return;
8311 }
8312
David S. Miller9360ffd2012-03-29 04:41:26 -04008313 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8314 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8315 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8316 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008317
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008318 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008319
Johannes Berg463d0182009-07-14 00:33:35 +02008320 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8321 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008322 return;
8323
8324 nla_put_failure:
8325 genlmsg_cancel(msg, hdr);
8326 nlmsg_free(msg);
8327}
8328
8329void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008330 struct net_device *netdev, const u8 *buf,
8331 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008332{
8333 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008334 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008335}
8336
8337void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8338 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008339 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008340{
Johannes Berge6d6e342009-07-01 21:26:47 +02008341 nl80211_send_mlme_event(rdev, netdev, buf, len,
8342 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008343}
8344
Jouni Malinen53b46b82009-03-27 20:53:56 +02008345void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008346 struct net_device *netdev, const u8 *buf,
8347 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008348{
8349 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008350 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008351}
8352
Jouni Malinen53b46b82009-03-27 20:53:56 +02008353void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8354 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008355 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008356{
8357 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008358 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008359}
8360
Jouni Malinencf4e5942010-12-16 00:52:40 +02008361void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8362 struct net_device *netdev, const u8 *buf,
8363 size_t len, gfp_t gfp)
8364{
8365 nl80211_send_mlme_event(rdev, netdev, buf, len,
8366 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8367}
8368
8369void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8370 struct net_device *netdev, const u8 *buf,
8371 size_t len, gfp_t gfp)
8372{
8373 nl80211_send_mlme_event(rdev, netdev, buf, len,
8374 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8375}
8376
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008377static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8378 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008379 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008380{
8381 struct sk_buff *msg;
8382 void *hdr;
8383
Johannes Berge6d6e342009-07-01 21:26:47 +02008384 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008385 if (!msg)
8386 return;
8387
8388 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8389 if (!hdr) {
8390 nlmsg_free(msg);
8391 return;
8392 }
8393
David S. Miller9360ffd2012-03-29 04:41:26 -04008394 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8395 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8396 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8397 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8398 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008399
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008400 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008401
Johannes Berg463d0182009-07-14 00:33:35 +02008402 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8403 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008404 return;
8405
8406 nla_put_failure:
8407 genlmsg_cancel(msg, hdr);
8408 nlmsg_free(msg);
8409}
8410
8411void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008412 struct net_device *netdev, const u8 *addr,
8413 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008414{
8415 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008416 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008417}
8418
8419void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008420 struct net_device *netdev, const u8 *addr,
8421 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008422{
Johannes Berge6d6e342009-07-01 21:26:47 +02008423 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8424 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008425}
8426
Samuel Ortizb23aa672009-07-01 21:26:54 +02008427void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8428 struct net_device *netdev, const u8 *bssid,
8429 const u8 *req_ie, size_t req_ie_len,
8430 const u8 *resp_ie, size_t resp_ie_len,
8431 u16 status, gfp_t gfp)
8432{
8433 struct sk_buff *msg;
8434 void *hdr;
8435
Thomas Graf58050fc2012-06-28 03:57:45 +00008436 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008437 if (!msg)
8438 return;
8439
8440 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8441 if (!hdr) {
8442 nlmsg_free(msg);
8443 return;
8444 }
8445
David S. Miller9360ffd2012-03-29 04:41:26 -04008446 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8447 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8448 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8449 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8450 (req_ie &&
8451 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8452 (resp_ie &&
8453 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8454 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008455
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008456 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008457
Johannes Berg463d0182009-07-14 00:33:35 +02008458 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8459 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008460 return;
8461
8462 nla_put_failure:
8463 genlmsg_cancel(msg, hdr);
8464 nlmsg_free(msg);
8465
8466}
8467
8468void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8469 struct net_device *netdev, const u8 *bssid,
8470 const u8 *req_ie, size_t req_ie_len,
8471 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8472{
8473 struct sk_buff *msg;
8474 void *hdr;
8475
Thomas Graf58050fc2012-06-28 03:57:45 +00008476 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008477 if (!msg)
8478 return;
8479
8480 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8481 if (!hdr) {
8482 nlmsg_free(msg);
8483 return;
8484 }
8485
David S. Miller9360ffd2012-03-29 04:41:26 -04008486 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8487 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8488 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8489 (req_ie &&
8490 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8491 (resp_ie &&
8492 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8493 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008494
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008495 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008496
Johannes Berg463d0182009-07-14 00:33:35 +02008497 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8498 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008499 return;
8500
8501 nla_put_failure:
8502 genlmsg_cancel(msg, hdr);
8503 nlmsg_free(msg);
8504
8505}
8506
8507void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8508 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008509 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008510{
8511 struct sk_buff *msg;
8512 void *hdr;
8513
Thomas Graf58050fc2012-06-28 03:57:45 +00008514 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008515 if (!msg)
8516 return;
8517
8518 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8519 if (!hdr) {
8520 nlmsg_free(msg);
8521 return;
8522 }
8523
David S. Miller9360ffd2012-03-29 04:41:26 -04008524 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8525 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8526 (from_ap && reason &&
8527 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8528 (from_ap &&
8529 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8530 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8531 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008532
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008533 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008534
Johannes Berg463d0182009-07-14 00:33:35 +02008535 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8536 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008537 return;
8538
8539 nla_put_failure:
8540 genlmsg_cancel(msg, hdr);
8541 nlmsg_free(msg);
8542
8543}
8544
Johannes Berg04a773a2009-04-19 21:24:32 +02008545void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8546 struct net_device *netdev, const u8 *bssid,
8547 gfp_t gfp)
8548{
8549 struct sk_buff *msg;
8550 void *hdr;
8551
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008553 if (!msg)
8554 return;
8555
8556 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8557 if (!hdr) {
8558 nlmsg_free(msg);
8559 return;
8560 }
8561
David S. Miller9360ffd2012-03-29 04:41:26 -04008562 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8563 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8564 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8565 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008566
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008567 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008568
Johannes Berg463d0182009-07-14 00:33:35 +02008569 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8570 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008571 return;
8572
8573 nla_put_failure:
8574 genlmsg_cancel(msg, hdr);
8575 nlmsg_free(msg);
8576}
8577
Javier Cardonac93b5e72011-04-07 15:08:34 -07008578void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8579 struct net_device *netdev,
8580 const u8 *macaddr, const u8* ie, u8 ie_len,
8581 gfp_t gfp)
8582{
8583 struct sk_buff *msg;
8584 void *hdr;
8585
8586 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8587 if (!msg)
8588 return;
8589
8590 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8591 if (!hdr) {
8592 nlmsg_free(msg);
8593 return;
8594 }
8595
David S. Miller9360ffd2012-03-29 04:41:26 -04008596 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8597 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8598 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8599 (ie_len && ie &&
8600 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8601 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008602
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008603 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008604
8605 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8606 nl80211_mlme_mcgrp.id, gfp);
8607 return;
8608
8609 nla_put_failure:
8610 genlmsg_cancel(msg, hdr);
8611 nlmsg_free(msg);
8612}
8613
Jouni Malinena3b8b052009-03-27 21:59:49 +02008614void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8615 struct net_device *netdev, const u8 *addr,
8616 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008617 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008618{
8619 struct sk_buff *msg;
8620 void *hdr;
8621
Johannes Berge6d6e342009-07-01 21:26:47 +02008622 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008623 if (!msg)
8624 return;
8625
8626 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8627 if (!hdr) {
8628 nlmsg_free(msg);
8629 return;
8630 }
8631
David S. Miller9360ffd2012-03-29 04:41:26 -04008632 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8633 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8634 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8635 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8636 (key_id != -1 &&
8637 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8638 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8639 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008640
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008641 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008642
Johannes Berg463d0182009-07-14 00:33:35 +02008643 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8644 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008645 return;
8646
8647 nla_put_failure:
8648 genlmsg_cancel(msg, hdr);
8649 nlmsg_free(msg);
8650}
8651
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008652void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8653 struct ieee80211_channel *channel_before,
8654 struct ieee80211_channel *channel_after)
8655{
8656 struct sk_buff *msg;
8657 void *hdr;
8658 struct nlattr *nl_freq;
8659
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008660 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008661 if (!msg)
8662 return;
8663
8664 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8665 if (!hdr) {
8666 nlmsg_free(msg);
8667 return;
8668 }
8669
8670 /*
8671 * Since we are applying the beacon hint to a wiphy we know its
8672 * wiphy_idx is valid
8673 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008674 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8675 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008676
8677 /* Before */
8678 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8679 if (!nl_freq)
8680 goto nla_put_failure;
8681 if (nl80211_msg_put_channel(msg, channel_before))
8682 goto nla_put_failure;
8683 nla_nest_end(msg, nl_freq);
8684
8685 /* After */
8686 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8687 if (!nl_freq)
8688 goto nla_put_failure;
8689 if (nl80211_msg_put_channel(msg, channel_after))
8690 goto nla_put_failure;
8691 nla_nest_end(msg, nl_freq);
8692
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008693 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008694
Johannes Berg463d0182009-07-14 00:33:35 +02008695 rcu_read_lock();
8696 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8697 GFP_ATOMIC);
8698 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008699
8700 return;
8701
8702nla_put_failure:
8703 genlmsg_cancel(msg, hdr);
8704 nlmsg_free(msg);
8705}
8706
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008707static void nl80211_send_remain_on_chan_event(
8708 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008709 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008710 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008711 unsigned int duration, gfp_t gfp)
8712{
8713 struct sk_buff *msg;
8714 void *hdr;
8715
8716 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8717 if (!msg)
8718 return;
8719
8720 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8721 if (!hdr) {
8722 nlmsg_free(msg);
8723 return;
8724 }
8725
David S. Miller9360ffd2012-03-29 04:41:26 -04008726 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008727 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8728 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008729 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008730 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01008731 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
8732 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008733 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8734 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008735
David S. Miller9360ffd2012-03-29 04:41:26 -04008736 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8737 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8738 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008739
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008740 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008741
8742 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8743 nl80211_mlme_mcgrp.id, gfp);
8744 return;
8745
8746 nla_put_failure:
8747 genlmsg_cancel(msg, hdr);
8748 nlmsg_free(msg);
8749}
8750
8751void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008752 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008753 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008754 unsigned int duration, gfp_t gfp)
8755{
8756 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008757 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01008758 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008759}
8760
8761void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008762 struct cfg80211_registered_device *rdev,
8763 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01008764 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008765{
8766 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01008767 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008768}
8769
Johannes Berg98b62182009-12-23 13:15:44 +01008770void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8771 struct net_device *dev, const u8 *mac_addr,
8772 struct station_info *sinfo, gfp_t gfp)
8773{
8774 struct sk_buff *msg;
8775
Thomas Graf58050fc2012-06-28 03:57:45 +00008776 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008777 if (!msg)
8778 return;
8779
John W. Linville66266b32012-03-15 13:25:41 -04008780 if (nl80211_send_station(msg, 0, 0, 0,
8781 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008782 nlmsg_free(msg);
8783 return;
8784 }
8785
8786 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8787 nl80211_mlme_mcgrp.id, gfp);
8788}
8789
Jouni Malinenec15e682011-03-23 15:29:52 +02008790void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8791 struct net_device *dev, const u8 *mac_addr,
8792 gfp_t gfp)
8793{
8794 struct sk_buff *msg;
8795 void *hdr;
8796
Thomas Graf58050fc2012-06-28 03:57:45 +00008797 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008798 if (!msg)
8799 return;
8800
8801 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8802 if (!hdr) {
8803 nlmsg_free(msg);
8804 return;
8805 }
8806
David S. Miller9360ffd2012-03-29 04:41:26 -04008807 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8808 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8809 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008810
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008811 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008812
8813 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8814 nl80211_mlme_mcgrp.id, gfp);
8815 return;
8816
8817 nla_put_failure:
8818 genlmsg_cancel(msg, hdr);
8819 nlmsg_free(msg);
8820}
8821
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308822void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8823 struct net_device *dev, const u8 *mac_addr,
8824 enum nl80211_connect_failed_reason reason,
8825 gfp_t gfp)
8826{
8827 struct sk_buff *msg;
8828 void *hdr;
8829
8830 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8831 if (!msg)
8832 return;
8833
8834 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8835 if (!hdr) {
8836 nlmsg_free(msg);
8837 return;
8838 }
8839
8840 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8841 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8842 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8843 goto nla_put_failure;
8844
8845 genlmsg_end(msg, hdr);
8846
8847 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8848 nl80211_mlme_mcgrp.id, gfp);
8849 return;
8850
8851 nla_put_failure:
8852 genlmsg_cancel(msg, hdr);
8853 nlmsg_free(msg);
8854}
8855
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008856static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8857 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008858{
8859 struct wireless_dev *wdev = dev->ieee80211_ptr;
8860 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8861 struct sk_buff *msg;
8862 void *hdr;
8863 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008864 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008865
Eric W. Biederman15e47302012-09-07 20:12:54 +00008866 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008867 return false;
8868
8869 msg = nlmsg_new(100, gfp);
8870 if (!msg)
8871 return true;
8872
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008873 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008874 if (!hdr) {
8875 nlmsg_free(msg);
8876 return true;
8877 }
8878
David S. Miller9360ffd2012-03-29 04:41:26 -04008879 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8880 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8881 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8882 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008883
8884 err = genlmsg_end(msg, hdr);
8885 if (err < 0) {
8886 nlmsg_free(msg);
8887 return true;
8888 }
8889
Eric W. Biederman15e47302012-09-07 20:12:54 +00008890 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008891 return true;
8892
8893 nla_put_failure:
8894 genlmsg_cancel(msg, hdr);
8895 nlmsg_free(msg);
8896 return true;
8897}
8898
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008899bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8900{
8901 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8902 addr, gfp);
8903}
8904
8905bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8906 const u8 *addr, gfp_t gfp)
8907{
8908 return __nl80211_unexpected_frame(dev,
8909 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8910 addr, gfp);
8911}
8912
Johannes Berg2e161f72010-08-12 15:38:38 +02008913int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008914 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008915 int freq, int sig_dbm,
8916 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008917{
Johannes Berg71bbc992012-06-15 15:30:18 +02008918 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008919 struct sk_buff *msg;
8920 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008921
8922 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8923 if (!msg)
8924 return -ENOMEM;
8925
Johannes Berg2e161f72010-08-12 15:38:38 +02008926 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008927 if (!hdr) {
8928 nlmsg_free(msg);
8929 return -ENOMEM;
8930 }
8931
David S. Miller9360ffd2012-03-29 04:41:26 -04008932 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008933 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8934 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008935 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8936 (sig_dbm &&
8937 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8938 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8939 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008940
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008941 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008942
Eric W. Biederman15e47302012-09-07 20:12:54 +00008943 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008944
8945 nla_put_failure:
8946 genlmsg_cancel(msg, hdr);
8947 nlmsg_free(msg);
8948 return -ENOBUFS;
8949}
8950
Johannes Berg2e161f72010-08-12 15:38:38 +02008951void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008952 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008953 const u8 *buf, size_t len, bool ack,
8954 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008955{
Johannes Berg71bbc992012-06-15 15:30:18 +02008956 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008957 struct sk_buff *msg;
8958 void *hdr;
8959
8960 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8961 if (!msg)
8962 return;
8963
Johannes Berg2e161f72010-08-12 15:38:38 +02008964 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008965 if (!hdr) {
8966 nlmsg_free(msg);
8967 return;
8968 }
8969
David S. Miller9360ffd2012-03-29 04:41:26 -04008970 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008971 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8972 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008973 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8974 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8975 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8976 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008977
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008978 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008979
8980 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8981 return;
8982
8983 nla_put_failure:
8984 genlmsg_cancel(msg, hdr);
8985 nlmsg_free(msg);
8986}
8987
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008988void
8989nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8990 struct net_device *netdev,
8991 enum nl80211_cqm_rssi_threshold_event rssi_event,
8992 gfp_t gfp)
8993{
8994 struct sk_buff *msg;
8995 struct nlattr *pinfoattr;
8996 void *hdr;
8997
Thomas Graf58050fc2012-06-28 03:57:45 +00008998 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008999 if (!msg)
9000 return;
9001
9002 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9003 if (!hdr) {
9004 nlmsg_free(msg);
9005 return;
9006 }
9007
David S. Miller9360ffd2012-03-29 04:41:26 -04009008 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9009 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9010 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009011
9012 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9013 if (!pinfoattr)
9014 goto nla_put_failure;
9015
David S. Miller9360ffd2012-03-29 04:41:26 -04009016 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9017 rssi_event))
9018 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009019
9020 nla_nest_end(msg, pinfoattr);
9021
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009022 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009023
9024 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9025 nl80211_mlme_mcgrp.id, gfp);
9026 return;
9027
9028 nla_put_failure:
9029 genlmsg_cancel(msg, hdr);
9030 nlmsg_free(msg);
9031}
9032
Johannes Berge5497d72011-07-05 16:35:40 +02009033void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9034 struct net_device *netdev, const u8 *bssid,
9035 const u8 *replay_ctr, gfp_t gfp)
9036{
9037 struct sk_buff *msg;
9038 struct nlattr *rekey_attr;
9039 void *hdr;
9040
Thomas Graf58050fc2012-06-28 03:57:45 +00009041 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009042 if (!msg)
9043 return;
9044
9045 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9046 if (!hdr) {
9047 nlmsg_free(msg);
9048 return;
9049 }
9050
David S. Miller9360ffd2012-03-29 04:41:26 -04009051 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9052 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9053 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9054 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009055
9056 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9057 if (!rekey_attr)
9058 goto nla_put_failure;
9059
David S. Miller9360ffd2012-03-29 04:41:26 -04009060 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9061 NL80211_REPLAY_CTR_LEN, replay_ctr))
9062 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009063
9064 nla_nest_end(msg, rekey_attr);
9065
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009066 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009067
9068 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9069 nl80211_mlme_mcgrp.id, gfp);
9070 return;
9071
9072 nla_put_failure:
9073 genlmsg_cancel(msg, hdr);
9074 nlmsg_free(msg);
9075}
9076
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009077void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9078 struct net_device *netdev, int index,
9079 const u8 *bssid, bool preauth, gfp_t gfp)
9080{
9081 struct sk_buff *msg;
9082 struct nlattr *attr;
9083 void *hdr;
9084
Thomas Graf58050fc2012-06-28 03:57:45 +00009085 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009086 if (!msg)
9087 return;
9088
9089 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9090 if (!hdr) {
9091 nlmsg_free(msg);
9092 return;
9093 }
9094
David S. Miller9360ffd2012-03-29 04:41:26 -04009095 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9096 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9097 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009098
9099 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9100 if (!attr)
9101 goto nla_put_failure;
9102
David S. Miller9360ffd2012-03-29 04:41:26 -04009103 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9104 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9105 (preauth &&
9106 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9107 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009108
9109 nla_nest_end(msg, attr);
9110
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009111 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009112
9113 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9114 nl80211_mlme_mcgrp.id, gfp);
9115 return;
9116
9117 nla_put_failure:
9118 genlmsg_cancel(msg, hdr);
9119 nlmsg_free(msg);
9120}
9121
Thomas Pedersen53145262012-04-06 13:35:47 -07009122void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009123 struct net_device *netdev,
9124 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009125{
9126 struct sk_buff *msg;
9127 void *hdr;
9128
Thomas Graf58050fc2012-06-28 03:57:45 +00009129 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009130 if (!msg)
9131 return;
9132
9133 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9134 if (!hdr) {
9135 nlmsg_free(msg);
9136 return;
9137 }
9138
Johannes Berg683b6d32012-11-08 21:25:48 +01009139 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9140 goto nla_put_failure;
9141
9142 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009143 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009144
9145 genlmsg_end(msg, hdr);
9146
9147 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9148 nl80211_mlme_mcgrp.id, gfp);
9149 return;
9150
9151 nla_put_failure:
9152 genlmsg_cancel(msg, hdr);
9153 nlmsg_free(msg);
9154}
9155
Johannes Bergc063dbf2010-11-24 08:10:05 +01009156void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009157nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9158 struct net_device *netdev, const u8 *peer,
9159 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9160{
9161 struct sk_buff *msg;
9162 struct nlattr *pinfoattr;
9163 void *hdr;
9164
9165 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9166 if (!msg)
9167 return;
9168
9169 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9170 if (!hdr) {
9171 nlmsg_free(msg);
9172 return;
9173 }
9174
9175 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9176 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9177 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9178 goto nla_put_failure;
9179
9180 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9181 if (!pinfoattr)
9182 goto nla_put_failure;
9183
9184 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9185 goto nla_put_failure;
9186
9187 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9188 goto nla_put_failure;
9189
9190 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9191 goto nla_put_failure;
9192
9193 nla_nest_end(msg, pinfoattr);
9194
9195 genlmsg_end(msg, hdr);
9196
9197 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9198 nl80211_mlme_mcgrp.id, gfp);
9199 return;
9200
9201 nla_put_failure:
9202 genlmsg_cancel(msg, hdr);
9203 nlmsg_free(msg);
9204}
9205
9206void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009207nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9208 struct net_device *netdev, const u8 *peer,
9209 u32 num_packets, gfp_t gfp)
9210{
9211 struct sk_buff *msg;
9212 struct nlattr *pinfoattr;
9213 void *hdr;
9214
Thomas Graf58050fc2012-06-28 03:57:45 +00009215 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009216 if (!msg)
9217 return;
9218
9219 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9220 if (!hdr) {
9221 nlmsg_free(msg);
9222 return;
9223 }
9224
David S. Miller9360ffd2012-03-29 04:41:26 -04009225 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9226 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9227 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9228 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009229
9230 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9231 if (!pinfoattr)
9232 goto nla_put_failure;
9233
David S. Miller9360ffd2012-03-29 04:41:26 -04009234 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9235 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009236
9237 nla_nest_end(msg, pinfoattr);
9238
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009239 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009240
9241 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9242 nl80211_mlme_mcgrp.id, gfp);
9243 return;
9244
9245 nla_put_failure:
9246 genlmsg_cancel(msg, hdr);
9247 nlmsg_free(msg);
9248}
9249
Johannes Berg7f6cf312011-11-04 11:18:15 +01009250void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9251 u64 cookie, bool acked, gfp_t gfp)
9252{
9253 struct wireless_dev *wdev = dev->ieee80211_ptr;
9254 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9255 struct sk_buff *msg;
9256 void *hdr;
9257 int err;
9258
Beni Lev4ee3e062012-08-27 12:49:39 +03009259 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9260
Thomas Graf58050fc2012-06-28 03:57:45 +00009261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009262
Johannes Berg7f6cf312011-11-04 11:18:15 +01009263 if (!msg)
9264 return;
9265
9266 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9267 if (!hdr) {
9268 nlmsg_free(msg);
9269 return;
9270 }
9271
David S. Miller9360ffd2012-03-29 04:41:26 -04009272 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9273 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9274 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9275 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9276 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9277 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009278
9279 err = genlmsg_end(msg, hdr);
9280 if (err < 0) {
9281 nlmsg_free(msg);
9282 return;
9283 }
9284
9285 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9286 nl80211_mlme_mcgrp.id, gfp);
9287 return;
9288
9289 nla_put_failure:
9290 genlmsg_cancel(msg, hdr);
9291 nlmsg_free(msg);
9292}
9293EXPORT_SYMBOL(cfg80211_probe_status);
9294
Johannes Berg5e760232011-11-04 11:18:17 +01009295void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9296 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009297 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009298{
9299 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9300 struct sk_buff *msg;
9301 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009302 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009303
Beni Lev4ee3e062012-08-27 12:49:39 +03009304 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9305
Ben Greear37c73b52012-10-26 14:49:25 -07009306 spin_lock_bh(&rdev->beacon_registrations_lock);
9307 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9308 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9309 if (!msg) {
9310 spin_unlock_bh(&rdev->beacon_registrations_lock);
9311 return;
9312 }
Johannes Berg5e760232011-11-04 11:18:17 +01009313
Ben Greear37c73b52012-10-26 14:49:25 -07009314 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9315 if (!hdr)
9316 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009317
Ben Greear37c73b52012-10-26 14:49:25 -07009318 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9319 (freq &&
9320 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9321 (sig_dbm &&
9322 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9323 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9324 goto nla_put_failure;
9325
9326 genlmsg_end(msg, hdr);
9327
9328 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009329 }
Ben Greear37c73b52012-10-26 14:49:25 -07009330 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009331 return;
9332
9333 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009334 spin_unlock_bh(&rdev->beacon_registrations_lock);
9335 if (hdr)
9336 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009337 nlmsg_free(msg);
9338}
9339EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9340
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009341#ifdef CONFIG_PM
9342void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9343 struct cfg80211_wowlan_wakeup *wakeup,
9344 gfp_t gfp)
9345{
9346 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9347 struct sk_buff *msg;
9348 void *hdr;
9349 int err, size = 200;
9350
9351 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9352
9353 if (wakeup)
9354 size += wakeup->packet_present_len;
9355
9356 msg = nlmsg_new(size, gfp);
9357 if (!msg)
9358 return;
9359
9360 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9361 if (!hdr)
9362 goto free_msg;
9363
9364 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9365 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9366 goto free_msg;
9367
9368 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9369 wdev->netdev->ifindex))
9370 goto free_msg;
9371
9372 if (wakeup) {
9373 struct nlattr *reasons;
9374
9375 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9376
9377 if (wakeup->disconnect &&
9378 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9379 goto free_msg;
9380 if (wakeup->magic_pkt &&
9381 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9382 goto free_msg;
9383 if (wakeup->gtk_rekey_failure &&
9384 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9385 goto free_msg;
9386 if (wakeup->eap_identity_req &&
9387 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9388 goto free_msg;
9389 if (wakeup->four_way_handshake &&
9390 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9391 goto free_msg;
9392 if (wakeup->rfkill_release &&
9393 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9394 goto free_msg;
9395
9396 if (wakeup->pattern_idx >= 0 &&
9397 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9398 wakeup->pattern_idx))
9399 goto free_msg;
9400
9401 if (wakeup->packet) {
9402 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9403 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9404
9405 if (!wakeup->packet_80211) {
9406 pkt_attr =
9407 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9408 len_attr =
9409 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9410 }
9411
9412 if (wakeup->packet_len &&
9413 nla_put_u32(msg, len_attr, wakeup->packet_len))
9414 goto free_msg;
9415
9416 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9417 wakeup->packet))
9418 goto free_msg;
9419 }
9420
9421 nla_nest_end(msg, reasons);
9422 }
9423
9424 err = genlmsg_end(msg, hdr);
9425 if (err < 0)
9426 goto free_msg;
9427
9428 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9429 nl80211_mlme_mcgrp.id, gfp);
9430 return;
9431
9432 free_msg:
9433 nlmsg_free(msg);
9434}
9435EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9436#endif
9437
Jouni Malinen3475b092012-11-16 22:49:57 +02009438void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9439 enum nl80211_tdls_operation oper,
9440 u16 reason_code, gfp_t gfp)
9441{
9442 struct wireless_dev *wdev = dev->ieee80211_ptr;
9443 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9444 struct sk_buff *msg;
9445 void *hdr;
9446 int err;
9447
9448 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9449 reason_code);
9450
9451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9452 if (!msg)
9453 return;
9454
9455 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9456 if (!hdr) {
9457 nlmsg_free(msg);
9458 return;
9459 }
9460
9461 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9462 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9463 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9464 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9465 (reason_code > 0 &&
9466 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9467 goto nla_put_failure;
9468
9469 err = genlmsg_end(msg, hdr);
9470 if (err < 0) {
9471 nlmsg_free(msg);
9472 return;
9473 }
9474
9475 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9476 nl80211_mlme_mcgrp.id, gfp);
9477 return;
9478
9479 nla_put_failure:
9480 genlmsg_cancel(msg, hdr);
9481 nlmsg_free(msg);
9482}
9483EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9484
Jouni Malinen026331c2010-02-15 12:53:10 +02009485static int nl80211_netlink_notify(struct notifier_block * nb,
9486 unsigned long state,
9487 void *_notify)
9488{
9489 struct netlink_notify *notify = _notify;
9490 struct cfg80211_registered_device *rdev;
9491 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009492 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009493
9494 if (state != NETLINK_URELEASE)
9495 return NOTIFY_DONE;
9496
9497 rcu_read_lock();
9498
Johannes Berg5e760232011-11-04 11:18:17 +01009499 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009500 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009501 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009502
9503 spin_lock_bh(&rdev->beacon_registrations_lock);
9504 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9505 list) {
9506 if (reg->nlportid == notify->portid) {
9507 list_del(&reg->list);
9508 kfree(reg);
9509 break;
9510 }
9511 }
9512 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009513 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009514
9515 rcu_read_unlock();
9516
9517 return NOTIFY_DONE;
9518}
9519
9520static struct notifier_block nl80211_netlink_notifier = {
9521 .notifier_call = nl80211_netlink_notify,
9522};
9523
Johannes Berg55682962007-09-20 13:09:35 -04009524/* initialisation/exit functions */
9525
9526int nl80211_init(void)
9527{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009528 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009529
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009530 err = genl_register_family_with_ops(&nl80211_fam,
9531 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009532 if (err)
9533 return err;
9534
Johannes Berg55682962007-09-20 13:09:35 -04009535 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9536 if (err)
9537 goto err_out;
9538
Johannes Berg2a519312009-02-10 21:25:55 +01009539 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9540 if (err)
9541 goto err_out;
9542
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009543 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9544 if (err)
9545 goto err_out;
9546
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009547 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9548 if (err)
9549 goto err_out;
9550
Johannes Bergaff89a92009-07-01 21:26:51 +02009551#ifdef CONFIG_NL80211_TESTMODE
9552 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9553 if (err)
9554 goto err_out;
9555#endif
9556
Jouni Malinen026331c2010-02-15 12:53:10 +02009557 err = netlink_register_notifier(&nl80211_netlink_notifier);
9558 if (err)
9559 goto err_out;
9560
Johannes Berg55682962007-09-20 13:09:35 -04009561 return 0;
9562 err_out:
9563 genl_unregister_family(&nl80211_fam);
9564 return err;
9565}
9566
9567void nl80211_exit(void)
9568{
Jouni Malinen026331c2010-02-15 12:53:10 +02009569 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009570 genl_unregister_family(&nl80211_fam);
9571}