blob: d5842eb35aec33c33ff86e11eb630e127cd586ca [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 Malinendc6382ce2009-05-06 22:09:37 +0300300 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300301 [NL80211_ATTR_STA_FLAGS2] = {
302 .len = sizeof(struct nl80211_sta_flag_update),
303 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300304 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300305 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
306 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200307 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
308 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
309 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200310 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100311 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100312 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
313 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100314 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
315 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200316 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200317 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
318 .len = IEEE80211_MAX_DATA_LEN },
319 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200320 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200321 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300322 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200323 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300324 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
325 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200326 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900327 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
328 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100329 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100330 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100331 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200332 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700333 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300334 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200335 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200336 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300337 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300338 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
339 .len = IEEE80211_MAX_DATA_LEN },
340 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
341 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530342 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300343 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530344 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300345 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
346 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
349 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100350 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200351 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
352 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700353 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800354 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
355 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
356 .len = NL80211_HT_CAPABILITY_LEN
357 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100358 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530359 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530360 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200361 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700362 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300363 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000364 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700365 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100366 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
367 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400368};
369
Johannes Berge31b8212010-10-05 19:39:30 +0200370/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000371static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200372 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200373 [NL80211_KEY_IDX] = { .type = NLA_U8 },
374 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200375 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200376 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
377 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200378 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100379 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
380};
381
382/* policy for the key default flags */
383static const struct nla_policy
384nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
385 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
386 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200387};
388
Johannes Bergff1b6e62011-05-04 15:37:28 +0200389/* policy for WoWLAN attributes */
390static const struct nla_policy
391nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
392 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
393 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
394 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
395 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200396 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
397 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
398 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200400};
401
Johannes Berge5497d72011-07-05 16:35:40 +0200402/* policy for GTK rekey offload attributes */
403static const struct nla_policy
404nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
405 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
406 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
407 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
408};
409
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300410static const struct nla_policy
411nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200412 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300413 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700414 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300415};
416
Holger Schuriga0438972009-11-11 11:30:02 +0100417/* ifidx get helper */
418static int nl80211_get_ifidx(struct netlink_callback *cb)
419{
420 int res;
421
422 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
423 nl80211_fam.attrbuf, nl80211_fam.maxattr,
424 nl80211_policy);
425 if (res)
426 return res;
427
428 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
429 return -EINVAL;
430
431 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
432 if (!res)
433 return -EINVAL;
434 return res;
435}
436
Johannes Berg67748892010-10-04 21:14:06 +0200437static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
438 struct netlink_callback *cb,
439 struct cfg80211_registered_device **rdev,
440 struct net_device **dev)
441{
442 int ifidx = cb->args[0];
443 int err;
444
445 if (!ifidx)
446 ifidx = nl80211_get_ifidx(cb);
447 if (ifidx < 0)
448 return ifidx;
449
450 cb->args[0] = ifidx;
451
452 rtnl_lock();
453
454 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
455 if (!*dev) {
456 err = -ENODEV;
457 goto out_rtnl;
458 }
459
460 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100461 if (IS_ERR(*rdev)) {
462 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200463 goto out_rtnl;
464 }
465
466 return 0;
467 out_rtnl:
468 rtnl_unlock();
469 return err;
470}
471
472static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
473{
474 cfg80211_unlock_rdev(rdev);
475 rtnl_unlock();
476}
477
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100478/* IE validation */
479static bool is_valid_ie_attr(const struct nlattr *attr)
480{
481 const u8 *pos;
482 int len;
483
484 if (!attr)
485 return true;
486
487 pos = nla_data(attr);
488 len = nla_len(attr);
489
490 while (len) {
491 u8 elemlen;
492
493 if (len < 2)
494 return false;
495 len -= 2;
496
497 elemlen = pos[1];
498 if (elemlen > len)
499 return false;
500
501 len -= elemlen;
502 pos += 2 + elemlen;
503 }
504
505 return true;
506}
507
Johannes Berg55682962007-09-20 13:09:35 -0400508/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000509static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400510 int flags, u8 cmd)
511{
512 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000513 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400514}
515
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400516static int nl80211_msg_put_channel(struct sk_buff *msg,
517 struct ieee80211_channel *chan)
518{
David S. Miller9360ffd2012-03-29 04:41:26 -0400519 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
520 chan->center_freq))
521 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400522
David S. Miller9360ffd2012-03-29 04:41:26 -0400523 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
524 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
525 goto nla_put_failure;
526 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
527 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
528 goto nla_put_failure;
529 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
530 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
531 goto nla_put_failure;
532 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
533 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
534 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400535
David S. Miller9360ffd2012-03-29 04:41:26 -0400536 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
537 DBM_TO_MBM(chan->max_power)))
538 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400539
540 return 0;
541
542 nla_put_failure:
543 return -ENOBUFS;
544}
545
Johannes Berg55682962007-09-20 13:09:35 -0400546/* netlink command implementations */
547
Johannes Bergb9454e82009-07-08 13:29:08 +0200548struct key_parse {
549 struct key_params p;
550 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200551 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200552 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100553 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200554};
555
556static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
557{
558 struct nlattr *tb[NL80211_KEY_MAX + 1];
559 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
560 nl80211_key_policy);
561 if (err)
562 return err;
563
564 k->def = !!tb[NL80211_KEY_DEFAULT];
565 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
566
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100567 if (k->def) {
568 k->def_uni = true;
569 k->def_multi = true;
570 }
571 if (k->defmgmt)
572 k->def_multi = true;
573
Johannes Bergb9454e82009-07-08 13:29:08 +0200574 if (tb[NL80211_KEY_IDX])
575 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
576
577 if (tb[NL80211_KEY_DATA]) {
578 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
579 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
580 }
581
582 if (tb[NL80211_KEY_SEQ]) {
583 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
584 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
585 }
586
587 if (tb[NL80211_KEY_CIPHER])
588 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
589
Johannes Berge31b8212010-10-05 19:39:30 +0200590 if (tb[NL80211_KEY_TYPE]) {
591 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
592 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
593 return -EINVAL;
594 }
595
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100596 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
597 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100598 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
599 tb[NL80211_KEY_DEFAULT_TYPES],
600 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100601 if (err)
602 return err;
603
604 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
605 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
606 }
607
Johannes Bergb9454e82009-07-08 13:29:08 +0200608 return 0;
609}
610
611static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
612{
613 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
614 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
615 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
616 }
617
618 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
619 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
620 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
621 }
622
623 if (info->attrs[NL80211_ATTR_KEY_IDX])
624 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
625
626 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
627 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
628
629 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
630 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
631
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100632 if (k->def) {
633 k->def_uni = true;
634 k->def_multi = true;
635 }
636 if (k->defmgmt)
637 k->def_multi = true;
638
Johannes Berge31b8212010-10-05 19:39:30 +0200639 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
640 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
641 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
642 return -EINVAL;
643 }
644
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100645 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
646 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
647 int err = nla_parse_nested(
648 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
649 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
650 nl80211_key_default_policy);
651 if (err)
652 return err;
653
654 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
655 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
656 }
657
Johannes Bergb9454e82009-07-08 13:29:08 +0200658 return 0;
659}
660
661static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
662{
663 int err;
664
665 memset(k, 0, sizeof(*k));
666 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200667 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200668
669 if (info->attrs[NL80211_ATTR_KEY])
670 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
671 else
672 err = nl80211_parse_key_old(info, k);
673
674 if (err)
675 return err;
676
677 if (k->def && k->defmgmt)
678 return -EINVAL;
679
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100680 if (k->defmgmt) {
681 if (k->def_uni || !k->def_multi)
682 return -EINVAL;
683 }
684
Johannes Bergb9454e82009-07-08 13:29:08 +0200685 if (k->idx != -1) {
686 if (k->defmgmt) {
687 if (k->idx < 4 || k->idx > 5)
688 return -EINVAL;
689 } else if (k->def) {
690 if (k->idx < 0 || k->idx > 3)
691 return -EINVAL;
692 } else {
693 if (k->idx < 0 || k->idx > 5)
694 return -EINVAL;
695 }
696 }
697
698 return 0;
699}
700
Johannes Bergfffd0932009-07-08 14:22:54 +0200701static struct cfg80211_cached_keys *
702nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530703 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200704{
705 struct key_parse parse;
706 struct nlattr *key;
707 struct cfg80211_cached_keys *result;
708 int rem, err, def = 0;
709
710 result = kzalloc(sizeof(*result), GFP_KERNEL);
711 if (!result)
712 return ERR_PTR(-ENOMEM);
713
714 result->def = -1;
715 result->defmgmt = -1;
716
717 nla_for_each_nested(key, keys, rem) {
718 memset(&parse, 0, sizeof(parse));
719 parse.idx = -1;
720
721 err = nl80211_parse_key_new(key, &parse);
722 if (err)
723 goto error;
724 err = -EINVAL;
725 if (!parse.p.key)
726 goto error;
727 if (parse.idx < 0 || parse.idx > 4)
728 goto error;
729 if (parse.def) {
730 if (def)
731 goto error;
732 def = 1;
733 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100734 if (!parse.def_uni || !parse.def_multi)
735 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200736 } else if (parse.defmgmt)
737 goto error;
738 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200739 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200740 if (err)
741 goto error;
742 result->params[parse.idx].cipher = parse.p.cipher;
743 result->params[parse.idx].key_len = parse.p.key_len;
744 result->params[parse.idx].key = result->data[parse.idx];
745 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530746
747 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
748 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
749 if (no_ht)
750 *no_ht = true;
751 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 }
753
754 return result;
755 error:
756 kfree(result);
757 return ERR_PTR(err);
758}
759
760static int nl80211_key_allowed(struct wireless_dev *wdev)
761{
762 ASSERT_WDEV_LOCK(wdev);
763
Johannes Bergfffd0932009-07-08 14:22:54 +0200764 switch (wdev->iftype) {
765 case NL80211_IFTYPE_AP:
766 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200767 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700768 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200769 break;
770 case NL80211_IFTYPE_ADHOC:
771 if (!wdev->current_bss)
772 return -ENOLINK;
773 break;
774 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200775 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200776 if (wdev->sme_state != CFG80211_SME_CONNECTED)
777 return -ENOLINK;
778 break;
779 default:
780 return -EINVAL;
781 }
782
783 return 0;
784}
785
Johannes Berg7527a782011-05-13 10:58:57 +0200786static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
787{
788 struct nlattr *nl_modes = nla_nest_start(msg, attr);
789 int i;
790
791 if (!nl_modes)
792 goto nla_put_failure;
793
794 i = 0;
795 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400796 if ((ifmodes & 1) && nla_put_flag(msg, i))
797 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200798 ifmodes >>= 1;
799 i++;
800 }
801
802 nla_nest_end(msg, nl_modes);
803 return 0;
804
805nla_put_failure:
806 return -ENOBUFS;
807}
808
809static int nl80211_put_iface_combinations(struct wiphy *wiphy,
810 struct sk_buff *msg)
811{
812 struct nlattr *nl_combis;
813 int i, j;
814
815 nl_combis = nla_nest_start(msg,
816 NL80211_ATTR_INTERFACE_COMBINATIONS);
817 if (!nl_combis)
818 goto nla_put_failure;
819
820 for (i = 0; i < wiphy->n_iface_combinations; i++) {
821 const struct ieee80211_iface_combination *c;
822 struct nlattr *nl_combi, *nl_limits;
823
824 c = &wiphy->iface_combinations[i];
825
826 nl_combi = nla_nest_start(msg, i + 1);
827 if (!nl_combi)
828 goto nla_put_failure;
829
830 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
831 if (!nl_limits)
832 goto nla_put_failure;
833
834 for (j = 0; j < c->n_limits; j++) {
835 struct nlattr *nl_limit;
836
837 nl_limit = nla_nest_start(msg, j + 1);
838 if (!nl_limit)
839 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400840 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
841 c->limits[j].max))
842 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200843 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
844 c->limits[j].types))
845 goto nla_put_failure;
846 nla_nest_end(msg, nl_limit);
847 }
848
849 nla_nest_end(msg, nl_limits);
850
David S. Miller9360ffd2012-03-29 04:41:26 -0400851 if (c->beacon_int_infra_match &&
852 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
853 goto nla_put_failure;
854 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
855 c->num_different_channels) ||
856 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
857 c->max_interfaces))
858 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200859
860 nla_nest_end(msg, nl_combi);
861 }
862
863 nla_nest_end(msg, nl_combis);
864
865 return 0;
866nla_put_failure:
867 return -ENOBUFS;
868}
869
Eric W. Biederman15e47302012-09-07 20:12:54 +0000870static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400871 struct cfg80211_registered_device *dev)
872{
873 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100874 struct nlattr *nl_bands, *nl_band;
875 struct nlattr *nl_freqs, *nl_freq;
876 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100877 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100878 enum ieee80211_band band;
879 struct ieee80211_channel *chan;
880 struct ieee80211_rate *rate;
881 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200882 const struct ieee80211_txrx_stypes *mgmt_stypes =
883 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400884
Eric W. Biederman15e47302012-09-07 20:12:54 +0000885 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400886 if (!hdr)
887 return -1;
888
David S. Miller9360ffd2012-03-29 04:41:26 -0400889 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
890 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
891 nla_put_u32(msg, NL80211_ATTR_GENERATION,
892 cfg80211_rdev_list_generation) ||
893 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
894 dev->wiphy.retry_short) ||
895 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
896 dev->wiphy.retry_long) ||
897 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
898 dev->wiphy.frag_threshold) ||
899 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
900 dev->wiphy.rts_threshold) ||
901 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
902 dev->wiphy.coverage_class) ||
903 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
904 dev->wiphy.max_scan_ssids) ||
905 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
906 dev->wiphy.max_sched_scan_ssids) ||
907 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
908 dev->wiphy.max_scan_ie_len) ||
909 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
910 dev->wiphy.max_sched_scan_ie_len) ||
911 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
912 dev->wiphy.max_match_sets))
913 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200914
David S. Miller9360ffd2012-03-29 04:41:26 -0400915 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
916 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
917 goto nla_put_failure;
918 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
919 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
920 goto nla_put_failure;
921 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
922 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
923 goto nla_put_failure;
924 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
925 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
926 goto nla_put_failure;
927 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
928 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
929 goto nla_put_failure;
930 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
931 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
932 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200933
David S. Miller9360ffd2012-03-29 04:41:26 -0400934 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
935 sizeof(u32) * dev->wiphy.n_cipher_suites,
936 dev->wiphy.cipher_suites))
937 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100938
David S. Miller9360ffd2012-03-29 04:41:26 -0400939 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
940 dev->wiphy.max_num_pmkids))
941 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530942
David S. Miller9360ffd2012-03-29 04:41:26 -0400943 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
944 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
945 goto nla_put_failure;
Johannes Berg25e47c182009-04-02 20:14:06 +0200946
David S. Miller9360ffd2012-03-29 04:41:26 -0400947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
948 dev->wiphy.available_antennas_tx) ||
949 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
950 dev->wiphy.available_antennas_rx))
951 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100952
David S. Miller9360ffd2012-03-29 04:41:26 -0400953 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
954 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
955 dev->wiphy.probe_resp_offload))
956 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200957
Bruno Randolf7f531e02010-12-16 11:30:22 +0900958 if ((dev->wiphy.available_antennas_tx ||
959 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900960 u32 tx_ant = 0, rx_ant = 0;
961 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300962 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900963 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400964 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
965 tx_ant) ||
966 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
967 rx_ant))
968 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900969 }
970 }
971
Johannes Berg7527a782011-05-13 10:58:57 +0200972 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
973 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700974 goto nla_put_failure;
975
Johannes Bergee688b002008-01-24 19:38:39 +0100976 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
977 if (!nl_bands)
978 goto nla_put_failure;
979
980 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
981 if (!dev->wiphy.bands[band])
982 continue;
983
984 nl_band = nla_nest_start(msg, band);
985 if (!nl_band)
986 goto nla_put_failure;
987
Johannes Bergd51626d2008-10-09 12:20:13 +0200988 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400989 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
990 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
991 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
992 &dev->wiphy.bands[band]->ht_cap.mcs) ||
993 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
994 dev->wiphy.bands[band]->ht_cap.cap) ||
995 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
996 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
997 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
998 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
999 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001000
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001001 /* add VHT info */
1002 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1003 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1004 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1005 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1006 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1007 dev->wiphy.bands[band]->vht_cap.cap)))
1008 goto nla_put_failure;
1009
Johannes Bergee688b002008-01-24 19:38:39 +01001010 /* add frequencies */
1011 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1012 if (!nl_freqs)
1013 goto nla_put_failure;
1014
1015 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1016 nl_freq = nla_nest_start(msg, i);
1017 if (!nl_freq)
1018 goto nla_put_failure;
1019
1020 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001021
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001022 if (nl80211_msg_put_channel(msg, chan))
1023 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001024
Johannes Bergee688b002008-01-24 19:38:39 +01001025 nla_nest_end(msg, nl_freq);
1026 }
1027
1028 nla_nest_end(msg, nl_freqs);
1029
1030 /* add bitrates */
1031 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1032 if (!nl_rates)
1033 goto nla_put_failure;
1034
1035 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1036 nl_rate = nla_nest_start(msg, i);
1037 if (!nl_rate)
1038 goto nla_put_failure;
1039
1040 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001041 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1042 rate->bitrate))
1043 goto nla_put_failure;
1044 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1045 nla_put_flag(msg,
1046 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1047 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001048
1049 nla_nest_end(msg, nl_rate);
1050 }
1051
1052 nla_nest_end(msg, nl_rates);
1053
1054 nla_nest_end(msg, nl_band);
1055 }
1056 nla_nest_end(msg, nl_bands);
1057
Johannes Berg8fdc6212009-03-14 09:34:01 +01001058 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1059 if (!nl_cmds)
1060 goto nla_put_failure;
1061
1062 i = 0;
1063#define CMD(op, n) \
1064 do { \
1065 if (dev->ops->op) { \
1066 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001067 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1068 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001069 } \
1070 } while (0)
1071
1072 CMD(add_virtual_intf, NEW_INTERFACE);
1073 CMD(change_virtual_intf, SET_INTERFACE);
1074 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001075 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001076 CMD(add_station, NEW_STATION);
1077 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001078 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001079 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001080 CMD(auth, AUTHENTICATE);
1081 CMD(assoc, ASSOCIATE);
1082 CMD(deauth, DEAUTHENTICATE);
1083 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001084 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001085 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001086 CMD(set_pmksa, SET_PMKSA);
1087 CMD(del_pmksa, DEL_PMKSA);
1088 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001089 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1090 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001091 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001092 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001093 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001094 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001095 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001096 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1097 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001098 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001099 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001100 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001101 i++;
1102 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1103 goto nla_put_failure;
1104 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001105 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001106 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1107 CMD(tdls_mgmt, TDLS_MGMT);
1108 CMD(tdls_oper, TDLS_OPER);
1109 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001110 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1111 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001112 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001113 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001114 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1115 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001116 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1117 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001118 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001119 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001120 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001121
Kalle Valo4745fc02011-11-17 19:06:10 +02001122#ifdef CONFIG_NL80211_TESTMODE
1123 CMD(testmode_cmd, TESTMODE);
1124#endif
1125
Johannes Berg8fdc6212009-03-14 09:34:01 +01001126#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001127
Johannes Berg6829c872009-07-02 09:13:27 +02001128 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001129 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001130 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1131 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001132 }
1133
Johannes Berg6829c872009-07-02 09:13:27 +02001134 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001135 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001136 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1137 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001138 }
1139
Johannes Berg8fdc6212009-03-14 09:34:01 +01001140 nla_nest_end(msg, nl_cmds);
1141
Johannes Berg7c4ef712011-11-18 15:33:48 +01001142 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001143 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1144 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1145 dev->wiphy.max_remain_on_channel_duration))
1146 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001147
David S. Miller9360ffd2012-03-29 04:41:26 -04001148 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1149 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1150 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001151
Johannes Berg2e161f72010-08-12 15:38:38 +02001152 if (mgmt_stypes) {
1153 u16 stypes;
1154 struct nlattr *nl_ftypes, *nl_ifs;
1155 enum nl80211_iftype ift;
1156
1157 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1158 if (!nl_ifs)
1159 goto nla_put_failure;
1160
1161 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1162 nl_ftypes = nla_nest_start(msg, ift);
1163 if (!nl_ftypes)
1164 goto nla_put_failure;
1165 i = 0;
1166 stypes = mgmt_stypes[ift].tx;
1167 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001168 if ((stypes & 1) &&
1169 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1170 (i << 4) | IEEE80211_FTYPE_MGMT))
1171 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001172 stypes >>= 1;
1173 i++;
1174 }
1175 nla_nest_end(msg, nl_ftypes);
1176 }
1177
Johannes Berg74b70a42010-08-24 12:15:53 +02001178 nla_nest_end(msg, nl_ifs);
1179
Johannes Berg2e161f72010-08-12 15:38:38 +02001180 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1181 if (!nl_ifs)
1182 goto nla_put_failure;
1183
1184 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1185 nl_ftypes = nla_nest_start(msg, ift);
1186 if (!nl_ftypes)
1187 goto nla_put_failure;
1188 i = 0;
1189 stypes = mgmt_stypes[ift].rx;
1190 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001191 if ((stypes & 1) &&
1192 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1193 (i << 4) | IEEE80211_FTYPE_MGMT))
1194 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001195 stypes >>= 1;
1196 i++;
1197 }
1198 nla_nest_end(msg, nl_ftypes);
1199 }
1200 nla_nest_end(msg, nl_ifs);
1201 }
1202
Johannes Bergdfb89c52012-06-27 09:23:48 +02001203#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001204 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1205 struct nlattr *nl_wowlan;
1206
1207 nl_wowlan = nla_nest_start(msg,
1208 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1209 if (!nl_wowlan)
1210 goto nla_put_failure;
1211
David S. Miller9360ffd2012-03-29 04:41:26 -04001212 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1213 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1214 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1215 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1216 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1217 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1218 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1219 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1220 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1221 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1222 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1223 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1224 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1226 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1227 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1228 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001229 if (dev->wiphy.wowlan.n_patterns) {
1230 struct nl80211_wowlan_pattern_support pat = {
1231 .max_patterns = dev->wiphy.wowlan.n_patterns,
1232 .min_pattern_len =
1233 dev->wiphy.wowlan.pattern_min_len,
1234 .max_pattern_len =
1235 dev->wiphy.wowlan.pattern_max_len,
1236 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1238 sizeof(pat), &pat))
1239 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001240 }
1241
1242 nla_nest_end(msg, nl_wowlan);
1243 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001244#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001245
Johannes Berg7527a782011-05-13 10:58:57 +02001246 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1247 dev->wiphy.software_iftypes))
1248 goto nla_put_failure;
1249
1250 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1251 goto nla_put_failure;
1252
David S. Miller9360ffd2012-03-29 04:41:26 -04001253 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1254 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1255 dev->wiphy.ap_sme_capa))
1256 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001257
David S. Miller9360ffd2012-03-29 04:41:26 -04001258 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1259 dev->wiphy.features))
1260 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001261
David S. Miller9360ffd2012-03-29 04:41:26 -04001262 if (dev->wiphy.ht_capa_mod_mask &&
1263 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1264 sizeof(*dev->wiphy.ht_capa_mod_mask),
1265 dev->wiphy.ht_capa_mod_mask))
1266 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001267
Johannes Berg55682962007-09-20 13:09:35 -04001268 return genlmsg_end(msg, hdr);
1269
1270 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001271 genlmsg_cancel(msg, hdr);
1272 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001273}
1274
1275static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1276{
1277 int idx = 0;
1278 int start = cb->args[0];
1279 struct cfg80211_registered_device *dev;
1280
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001281 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001282 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001283 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1284 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001285 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001286 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001287 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001288 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001289 dev) < 0) {
1290 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001291 break;
Julius Volzb4637272008-07-08 14:02:19 +02001292 }
Johannes Berg55682962007-09-20 13:09:35 -04001293 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001294 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001295
1296 cb->args[0] = idx;
1297
1298 return skb->len;
1299}
1300
1301static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1302{
1303 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001304 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001305
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001306 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001307 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001308 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001309
Eric W. Biederman15e47302012-09-07 20:12:54 +00001310 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001311 nlmsg_free(msg);
1312 return -ENOBUFS;
1313 }
Johannes Berg55682962007-09-20 13:09:35 -04001314
Johannes Berg134e6372009-07-10 09:51:34 +00001315 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001316}
1317
Jouni Malinen31888482008-10-30 16:59:24 +02001318static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1319 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1320 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1321 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1322 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1323 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1324};
1325
1326static int parse_txq_params(struct nlattr *tb[],
1327 struct ieee80211_txq_params *txq_params)
1328{
Johannes Berga3304b02012-03-28 11:04:24 +02001329 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001330 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1331 !tb[NL80211_TXQ_ATTR_AIFS])
1332 return -EINVAL;
1333
Johannes Berga3304b02012-03-28 11:04:24 +02001334 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001335 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1336 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1337 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1338 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1339
Johannes Berga3304b02012-03-28 11:04:24 +02001340 if (txq_params->ac >= NL80211_NUM_ACS)
1341 return -EINVAL;
1342
Jouni Malinen31888482008-10-30 16:59:24 +02001343 return 0;
1344}
1345
Johannes Bergf444de02010-05-05 15:25:02 +02001346static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1347{
1348 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001349 * You can only set the channel explicitly for WDS interfaces,
1350 * all others have their channel managed via their respective
1351 * "establish a connection" command (connect, join, ...)
1352 *
1353 * For AP/GO and mesh mode, the channel can be set with the
1354 * channel userspace API, but is only stored and passed to the
1355 * low-level driver when the AP starts or the mesh is joined.
1356 * This is for backward compatibility, userspace can also give
1357 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001358 *
1359 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001360 * whatever else is going on, so they have their own special
1361 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001362 */
1363 return !wdev ||
1364 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001365 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001366 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1367 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001368}
1369
Johannes Berg683b6d32012-11-08 21:25:48 +01001370static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1371 struct genl_info *info,
1372 struct cfg80211_chan_def *chandef)
1373{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301374 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001375
1376 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1377 return -EINVAL;
1378
1379 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1380
1381 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001382 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1383 chandef->center_freq1 = control_freq;
1384 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001385
1386 /* Primary channel not allowed */
1387 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1388 return -EINVAL;
1389
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001390 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1391 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001392
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001393 chantype = nla_get_u32(
1394 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1395
1396 switch (chantype) {
1397 case NL80211_CHAN_NO_HT:
1398 case NL80211_CHAN_HT20:
1399 case NL80211_CHAN_HT40PLUS:
1400 case NL80211_CHAN_HT40MINUS:
1401 cfg80211_chandef_create(chandef, chandef->chan,
1402 chantype);
1403 break;
1404 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001405 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001406 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001407 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1408 chandef->width =
1409 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1410 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1411 chandef->center_freq1 =
1412 nla_get_u32(
1413 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1414 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1415 chandef->center_freq2 =
1416 nla_get_u32(
1417 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1418 }
1419
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001420 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001421 return -EINVAL;
1422
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001423 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1424 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001425 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001426
Johannes Berg683b6d32012-11-08 21:25:48 +01001427 return 0;
1428}
1429
Johannes Bergf444de02010-05-05 15:25:02 +02001430static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1431 struct wireless_dev *wdev,
1432 struct genl_info *info)
1433{
Johannes Berg683b6d32012-11-08 21:25:48 +01001434 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001435 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001436 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1437
1438 if (wdev)
1439 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001440
Johannes Bergf444de02010-05-05 15:25:02 +02001441 if (!nl80211_can_set_dev_channel(wdev))
1442 return -EOPNOTSUPP;
1443
Johannes Berg683b6d32012-11-08 21:25:48 +01001444 result = nl80211_parse_chandef(rdev, info, &chandef);
1445 if (result)
1446 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001447
1448 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001449 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001450 case NL80211_IFTYPE_AP:
1451 case NL80211_IFTYPE_P2P_GO:
1452 if (wdev->beacon_interval) {
1453 result = -EBUSY;
1454 break;
1455 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001456 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001457 result = -EINVAL;
1458 break;
1459 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001460 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001461 result = 0;
1462 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001463 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001464 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001465 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001466 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001467 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001468 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001469 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001470 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001471 }
1472 mutex_unlock(&rdev->devlist_mtx);
1473
1474 return result;
1475}
1476
1477static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1478{
Johannes Berg4c476992010-10-04 21:36:35 +02001479 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1480 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001481
Johannes Berg4c476992010-10-04 21:36:35 +02001482 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001483}
1484
Bill Jordane8347eb2010-10-01 13:54:28 -04001485static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1486{
Johannes Berg43b19952010-10-07 13:10:30 +02001487 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1488 struct net_device *dev = info->user_ptr[1];
1489 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001490 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001491
1492 if (!info->attrs[NL80211_ATTR_MAC])
1493 return -EINVAL;
1494
Johannes Berg43b19952010-10-07 13:10:30 +02001495 if (netif_running(dev))
1496 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001497
Johannes Berg43b19952010-10-07 13:10:30 +02001498 if (!rdev->ops->set_wds_peer)
1499 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001500
Johannes Berg43b19952010-10-07 13:10:30 +02001501 if (wdev->iftype != NL80211_IFTYPE_WDS)
1502 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001503
1504 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001505 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001506}
1507
1508
Johannes Berg55682962007-09-20 13:09:35 -04001509static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1510{
1511 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001512 struct net_device *netdev = NULL;
1513 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001514 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001515 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001516 u32 changed;
1517 u8 retry_short = 0, retry_long = 0;
1518 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001519 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001520
Johannes Bergf444de02010-05-05 15:25:02 +02001521 /*
1522 * Try to find the wiphy and netdev. Normally this
1523 * function shouldn't need the netdev, but this is
1524 * done for backward compatibility -- previously
1525 * setting the channel was done per wiphy, but now
1526 * it is per netdev. Previous userland like hostapd
1527 * also passed a netdev to set_wiphy, so that it is
1528 * possible to let that go to the right netdev!
1529 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001530 mutex_lock(&cfg80211_mutex);
1531
Johannes Bergf444de02010-05-05 15:25:02 +02001532 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1533 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1534
1535 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1536 if (netdev && netdev->ieee80211_ptr) {
1537 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1538 mutex_lock(&rdev->mtx);
1539 } else
1540 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001541 }
1542
Johannes Bergf444de02010-05-05 15:25:02 +02001543 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001544 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1545 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001546 if (IS_ERR(rdev)) {
1547 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001548 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001549 }
1550 wdev = NULL;
1551 netdev = NULL;
1552 result = 0;
1553
1554 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001555 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001556 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001557
1558 /*
1559 * end workaround code, by now the rdev is available
1560 * and locked, and wdev may or may not be NULL.
1561 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001562
1563 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001564 result = cfg80211_dev_rename(
1565 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001566
1567 mutex_unlock(&cfg80211_mutex);
1568
1569 if (result)
1570 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001571
Jouni Malinen31888482008-10-30 16:59:24 +02001572 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1573 struct ieee80211_txq_params txq_params;
1574 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1575
1576 if (!rdev->ops->set_txq_params) {
1577 result = -EOPNOTSUPP;
1578 goto bad_res;
1579 }
1580
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001581 if (!netdev) {
1582 result = -EINVAL;
1583 goto bad_res;
1584 }
1585
Johannes Berg133a3ff2011-11-03 14:50:13 +01001586 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1587 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1588 result = -EINVAL;
1589 goto bad_res;
1590 }
1591
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001592 if (!netif_running(netdev)) {
1593 result = -ENETDOWN;
1594 goto bad_res;
1595 }
1596
Jouni Malinen31888482008-10-30 16:59:24 +02001597 nla_for_each_nested(nl_txq_params,
1598 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1599 rem_txq_params) {
1600 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1601 nla_data(nl_txq_params),
1602 nla_len(nl_txq_params),
1603 txq_params_policy);
1604 result = parse_txq_params(tb, &txq_params);
1605 if (result)
1606 goto bad_res;
1607
Hila Gonene35e4d22012-06-27 17:19:42 +03001608 result = rdev_set_txq_params(rdev, netdev,
1609 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001610 if (result)
1611 goto bad_res;
1612 }
1613 }
1614
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001615 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001616 result = __nl80211_set_channel(rdev,
1617 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1618 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001619 if (result)
1620 goto bad_res;
1621 }
1622
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001623 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001624 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001625 enum nl80211_tx_power_setting type;
1626 int idx, mbm = 0;
1627
Johannes Bergc8442112012-10-24 10:17:18 +02001628 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1629 txp_wdev = NULL;
1630
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001631 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001632 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001633 goto bad_res;
1634 }
1635
1636 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1637 type = nla_get_u32(info->attrs[idx]);
1638
1639 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1640 (type != NL80211_TX_POWER_AUTOMATIC)) {
1641 result = -EINVAL;
1642 goto bad_res;
1643 }
1644
1645 if (type != NL80211_TX_POWER_AUTOMATIC) {
1646 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1647 mbm = nla_get_u32(info->attrs[idx]);
1648 }
1649
Johannes Bergc8442112012-10-24 10:17:18 +02001650 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001651 if (result)
1652 goto bad_res;
1653 }
1654
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001655 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1656 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1657 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001658 if ((!rdev->wiphy.available_antennas_tx &&
1659 !rdev->wiphy.available_antennas_rx) ||
1660 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001661 result = -EOPNOTSUPP;
1662 goto bad_res;
1663 }
1664
1665 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1666 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1667
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001668 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001669 * available antenna masks, except for the "all" mask */
1670 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1671 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001672 result = -EINVAL;
1673 goto bad_res;
1674 }
1675
Bruno Randolf7f531e02010-12-16 11:30:22 +09001676 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1677 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001678
Hila Gonene35e4d22012-06-27 17:19:42 +03001679 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001680 if (result)
1681 goto bad_res;
1682 }
1683
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001684 changed = 0;
1685
1686 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1687 retry_short = nla_get_u8(
1688 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1689 if (retry_short == 0) {
1690 result = -EINVAL;
1691 goto bad_res;
1692 }
1693 changed |= WIPHY_PARAM_RETRY_SHORT;
1694 }
1695
1696 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1697 retry_long = nla_get_u8(
1698 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1699 if (retry_long == 0) {
1700 result = -EINVAL;
1701 goto bad_res;
1702 }
1703 changed |= WIPHY_PARAM_RETRY_LONG;
1704 }
1705
1706 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1707 frag_threshold = nla_get_u32(
1708 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1709 if (frag_threshold < 256) {
1710 result = -EINVAL;
1711 goto bad_res;
1712 }
1713 if (frag_threshold != (u32) -1) {
1714 /*
1715 * Fragments (apart from the last one) are required to
1716 * have even length. Make the fragmentation code
1717 * simpler by stripping LSB should someone try to use
1718 * odd threshold value.
1719 */
1720 frag_threshold &= ~0x1;
1721 }
1722 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1723 }
1724
1725 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1726 rts_threshold = nla_get_u32(
1727 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1728 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1729 }
1730
Lukáš Turek81077e82009-12-21 22:50:47 +01001731 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1732 coverage_class = nla_get_u8(
1733 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1734 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1735 }
1736
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001737 if (changed) {
1738 u8 old_retry_short, old_retry_long;
1739 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001740 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001741
1742 if (!rdev->ops->set_wiphy_params) {
1743 result = -EOPNOTSUPP;
1744 goto bad_res;
1745 }
1746
1747 old_retry_short = rdev->wiphy.retry_short;
1748 old_retry_long = rdev->wiphy.retry_long;
1749 old_frag_threshold = rdev->wiphy.frag_threshold;
1750 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001751 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001752
1753 if (changed & WIPHY_PARAM_RETRY_SHORT)
1754 rdev->wiphy.retry_short = retry_short;
1755 if (changed & WIPHY_PARAM_RETRY_LONG)
1756 rdev->wiphy.retry_long = retry_long;
1757 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1758 rdev->wiphy.frag_threshold = frag_threshold;
1759 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1760 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001761 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1762 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001763
Hila Gonene35e4d22012-06-27 17:19:42 +03001764 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001765 if (result) {
1766 rdev->wiphy.retry_short = old_retry_short;
1767 rdev->wiphy.retry_long = old_retry_long;
1768 rdev->wiphy.frag_threshold = old_frag_threshold;
1769 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001770 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001771 }
1772 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001773
Johannes Berg306d6112008-12-08 12:39:04 +01001774 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001775 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001776 if (netdev)
1777 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001778 return result;
1779}
1780
Johannes Berg71bbc992012-06-15 15:30:18 +02001781static inline u64 wdev_id(struct wireless_dev *wdev)
1782{
1783 return (u64)wdev->identifier |
1784 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1785}
Johannes Berg55682962007-09-20 13:09:35 -04001786
Johannes Berg683b6d32012-11-08 21:25:48 +01001787static int nl80211_send_chandef(struct sk_buff *msg,
1788 struct cfg80211_chan_def *chandef)
1789{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001790 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001791
Johannes Berg683b6d32012-11-08 21:25:48 +01001792 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1793 chandef->chan->center_freq))
1794 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001795 switch (chandef->width) {
1796 case NL80211_CHAN_WIDTH_20_NOHT:
1797 case NL80211_CHAN_WIDTH_20:
1798 case NL80211_CHAN_WIDTH_40:
1799 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1800 cfg80211_get_chandef_type(chandef)))
1801 return -ENOBUFS;
1802 break;
1803 default:
1804 break;
1805 }
1806 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1807 return -ENOBUFS;
1808 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1809 return -ENOBUFS;
1810 if (chandef->center_freq2 &&
1811 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001812 return -ENOBUFS;
1813 return 0;
1814}
1815
Eric W. Biederman15e47302012-09-07 20:12:54 +00001816static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001817 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001818 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001819{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001820 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001821 void *hdr;
1822
Eric W. Biederman15e47302012-09-07 20:12:54 +00001823 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001824 if (!hdr)
1825 return -1;
1826
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001827 if (dev &&
1828 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001829 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001830 goto nla_put_failure;
1831
1832 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1833 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001834 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001835 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001836 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1837 rdev->devlist_generation ^
1838 (cfg80211_rdev_list_generation << 2)))
1839 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001840
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001841 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001842 int ret;
1843 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001844
Johannes Berg683b6d32012-11-08 21:25:48 +01001845 ret = rdev_get_channel(rdev, wdev, &chandef);
1846 if (ret == 0) {
1847 if (nl80211_send_chandef(msg, &chandef))
1848 goto nla_put_failure;
1849 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001850 }
1851
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001852 if (wdev->ssid_len) {
1853 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1854 goto nla_put_failure;
1855 }
1856
Johannes Berg55682962007-09-20 13:09:35 -04001857 return genlmsg_end(msg, hdr);
1858
1859 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001860 genlmsg_cancel(msg, hdr);
1861 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001862}
1863
1864static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1865{
1866 int wp_idx = 0;
1867 int if_idx = 0;
1868 int wp_start = cb->args[0];
1869 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001870 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001871 struct wireless_dev *wdev;
1872
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001873 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001874 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1875 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001876 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001877 if (wp_idx < wp_start) {
1878 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001879 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001880 }
Johannes Berg55682962007-09-20 13:09:35 -04001881 if_idx = 0;
1882
Johannes Bergf5ea9122009-08-07 16:17:38 +02001883 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001884 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001885 if (if_idx < if_start) {
1886 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001887 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001888 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001889 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001890 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001891 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001892 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001893 goto out;
1894 }
1895 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001896 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001897 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001898
1899 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001900 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001901 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001902 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001903
1904 cb->args[0] = wp_idx;
1905 cb->args[1] = if_idx;
1906
1907 return skb->len;
1908}
1909
1910static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1911{
1912 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001913 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001914 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001915
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001916 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001917 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001918 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001919
Eric W. Biederman15e47302012-09-07 20:12:54 +00001920 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001921 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001922 nlmsg_free(msg);
1923 return -ENOBUFS;
1924 }
Johannes Berg55682962007-09-20 13:09:35 -04001925
Johannes Berg134e6372009-07-10 09:51:34 +00001926 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001927}
1928
Michael Wu66f7ac52008-01-31 19:48:22 +01001929static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1930 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1931 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1932 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1933 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1934 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1935};
1936
1937static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1938{
1939 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1940 int flag;
1941
1942 *mntrflags = 0;
1943
1944 if (!nla)
1945 return -EINVAL;
1946
1947 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1948 nla, mntr_flags_policy))
1949 return -EINVAL;
1950
1951 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1952 if (flags[flag])
1953 *mntrflags |= (1<<flag);
1954
1955 return 0;
1956}
1957
Johannes Berg9bc383d2009-11-19 11:55:19 +01001958static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001959 struct net_device *netdev, u8 use_4addr,
1960 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001961{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001962 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001963 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001964 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001965 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001966 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001967
1968 switch (iftype) {
1969 case NL80211_IFTYPE_AP_VLAN:
1970 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1971 return 0;
1972 break;
1973 case NL80211_IFTYPE_STATION:
1974 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1975 return 0;
1976 break;
1977 default:
1978 break;
1979 }
1980
1981 return -EOPNOTSUPP;
1982}
1983
Johannes Berg55682962007-09-20 13:09:35 -04001984static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1985{
Johannes Berg4c476992010-10-04 21:36:35 +02001986 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001987 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001988 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001989 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001990 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001991 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001992 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001993
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001994 memset(&params, 0, sizeof(params));
1995
Johannes Berg04a773a2009-04-19 21:24:32 +02001996 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001997
Johannes Berg723b0382008-09-16 20:22:09 +02001998 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001999 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002000 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002001 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002002 if (ntype > NL80211_IFTYPE_MAX)
2003 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002004 }
2005
Johannes Berg92ffe052008-09-16 20:39:36 +02002006 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002007 struct wireless_dev *wdev = dev->ieee80211_ptr;
2008
Johannes Berg4c476992010-10-04 21:36:35 +02002009 if (ntype != NL80211_IFTYPE_MESH_POINT)
2010 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002011 if (netif_running(dev))
2012 return -EBUSY;
2013
2014 wdev_lock(wdev);
2015 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2016 IEEE80211_MAX_MESH_ID_LEN);
2017 wdev->mesh_id_up_len =
2018 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2019 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2020 wdev->mesh_id_up_len);
2021 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002022 }
2023
Felix Fietkau8b787642009-11-10 18:53:10 +01002024 if (info->attrs[NL80211_ATTR_4ADDR]) {
2025 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2026 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002027 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002028 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002029 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002030 } else {
2031 params.use_4addr = -1;
2032 }
2033
Johannes Berg92ffe052008-09-16 20:39:36 +02002034 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002035 if (ntype != NL80211_IFTYPE_MONITOR)
2036 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002037 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2038 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002039 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002040 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002041
2042 flags = &_flags;
2043 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002044 }
Johannes Berg3b858752009-03-12 09:55:09 +01002045
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002046 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002047 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002048 else
2049 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002050
Johannes Berg9bc383d2009-11-19 11:55:19 +01002051 if (!err && params.use_4addr != -1)
2052 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2053
Johannes Berg55682962007-09-20 13:09:35 -04002054 return err;
2055}
2056
2057static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2058{
Johannes Berg4c476992010-10-04 21:36:35 +02002059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002060 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002061 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002062 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002063 int err;
2064 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002065 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002066
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002067 memset(&params, 0, sizeof(params));
2068
Johannes Berg55682962007-09-20 13:09:35 -04002069 if (!info->attrs[NL80211_ATTR_IFNAME])
2070 return -EINVAL;
2071
2072 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2073 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2074 if (type > NL80211_IFTYPE_MAX)
2075 return -EINVAL;
2076 }
2077
Johannes Berg79c97e92009-07-07 03:56:12 +02002078 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002079 !(rdev->wiphy.interface_modes & (1 << type)))
2080 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002081
Johannes Berg9bc383d2009-11-19 11:55:19 +01002082 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002083 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002084 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002085 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002086 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002087 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002088
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002089 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2090 if (!msg)
2091 return -ENOMEM;
2092
Michael Wu66f7ac52008-01-31 19:48:22 +01002093 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2094 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2095 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002096 wdev = rdev_add_virtual_intf(rdev,
2097 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2098 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002099 if (IS_ERR(wdev)) {
2100 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002101 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002102 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002103
Johannes Berg98104fde2012-06-16 00:19:54 +02002104 switch (type) {
2105 case NL80211_IFTYPE_MESH_POINT:
2106 if (!info->attrs[NL80211_ATTR_MESH_ID])
2107 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002108 wdev_lock(wdev);
2109 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2110 IEEE80211_MAX_MESH_ID_LEN);
2111 wdev->mesh_id_up_len =
2112 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2113 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2114 wdev->mesh_id_up_len);
2115 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002116 break;
2117 case NL80211_IFTYPE_P2P_DEVICE:
2118 /*
2119 * P2P Device doesn't have a netdev, so doesn't go
2120 * through the netdev notifier and must be added here
2121 */
2122 mutex_init(&wdev->mtx);
2123 INIT_LIST_HEAD(&wdev->event_list);
2124 spin_lock_init(&wdev->event_lock);
2125 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2126 spin_lock_init(&wdev->mgmt_registrations_lock);
2127
2128 mutex_lock(&rdev->devlist_mtx);
2129 wdev->identifier = ++rdev->wdev_id;
2130 list_add_rcu(&wdev->list, &rdev->wdev_list);
2131 rdev->devlist_generation++;
2132 mutex_unlock(&rdev->devlist_mtx);
2133 break;
2134 default:
2135 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002136 }
2137
Eric W. Biederman15e47302012-09-07 20:12:54 +00002138 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002139 rdev, wdev) < 0) {
2140 nlmsg_free(msg);
2141 return -ENOBUFS;
2142 }
2143
2144 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002145}
2146
2147static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2148{
Johannes Berg4c476992010-10-04 21:36:35 +02002149 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002150 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002151
Johannes Berg4c476992010-10-04 21:36:35 +02002152 if (!rdev->ops->del_virtual_intf)
2153 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002154
Johannes Berg84efbb82012-06-16 00:00:26 +02002155 /*
2156 * If we remove a wireless device without a netdev then clear
2157 * user_ptr[1] so that nl80211_post_doit won't dereference it
2158 * to check if it needs to do dev_put(). Otherwise it crashes
2159 * since the wdev has been freed, unlike with a netdev where
2160 * we need the dev_put() for the netdev to really be freed.
2161 */
2162 if (!wdev->netdev)
2163 info->user_ptr[1] = NULL;
2164
Hila Gonene35e4d22012-06-27 17:19:42 +03002165 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002166}
2167
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002168static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2169{
2170 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2171 struct net_device *dev = info->user_ptr[1];
2172 u16 noack_map;
2173
2174 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2175 return -EINVAL;
2176
2177 if (!rdev->ops->set_noack_map)
2178 return -EOPNOTSUPP;
2179
2180 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2181
Hila Gonene35e4d22012-06-27 17:19:42 +03002182 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002183}
2184
Johannes Berg41ade002007-12-19 02:03:29 +01002185struct get_key_cookie {
2186 struct sk_buff *msg;
2187 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002188 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002189};
2190
2191static void get_key_callback(void *c, struct key_params *params)
2192{
Johannes Bergb9454e82009-07-08 13:29:08 +02002193 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002194 struct get_key_cookie *cookie = c;
2195
David S. Miller9360ffd2012-03-29 04:41:26 -04002196 if ((params->key &&
2197 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2198 params->key_len, params->key)) ||
2199 (params->seq &&
2200 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2201 params->seq_len, params->seq)) ||
2202 (params->cipher &&
2203 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2204 params->cipher)))
2205 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002206
Johannes Bergb9454e82009-07-08 13:29:08 +02002207 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2208 if (!key)
2209 goto nla_put_failure;
2210
David S. Miller9360ffd2012-03-29 04:41:26 -04002211 if ((params->key &&
2212 nla_put(cookie->msg, NL80211_KEY_DATA,
2213 params->key_len, params->key)) ||
2214 (params->seq &&
2215 nla_put(cookie->msg, NL80211_KEY_SEQ,
2216 params->seq_len, params->seq)) ||
2217 (params->cipher &&
2218 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2219 params->cipher)))
2220 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002221
David S. Miller9360ffd2012-03-29 04:41:26 -04002222 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2223 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002224
2225 nla_nest_end(cookie->msg, key);
2226
Johannes Berg41ade002007-12-19 02:03:29 +01002227 return;
2228 nla_put_failure:
2229 cookie->error = 1;
2230}
2231
2232static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2233{
Johannes Berg4c476992010-10-04 21:36:35 +02002234 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002235 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002236 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002237 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002238 const u8 *mac_addr = NULL;
2239 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002240 struct get_key_cookie cookie = {
2241 .error = 0,
2242 };
2243 void *hdr;
2244 struct sk_buff *msg;
2245
2246 if (info->attrs[NL80211_ATTR_KEY_IDX])
2247 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2248
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002249 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002250 return -EINVAL;
2251
2252 if (info->attrs[NL80211_ATTR_MAC])
2253 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2254
Johannes Berge31b8212010-10-05 19:39:30 +02002255 pairwise = !!mac_addr;
2256 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2257 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2258 if (kt >= NUM_NL80211_KEYTYPES)
2259 return -EINVAL;
2260 if (kt != NL80211_KEYTYPE_GROUP &&
2261 kt != NL80211_KEYTYPE_PAIRWISE)
2262 return -EINVAL;
2263 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2264 }
2265
Johannes Berg4c476992010-10-04 21:36:35 +02002266 if (!rdev->ops->get_key)
2267 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002268
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002269 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002270 if (!msg)
2271 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002272
Eric W. Biederman15e47302012-09-07 20:12:54 +00002273 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002274 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002275 if (IS_ERR(hdr))
2276 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002277
2278 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002279 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002280
David S. Miller9360ffd2012-03-29 04:41:26 -04002281 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2282 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2283 goto nla_put_failure;
2284 if (mac_addr &&
2285 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2286 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002287
Johannes Berge31b8212010-10-05 19:39:30 +02002288 if (pairwise && mac_addr &&
2289 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2290 return -ENOENT;
2291
Hila Gonene35e4d22012-06-27 17:19:42 +03002292 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2293 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002294
2295 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002296 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002297
2298 if (cookie.error)
2299 goto nla_put_failure;
2300
2301 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002302 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002303
2304 nla_put_failure:
2305 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002306 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002307 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002308 return err;
2309}
2310
2311static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2312{
Johannes Berg4c476992010-10-04 21:36:35 +02002313 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002314 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002315 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002316 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002317
Johannes Bergb9454e82009-07-08 13:29:08 +02002318 err = nl80211_parse_key(info, &key);
2319 if (err)
2320 return err;
2321
2322 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002323 return -EINVAL;
2324
Johannes Bergb9454e82009-07-08 13:29:08 +02002325 /* only support setting default key */
2326 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002327 return -EINVAL;
2328
Johannes Bergfffd0932009-07-08 14:22:54 +02002329 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002330
2331 if (key.def) {
2332 if (!rdev->ops->set_default_key) {
2333 err = -EOPNOTSUPP;
2334 goto out;
2335 }
2336
2337 err = nl80211_key_allowed(dev->ieee80211_ptr);
2338 if (err)
2339 goto out;
2340
Hila Gonene35e4d22012-06-27 17:19:42 +03002341 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002342 key.def_uni, key.def_multi);
2343
2344 if (err)
2345 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002346
Johannes Berg3d23e342009-09-29 23:27:28 +02002347#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002348 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002349#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002350 } else {
2351 if (key.def_uni || !key.def_multi) {
2352 err = -EINVAL;
2353 goto out;
2354 }
2355
2356 if (!rdev->ops->set_default_mgmt_key) {
2357 err = -EOPNOTSUPP;
2358 goto out;
2359 }
2360
2361 err = nl80211_key_allowed(dev->ieee80211_ptr);
2362 if (err)
2363 goto out;
2364
Hila Gonene35e4d22012-06-27 17:19:42 +03002365 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002366 if (err)
2367 goto out;
2368
2369#ifdef CONFIG_CFG80211_WEXT
2370 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2371#endif
2372 }
2373
2374 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002375 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002376
Johannes Berg41ade002007-12-19 02:03:29 +01002377 return err;
2378}
2379
2380static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2381{
Johannes Berg4c476992010-10-04 21:36:35 +02002382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002383 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002384 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002385 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002386 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002387
Johannes Bergb9454e82009-07-08 13:29:08 +02002388 err = nl80211_parse_key(info, &key);
2389 if (err)
2390 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002391
Johannes Bergb9454e82009-07-08 13:29:08 +02002392 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002393 return -EINVAL;
2394
Johannes Berg41ade002007-12-19 02:03:29 +01002395 if (info->attrs[NL80211_ATTR_MAC])
2396 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2397
Johannes Berge31b8212010-10-05 19:39:30 +02002398 if (key.type == -1) {
2399 if (mac_addr)
2400 key.type = NL80211_KEYTYPE_PAIRWISE;
2401 else
2402 key.type = NL80211_KEYTYPE_GROUP;
2403 }
2404
2405 /* for now */
2406 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2407 key.type != NL80211_KEYTYPE_GROUP)
2408 return -EINVAL;
2409
Johannes Berg4c476992010-10-04 21:36:35 +02002410 if (!rdev->ops->add_key)
2411 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002412
Johannes Berge31b8212010-10-05 19:39:30 +02002413 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2414 key.type == NL80211_KEYTYPE_PAIRWISE,
2415 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002416 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002417
2418 wdev_lock(dev->ieee80211_ptr);
2419 err = nl80211_key_allowed(dev->ieee80211_ptr);
2420 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002421 err = rdev_add_key(rdev, dev, key.idx,
2422 key.type == NL80211_KEYTYPE_PAIRWISE,
2423 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002424 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002425
Johannes Berg41ade002007-12-19 02:03:29 +01002426 return err;
2427}
2428
2429static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2430{
Johannes Berg4c476992010-10-04 21:36:35 +02002431 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002432 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002433 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002434 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002435 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002436
Johannes Bergb9454e82009-07-08 13:29:08 +02002437 err = nl80211_parse_key(info, &key);
2438 if (err)
2439 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002440
2441 if (info->attrs[NL80211_ATTR_MAC])
2442 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2443
Johannes Berge31b8212010-10-05 19:39:30 +02002444 if (key.type == -1) {
2445 if (mac_addr)
2446 key.type = NL80211_KEYTYPE_PAIRWISE;
2447 else
2448 key.type = NL80211_KEYTYPE_GROUP;
2449 }
2450
2451 /* for now */
2452 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2453 key.type != NL80211_KEYTYPE_GROUP)
2454 return -EINVAL;
2455
Johannes Berg4c476992010-10-04 21:36:35 +02002456 if (!rdev->ops->del_key)
2457 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002458
Johannes Bergfffd0932009-07-08 14:22:54 +02002459 wdev_lock(dev->ieee80211_ptr);
2460 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002461
2462 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2463 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2464 err = -ENOENT;
2465
Johannes Bergfffd0932009-07-08 14:22:54 +02002466 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002467 err = rdev_del_key(rdev, dev, key.idx,
2468 key.type == NL80211_KEYTYPE_PAIRWISE,
2469 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002470
Johannes Berg3d23e342009-09-29 23:27:28 +02002471#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002472 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002473 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002474 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002475 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002476 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2477 }
2478#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002479 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002480
Johannes Berg41ade002007-12-19 02:03:29 +01002481 return err;
2482}
2483
Johannes Berg88600202012-02-13 15:17:18 +01002484static int nl80211_parse_beacon(struct genl_info *info,
2485 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002486{
Johannes Berg88600202012-02-13 15:17:18 +01002487 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002488
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002489 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2490 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2491 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2492 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002493 return -EINVAL;
2494
Johannes Berg88600202012-02-13 15:17:18 +01002495 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002496
Johannes Berged1b6cc2007-12-19 02:03:32 +01002497 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002498 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2499 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2500 if (!bcn->head_len)
2501 return -EINVAL;
2502 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002503 }
2504
2505 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002506 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2507 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002508 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002509 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002510 }
2511
Johannes Berg4c476992010-10-04 21:36:35 +02002512 if (!haveinfo)
2513 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002514
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002515 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002516 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2517 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002518 }
2519
2520 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002521 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002522 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002523 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002524 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2525 }
2526
2527 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002528 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002529 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002530 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002531 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2532 }
2533
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002534 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002535 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002536 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002537 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002538 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2539 }
2540
Johannes Berg88600202012-02-13 15:17:18 +01002541 return 0;
2542}
2543
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002544static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2545 struct cfg80211_ap_settings *params)
2546{
2547 struct wireless_dev *wdev;
2548 bool ret = false;
2549
2550 mutex_lock(&rdev->devlist_mtx);
2551
Johannes Berg89a54e42012-06-15 14:33:17 +02002552 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002553 if (wdev->iftype != NL80211_IFTYPE_AP &&
2554 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2555 continue;
2556
Johannes Berg683b6d32012-11-08 21:25:48 +01002557 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002558 continue;
2559
Johannes Berg683b6d32012-11-08 21:25:48 +01002560 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002561 ret = true;
2562 break;
2563 }
2564
2565 mutex_unlock(&rdev->devlist_mtx);
2566
2567 return ret;
2568}
2569
Jouni Malinene39e5b52012-09-30 19:29:39 +03002570static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2571 enum nl80211_auth_type auth_type,
2572 enum nl80211_commands cmd)
2573{
2574 if (auth_type > NL80211_AUTHTYPE_MAX)
2575 return false;
2576
2577 switch (cmd) {
2578 case NL80211_CMD_AUTHENTICATE:
2579 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2580 auth_type == NL80211_AUTHTYPE_SAE)
2581 return false;
2582 return true;
2583 case NL80211_CMD_CONNECT:
2584 case NL80211_CMD_START_AP:
2585 /* SAE not supported yet */
2586 if (auth_type == NL80211_AUTHTYPE_SAE)
2587 return false;
2588 return true;
2589 default:
2590 return false;
2591 }
2592}
2593
Johannes Berg88600202012-02-13 15:17:18 +01002594static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2595{
2596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2597 struct net_device *dev = info->user_ptr[1];
2598 struct wireless_dev *wdev = dev->ieee80211_ptr;
2599 struct cfg80211_ap_settings params;
2600 int err;
2601
2602 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2603 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2604 return -EOPNOTSUPP;
2605
2606 if (!rdev->ops->start_ap)
2607 return -EOPNOTSUPP;
2608
2609 if (wdev->beacon_interval)
2610 return -EALREADY;
2611
2612 memset(&params, 0, sizeof(params));
2613
2614 /* these are required for START_AP */
2615 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2616 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2617 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2618 return -EINVAL;
2619
2620 err = nl80211_parse_beacon(info, &params.beacon);
2621 if (err)
2622 return err;
2623
2624 params.beacon_interval =
2625 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2626 params.dtim_period =
2627 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2628
2629 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2630 if (err)
2631 return err;
2632
2633 /*
2634 * In theory, some of these attributes should be required here
2635 * but since they were not used when the command was originally
2636 * added, keep them optional for old user space programs to let
2637 * them continue to work with drivers that do not need the
2638 * additional information -- drivers must check!
2639 */
2640 if (info->attrs[NL80211_ATTR_SSID]) {
2641 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2642 params.ssid_len =
2643 nla_len(info->attrs[NL80211_ATTR_SSID]);
2644 if (params.ssid_len == 0 ||
2645 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2646 return -EINVAL;
2647 }
2648
2649 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2650 params.hidden_ssid = nla_get_u32(
2651 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2652 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2653 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2654 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2655 return -EINVAL;
2656 }
2657
2658 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2659
2660 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2661 params.auth_type = nla_get_u32(
2662 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002663 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2664 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002665 return -EINVAL;
2666 } else
2667 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2668
2669 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2670 NL80211_MAX_NR_CIPHER_SUITES);
2671 if (err)
2672 return err;
2673
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302674 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2675 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2676 return -EOPNOTSUPP;
2677 params.inactivity_timeout = nla_get_u16(
2678 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2679 }
2680
Johannes Berg53cabad2012-11-14 15:17:28 +01002681 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2682 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2683 return -EINVAL;
2684 params.p2p_ctwindow =
2685 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2686 if (params.p2p_ctwindow > 127)
2687 return -EINVAL;
2688 if (params.p2p_ctwindow != 0 &&
2689 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2690 return -EINVAL;
2691 }
2692
2693 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2694 u8 tmp;
2695
2696 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2697 return -EINVAL;
2698 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2699 if (tmp > 1)
2700 return -EINVAL;
2701 params.p2p_opp_ps = tmp;
2702 if (params.p2p_opp_ps != 0 &&
2703 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2704 return -EINVAL;
2705 }
2706
Johannes Bergaa430da2012-05-16 23:50:18 +02002707 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002708 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2709 if (err)
2710 return err;
2711 } else if (wdev->preset_chandef.chan) {
2712 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002713 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002714 return -EINVAL;
2715
Johannes Berg683b6d32012-11-08 21:25:48 +01002716 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002717 return -EINVAL;
2718
Michal Kaziore4e32452012-06-29 12:47:08 +02002719 mutex_lock(&rdev->devlist_mtx);
Johannes Berg683b6d32012-11-08 21:25:48 +01002720 err = cfg80211_can_use_chan(rdev, wdev, params.chandef.chan,
Michal Kaziore4e32452012-06-29 12:47:08 +02002721 CHAN_MODE_SHARED);
2722 mutex_unlock(&rdev->devlist_mtx);
2723
2724 if (err)
2725 return err;
2726
Hila Gonene35e4d22012-06-27 17:19:42 +03002727 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002728 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002729 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002730 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002731 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002732 wdev->ssid_len = params.ssid_len;
2733 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002734 }
Johannes Berg56d18932011-05-09 18:41:15 +02002735 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002736}
2737
Johannes Berg88600202012-02-13 15:17:18 +01002738static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2739{
2740 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2741 struct net_device *dev = info->user_ptr[1];
2742 struct wireless_dev *wdev = dev->ieee80211_ptr;
2743 struct cfg80211_beacon_data params;
2744 int err;
2745
2746 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2747 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2748 return -EOPNOTSUPP;
2749
2750 if (!rdev->ops->change_beacon)
2751 return -EOPNOTSUPP;
2752
2753 if (!wdev->beacon_interval)
2754 return -EINVAL;
2755
2756 err = nl80211_parse_beacon(info, &params);
2757 if (err)
2758 return err;
2759
Hila Gonene35e4d22012-06-27 17:19:42 +03002760 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002761}
2762
2763static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002764{
Johannes Berg4c476992010-10-04 21:36:35 +02002765 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2766 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002767
Michal Kazior60771782012-06-29 12:46:56 +02002768 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002769}
2770
Johannes Berg5727ef12007-12-19 02:03:34 +01002771static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2772 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2773 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2774 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002775 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002776 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002777 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002778};
2779
Johannes Bergeccb8e82009-05-11 21:57:56 +03002780static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002781 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002782 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002783{
2784 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002785 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002786 int flag;
2787
Johannes Bergeccb8e82009-05-11 21:57:56 +03002788 /*
2789 * Try parsing the new attribute first so userspace
2790 * can specify both for older kernels.
2791 */
2792 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2793 if (nla) {
2794 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002795
Johannes Bergeccb8e82009-05-11 21:57:56 +03002796 sta_flags = nla_data(nla);
2797 params->sta_flags_mask = sta_flags->mask;
2798 params->sta_flags_set = sta_flags->set;
2799 if ((params->sta_flags_mask |
2800 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2801 return -EINVAL;
2802 return 0;
2803 }
2804
2805 /* if present, parse the old attribute */
2806
2807 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002808 if (!nla)
2809 return 0;
2810
2811 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2812 nla, sta_flags_policy))
2813 return -EINVAL;
2814
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002815 /*
2816 * Only allow certain flags for interface types so that
2817 * other attributes are silently ignored. Remember that
2818 * this is backward compatibility code with old userspace
2819 * and shouldn't be hit in other cases anyway.
2820 */
2821 switch (iftype) {
2822 case NL80211_IFTYPE_AP:
2823 case NL80211_IFTYPE_AP_VLAN:
2824 case NL80211_IFTYPE_P2P_GO:
2825 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2826 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2827 BIT(NL80211_STA_FLAG_WME) |
2828 BIT(NL80211_STA_FLAG_MFP);
2829 break;
2830 case NL80211_IFTYPE_P2P_CLIENT:
2831 case NL80211_IFTYPE_STATION:
2832 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2833 BIT(NL80211_STA_FLAG_TDLS_PEER);
2834 break;
2835 case NL80211_IFTYPE_MESH_POINT:
2836 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2837 BIT(NL80211_STA_FLAG_MFP) |
2838 BIT(NL80211_STA_FLAG_AUTHORIZED);
2839 default:
2840 return -EINVAL;
2841 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002842
Johannes Berg3383b5a2012-05-10 20:14:43 +02002843 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2844 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002845 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002846
Johannes Berg3383b5a2012-05-10 20:14:43 +02002847 /* no longer support new API additions in old API */
2848 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2849 return -EINVAL;
2850 }
2851 }
2852
Johannes Berg5727ef12007-12-19 02:03:34 +01002853 return 0;
2854}
2855
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002856static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2857 int attr)
2858{
2859 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002860 u32 bitrate;
2861 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002862
2863 rate = nla_nest_start(msg, attr);
2864 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002865 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002866
2867 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2868 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002869 /* report 16-bit bitrate only if we can */
2870 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002871 if (bitrate > 0 &&
2872 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
2873 return false;
2874 if (bitrate_compat > 0 &&
2875 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
2876 return false;
2877
2878 if (info->flags & RATE_INFO_FLAGS_MCS) {
2879 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
2880 return false;
2881 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2882 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2883 return false;
2884 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2885 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2886 return false;
2887 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
2888 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
2889 return false;
2890 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
2891 return false;
2892 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2893 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2894 return false;
2895 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
2896 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
2897 return false;
2898 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
2899 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
2900 return false;
2901 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
2902 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
2903 return false;
2904 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2905 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2906 return false;
2907 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002908
2909 nla_nest_end(msg, rate);
2910 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002911}
2912
Eric W. Biederman15e47302012-09-07 20:12:54 +00002913static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002914 int flags,
2915 struct cfg80211_registered_device *rdev,
2916 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002917 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002918{
2919 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002920 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002921
Eric W. Biederman15e47302012-09-07 20:12:54 +00002922 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002923 if (!hdr)
2924 return -1;
2925
David S. Miller9360ffd2012-03-29 04:41:26 -04002926 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2927 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2928 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2929 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002930
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002931 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2932 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002933 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002934 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2935 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2936 sinfo->connected_time))
2937 goto nla_put_failure;
2938 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2939 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2940 sinfo->inactive_time))
2941 goto nla_put_failure;
2942 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2943 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2944 sinfo->rx_bytes))
2945 goto nla_put_failure;
2946 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2947 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2948 sinfo->tx_bytes))
2949 goto nla_put_failure;
2950 if ((sinfo->filled & STATION_INFO_LLID) &&
2951 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2952 goto nla_put_failure;
2953 if ((sinfo->filled & STATION_INFO_PLID) &&
2954 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2955 goto nla_put_failure;
2956 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2957 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2958 sinfo->plink_state))
2959 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002960 switch (rdev->wiphy.signal_type) {
2961 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002962 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2963 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2964 sinfo->signal))
2965 goto nla_put_failure;
2966 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2967 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2968 sinfo->signal_avg))
2969 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002970 break;
2971 default:
2972 break;
2973 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002974 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002975 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2976 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002977 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002978 }
2979 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2980 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2981 NL80211_STA_INFO_RX_BITRATE))
2982 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002983 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002984 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2985 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2986 sinfo->rx_packets))
2987 goto nla_put_failure;
2988 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2989 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2990 sinfo->tx_packets))
2991 goto nla_put_failure;
2992 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2993 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2994 sinfo->tx_retries))
2995 goto nla_put_failure;
2996 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2997 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2998 sinfo->tx_failed))
2999 goto nla_put_failure;
3000 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3001 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3002 sinfo->beacon_loss_count))
3003 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003004 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3005 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3006 if (!bss_param)
3007 goto nla_put_failure;
3008
David S. Miller9360ffd2012-03-29 04:41:26 -04003009 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3010 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3011 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3012 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3013 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3014 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3015 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3016 sinfo->bss_param.dtim_period) ||
3017 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3018 sinfo->bss_param.beacon_interval))
3019 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003020
3021 nla_nest_end(msg, bss_param);
3022 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003023 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3024 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3025 sizeof(struct nl80211_sta_flag_update),
3026 &sinfo->sta_flags))
3027 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003028 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3029 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3030 sinfo->t_offset))
3031 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003032 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003033
David S. Miller9360ffd2012-03-29 04:41:26 -04003034 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3035 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3036 sinfo->assoc_req_ies))
3037 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003038
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003039 return genlmsg_end(msg, hdr);
3040
3041 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003042 genlmsg_cancel(msg, hdr);
3043 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003044}
3045
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003046static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003047 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003048{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003049 struct station_info sinfo;
3050 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003051 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003052 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003053 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003054 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003055
Johannes Berg67748892010-10-04 21:14:06 +02003056 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3057 if (err)
3058 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003059
3060 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003061 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003062 goto out_err;
3063 }
3064
Johannes Bergbba95fe2008-07-29 13:22:51 +02003065 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003066 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003067 err = rdev_dump_station(dev, netdev, sta_idx,
3068 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003069 if (err == -ENOENT)
3070 break;
3071 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003072 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003073
3074 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003075 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003076 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003077 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003078 &sinfo) < 0)
3079 goto out;
3080
3081 sta_idx++;
3082 }
3083
3084
3085 out:
3086 cb->args[1] = sta_idx;
3087 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003088 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003089 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003090
3091 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003092}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003093
Johannes Berg5727ef12007-12-19 02:03:34 +01003094static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3095{
Johannes Berg4c476992010-10-04 21:36:35 +02003096 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3097 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003098 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003099 struct sk_buff *msg;
3100 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003101 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003102
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003103 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003104
3105 if (!info->attrs[NL80211_ATTR_MAC])
3106 return -EINVAL;
3107
3108 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3109
Johannes Berg4c476992010-10-04 21:36:35 +02003110 if (!rdev->ops->get_station)
3111 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003112
Hila Gonene35e4d22012-06-27 17:19:42 +03003113 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003114 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003115 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003116
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003117 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003118 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003119 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003120
Eric W. Biederman15e47302012-09-07 20:12:54 +00003121 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003122 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003123 nlmsg_free(msg);
3124 return -ENOBUFS;
3125 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003126
Johannes Berg4c476992010-10-04 21:36:35 +02003127 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003128}
3129
3130/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003131 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003132 */
Johannes Berg80b99892011-11-18 16:23:01 +01003133static struct net_device *get_vlan(struct genl_info *info,
3134 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003135{
Johannes Berg463d0182009-07-14 00:33:35 +02003136 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003137 struct net_device *v;
3138 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003139
Johannes Berg80b99892011-11-18 16:23:01 +01003140 if (!vlanattr)
3141 return NULL;
3142
3143 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3144 if (!v)
3145 return ERR_PTR(-ENODEV);
3146
3147 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3148 ret = -EINVAL;
3149 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003150 }
Johannes Berg80b99892011-11-18 16:23:01 +01003151
3152 if (!netif_running(v)) {
3153 ret = -ENETDOWN;
3154 goto error;
3155 }
3156
3157 return v;
3158 error:
3159 dev_put(v);
3160 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003161}
3162
3163static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3164{
Johannes Berg4c476992010-10-04 21:36:35 +02003165 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003166 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003167 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003168 struct station_parameters params;
3169 u8 *mac_addr = NULL;
3170
3171 memset(&params, 0, sizeof(params));
3172
3173 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003174 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003175
3176 if (info->attrs[NL80211_ATTR_STA_AID])
3177 return -EINVAL;
3178
3179 if (!info->attrs[NL80211_ATTR_MAC])
3180 return -EINVAL;
3181
3182 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3183
3184 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3185 params.supported_rates =
3186 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3187 params.supported_rates_len =
3188 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3189 }
3190
Johannes Bergba23d202012-12-27 17:32:09 +01003191 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL] ||
3192 info->attrs[NL80211_ATTR_HT_CAPABILITY])
3193 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003194
Johannes Bergbdd90d52011-12-14 12:20:27 +01003195 if (!rdev->ops->change_station)
3196 return -EOPNOTSUPP;
3197
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003198 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003199 return -EINVAL;
3200
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003201 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3202 params.plink_action =
3203 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3204
Javier Cardona9c3990a2011-05-03 16:57:11 -07003205 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3206 params.plink_state =
3207 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3208
Johannes Berga97f4422009-06-18 17:23:43 +02003209 switch (dev->ieee80211_ptr->iftype) {
3210 case NL80211_IFTYPE_AP:
3211 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003212 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003213 /* disallow mesh-specific things */
3214 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003215 return -EINVAL;
3216
3217 /* TDLS can't be set, ... */
3218 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3219 return -EINVAL;
3220 /*
3221 * ... but don't bother the driver with it. This works around
3222 * a hostapd/wpa_supplicant issue -- it always includes the
3223 * TLDS_PEER flag in the mask even for AP mode.
3224 */
3225 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3226
3227 /* accept only the listed bits */
3228 if (params.sta_flags_mask &
3229 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003230 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3231 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003232 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3233 BIT(NL80211_STA_FLAG_WME) |
3234 BIT(NL80211_STA_FLAG_MFP)))
3235 return -EINVAL;
3236
Johannes Bergd582cff2012-10-26 17:53:44 +02003237 /* but authenticated/associated only if driver handles it */
3238 if (!(rdev->wiphy.features &
3239 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3240 params.sta_flags_mask &
3241 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3242 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3243 return -EINVAL;
3244
Johannes Bergba23d202012-12-27 17:32:09 +01003245 /* reject other things that can't change */
3246 if (params.supported_rates)
3247 return -EINVAL;
3248
Johannes Bergbdd90d52011-12-14 12:20:27 +01003249 /* must be last in here for error handling */
3250 params.vlan = get_vlan(info, rdev);
3251 if (IS_ERR(params.vlan))
3252 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003253 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003254 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003255 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003256 /*
3257 * Don't allow userspace to change the TDLS_PEER flag,
3258 * but silently ignore attempts to change it since we
3259 * don't have state here to verify that it doesn't try
3260 * to change the flag.
3261 */
3262 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003263 /* fall through */
3264 case NL80211_IFTYPE_ADHOC:
3265 /* disallow things sta doesn't support */
3266 if (params.plink_action)
3267 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003268 /* reject any changes other than AUTHORIZED */
3269 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3270 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003271 break;
3272 case NL80211_IFTYPE_MESH_POINT:
3273 /* disallow things mesh doesn't support */
3274 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003275 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003276 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003277 return -EINVAL;
3278 /*
3279 * No special handling for TDLS here -- the userspace
3280 * mesh code doesn't have this bug.
3281 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003282 if (params.sta_flags_mask &
3283 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003284 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003285 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003286 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003287 break;
3288 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003289 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003290 }
3291
Johannes Bergbdd90d52011-12-14 12:20:27 +01003292 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003293
Hila Gonene35e4d22012-06-27 17:19:42 +03003294 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003295
Johannes Berg5727ef12007-12-19 02:03:34 +01003296 if (params.vlan)
3297 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003298
Johannes Berg5727ef12007-12-19 02:03:34 +01003299 return err;
3300}
3301
Eliad Pellerc75786c2011-08-23 14:37:46 +03003302static struct nla_policy
3303nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3304 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3305 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3306};
3307
Johannes Berg5727ef12007-12-19 02:03:34 +01003308static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3309{
Johannes Berg4c476992010-10-04 21:36:35 +02003310 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003311 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003312 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003313 struct station_parameters params;
3314 u8 *mac_addr = NULL;
3315
3316 memset(&params, 0, sizeof(params));
3317
3318 if (!info->attrs[NL80211_ATTR_MAC])
3319 return -EINVAL;
3320
Johannes Berg5727ef12007-12-19 02:03:34 +01003321 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3322 return -EINVAL;
3323
3324 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3325 return -EINVAL;
3326
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003327 if (!info->attrs[NL80211_ATTR_STA_AID])
3328 return -EINVAL;
3329
Johannes Berg5727ef12007-12-19 02:03:34 +01003330 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3331 params.supported_rates =
3332 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3333 params.supported_rates_len =
3334 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3335 params.listen_interval =
3336 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003337
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003338 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3339 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3340 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003341
Jouni Malinen36aedc92008-08-25 11:58:58 +03003342 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3343 params.ht_capa =
3344 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003345
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003346 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3347 params.vht_capa =
3348 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3349
Javier Cardona96b78df2011-04-07 15:08:33 -07003350 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3351 params.plink_action =
3352 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3353
Johannes Bergbdd90d52011-12-14 12:20:27 +01003354 if (!rdev->ops->add_station)
3355 return -EOPNOTSUPP;
3356
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003357 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003358 return -EINVAL;
3359
Johannes Bergbdd90d52011-12-14 12:20:27 +01003360 switch (dev->ieee80211_ptr->iftype) {
3361 case NL80211_IFTYPE_AP:
3362 case NL80211_IFTYPE_AP_VLAN:
3363 case NL80211_IFTYPE_P2P_GO:
3364 /* parse WME attributes if sta is WME capable */
3365 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3366 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3367 info->attrs[NL80211_ATTR_STA_WME]) {
3368 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3369 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003370
Johannes Bergbdd90d52011-12-14 12:20:27 +01003371 nla = info->attrs[NL80211_ATTR_STA_WME];
3372 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3373 nl80211_sta_wme_policy);
3374 if (err)
3375 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003376
Johannes Bergbdd90d52011-12-14 12:20:27 +01003377 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3378 params.uapsd_queues =
3379 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3380 if (params.uapsd_queues &
3381 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3382 return -EINVAL;
3383
3384 if (tb[NL80211_STA_WME_MAX_SP])
3385 params.max_sp =
3386 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3387
3388 if (params.max_sp &
3389 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3390 return -EINVAL;
3391
3392 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3393 }
3394 /* TDLS peers cannot be added */
3395 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003396 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003397 /* but don't bother the driver with it */
3398 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003399
Johannes Bergd582cff2012-10-26 17:53:44 +02003400 /* allow authenticated/associated only if driver handles it */
3401 if (!(rdev->wiphy.features &
3402 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3403 params.sta_flags_mask &
3404 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3405 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3406 return -EINVAL;
3407
Johannes Bergbdd90d52011-12-14 12:20:27 +01003408 /* must be last in here for error handling */
3409 params.vlan = get_vlan(info, rdev);
3410 if (IS_ERR(params.vlan))
3411 return PTR_ERR(params.vlan);
3412 break;
3413 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergd582cff2012-10-26 17:53:44 +02003414 /* associated is disallowed */
3415 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3416 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003417 /* TDLS peers cannot be added */
3418 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003419 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003420 break;
3421 case NL80211_IFTYPE_STATION:
Johannes Bergd582cff2012-10-26 17:53:44 +02003422 /* associated is disallowed */
3423 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3424 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003425 /* Only TDLS peers can be added */
3426 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3427 return -EINVAL;
3428 /* Can only add if TDLS ... */
3429 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3430 return -EOPNOTSUPP;
3431 /* ... with external setup is supported */
3432 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3433 return -EOPNOTSUPP;
3434 break;
3435 default:
3436 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003437 }
3438
Johannes Bergbdd90d52011-12-14 12:20:27 +01003439 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003440
Hila Gonene35e4d22012-06-27 17:19:42 +03003441 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003442
Johannes Berg5727ef12007-12-19 02:03:34 +01003443 if (params.vlan)
3444 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003445 return err;
3446}
3447
3448static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3449{
Johannes Berg4c476992010-10-04 21:36:35 +02003450 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3451 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003452 u8 *mac_addr = NULL;
3453
3454 if (info->attrs[NL80211_ATTR_MAC])
3455 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3456
Johannes Berge80cf852009-05-11 14:43:13 +02003457 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003458 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003459 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003460 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3461 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003462
Johannes Berg4c476992010-10-04 21:36:35 +02003463 if (!rdev->ops->del_station)
3464 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003465
Hila Gonene35e4d22012-06-27 17:19:42 +03003466 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003467}
3468
Eric W. Biederman15e47302012-09-07 20:12:54 +00003469static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003470 int flags, struct net_device *dev,
3471 u8 *dst, u8 *next_hop,
3472 struct mpath_info *pinfo)
3473{
3474 void *hdr;
3475 struct nlattr *pinfoattr;
3476
Eric W. Biederman15e47302012-09-07 20:12:54 +00003477 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003478 if (!hdr)
3479 return -1;
3480
David S. Miller9360ffd2012-03-29 04:41:26 -04003481 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3482 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3483 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3484 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3485 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003486
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003487 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3488 if (!pinfoattr)
3489 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003490 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3491 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3492 pinfo->frame_qlen))
3493 goto nla_put_failure;
3494 if (((pinfo->filled & MPATH_INFO_SN) &&
3495 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3496 ((pinfo->filled & MPATH_INFO_METRIC) &&
3497 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3498 pinfo->metric)) ||
3499 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3500 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3501 pinfo->exptime)) ||
3502 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3503 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3504 pinfo->flags)) ||
3505 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3506 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3507 pinfo->discovery_timeout)) ||
3508 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3509 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3510 pinfo->discovery_retries)))
3511 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003512
3513 nla_nest_end(msg, pinfoattr);
3514
3515 return genlmsg_end(msg, hdr);
3516
3517 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003518 genlmsg_cancel(msg, hdr);
3519 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003520}
3521
3522static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003523 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003524{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003525 struct mpath_info pinfo;
3526 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003527 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003528 u8 dst[ETH_ALEN];
3529 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003530 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003531 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003532
Johannes Berg67748892010-10-04 21:14:06 +02003533 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3534 if (err)
3535 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003536
3537 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003538 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003539 goto out_err;
3540 }
3541
Jouni Malineneec60b02009-03-20 21:21:19 +02003542 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3543 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003544 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003545 }
3546
Johannes Bergbba95fe2008-07-29 13:22:51 +02003547 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003548 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3549 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003550 if (err == -ENOENT)
3551 break;
3552 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003553 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003554
Eric W. Biederman15e47302012-09-07 20:12:54 +00003555 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003556 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3557 netdev, dst, next_hop,
3558 &pinfo) < 0)
3559 goto out;
3560
3561 path_idx++;
3562 }
3563
3564
3565 out:
3566 cb->args[1] = path_idx;
3567 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003568 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003569 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003570 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003571}
3572
3573static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3574{
Johannes Berg4c476992010-10-04 21:36:35 +02003575 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003576 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003577 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003578 struct mpath_info pinfo;
3579 struct sk_buff *msg;
3580 u8 *dst = NULL;
3581 u8 next_hop[ETH_ALEN];
3582
3583 memset(&pinfo, 0, sizeof(pinfo));
3584
3585 if (!info->attrs[NL80211_ATTR_MAC])
3586 return -EINVAL;
3587
3588 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3589
Johannes Berg4c476992010-10-04 21:36:35 +02003590 if (!rdev->ops->get_mpath)
3591 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003592
Johannes Berg4c476992010-10-04 21:36:35 +02003593 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3594 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003595
Hila Gonene35e4d22012-06-27 17:19:42 +03003596 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003597 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003598 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003599
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003600 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003601 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003602 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003603
Eric W. Biederman15e47302012-09-07 20:12:54 +00003604 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003605 dev, dst, next_hop, &pinfo) < 0) {
3606 nlmsg_free(msg);
3607 return -ENOBUFS;
3608 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003609
Johannes Berg4c476992010-10-04 21:36:35 +02003610 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003611}
3612
3613static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3614{
Johannes Berg4c476992010-10-04 21:36:35 +02003615 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3616 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003617 u8 *dst = NULL;
3618 u8 *next_hop = NULL;
3619
3620 if (!info->attrs[NL80211_ATTR_MAC])
3621 return -EINVAL;
3622
3623 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3624 return -EINVAL;
3625
3626 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3627 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3628
Johannes Berg4c476992010-10-04 21:36:35 +02003629 if (!rdev->ops->change_mpath)
3630 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003631
Johannes Berg4c476992010-10-04 21:36:35 +02003632 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3633 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003634
Hila Gonene35e4d22012-06-27 17:19:42 +03003635 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003636}
Johannes Berg4c476992010-10-04 21:36:35 +02003637
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003638static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3639{
Johannes Berg4c476992010-10-04 21:36:35 +02003640 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3641 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003642 u8 *dst = NULL;
3643 u8 *next_hop = NULL;
3644
3645 if (!info->attrs[NL80211_ATTR_MAC])
3646 return -EINVAL;
3647
3648 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3649 return -EINVAL;
3650
3651 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3652 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3653
Johannes Berg4c476992010-10-04 21:36:35 +02003654 if (!rdev->ops->add_mpath)
3655 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003656
Johannes Berg4c476992010-10-04 21:36:35 +02003657 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3658 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003659
Hila Gonene35e4d22012-06-27 17:19:42 +03003660 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003661}
3662
3663static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3664{
Johannes Berg4c476992010-10-04 21:36:35 +02003665 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3666 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003667 u8 *dst = NULL;
3668
3669 if (info->attrs[NL80211_ATTR_MAC])
3670 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3671
Johannes Berg4c476992010-10-04 21:36:35 +02003672 if (!rdev->ops->del_mpath)
3673 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003674
Hila Gonene35e4d22012-06-27 17:19:42 +03003675 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003676}
3677
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003678static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3679{
Johannes Berg4c476992010-10-04 21:36:35 +02003680 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3681 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003682 struct bss_parameters params;
3683
3684 memset(&params, 0, sizeof(params));
3685 /* default to not changing parameters */
3686 params.use_cts_prot = -1;
3687 params.use_short_preamble = -1;
3688 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003689 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003690 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01003691 params.p2p_ctwindow = -1;
3692 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003693
3694 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3695 params.use_cts_prot =
3696 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3697 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3698 params.use_short_preamble =
3699 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3700 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3701 params.use_short_slot_time =
3702 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003703 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3704 params.basic_rates =
3705 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3706 params.basic_rates_len =
3707 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3708 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003709 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3710 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003711 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3712 params.ht_opmode =
3713 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003714
Johannes Berg53cabad2012-11-14 15:17:28 +01003715 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3716 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3717 return -EINVAL;
3718 params.p2p_ctwindow =
3719 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3720 if (params.p2p_ctwindow < 0)
3721 return -EINVAL;
3722 if (params.p2p_ctwindow != 0 &&
3723 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3724 return -EINVAL;
3725 }
3726
3727 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3728 u8 tmp;
3729
3730 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3731 return -EINVAL;
3732 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3733 if (tmp > 1)
3734 return -EINVAL;
3735 params.p2p_opp_ps = tmp;
3736 if (params.p2p_opp_ps &&
3737 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3738 return -EINVAL;
3739 }
3740
Johannes Berg4c476992010-10-04 21:36:35 +02003741 if (!rdev->ops->change_bss)
3742 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003743
Johannes Berg074ac8d2010-09-16 14:58:22 +02003744 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003745 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3746 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003747
Hila Gonene35e4d22012-06-27 17:19:42 +03003748 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003749}
3750
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003751static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003752 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3753 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3754 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3755 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3756 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3757 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3758};
3759
3760static int parse_reg_rule(struct nlattr *tb[],
3761 struct ieee80211_reg_rule *reg_rule)
3762{
3763 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3764 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3765
3766 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3767 return -EINVAL;
3768 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3769 return -EINVAL;
3770 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3771 return -EINVAL;
3772 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3773 return -EINVAL;
3774 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3775 return -EINVAL;
3776
3777 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3778
3779 freq_range->start_freq_khz =
3780 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3781 freq_range->end_freq_khz =
3782 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3783 freq_range->max_bandwidth_khz =
3784 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3785
3786 power_rule->max_eirp =
3787 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3788
3789 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3790 power_rule->max_antenna_gain =
3791 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3792
3793 return 0;
3794}
3795
3796static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3797{
3798 int r;
3799 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003800 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003801
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003802 /*
3803 * You should only get this when cfg80211 hasn't yet initialized
3804 * completely when built-in to the kernel right between the time
3805 * window between nl80211_init() and regulatory_init(), if that is
3806 * even possible.
3807 */
Johannes Berg458f4f92012-12-06 15:47:38 +01003808 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003809 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003810
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003811 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3812 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003813
3814 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3815
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003816 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3817 user_reg_hint_type =
3818 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3819 else
3820 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3821
3822 switch (user_reg_hint_type) {
3823 case NL80211_USER_REG_HINT_USER:
3824 case NL80211_USER_REG_HINT_CELL_BASE:
3825 break;
3826 default:
3827 return -EINVAL;
3828 }
3829
3830 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003831
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003832 return r;
3833}
3834
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003835static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003836 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003837{
Johannes Berg4c476992010-10-04 21:36:35 +02003838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003839 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003840 struct wireless_dev *wdev = dev->ieee80211_ptr;
3841 struct mesh_config cur_params;
3842 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003843 void *hdr;
3844 struct nlattr *pinfoattr;
3845 struct sk_buff *msg;
3846
Johannes Berg29cbe682010-12-03 09:20:44 +01003847 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3848 return -EOPNOTSUPP;
3849
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003850 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003851 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003852
Johannes Berg29cbe682010-12-03 09:20:44 +01003853 wdev_lock(wdev);
3854 /* If not connected, get default parameters */
3855 if (!wdev->mesh_id_len)
3856 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3857 else
Hila Gonene35e4d22012-06-27 17:19:42 +03003858 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01003859 wdev_unlock(wdev);
3860
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003861 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003862 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003863
3864 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003865 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003866 if (!msg)
3867 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003868 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003869 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003870 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003871 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003872 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003873 if (!pinfoattr)
3874 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003875 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3876 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3877 cur_params.dot11MeshRetryTimeout) ||
3878 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3879 cur_params.dot11MeshConfirmTimeout) ||
3880 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3881 cur_params.dot11MeshHoldingTimeout) ||
3882 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3883 cur_params.dot11MeshMaxPeerLinks) ||
3884 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3885 cur_params.dot11MeshMaxRetries) ||
3886 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3887 cur_params.dot11MeshTTL) ||
3888 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3889 cur_params.element_ttl) ||
3890 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3891 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003892 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3893 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003894 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3895 cur_params.dot11MeshHWMPmaxPREQretries) ||
3896 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3897 cur_params.path_refresh_time) ||
3898 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3899 cur_params.min_discovery_timeout) ||
3900 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3901 cur_params.dot11MeshHWMPactivePathTimeout) ||
3902 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3903 cur_params.dot11MeshHWMPpreqMinInterval) ||
3904 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3905 cur_params.dot11MeshHWMPperrMinInterval) ||
3906 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3907 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3908 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3909 cur_params.dot11MeshHWMPRootMode) ||
3910 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3911 cur_params.dot11MeshHWMPRannInterval) ||
3912 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3913 cur_params.dot11MeshGateAnnouncementProtocol) ||
3914 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3915 cur_params.dot11MeshForwarding) ||
3916 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003917 cur_params.rssi_threshold) ||
3918 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003919 cur_params.ht_opmode) ||
3920 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3921 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3922 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003923 cur_params.dot11MeshHWMProotInterval) ||
3924 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3925 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003926 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003927 nla_nest_end(msg, pinfoattr);
3928 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003929 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003930
Johannes Berg3b858752009-03-12 09:55:09 +01003931 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003932 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003933 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003934 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003935 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003936}
3937
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003938static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003939 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3940 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3941 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3942 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3943 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3944 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003945 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003946 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003947 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003948 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3949 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3950 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3951 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3952 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003953 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003954 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003955 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003956 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003957 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003958 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003959 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3960 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003961 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3962 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003963 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003964};
3965
Javier Cardonac80d5452010-12-16 17:37:49 -08003966static const struct nla_policy
3967 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003968 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003969 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3970 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003971 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003972 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003973 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003974 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003975};
3976
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003977static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003978 struct mesh_config *cfg,
3979 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003980{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003981 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003982 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003983
Marco Porschea54fba2013-01-07 16:04:48 +01003984#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
3985do { \
3986 if (tb[attr]) { \
3987 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
3988 return -EINVAL; \
3989 cfg->param = fn(tb[attr]); \
3990 mask |= (1 << (attr - 1)); \
3991 } \
3992} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003993
3994
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003995 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003996 return -EINVAL;
3997 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003998 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003999 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004000 return -EINVAL;
4001
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004002 /* This makes sure that there aren't more than 32 mesh config
4003 * parameters (otherwise our bitfield scheme would not work.) */
4004 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4005
4006 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004007 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004008 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4009 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004010 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004011 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4012 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004013 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004014 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4015 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004016 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004017 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4018 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004019 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004020 mask, NL80211_MESHCONF_MAX_RETRIES,
4021 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004022 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004023 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004024 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004025 mask, NL80211_MESHCONF_ELEMENT_TTL,
4026 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004027 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004028 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4029 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004030 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4031 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004032 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4033 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004034 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004035 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4036 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004037 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004038 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4039 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004040 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004041 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4042 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004043 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4044 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004045 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4046 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004047 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004048 1, 65535, mask,
4049 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004050 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004051 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004052 1, 65535, mask,
4053 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004054 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004055 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004056 dot11MeshHWMPnetDiameterTraversalTime,
4057 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004058 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4059 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004060 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4061 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4062 nla_get_u8);
4063 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4064 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004065 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004066 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004067 dot11MeshGateAnnouncementProtocol, 0, 1,
4068 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004069 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004070 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004071 mask, NL80211_MESHCONF_FORWARDING,
4072 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004073 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004074 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4075 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004076 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004077 mask, NL80211_MESHCONF_HT_OPMODE,
4078 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004079 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004080 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004081 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4082 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004083 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004084 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4085 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004086 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004087 dot11MeshHWMPconfirmationInterval,
4088 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004089 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4090 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004091 if (mask_out)
4092 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004093
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004094 return 0;
4095
4096#undef FILL_IN_MESH_PARAM_IF_SET
4097}
4098
Javier Cardonac80d5452010-12-16 17:37:49 -08004099static int nl80211_parse_mesh_setup(struct genl_info *info,
4100 struct mesh_setup *setup)
4101{
4102 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4103
4104 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4105 return -EINVAL;
4106 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4107 info->attrs[NL80211_ATTR_MESH_SETUP],
4108 nl80211_mesh_setup_params_policy))
4109 return -EINVAL;
4110
Javier Cardonad299a1f2012-03-31 11:31:33 -07004111 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4112 setup->sync_method =
4113 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4114 IEEE80211_SYNC_METHOD_VENDOR :
4115 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4116
Javier Cardonac80d5452010-12-16 17:37:49 -08004117 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4118 setup->path_sel_proto =
4119 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4120 IEEE80211_PATH_PROTOCOL_VENDOR :
4121 IEEE80211_PATH_PROTOCOL_HWMP;
4122
4123 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4124 setup->path_metric =
4125 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4126 IEEE80211_PATH_METRIC_VENDOR :
4127 IEEE80211_PATH_METRIC_AIRTIME;
4128
Javier Cardona581a8b02011-04-07 15:08:27 -07004129
4130 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004131 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004132 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004133 if (!is_valid_ie_attr(ieattr))
4134 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004135 setup->ie = nla_data(ieattr);
4136 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004137 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004138 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4139 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004140
4141 return 0;
4142}
4143
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004144static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004145 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004146{
4147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4148 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004149 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004150 struct mesh_config cfg;
4151 u32 mask;
4152 int err;
4153
Johannes Berg29cbe682010-12-03 09:20:44 +01004154 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4155 return -EOPNOTSUPP;
4156
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004157 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004158 return -EOPNOTSUPP;
4159
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004160 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004161 if (err)
4162 return err;
4163
Johannes Berg29cbe682010-12-03 09:20:44 +01004164 wdev_lock(wdev);
4165 if (!wdev->mesh_id_len)
4166 err = -ENOLINK;
4167
4168 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004169 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004170
4171 wdev_unlock(wdev);
4172
4173 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004174}
4175
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004176static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4177{
Johannes Berg458f4f92012-12-06 15:47:38 +01004178 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004179 struct sk_buff *msg;
4180 void *hdr = NULL;
4181 struct nlattr *nl_reg_rules;
4182 unsigned int i;
4183 int err = -EINVAL;
4184
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004185 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004186
4187 if (!cfg80211_regdomain)
4188 goto out;
4189
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004190 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004191 if (!msg) {
4192 err = -ENOBUFS;
4193 goto out;
4194 }
4195
Eric W. Biederman15e47302012-09-07 20:12:54 +00004196 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004197 NL80211_CMD_GET_REG);
4198 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004199 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004200
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004201 if (reg_last_request_cell_base() &&
4202 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4203 NL80211_USER_REG_HINT_CELL_BASE))
4204 goto nla_put_failure;
4205
Johannes Berg458f4f92012-12-06 15:47:38 +01004206 rcu_read_lock();
4207 regdom = rcu_dereference(cfg80211_regdomain);
4208
4209 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4210 (regdom->dfs_region &&
4211 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4212 goto nla_put_failure_rcu;
4213
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004214 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4215 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004216 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004217
Johannes Berg458f4f92012-12-06 15:47:38 +01004218 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004219 struct nlattr *nl_reg_rule;
4220 const struct ieee80211_reg_rule *reg_rule;
4221 const struct ieee80211_freq_range *freq_range;
4222 const struct ieee80211_power_rule *power_rule;
4223
Johannes Berg458f4f92012-12-06 15:47:38 +01004224 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004225 freq_range = &reg_rule->freq_range;
4226 power_rule = &reg_rule->power_rule;
4227
4228 nl_reg_rule = nla_nest_start(msg, i);
4229 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004230 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004231
David S. Miller9360ffd2012-03-29 04:41:26 -04004232 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4233 reg_rule->flags) ||
4234 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4235 freq_range->start_freq_khz) ||
4236 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4237 freq_range->end_freq_khz) ||
4238 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4239 freq_range->max_bandwidth_khz) ||
4240 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4241 power_rule->max_antenna_gain) ||
4242 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4243 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004244 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004245
4246 nla_nest_end(msg, nl_reg_rule);
4247 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004248 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004249
4250 nla_nest_end(msg, nl_reg_rules);
4251
4252 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004253 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004254 goto out;
4255
Johannes Berg458f4f92012-12-06 15:47:38 +01004256nla_put_failure_rcu:
4257 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004258nla_put_failure:
4259 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004260put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004261 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004262 err = -EMSGSIZE;
4263out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004264 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004265 return err;
4266}
4267
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004268static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4269{
4270 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4271 struct nlattr *nl_reg_rule;
4272 char *alpha2 = NULL;
4273 int rem_reg_rules = 0, r = 0;
4274 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004275 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004276 struct ieee80211_regdomain *rd = NULL;
4277
4278 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4279 return -EINVAL;
4280
4281 if (!info->attrs[NL80211_ATTR_REG_RULES])
4282 return -EINVAL;
4283
4284 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4285
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004286 if (info->attrs[NL80211_ATTR_DFS_REGION])
4287 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4288
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004289 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004290 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004291 num_rules++;
4292 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004293 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004294 }
4295
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004296 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004297 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004298
4299 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004300 if (!rd)
4301 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004302
4303 rd->n_reg_rules = num_rules;
4304 rd->alpha2[0] = alpha2[0];
4305 rd->alpha2[1] = alpha2[1];
4306
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004307 /*
4308 * Disable DFS master mode if the DFS region was
4309 * not supported or known on this kernel.
4310 */
4311 if (reg_supported_dfs_region(dfs_region))
4312 rd->dfs_region = dfs_region;
4313
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004314 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004315 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004316 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004317 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4318 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004319 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4320 if (r)
4321 goto bad_reg;
4322
4323 rule_idx++;
4324
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004325 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4326 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004327 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004328 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004329 }
4330
Johannes Berg6913b492012-12-04 00:48:59 +01004331 mutex_lock(&cfg80211_mutex);
4332
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004333 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004334 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004335 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004336 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004337
Johannes Bergd2372b32008-10-24 20:32:20 +02004338 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004339 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004340 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004341}
4342
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004343static int validate_scan_freqs(struct nlattr *freqs)
4344{
4345 struct nlattr *attr1, *attr2;
4346 int n_channels = 0, tmp1, tmp2;
4347
4348 nla_for_each_nested(attr1, freqs, tmp1) {
4349 n_channels++;
4350 /*
4351 * Some hardware has a limited channel list for
4352 * scanning, and it is pretty much nonsensical
4353 * to scan for a channel twice, so disallow that
4354 * and don't require drivers to check that the
4355 * channel list they get isn't longer than what
4356 * they can scan, as long as they can scan all
4357 * the channels they registered at once.
4358 */
4359 nla_for_each_nested(attr2, freqs, tmp2)
4360 if (attr1 != attr2 &&
4361 nla_get_u32(attr1) == nla_get_u32(attr2))
4362 return 0;
4363 }
4364
4365 return n_channels;
4366}
4367
Johannes Berg2a519312009-02-10 21:25:55 +01004368static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4369{
Johannes Berg4c476992010-10-04 21:36:35 +02004370 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004371 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004372 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004373 struct nlattr *attr;
4374 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004375 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004376 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004377
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004378 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4379 return -EINVAL;
4380
Johannes Berg79c97e92009-07-07 03:56:12 +02004381 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004382
Johannes Berg4c476992010-10-04 21:36:35 +02004383 if (!rdev->ops->scan)
4384 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004385
Johannes Berg4c476992010-10-04 21:36:35 +02004386 if (rdev->scan_req)
4387 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004388
4389 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004390 n_channels = validate_scan_freqs(
4391 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004392 if (!n_channels)
4393 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004394 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004395 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004396 n_channels = 0;
4397
Johannes Berg2a519312009-02-10 21:25:55 +01004398 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4399 if (wiphy->bands[band])
4400 n_channels += wiphy->bands[band]->n_channels;
4401 }
4402
4403 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4404 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4405 n_ssids++;
4406
Johannes Berg4c476992010-10-04 21:36:35 +02004407 if (n_ssids > wiphy->max_scan_ssids)
4408 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004409
Jouni Malinen70692ad2009-02-16 19:39:13 +02004410 if (info->attrs[NL80211_ATTR_IE])
4411 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4412 else
4413 ie_len = 0;
4414
Johannes Berg4c476992010-10-04 21:36:35 +02004415 if (ie_len > wiphy->max_scan_ie_len)
4416 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004417
Johannes Berg2a519312009-02-10 21:25:55 +01004418 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004419 + sizeof(*request->ssids) * n_ssids
4420 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004421 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004422 if (!request)
4423 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004424
Johannes Berg2a519312009-02-10 21:25:55 +01004425 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004426 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004427 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004428 if (ie_len) {
4429 if (request->ssids)
4430 request->ie = (void *)(request->ssids + n_ssids);
4431 else
4432 request->ie = (void *)(request->channels + n_channels);
4433 }
Johannes Berg2a519312009-02-10 21:25:55 +01004434
Johannes Berg584991d2009-11-02 13:32:03 +01004435 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004436 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4437 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004438 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004439 struct ieee80211_channel *chan;
4440
4441 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4442
4443 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004444 err = -EINVAL;
4445 goto out_free;
4446 }
Johannes Berg584991d2009-11-02 13:32:03 +01004447
4448 /* ignore disabled channels */
4449 if (chan->flags & IEEE80211_CHAN_DISABLED)
4450 continue;
4451
4452 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004453 i++;
4454 }
4455 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004456 enum ieee80211_band band;
4457
Johannes Berg2a519312009-02-10 21:25:55 +01004458 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004459 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4460 int j;
4461 if (!wiphy->bands[band])
4462 continue;
4463 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004464 struct ieee80211_channel *chan;
4465
4466 chan = &wiphy->bands[band]->channels[j];
4467
4468 if (chan->flags & IEEE80211_CHAN_DISABLED)
4469 continue;
4470
4471 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004472 i++;
4473 }
4474 }
4475 }
4476
Johannes Berg584991d2009-11-02 13:32:03 +01004477 if (!i) {
4478 err = -EINVAL;
4479 goto out_free;
4480 }
4481
4482 request->n_channels = i;
4483
Johannes Berg2a519312009-02-10 21:25:55 +01004484 i = 0;
4485 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4486 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004487 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004488 err = -EINVAL;
4489 goto out_free;
4490 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004491 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004492 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004493 i++;
4494 }
4495 }
4496
Jouni Malinen70692ad2009-02-16 19:39:13 +02004497 if (info->attrs[NL80211_ATTR_IE]) {
4498 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004499 memcpy((void *)request->ie,
4500 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004501 request->ie_len);
4502 }
4503
Johannes Berg34850ab2011-07-18 18:08:35 +02004504 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004505 if (wiphy->bands[i])
4506 request->rates[i] =
4507 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004508
4509 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4510 nla_for_each_nested(attr,
4511 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4512 tmp) {
4513 enum ieee80211_band band = nla_type(attr);
4514
Dan Carpenter84404622011-07-29 11:52:18 +03004515 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004516 err = -EINVAL;
4517 goto out_free;
4518 }
4519 err = ieee80211_get_ratemask(wiphy->bands[band],
4520 nla_data(attr),
4521 nla_len(attr),
4522 &request->rates[band]);
4523 if (err)
4524 goto out_free;
4525 }
4526 }
4527
Sam Leffler46856bb2012-10-11 21:03:32 -07004528 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004529 request->flags = nla_get_u32(
4530 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004531 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4532 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4533 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4534 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004535 err = -EOPNOTSUPP;
4536 goto out_free;
4537 }
4538 }
Sam Lefflered4737712012-10-11 21:03:31 -07004539
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304540 request->no_cck =
4541 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4542
Johannes Bergfd014282012-06-18 19:17:03 +02004543 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004544 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004545 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004546
Johannes Berg79c97e92009-07-07 03:56:12 +02004547 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004548 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004549
Johannes Berg463d0182009-07-14 00:33:35 +02004550 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004551 nl80211_send_scan_start(rdev, wdev);
4552 if (wdev->netdev)
4553 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004554 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004555 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004556 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004557 kfree(request);
4558 }
Johannes Berg3b858752009-03-12 09:55:09 +01004559
Johannes Berg2a519312009-02-10 21:25:55 +01004560 return err;
4561}
4562
Luciano Coelho807f8a82011-05-11 17:09:35 +03004563static int nl80211_start_sched_scan(struct sk_buff *skb,
4564 struct genl_info *info)
4565{
4566 struct cfg80211_sched_scan_request *request;
4567 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4568 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004569 struct nlattr *attr;
4570 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004571 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004572 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004573 enum ieee80211_band band;
4574 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004575 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004576
4577 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4578 !rdev->ops->sched_scan_start)
4579 return -EOPNOTSUPP;
4580
4581 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4582 return -EINVAL;
4583
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004584 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4585 return -EINVAL;
4586
4587 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4588 if (interval == 0)
4589 return -EINVAL;
4590
Luciano Coelho807f8a82011-05-11 17:09:35 +03004591 wiphy = &rdev->wiphy;
4592
4593 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4594 n_channels = validate_scan_freqs(
4595 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4596 if (!n_channels)
4597 return -EINVAL;
4598 } else {
4599 n_channels = 0;
4600
4601 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4602 if (wiphy->bands[band])
4603 n_channels += wiphy->bands[band]->n_channels;
4604 }
4605
4606 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4607 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4608 tmp)
4609 n_ssids++;
4610
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004611 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004612 return -EINVAL;
4613
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004614 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4615 nla_for_each_nested(attr,
4616 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4617 tmp)
4618 n_match_sets++;
4619
4620 if (n_match_sets > wiphy->max_match_sets)
4621 return -EINVAL;
4622
Luciano Coelho807f8a82011-05-11 17:09:35 +03004623 if (info->attrs[NL80211_ATTR_IE])
4624 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4625 else
4626 ie_len = 0;
4627
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004628 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004629 return -EINVAL;
4630
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004631 mutex_lock(&rdev->sched_scan_mtx);
4632
4633 if (rdev->sched_scan_req) {
4634 err = -EINPROGRESS;
4635 goto out;
4636 }
4637
Luciano Coelho807f8a82011-05-11 17:09:35 +03004638 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004639 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004640 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004641 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004642 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004643 if (!request) {
4644 err = -ENOMEM;
4645 goto out;
4646 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004647
4648 if (n_ssids)
4649 request->ssids = (void *)&request->channels[n_channels];
4650 request->n_ssids = n_ssids;
4651 if (ie_len) {
4652 if (request->ssids)
4653 request->ie = (void *)(request->ssids + n_ssids);
4654 else
4655 request->ie = (void *)(request->channels + n_channels);
4656 }
4657
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004658 if (n_match_sets) {
4659 if (request->ie)
4660 request->match_sets = (void *)(request->ie + ie_len);
4661 else if (request->ssids)
4662 request->match_sets =
4663 (void *)(request->ssids + n_ssids);
4664 else
4665 request->match_sets =
4666 (void *)(request->channels + n_channels);
4667 }
4668 request->n_match_sets = n_match_sets;
4669
Luciano Coelho807f8a82011-05-11 17:09:35 +03004670 i = 0;
4671 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4672 /* user specified, bail out if channel not found */
4673 nla_for_each_nested(attr,
4674 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4675 tmp) {
4676 struct ieee80211_channel *chan;
4677
4678 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4679
4680 if (!chan) {
4681 err = -EINVAL;
4682 goto out_free;
4683 }
4684
4685 /* ignore disabled channels */
4686 if (chan->flags & IEEE80211_CHAN_DISABLED)
4687 continue;
4688
4689 request->channels[i] = chan;
4690 i++;
4691 }
4692 } else {
4693 /* all channels */
4694 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4695 int j;
4696 if (!wiphy->bands[band])
4697 continue;
4698 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4699 struct ieee80211_channel *chan;
4700
4701 chan = &wiphy->bands[band]->channels[j];
4702
4703 if (chan->flags & IEEE80211_CHAN_DISABLED)
4704 continue;
4705
4706 request->channels[i] = chan;
4707 i++;
4708 }
4709 }
4710 }
4711
4712 if (!i) {
4713 err = -EINVAL;
4714 goto out_free;
4715 }
4716
4717 request->n_channels = i;
4718
4719 i = 0;
4720 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4721 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4722 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004723 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004724 err = -EINVAL;
4725 goto out_free;
4726 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004727 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004728 memcpy(request->ssids[i].ssid, nla_data(attr),
4729 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004730 i++;
4731 }
4732 }
4733
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004734 i = 0;
4735 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4736 nla_for_each_nested(attr,
4737 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4738 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004739 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004740
4741 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4742 nla_data(attr), nla_len(attr),
4743 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004744 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004745 if (ssid) {
4746 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4747 err = -EINVAL;
4748 goto out_free;
4749 }
4750 memcpy(request->match_sets[i].ssid.ssid,
4751 nla_data(ssid), nla_len(ssid));
4752 request->match_sets[i].ssid.ssid_len =
4753 nla_len(ssid);
4754 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004755 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4756 if (rssi)
4757 request->rssi_thold = nla_get_u32(rssi);
4758 else
4759 request->rssi_thold =
4760 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004761 i++;
4762 }
4763 }
4764
Luciano Coelho807f8a82011-05-11 17:09:35 +03004765 if (info->attrs[NL80211_ATTR_IE]) {
4766 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4767 memcpy((void *)request->ie,
4768 nla_data(info->attrs[NL80211_ATTR_IE]),
4769 request->ie_len);
4770 }
4771
Sam Leffler46856bb2012-10-11 21:03:32 -07004772 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004773 request->flags = nla_get_u32(
4774 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004775 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4776 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4777 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4778 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004779 err = -EOPNOTSUPP;
4780 goto out_free;
4781 }
4782 }
Sam Lefflered4737712012-10-11 21:03:31 -07004783
Luciano Coelho807f8a82011-05-11 17:09:35 +03004784 request->dev = dev;
4785 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004786 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004787 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004788
Hila Gonene35e4d22012-06-27 17:19:42 +03004789 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004790 if (!err) {
4791 rdev->sched_scan_req = request;
4792 nl80211_send_sched_scan(rdev, dev,
4793 NL80211_CMD_START_SCHED_SCAN);
4794 goto out;
4795 }
4796
4797out_free:
4798 kfree(request);
4799out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004800 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004801 return err;
4802}
4803
4804static int nl80211_stop_sched_scan(struct sk_buff *skb,
4805 struct genl_info *info)
4806{
4807 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004808 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004809
4810 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4811 !rdev->ops->sched_scan_stop)
4812 return -EOPNOTSUPP;
4813
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004814 mutex_lock(&rdev->sched_scan_mtx);
4815 err = __cfg80211_stop_sched_scan(rdev, false);
4816 mutex_unlock(&rdev->sched_scan_mtx);
4817
4818 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004819}
4820
Johannes Berg9720bb32011-06-21 09:45:33 +02004821static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4822 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004823 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004824 struct wireless_dev *wdev,
4825 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004826{
Johannes Berg48ab9052009-07-10 18:42:31 +02004827 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01004828 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01004829 void *hdr;
4830 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004831
4832 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004833
Eric W. Biederman15e47302012-09-07 20:12:54 +00004834 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004835 NL80211_CMD_NEW_SCAN_RESULTS);
4836 if (!hdr)
4837 return -1;
4838
Johannes Berg9720bb32011-06-21 09:45:33 +02004839 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4840
David S. Miller9360ffd2012-03-29 04:41:26 -04004841 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4842 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4843 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004844
4845 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4846 if (!bss)
4847 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004848 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01004849 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04004850 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01004851
4852 rcu_read_lock();
4853 ies = rcu_dereference(res->ies);
4854 if (ies && ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4855 ies->len, ies->data)) {
4856 rcu_read_unlock();
4857 goto nla_put_failure;
4858 }
4859 ies = rcu_dereference(res->beacon_ies);
4860 if (ies && ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
4861 ies->len, ies->data)) {
4862 rcu_read_unlock();
4863 goto nla_put_failure;
4864 }
4865 rcu_read_unlock();
4866
David S. Miller9360ffd2012-03-29 04:41:26 -04004867 if (res->tsf &&
4868 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4869 goto nla_put_failure;
4870 if (res->beacon_interval &&
4871 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4872 goto nla_put_failure;
4873 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4874 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4875 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4876 jiffies_to_msecs(jiffies - intbss->ts)))
4877 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004878
Johannes Berg77965c92009-02-18 18:45:06 +01004879 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004880 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004881 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4882 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004883 break;
4884 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004885 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4886 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004887 break;
4888 default:
4889 break;
4890 }
4891
Johannes Berg48ab9052009-07-10 18:42:31 +02004892 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004893 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004894 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004895 if (intbss == wdev->current_bss &&
4896 nla_put_u32(msg, NL80211_BSS_STATUS,
4897 NL80211_BSS_STATUS_ASSOCIATED))
4898 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004899 break;
4900 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004901 if (intbss == wdev->current_bss &&
4902 nla_put_u32(msg, NL80211_BSS_STATUS,
4903 NL80211_BSS_STATUS_IBSS_JOINED))
4904 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004905 break;
4906 default:
4907 break;
4908 }
4909
Johannes Berg2a519312009-02-10 21:25:55 +01004910 nla_nest_end(msg, bss);
4911
4912 return genlmsg_end(msg, hdr);
4913
4914 nla_put_failure:
4915 genlmsg_cancel(msg, hdr);
4916 return -EMSGSIZE;
4917}
4918
4919static int nl80211_dump_scan(struct sk_buff *skb,
4920 struct netlink_callback *cb)
4921{
Johannes Berg48ab9052009-07-10 18:42:31 +02004922 struct cfg80211_registered_device *rdev;
4923 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004924 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004925 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004926 int start = cb->args[1], idx = 0;
4927 int err;
4928
Johannes Berg67748892010-10-04 21:14:06 +02004929 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4930 if (err)
4931 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004932
Johannes Berg48ab9052009-07-10 18:42:31 +02004933 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004934
Johannes Berg48ab9052009-07-10 18:42:31 +02004935 wdev_lock(wdev);
4936 spin_lock_bh(&rdev->bss_lock);
4937 cfg80211_bss_expire(rdev);
4938
Johannes Berg9720bb32011-06-21 09:45:33 +02004939 cb->seq = rdev->bss_generation;
4940
Johannes Berg48ab9052009-07-10 18:42:31 +02004941 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004942 if (++idx <= start)
4943 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004944 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004945 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004946 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004947 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004948 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004949 }
4950 }
4951
Johannes Berg48ab9052009-07-10 18:42:31 +02004952 spin_unlock_bh(&rdev->bss_lock);
4953 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004954
4955 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004956 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004957
Johannes Berg67748892010-10-04 21:14:06 +02004958 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004959}
4960
Eric W. Biederman15e47302012-09-07 20:12:54 +00004961static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004962 int flags, struct net_device *dev,
4963 struct survey_info *survey)
4964{
4965 void *hdr;
4966 struct nlattr *infoattr;
4967
Eric W. Biederman15e47302012-09-07 20:12:54 +00004968 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004969 NL80211_CMD_NEW_SURVEY_RESULTS);
4970 if (!hdr)
4971 return -ENOMEM;
4972
David S. Miller9360ffd2012-03-29 04:41:26 -04004973 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4974 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004975
4976 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4977 if (!infoattr)
4978 goto nla_put_failure;
4979
David S. Miller9360ffd2012-03-29 04:41:26 -04004980 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4981 survey->channel->center_freq))
4982 goto nla_put_failure;
4983
4984 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4985 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4986 goto nla_put_failure;
4987 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4988 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4989 goto nla_put_failure;
4990 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4991 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4992 survey->channel_time))
4993 goto nla_put_failure;
4994 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4995 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4996 survey->channel_time_busy))
4997 goto nla_put_failure;
4998 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4999 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5000 survey->channel_time_ext_busy))
5001 goto nla_put_failure;
5002 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5003 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5004 survey->channel_time_rx))
5005 goto nla_put_failure;
5006 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5007 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5008 survey->channel_time_tx))
5009 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005010
5011 nla_nest_end(msg, infoattr);
5012
5013 return genlmsg_end(msg, hdr);
5014
5015 nla_put_failure:
5016 genlmsg_cancel(msg, hdr);
5017 return -EMSGSIZE;
5018}
5019
5020static int nl80211_dump_survey(struct sk_buff *skb,
5021 struct netlink_callback *cb)
5022{
5023 struct survey_info survey;
5024 struct cfg80211_registered_device *dev;
5025 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005026 int survey_idx = cb->args[1];
5027 int res;
5028
Johannes Berg67748892010-10-04 21:14:06 +02005029 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5030 if (res)
5031 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005032
5033 if (!dev->ops->dump_survey) {
5034 res = -EOPNOTSUPP;
5035 goto out_err;
5036 }
5037
5038 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005039 struct ieee80211_channel *chan;
5040
Hila Gonene35e4d22012-06-27 17:19:42 +03005041 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005042 if (res == -ENOENT)
5043 break;
5044 if (res)
5045 goto out_err;
5046
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005047 /* Survey without a channel doesn't make sense */
5048 if (!survey.channel) {
5049 res = -EINVAL;
5050 goto out;
5051 }
5052
5053 chan = ieee80211_get_channel(&dev->wiphy,
5054 survey.channel->center_freq);
5055 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5056 survey_idx++;
5057 continue;
5058 }
5059
Holger Schurig61fa7132009-11-11 12:25:40 +01005060 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005061 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005062 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5063 netdev,
5064 &survey) < 0)
5065 goto out;
5066 survey_idx++;
5067 }
5068
5069 out:
5070 cb->args[1] = survey_idx;
5071 res = skb->len;
5072 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005073 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005074 return res;
5075}
5076
Samuel Ortizb23aa672009-07-01 21:26:54 +02005077static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5078{
5079 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5080 NL80211_WPA_VERSION_2));
5081}
5082
Jouni Malinen636a5d32009-03-19 13:39:22 +02005083static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5084{
Johannes Berg4c476992010-10-04 21:36:35 +02005085 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5086 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005087 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005088 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5089 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005090 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005091 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005092 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005093
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005094 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5095 return -EINVAL;
5096
5097 if (!info->attrs[NL80211_ATTR_MAC])
5098 return -EINVAL;
5099
Jouni Malinen17780922009-03-27 20:52:47 +02005100 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5101 return -EINVAL;
5102
Johannes Berg19957bb2009-07-02 17:20:43 +02005103 if (!info->attrs[NL80211_ATTR_SSID])
5104 return -EINVAL;
5105
5106 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5107 return -EINVAL;
5108
Johannes Bergfffd0932009-07-08 14:22:54 +02005109 err = nl80211_parse_key(info, &key);
5110 if (err)
5111 return err;
5112
5113 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005114 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5115 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005116 if (!key.p.key || !key.p.key_len)
5117 return -EINVAL;
5118 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5119 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5120 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5121 key.p.key_len != WLAN_KEY_LEN_WEP104))
5122 return -EINVAL;
5123 if (key.idx > 4)
5124 return -EINVAL;
5125 } else {
5126 key.p.key_len = 0;
5127 key.p.key = NULL;
5128 }
5129
Johannes Bergafea0b72010-08-10 09:46:42 +02005130 if (key.idx >= 0) {
5131 int i;
5132 bool ok = false;
5133 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5134 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5135 ok = true;
5136 break;
5137 }
5138 }
Johannes Berg4c476992010-10-04 21:36:35 +02005139 if (!ok)
5140 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005141 }
5142
Johannes Berg4c476992010-10-04 21:36:35 +02005143 if (!rdev->ops->auth)
5144 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005145
Johannes Berg074ac8d2010-09-16 14:58:22 +02005146 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005147 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5148 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005149
Johannes Berg19957bb2009-07-02 17:20:43 +02005150 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005151 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005152 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005153 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5154 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005155
Johannes Berg19957bb2009-07-02 17:20:43 +02005156 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5157 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5158
5159 if (info->attrs[NL80211_ATTR_IE]) {
5160 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5161 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5162 }
5163
5164 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005165 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005166 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005167
Jouni Malinene39e5b52012-09-30 19:29:39 +03005168 if (auth_type == NL80211_AUTHTYPE_SAE &&
5169 !info->attrs[NL80211_ATTR_SAE_DATA])
5170 return -EINVAL;
5171
5172 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5173 if (auth_type != NL80211_AUTHTYPE_SAE)
5174 return -EINVAL;
5175 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5176 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5177 /* need to include at least Auth Transaction and Status Code */
5178 if (sae_data_len < 4)
5179 return -EINVAL;
5180 }
5181
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005182 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5183
Johannes Berg95de8172012-01-20 13:55:25 +01005184 /*
5185 * Since we no longer track auth state, ignore
5186 * requests to only change local state.
5187 */
5188 if (local_state_change)
5189 return 0;
5190
Johannes Berg4c476992010-10-04 21:36:35 +02005191 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5192 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005193 key.p.key, key.p.key_len, key.idx,
5194 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005195}
5196
Johannes Bergc0692b82010-08-27 14:26:53 +03005197static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5198 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005199 struct cfg80211_crypto_settings *settings,
5200 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005201{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005202 memset(settings, 0, sizeof(*settings));
5203
Samuel Ortizb23aa672009-07-01 21:26:54 +02005204 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5205
Johannes Bergc0692b82010-08-27 14:26:53 +03005206 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5207 u16 proto;
5208 proto = nla_get_u16(
5209 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5210 settings->control_port_ethertype = cpu_to_be16(proto);
5211 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5212 proto != ETH_P_PAE)
5213 return -EINVAL;
5214 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5215 settings->control_port_no_encrypt = true;
5216 } else
5217 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5218
Samuel Ortizb23aa672009-07-01 21:26:54 +02005219 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5220 void *data;
5221 int len, i;
5222
5223 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5224 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5225 settings->n_ciphers_pairwise = len / sizeof(u32);
5226
5227 if (len % sizeof(u32))
5228 return -EINVAL;
5229
Johannes Berg3dc27d22009-07-02 21:36:37 +02005230 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005231 return -EINVAL;
5232
5233 memcpy(settings->ciphers_pairwise, data, len);
5234
5235 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005236 if (!cfg80211_supported_cipher_suite(
5237 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005238 settings->ciphers_pairwise[i]))
5239 return -EINVAL;
5240 }
5241
5242 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5243 settings->cipher_group =
5244 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005245 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5246 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005247 return -EINVAL;
5248 }
5249
5250 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5251 settings->wpa_versions =
5252 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5253 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5254 return -EINVAL;
5255 }
5256
5257 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5258 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005259 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005260
5261 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5262 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5263 settings->n_akm_suites = len / sizeof(u32);
5264
5265 if (len % sizeof(u32))
5266 return -EINVAL;
5267
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005268 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5269 return -EINVAL;
5270
Samuel Ortizb23aa672009-07-01 21:26:54 +02005271 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005272 }
5273
5274 return 0;
5275}
5276
Jouni Malinen636a5d32009-03-19 13:39:22 +02005277static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5278{
Johannes Berg4c476992010-10-04 21:36:35 +02005279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5280 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005281 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005282 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005283 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005284 int err, ssid_len, ie_len = 0;
5285 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005286 u32 flags = 0;
5287 struct ieee80211_ht_cap *ht_capa = NULL;
5288 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005289
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005290 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5291 return -EINVAL;
5292
5293 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005294 !info->attrs[NL80211_ATTR_SSID] ||
5295 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005296 return -EINVAL;
5297
Johannes Berg4c476992010-10-04 21:36:35 +02005298 if (!rdev->ops->assoc)
5299 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005300
Johannes Berg074ac8d2010-09-16 14:58:22 +02005301 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005302 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5303 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005304
Johannes Berg19957bb2009-07-02 17:20:43 +02005305 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005306
Johannes Berg19957bb2009-07-02 17:20:43 +02005307 chan = ieee80211_get_channel(&rdev->wiphy,
5308 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005309 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5310 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005311
Johannes Berg19957bb2009-07-02 17:20:43 +02005312 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5313 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005314
5315 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005316 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5317 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005318 }
5319
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005320 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005321 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005322 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005323 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005324 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005325 else if (mfp != NL80211_MFP_NO)
5326 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005327 }
5328
Johannes Berg3e5d7642009-07-07 14:37:26 +02005329 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5330 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5331
Ben Greear7e7c8922011-11-18 11:31:59 -08005332 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5333 flags |= ASSOC_REQ_DISABLE_HT;
5334
5335 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5336 ht_capa_mask =
5337 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5338
5339 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5340 if (!ht_capa_mask)
5341 return -EINVAL;
5342 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5343 }
5344
Johannes Bergc0692b82010-08-27 14:26:53 +03005345 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005346 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005347 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5348 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005349 &crypto, flags, ht_capa,
5350 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005351
Jouni Malinen636a5d32009-03-19 13:39:22 +02005352 return err;
5353}
5354
5355static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5356{
Johannes Berg4c476992010-10-04 21:36:35 +02005357 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5358 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005359 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005360 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005361 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005362 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005363
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005364 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5365 return -EINVAL;
5366
5367 if (!info->attrs[NL80211_ATTR_MAC])
5368 return -EINVAL;
5369
5370 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5371 return -EINVAL;
5372
Johannes Berg4c476992010-10-04 21:36:35 +02005373 if (!rdev->ops->deauth)
5374 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005375
Johannes Berg074ac8d2010-09-16 14:58:22 +02005376 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005377 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5378 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005379
Johannes Berg19957bb2009-07-02 17:20:43 +02005380 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005381
Johannes Berg19957bb2009-07-02 17:20:43 +02005382 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5383 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005384 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005385 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005386 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005387
5388 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005389 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5390 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005391 }
5392
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005393 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5394
Johannes Berg4c476992010-10-04 21:36:35 +02005395 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5396 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005397}
5398
5399static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5400{
Johannes Berg4c476992010-10-04 21:36:35 +02005401 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5402 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005403 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005404 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005405 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005406 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005407
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005408 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5409 return -EINVAL;
5410
5411 if (!info->attrs[NL80211_ATTR_MAC])
5412 return -EINVAL;
5413
5414 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5415 return -EINVAL;
5416
Johannes Berg4c476992010-10-04 21:36:35 +02005417 if (!rdev->ops->disassoc)
5418 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005419
Johannes Berg074ac8d2010-09-16 14:58:22 +02005420 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005421 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5422 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005423
Johannes Berg19957bb2009-07-02 17:20:43 +02005424 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005425
Johannes Berg19957bb2009-07-02 17:20:43 +02005426 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5427 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005428 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005429 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005430 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005431
5432 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005433 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5434 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005435 }
5436
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005437 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5438
Johannes Berg4c476992010-10-04 21:36:35 +02005439 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5440 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005441}
5442
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005443static bool
5444nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5445 int mcast_rate[IEEE80211_NUM_BANDS],
5446 int rateval)
5447{
5448 struct wiphy *wiphy = &rdev->wiphy;
5449 bool found = false;
5450 int band, i;
5451
5452 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5453 struct ieee80211_supported_band *sband;
5454
5455 sband = wiphy->bands[band];
5456 if (!sband)
5457 continue;
5458
5459 for (i = 0; i < sband->n_bitrates; i++) {
5460 if (sband->bitrates[i].bitrate == rateval) {
5461 mcast_rate[band] = i + 1;
5462 found = true;
5463 break;
5464 }
5465 }
5466 }
5467
5468 return found;
5469}
5470
Johannes Berg04a773a2009-04-19 21:24:32 +02005471static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5472{
Johannes Berg4c476992010-10-04 21:36:35 +02005473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5474 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005475 struct cfg80211_ibss_params ibss;
5476 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005477 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005478 int err;
5479
Johannes Berg8e30bc52009-04-22 17:45:38 +02005480 memset(&ibss, 0, sizeof(ibss));
5481
Johannes Berg04a773a2009-04-19 21:24:32 +02005482 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5483 return -EINVAL;
5484
Johannes Berg683b6d32012-11-08 21:25:48 +01005485 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005486 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5487 return -EINVAL;
5488
Johannes Berg8e30bc52009-04-22 17:45:38 +02005489 ibss.beacon_interval = 100;
5490
5491 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5492 ibss.beacon_interval =
5493 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5494 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5495 return -EINVAL;
5496 }
5497
Johannes Berg4c476992010-10-04 21:36:35 +02005498 if (!rdev->ops->join_ibss)
5499 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005500
Johannes Berg4c476992010-10-04 21:36:35 +02005501 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5502 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005503
Johannes Berg79c97e92009-07-07 03:56:12 +02005504 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005505
Johannes Berg39193492011-09-16 13:45:25 +02005506 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005507 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005508
5509 if (!is_valid_ether_addr(ibss.bssid))
5510 return -EINVAL;
5511 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005512 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5513 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5514
5515 if (info->attrs[NL80211_ATTR_IE]) {
5516 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5517 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5518 }
5519
Johannes Berg683b6d32012-11-08 21:25:48 +01005520 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5521 if (err)
5522 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005523
Johannes Berg683b6d32012-11-08 21:25:48 +01005524 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005525 return -EINVAL;
5526
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005527 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5528 return -EINVAL;
5529 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5530 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005531 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005532
Johannes Berg04a773a2009-04-19 21:24:32 +02005533 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005534 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005535
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005536 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5537 u8 *rates =
5538 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5539 int n_rates =
5540 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5541 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005542 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005543
Johannes Berg34850ab2011-07-18 18:08:35 +02005544 err = ieee80211_get_ratemask(sband, rates, n_rates,
5545 &ibss.basic_rates);
5546 if (err)
5547 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005548 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005549
5550 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5551 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5552 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5553 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005554
Johannes Berg4c476992010-10-04 21:36:35 +02005555 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305556 bool no_ht = false;
5557
Johannes Berg4c476992010-10-04 21:36:35 +02005558 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305559 info->attrs[NL80211_ATTR_KEYS],
5560 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005561 if (IS_ERR(connkeys))
5562 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305563
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005564 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5565 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305566 kfree(connkeys);
5567 return -EINVAL;
5568 }
Johannes Berg4c476992010-10-04 21:36:35 +02005569 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005570
Antonio Quartulli267335d2012-01-31 20:25:47 +01005571 ibss.control_port =
5572 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5573
Johannes Berg4c476992010-10-04 21:36:35 +02005574 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005575 if (err)
5576 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005577 return err;
5578}
5579
5580static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5581{
Johannes Berg4c476992010-10-04 21:36:35 +02005582 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5583 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005584
Johannes Berg4c476992010-10-04 21:36:35 +02005585 if (!rdev->ops->leave_ibss)
5586 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005587
Johannes Berg4c476992010-10-04 21:36:35 +02005588 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5589 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005590
Johannes Berg4c476992010-10-04 21:36:35 +02005591 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005592}
5593
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005594static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5595{
5596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5597 struct net_device *dev = info->user_ptr[1];
5598 int mcast_rate[IEEE80211_NUM_BANDS];
5599 u32 nla_rate;
5600 int err;
5601
5602 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5603 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5604 return -EOPNOTSUPP;
5605
5606 if (!rdev->ops->set_mcast_rate)
5607 return -EOPNOTSUPP;
5608
5609 memset(mcast_rate, 0, sizeof(mcast_rate));
5610
5611 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5612 return -EINVAL;
5613
5614 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5615 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5616 return -EINVAL;
5617
5618 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5619
5620 return err;
5621}
5622
5623
Johannes Bergaff89a92009-07-01 21:26:51 +02005624#ifdef CONFIG_NL80211_TESTMODE
5625static struct genl_multicast_group nl80211_testmode_mcgrp = {
5626 .name = "testmode",
5627};
5628
5629static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5630{
Johannes Berg4c476992010-10-04 21:36:35 +02005631 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005632 int err;
5633
5634 if (!info->attrs[NL80211_ATTR_TESTDATA])
5635 return -EINVAL;
5636
Johannes Bergaff89a92009-07-01 21:26:51 +02005637 err = -EOPNOTSUPP;
5638 if (rdev->ops->testmode_cmd) {
5639 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005640 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005641 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5642 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5643 rdev->testmode_info = NULL;
5644 }
5645
Johannes Bergaff89a92009-07-01 21:26:51 +02005646 return err;
5647}
5648
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005649static int nl80211_testmode_dump(struct sk_buff *skb,
5650 struct netlink_callback *cb)
5651{
Johannes Berg00918d32011-12-13 17:22:05 +01005652 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005653 int err;
5654 long phy_idx;
5655 void *data = NULL;
5656 int data_len = 0;
5657
5658 if (cb->args[0]) {
5659 /*
5660 * 0 is a valid index, but not valid for args[0],
5661 * so we need to offset by 1.
5662 */
5663 phy_idx = cb->args[0] - 1;
5664 } else {
5665 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5666 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5667 nl80211_policy);
5668 if (err)
5669 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005670
Johannes Berg2bd7e352012-06-15 14:23:16 +02005671 mutex_lock(&cfg80211_mutex);
5672 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5673 nl80211_fam.attrbuf);
5674 if (IS_ERR(rdev)) {
5675 mutex_unlock(&cfg80211_mutex);
5676 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005677 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005678 phy_idx = rdev->wiphy_idx;
5679 rdev = NULL;
5680 mutex_unlock(&cfg80211_mutex);
5681
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005682 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5683 cb->args[1] =
5684 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5685 }
5686
5687 if (cb->args[1]) {
5688 data = nla_data((void *)cb->args[1]);
5689 data_len = nla_len((void *)cb->args[1]);
5690 }
5691
5692 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005693 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5694 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005695 mutex_unlock(&cfg80211_mutex);
5696 return -ENOENT;
5697 }
Johannes Berg00918d32011-12-13 17:22:05 +01005698 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005699 mutex_unlock(&cfg80211_mutex);
5700
Johannes Berg00918d32011-12-13 17:22:05 +01005701 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005702 err = -EOPNOTSUPP;
5703 goto out_err;
5704 }
5705
5706 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005707 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005708 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5709 NL80211_CMD_TESTMODE);
5710 struct nlattr *tmdata;
5711
David S. Miller9360ffd2012-03-29 04:41:26 -04005712 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005713 genlmsg_cancel(skb, hdr);
5714 break;
5715 }
5716
5717 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5718 if (!tmdata) {
5719 genlmsg_cancel(skb, hdr);
5720 break;
5721 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005722 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005723 nla_nest_end(skb, tmdata);
5724
5725 if (err == -ENOBUFS || err == -ENOENT) {
5726 genlmsg_cancel(skb, hdr);
5727 break;
5728 } else if (err) {
5729 genlmsg_cancel(skb, hdr);
5730 goto out_err;
5731 }
5732
5733 genlmsg_end(skb, hdr);
5734 }
5735
5736 err = skb->len;
5737 /* see above */
5738 cb->args[0] = phy_idx + 1;
5739 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005740 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005741 return err;
5742}
5743
Johannes Bergaff89a92009-07-01 21:26:51 +02005744static struct sk_buff *
5745__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005746 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005747{
5748 struct sk_buff *skb;
5749 void *hdr;
5750 struct nlattr *data;
5751
5752 skb = nlmsg_new(approxlen + 100, gfp);
5753 if (!skb)
5754 return NULL;
5755
Eric W. Biederman15e47302012-09-07 20:12:54 +00005756 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005757 if (!hdr) {
5758 kfree_skb(skb);
5759 return NULL;
5760 }
5761
David S. Miller9360ffd2012-03-29 04:41:26 -04005762 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5763 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005764 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5765
5766 ((void **)skb->cb)[0] = rdev;
5767 ((void **)skb->cb)[1] = hdr;
5768 ((void **)skb->cb)[2] = data;
5769
5770 return skb;
5771
5772 nla_put_failure:
5773 kfree_skb(skb);
5774 return NULL;
5775}
5776
5777struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5778 int approxlen)
5779{
5780 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5781
5782 if (WARN_ON(!rdev->testmode_info))
5783 return NULL;
5784
5785 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005786 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005787 rdev->testmode_info->snd_seq,
5788 GFP_KERNEL);
5789}
5790EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5791
5792int cfg80211_testmode_reply(struct sk_buff *skb)
5793{
5794 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5795 void *hdr = ((void **)skb->cb)[1];
5796 struct nlattr *data = ((void **)skb->cb)[2];
5797
5798 if (WARN_ON(!rdev->testmode_info)) {
5799 kfree_skb(skb);
5800 return -EINVAL;
5801 }
5802
5803 nla_nest_end(skb, data);
5804 genlmsg_end(skb, hdr);
5805 return genlmsg_reply(skb, rdev->testmode_info);
5806}
5807EXPORT_SYMBOL(cfg80211_testmode_reply);
5808
5809struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5810 int approxlen, gfp_t gfp)
5811{
5812 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5813
5814 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5815}
5816EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5817
5818void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5819{
5820 void *hdr = ((void **)skb->cb)[1];
5821 struct nlattr *data = ((void **)skb->cb)[2];
5822
5823 nla_nest_end(skb, data);
5824 genlmsg_end(skb, hdr);
5825 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5826}
5827EXPORT_SYMBOL(cfg80211_testmode_event);
5828#endif
5829
Samuel Ortizb23aa672009-07-01 21:26:54 +02005830static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5831{
Johannes Berg4c476992010-10-04 21:36:35 +02005832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5833 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005834 struct cfg80211_connect_params connect;
5835 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005836 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005837 int err;
5838
5839 memset(&connect, 0, sizeof(connect));
5840
5841 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5842 return -EINVAL;
5843
5844 if (!info->attrs[NL80211_ATTR_SSID] ||
5845 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5846 return -EINVAL;
5847
5848 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5849 connect.auth_type =
5850 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005851 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5852 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005853 return -EINVAL;
5854 } else
5855 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5856
5857 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5858
Johannes Bergc0692b82010-08-27 14:26:53 +03005859 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005860 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005861 if (err)
5862 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005863
Johannes Berg074ac8d2010-09-16 14:58:22 +02005864 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005865 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5866 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005867
Johannes Berg79c97e92009-07-07 03:56:12 +02005868 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005869
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305870 connect.bg_scan_period = -1;
5871 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5872 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5873 connect.bg_scan_period =
5874 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5875 }
5876
Samuel Ortizb23aa672009-07-01 21:26:54 +02005877 if (info->attrs[NL80211_ATTR_MAC])
5878 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5879 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5880 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5881
5882 if (info->attrs[NL80211_ATTR_IE]) {
5883 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5884 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5885 }
5886
5887 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5888 connect.channel =
5889 ieee80211_get_channel(wiphy,
5890 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5891 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005892 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5893 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005894 }
5895
Johannes Bergfffd0932009-07-08 14:22:54 +02005896 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5897 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305898 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005899 if (IS_ERR(connkeys))
5900 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005901 }
5902
Ben Greear7e7c8922011-11-18 11:31:59 -08005903 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5904 connect.flags |= ASSOC_REQ_DISABLE_HT;
5905
5906 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5907 memcpy(&connect.ht_capa_mask,
5908 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5909 sizeof(connect.ht_capa_mask));
5910
5911 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005912 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5913 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005914 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005915 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005916 memcpy(&connect.ht_capa,
5917 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5918 sizeof(connect.ht_capa));
5919 }
5920
Johannes Bergfffd0932009-07-08 14:22:54 +02005921 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005922 if (err)
5923 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005924 return err;
5925}
5926
5927static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5928{
Johannes Berg4c476992010-10-04 21:36:35 +02005929 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5930 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005931 u16 reason;
5932
5933 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5934 reason = WLAN_REASON_DEAUTH_LEAVING;
5935 else
5936 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5937
5938 if (reason == 0)
5939 return -EINVAL;
5940
Johannes Berg074ac8d2010-09-16 14:58:22 +02005941 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005942 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5943 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005944
Johannes Berg4c476992010-10-04 21:36:35 +02005945 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005946}
5947
Johannes Berg463d0182009-07-14 00:33:35 +02005948static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5949{
Johannes Berg4c476992010-10-04 21:36:35 +02005950 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005951 struct net *net;
5952 int err;
5953 u32 pid;
5954
5955 if (!info->attrs[NL80211_ATTR_PID])
5956 return -EINVAL;
5957
5958 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5959
Johannes Berg463d0182009-07-14 00:33:35 +02005960 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005961 if (IS_ERR(net))
5962 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005963
5964 err = 0;
5965
5966 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005967 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5968 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005969
Johannes Berg463d0182009-07-14 00:33:35 +02005970 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005971 return err;
5972}
5973
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005974static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5975{
Johannes Berg4c476992010-10-04 21:36:35 +02005976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005977 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5978 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005979 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005980 struct cfg80211_pmksa pmksa;
5981
5982 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5983
5984 if (!info->attrs[NL80211_ATTR_MAC])
5985 return -EINVAL;
5986
5987 if (!info->attrs[NL80211_ATTR_PMKID])
5988 return -EINVAL;
5989
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005990 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5991 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5992
Johannes Berg074ac8d2010-09-16 14:58:22 +02005993 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005994 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5995 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005996
5997 switch (info->genlhdr->cmd) {
5998 case NL80211_CMD_SET_PMKSA:
5999 rdev_ops = rdev->ops->set_pmksa;
6000 break;
6001 case NL80211_CMD_DEL_PMKSA:
6002 rdev_ops = rdev->ops->del_pmksa;
6003 break;
6004 default:
6005 WARN_ON(1);
6006 break;
6007 }
6008
Johannes Berg4c476992010-10-04 21:36:35 +02006009 if (!rdev_ops)
6010 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006011
Johannes Berg4c476992010-10-04 21:36:35 +02006012 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006013}
6014
6015static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6016{
Johannes Berg4c476992010-10-04 21:36:35 +02006017 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6018 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006019
Johannes Berg074ac8d2010-09-16 14:58:22 +02006020 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006021 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6022 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006023
Johannes Berg4c476992010-10-04 21:36:35 +02006024 if (!rdev->ops->flush_pmksa)
6025 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006026
Hila Gonene35e4d22012-06-27 17:19:42 +03006027 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006028}
6029
Arik Nemtsov109086c2011-09-28 14:12:50 +03006030static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6031{
6032 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6033 struct net_device *dev = info->user_ptr[1];
6034 u8 action_code, dialog_token;
6035 u16 status_code;
6036 u8 *peer;
6037
6038 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6039 !rdev->ops->tdls_mgmt)
6040 return -EOPNOTSUPP;
6041
6042 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6043 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6044 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6045 !info->attrs[NL80211_ATTR_IE] ||
6046 !info->attrs[NL80211_ATTR_MAC])
6047 return -EINVAL;
6048
6049 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6050 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6051 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6052 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6053
Hila Gonene35e4d22012-06-27 17:19:42 +03006054 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6055 dialog_token, status_code,
6056 nla_data(info->attrs[NL80211_ATTR_IE]),
6057 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006058}
6059
6060static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6061{
6062 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6063 struct net_device *dev = info->user_ptr[1];
6064 enum nl80211_tdls_operation operation;
6065 u8 *peer;
6066
6067 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6068 !rdev->ops->tdls_oper)
6069 return -EOPNOTSUPP;
6070
6071 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6072 !info->attrs[NL80211_ATTR_MAC])
6073 return -EINVAL;
6074
6075 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6076 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6077
Hila Gonene35e4d22012-06-27 17:19:42 +03006078 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006079}
6080
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006081static int nl80211_remain_on_channel(struct sk_buff *skb,
6082 struct genl_info *info)
6083{
Johannes Berg4c476992010-10-04 21:36:35 +02006084 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006085 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006086 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006087 struct sk_buff *msg;
6088 void *hdr;
6089 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006090 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006091 int err;
6092
6093 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6094 !info->attrs[NL80211_ATTR_DURATION])
6095 return -EINVAL;
6096
6097 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6098
Johannes Berg7c4ef712011-11-18 15:33:48 +01006099 if (!rdev->ops->remain_on_channel ||
6100 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006101 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006102
Johannes Bergebf348f2012-06-01 12:50:54 +02006103 /*
6104 * We should be on that channel for at least a minimum amount of
6105 * time (10ms) but no longer than the driver supports.
6106 */
6107 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6108 duration > rdev->wiphy.max_remain_on_channel_duration)
6109 return -EINVAL;
6110
Johannes Berg683b6d32012-11-08 21:25:48 +01006111 err = nl80211_parse_chandef(rdev, info, &chandef);
6112 if (err)
6113 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006114
6115 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006116 if (!msg)
6117 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006118
Eric W. Biederman15e47302012-09-07 20:12:54 +00006119 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006120 NL80211_CMD_REMAIN_ON_CHANNEL);
6121
6122 if (IS_ERR(hdr)) {
6123 err = PTR_ERR(hdr);
6124 goto free_msg;
6125 }
6126
Johannes Berg683b6d32012-11-08 21:25:48 +01006127 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6128 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006129
6130 if (err)
6131 goto free_msg;
6132
David S. Miller9360ffd2012-03-29 04:41:26 -04006133 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6134 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006135
6136 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006137
6138 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006139
6140 nla_put_failure:
6141 err = -ENOBUFS;
6142 free_msg:
6143 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006144 return err;
6145}
6146
6147static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6148 struct genl_info *info)
6149{
Johannes Berg4c476992010-10-04 21:36:35 +02006150 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006151 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006152 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006153
6154 if (!info->attrs[NL80211_ATTR_COOKIE])
6155 return -EINVAL;
6156
Johannes Berg4c476992010-10-04 21:36:35 +02006157 if (!rdev->ops->cancel_remain_on_channel)
6158 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006159
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006160 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6161
Hila Gonene35e4d22012-06-27 17:19:42 +03006162 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006163}
6164
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006165static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6166 u8 *rates, u8 rates_len)
6167{
6168 u8 i;
6169 u32 mask = 0;
6170
6171 for (i = 0; i < rates_len; i++) {
6172 int rate = (rates[i] & 0x7f) * 5;
6173 int ridx;
6174 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6175 struct ieee80211_rate *srate =
6176 &sband->bitrates[ridx];
6177 if (rate == srate->bitrate) {
6178 mask |= 1 << ridx;
6179 break;
6180 }
6181 }
6182 if (ridx == sband->n_bitrates)
6183 return 0; /* rate not found */
6184 }
6185
6186 return mask;
6187}
6188
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006189static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6190 u8 *rates, u8 rates_len,
6191 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6192{
6193 u8 i;
6194
6195 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6196
6197 for (i = 0; i < rates_len; i++) {
6198 int ridx, rbit;
6199
6200 ridx = rates[i] / 8;
6201 rbit = BIT(rates[i] % 8);
6202
6203 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006204 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006205 return false;
6206
6207 /* check availability */
6208 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6209 mcs[ridx] |= rbit;
6210 else
6211 return false;
6212 }
6213
6214 return true;
6215}
6216
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006217static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006218 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6219 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006220 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6221 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006222};
6223
6224static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6225 struct genl_info *info)
6226{
6227 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006228 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006229 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006230 int rem, i;
6231 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006232 struct nlattr *tx_rates;
6233 struct ieee80211_supported_band *sband;
6234
6235 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6236 return -EINVAL;
6237
Johannes Berg4c476992010-10-04 21:36:35 +02006238 if (!rdev->ops->set_bitrate_mask)
6239 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006240
6241 memset(&mask, 0, sizeof(mask));
6242 /* Default to all rates enabled */
6243 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6244 sband = rdev->wiphy.bands[i];
6245 mask.control[i].legacy =
6246 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006247 if (sband)
6248 memcpy(mask.control[i].mcs,
6249 sband->ht_cap.mcs.rx_mask,
6250 sizeof(mask.control[i].mcs));
6251 else
6252 memset(mask.control[i].mcs, 0,
6253 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006254 }
6255
6256 /*
6257 * The nested attribute uses enum nl80211_band as the index. This maps
6258 * directly to the enum ieee80211_band values used in cfg80211.
6259 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006260 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006261 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6262 {
6263 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006264 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6265 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006266 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006267 if (sband == NULL)
6268 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006269 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6270 nla_len(tx_rates), nl80211_txattr_policy);
6271 if (tb[NL80211_TXRATE_LEGACY]) {
6272 mask.control[band].legacy = rateset_to_mask(
6273 sband,
6274 nla_data(tb[NL80211_TXRATE_LEGACY]),
6275 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306276 if ((mask.control[band].legacy == 0) &&
6277 nla_len(tb[NL80211_TXRATE_LEGACY]))
6278 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006279 }
6280 if (tb[NL80211_TXRATE_MCS]) {
6281 if (!ht_rateset_to_mask(
6282 sband,
6283 nla_data(tb[NL80211_TXRATE_MCS]),
6284 nla_len(tb[NL80211_TXRATE_MCS]),
6285 mask.control[band].mcs))
6286 return -EINVAL;
6287 }
6288
6289 if (mask.control[band].legacy == 0) {
6290 /* don't allow empty legacy rates if HT
6291 * is not even supported. */
6292 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6293 return -EINVAL;
6294
6295 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6296 if (mask.control[band].mcs[i])
6297 break;
6298
6299 /* legacy and mcs rates may not be both empty */
6300 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006301 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006302 }
6303 }
6304
Hila Gonene35e4d22012-06-27 17:19:42 +03006305 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006306}
6307
Johannes Berg2e161f72010-08-12 15:38:38 +02006308static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006309{
Johannes Berg4c476992010-10-04 21:36:35 +02006310 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006311 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006312 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006313
6314 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6315 return -EINVAL;
6316
Johannes Berg2e161f72010-08-12 15:38:38 +02006317 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6318 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006319
Johannes Berg71bbc992012-06-15 15:30:18 +02006320 switch (wdev->iftype) {
6321 case NL80211_IFTYPE_STATION:
6322 case NL80211_IFTYPE_ADHOC:
6323 case NL80211_IFTYPE_P2P_CLIENT:
6324 case NL80211_IFTYPE_AP:
6325 case NL80211_IFTYPE_AP_VLAN:
6326 case NL80211_IFTYPE_MESH_POINT:
6327 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006328 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006329 break;
6330 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006331 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006332 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006333
6334 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006335 if (!rdev->ops->mgmt_tx)
6336 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006337
Eric W. Biederman15e47302012-09-07 20:12:54 +00006338 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006339 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6340 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006341}
6342
Johannes Berg2e161f72010-08-12 15:38:38 +02006343static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006344{
Johannes Berg4c476992010-10-04 21:36:35 +02006345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006346 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006347 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006348 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006349 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006350 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006351 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006352 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006353 bool offchan, no_cck, dont_wait_for_ack;
6354
6355 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006356
Johannes Berg683b6d32012-11-08 21:25:48 +01006357 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006358 return -EINVAL;
6359
Johannes Berg4c476992010-10-04 21:36:35 +02006360 if (!rdev->ops->mgmt_tx)
6361 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006362
Johannes Berg71bbc992012-06-15 15:30:18 +02006363 switch (wdev->iftype) {
6364 case NL80211_IFTYPE_STATION:
6365 case NL80211_IFTYPE_ADHOC:
6366 case NL80211_IFTYPE_P2P_CLIENT:
6367 case NL80211_IFTYPE_AP:
6368 case NL80211_IFTYPE_AP_VLAN:
6369 case NL80211_IFTYPE_MESH_POINT:
6370 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006371 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006372 break;
6373 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006374 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006375 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006376
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006377 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006378 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006379 return -EINVAL;
6380 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006381
6382 /*
6383 * We should wait on the channel for at least a minimum amount
6384 * of time (10ms) but no longer than the driver supports.
6385 */
6386 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6387 wait > rdev->wiphy.max_remain_on_channel_duration)
6388 return -EINVAL;
6389
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006390 }
6391
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006392 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6393
Johannes Berg7c4ef712011-11-18 15:33:48 +01006394 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6395 return -EINVAL;
6396
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306397 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6398
Johannes Berg683b6d32012-11-08 21:25:48 +01006399 err = nl80211_parse_chandef(rdev, info, &chandef);
6400 if (err)
6401 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006402
Johannes Berge247bd902011-11-04 11:18:21 +01006403 if (!dont_wait_for_ack) {
6404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6405 if (!msg)
6406 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006407
Eric W. Biederman15e47302012-09-07 20:12:54 +00006408 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006409 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006410
Johannes Berge247bd902011-11-04 11:18:21 +01006411 if (IS_ERR(hdr)) {
6412 err = PTR_ERR(hdr);
6413 goto free_msg;
6414 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006415 }
Johannes Berge247bd902011-11-04 11:18:21 +01006416
Johannes Berg683b6d32012-11-08 21:25:48 +01006417 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006418 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6419 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006420 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006421 if (err)
6422 goto free_msg;
6423
Johannes Berge247bd902011-11-04 11:18:21 +01006424 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006425 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6426 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006427
Johannes Berge247bd902011-11-04 11:18:21 +01006428 genlmsg_end(msg, hdr);
6429 return genlmsg_reply(msg, info);
6430 }
6431
6432 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006433
6434 nla_put_failure:
6435 err = -ENOBUFS;
6436 free_msg:
6437 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006438 return err;
6439}
6440
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006441static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6442{
6443 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006444 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006445 u64 cookie;
6446
6447 if (!info->attrs[NL80211_ATTR_COOKIE])
6448 return -EINVAL;
6449
6450 if (!rdev->ops->mgmt_tx_cancel_wait)
6451 return -EOPNOTSUPP;
6452
Johannes Berg71bbc992012-06-15 15:30:18 +02006453 switch (wdev->iftype) {
6454 case NL80211_IFTYPE_STATION:
6455 case NL80211_IFTYPE_ADHOC:
6456 case NL80211_IFTYPE_P2P_CLIENT:
6457 case NL80211_IFTYPE_AP:
6458 case NL80211_IFTYPE_AP_VLAN:
6459 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006460 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006461 break;
6462 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006463 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006464 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006465
6466 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6467
Hila Gonene35e4d22012-06-27 17:19:42 +03006468 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006469}
6470
Kalle Valoffb9eb32010-02-17 17:58:10 +02006471static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6472{
Johannes Berg4c476992010-10-04 21:36:35 +02006473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006474 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006475 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006476 u8 ps_state;
6477 bool state;
6478 int err;
6479
Johannes Berg4c476992010-10-04 21:36:35 +02006480 if (!info->attrs[NL80211_ATTR_PS_STATE])
6481 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006482
6483 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6484
Johannes Berg4c476992010-10-04 21:36:35 +02006485 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6486 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006487
6488 wdev = dev->ieee80211_ptr;
6489
Johannes Berg4c476992010-10-04 21:36:35 +02006490 if (!rdev->ops->set_power_mgmt)
6491 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006492
6493 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6494
6495 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006496 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006497
Hila Gonene35e4d22012-06-27 17:19:42 +03006498 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006499 if (!err)
6500 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006501 return err;
6502}
6503
6504static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6505{
Johannes Berg4c476992010-10-04 21:36:35 +02006506 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006507 enum nl80211_ps_state ps_state;
6508 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006509 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006510 struct sk_buff *msg;
6511 void *hdr;
6512 int err;
6513
Kalle Valoffb9eb32010-02-17 17:58:10 +02006514 wdev = dev->ieee80211_ptr;
6515
Johannes Berg4c476992010-10-04 21:36:35 +02006516 if (!rdev->ops->set_power_mgmt)
6517 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006518
6519 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006520 if (!msg)
6521 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006522
Eric W. Biederman15e47302012-09-07 20:12:54 +00006523 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006524 NL80211_CMD_GET_POWER_SAVE);
6525 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006526 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006527 goto free_msg;
6528 }
6529
6530 if (wdev->ps)
6531 ps_state = NL80211_PS_ENABLED;
6532 else
6533 ps_state = NL80211_PS_DISABLED;
6534
David S. Miller9360ffd2012-03-29 04:41:26 -04006535 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6536 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006537
6538 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006539 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006540
Johannes Berg4c476992010-10-04 21:36:35 +02006541 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006542 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006543 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006544 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006545 return err;
6546}
6547
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006548static struct nla_policy
6549nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6550 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6551 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6552 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006553 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6554 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6555 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006556};
6557
Thomas Pedersen84f10702012-07-12 16:17:33 -07006558static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006559 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006560{
6561 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6562 struct wireless_dev *wdev;
6563 struct net_device *dev = info->user_ptr[1];
6564
Johannes Bergd9d8b012012-11-26 12:51:52 +01006565 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006566 return -EINVAL;
6567
6568 wdev = dev->ieee80211_ptr;
6569
6570 if (!rdev->ops->set_cqm_txe_config)
6571 return -EOPNOTSUPP;
6572
6573 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6574 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6575 return -EOPNOTSUPP;
6576
Hila Gonene35e4d22012-06-27 17:19:42 +03006577 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006578}
6579
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006580static int nl80211_set_cqm_rssi(struct genl_info *info,
6581 s32 threshold, u32 hysteresis)
6582{
Johannes Berg4c476992010-10-04 21:36:35 +02006583 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006584 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006585 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006586
6587 if (threshold > 0)
6588 return -EINVAL;
6589
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006590 wdev = dev->ieee80211_ptr;
6591
Johannes Berg4c476992010-10-04 21:36:35 +02006592 if (!rdev->ops->set_cqm_rssi_config)
6593 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006594
Johannes Berg074ac8d2010-09-16 14:58:22 +02006595 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006596 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6597 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006598
Hila Gonene35e4d22012-06-27 17:19:42 +03006599 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006600}
6601
6602static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6603{
6604 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6605 struct nlattr *cqm;
6606 int err;
6607
6608 cqm = info->attrs[NL80211_ATTR_CQM];
6609 if (!cqm) {
6610 err = -EINVAL;
6611 goto out;
6612 }
6613
6614 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6615 nl80211_attr_cqm_policy);
6616 if (err)
6617 goto out;
6618
6619 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6620 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6621 s32 threshold;
6622 u32 hysteresis;
6623 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6624 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6625 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006626 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6627 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6628 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6629 u32 rate, pkts, intvl;
6630 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6631 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6632 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6633 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006634 } else
6635 err = -EINVAL;
6636
6637out:
6638 return err;
6639}
6640
Johannes Berg29cbe682010-12-03 09:20:44 +01006641static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6642{
6643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6644 struct net_device *dev = info->user_ptr[1];
6645 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006646 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006647 int err;
6648
6649 /* start with default */
6650 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006651 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006652
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006653 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006654 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006655 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006656 if (err)
6657 return err;
6658 }
6659
6660 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6661 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6662 return -EINVAL;
6663
Javier Cardonac80d5452010-12-16 17:37:49 -08006664 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6665 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6666
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006667 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6668 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6669 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6670 return -EINVAL;
6671
Marco Porsch9bdbf042013-01-07 16:04:51 +01006672 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6673 setup.beacon_interval =
6674 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6675 if (setup.beacon_interval < 10 ||
6676 setup.beacon_interval > 10000)
6677 return -EINVAL;
6678 }
6679
6680 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
6681 setup.dtim_period =
6682 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
6683 if (setup.dtim_period < 1 || setup.dtim_period > 100)
6684 return -EINVAL;
6685 }
6686
Javier Cardonac80d5452010-12-16 17:37:49 -08006687 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6688 /* parse additional setup parameters if given */
6689 err = nl80211_parse_mesh_setup(info, &setup);
6690 if (err)
6691 return err;
6692 }
6693
Johannes Bergcc1d2802012-05-16 23:50:20 +02006694 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01006695 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
6696 if (err)
6697 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006698 } else {
6699 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01006700 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006701 }
6702
Javier Cardonac80d5452010-12-16 17:37:49 -08006703 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006704}
6705
6706static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6707{
6708 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6709 struct net_device *dev = info->user_ptr[1];
6710
6711 return cfg80211_leave_mesh(rdev, dev);
6712}
6713
Johannes Bergdfb89c52012-06-27 09:23:48 +02006714#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006715static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6716{
6717 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6718 struct sk_buff *msg;
6719 void *hdr;
6720
6721 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6722 return -EOPNOTSUPP;
6723
6724 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6725 if (!msg)
6726 return -ENOMEM;
6727
Eric W. Biederman15e47302012-09-07 20:12:54 +00006728 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006729 NL80211_CMD_GET_WOWLAN);
6730 if (!hdr)
6731 goto nla_put_failure;
6732
6733 if (rdev->wowlan) {
6734 struct nlattr *nl_wowlan;
6735
6736 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6737 if (!nl_wowlan)
6738 goto nla_put_failure;
6739
David S. Miller9360ffd2012-03-29 04:41:26 -04006740 if ((rdev->wowlan->any &&
6741 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6742 (rdev->wowlan->disconnect &&
6743 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6744 (rdev->wowlan->magic_pkt &&
6745 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6746 (rdev->wowlan->gtk_rekey_failure &&
6747 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6748 (rdev->wowlan->eap_identity_req &&
6749 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6750 (rdev->wowlan->four_way_handshake &&
6751 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6752 (rdev->wowlan->rfkill_release &&
6753 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6754 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006755 if (rdev->wowlan->n_patterns) {
6756 struct nlattr *nl_pats, *nl_pat;
6757 int i, pat_len;
6758
6759 nl_pats = nla_nest_start(msg,
6760 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6761 if (!nl_pats)
6762 goto nla_put_failure;
6763
6764 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6765 nl_pat = nla_nest_start(msg, i + 1);
6766 if (!nl_pat)
6767 goto nla_put_failure;
6768 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006769 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6770 DIV_ROUND_UP(pat_len, 8),
6771 rdev->wowlan->patterns[i].mask) ||
6772 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6773 pat_len,
6774 rdev->wowlan->patterns[i].pattern))
6775 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006776 nla_nest_end(msg, nl_pat);
6777 }
6778 nla_nest_end(msg, nl_pats);
6779 }
6780
6781 nla_nest_end(msg, nl_wowlan);
6782 }
6783
6784 genlmsg_end(msg, hdr);
6785 return genlmsg_reply(msg, info);
6786
6787nla_put_failure:
6788 nlmsg_free(msg);
6789 return -ENOBUFS;
6790}
6791
6792static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6793{
6794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6795 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006796 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006797 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006798 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6799 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006800 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006801
6802 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6803 return -EOPNOTSUPP;
6804
Johannes Bergae33bd82012-07-12 16:25:02 +02006805 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6806 cfg80211_rdev_free_wowlan(rdev);
6807 rdev->wowlan = NULL;
6808 goto set_wakeup;
6809 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006810
6811 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6812 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6813 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6814 nl80211_wowlan_policy);
6815 if (err)
6816 return err;
6817
6818 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6819 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6820 return -EINVAL;
6821 new_triggers.any = true;
6822 }
6823
6824 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6825 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6826 return -EINVAL;
6827 new_triggers.disconnect = true;
6828 }
6829
6830 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6831 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6832 return -EINVAL;
6833 new_triggers.magic_pkt = true;
6834 }
6835
Johannes Berg77dbbb12011-07-13 10:48:55 +02006836 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6837 return -EINVAL;
6838
6839 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6840 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6841 return -EINVAL;
6842 new_triggers.gtk_rekey_failure = true;
6843 }
6844
6845 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6846 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6847 return -EINVAL;
6848 new_triggers.eap_identity_req = true;
6849 }
6850
6851 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6852 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6853 return -EINVAL;
6854 new_triggers.four_way_handshake = true;
6855 }
6856
6857 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6858 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6859 return -EINVAL;
6860 new_triggers.rfkill_release = true;
6861 }
6862
Johannes Bergff1b6e62011-05-04 15:37:28 +02006863 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6864 struct nlattr *pat;
6865 int n_patterns = 0;
6866 int rem, pat_len, mask_len;
6867 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6868
6869 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6870 rem)
6871 n_patterns++;
6872 if (n_patterns > wowlan->n_patterns)
6873 return -EINVAL;
6874
6875 new_triggers.patterns = kcalloc(n_patterns,
6876 sizeof(new_triggers.patterns[0]),
6877 GFP_KERNEL);
6878 if (!new_triggers.patterns)
6879 return -ENOMEM;
6880
6881 new_triggers.n_patterns = n_patterns;
6882 i = 0;
6883
6884 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6885 rem) {
6886 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6887 nla_data(pat), nla_len(pat), NULL);
6888 err = -EINVAL;
6889 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6890 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6891 goto error;
6892 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6893 mask_len = DIV_ROUND_UP(pat_len, 8);
6894 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6895 mask_len)
6896 goto error;
6897 if (pat_len > wowlan->pattern_max_len ||
6898 pat_len < wowlan->pattern_min_len)
6899 goto error;
6900
6901 new_triggers.patterns[i].mask =
6902 kmalloc(mask_len + pat_len, GFP_KERNEL);
6903 if (!new_triggers.patterns[i].mask) {
6904 err = -ENOMEM;
6905 goto error;
6906 }
6907 new_triggers.patterns[i].pattern =
6908 new_triggers.patterns[i].mask + mask_len;
6909 memcpy(new_triggers.patterns[i].mask,
6910 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6911 mask_len);
6912 new_triggers.patterns[i].pattern_len = pat_len;
6913 memcpy(new_triggers.patterns[i].pattern,
6914 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6915 pat_len);
6916 i++;
6917 }
6918 }
6919
Johannes Bergae33bd82012-07-12 16:25:02 +02006920 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6921 if (!ntrig) {
6922 err = -ENOMEM;
6923 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006924 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006925 cfg80211_rdev_free_wowlan(rdev);
6926 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006927
Johannes Bergae33bd82012-07-12 16:25:02 +02006928 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006929 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03006930 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02006931
Johannes Bergff1b6e62011-05-04 15:37:28 +02006932 return 0;
6933 error:
6934 for (i = 0; i < new_triggers.n_patterns; i++)
6935 kfree(new_triggers.patterns[i].mask);
6936 kfree(new_triggers.patterns);
6937 return err;
6938}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006939#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006940
Johannes Berge5497d72011-07-05 16:35:40 +02006941static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6942{
6943 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6944 struct net_device *dev = info->user_ptr[1];
6945 struct wireless_dev *wdev = dev->ieee80211_ptr;
6946 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6947 struct cfg80211_gtk_rekey_data rekey_data;
6948 int err;
6949
6950 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6951 return -EINVAL;
6952
6953 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6954 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6955 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6956 nl80211_rekey_policy);
6957 if (err)
6958 return err;
6959
6960 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6961 return -ERANGE;
6962 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6963 return -ERANGE;
6964 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6965 return -ERANGE;
6966
6967 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6968 NL80211_KEK_LEN);
6969 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6970 NL80211_KCK_LEN);
6971 memcpy(rekey_data.replay_ctr,
6972 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6973 NL80211_REPLAY_CTR_LEN);
6974
6975 wdev_lock(wdev);
6976 if (!wdev->current_bss) {
6977 err = -ENOTCONN;
6978 goto out;
6979 }
6980
6981 if (!rdev->ops->set_rekey_data) {
6982 err = -EOPNOTSUPP;
6983 goto out;
6984 }
6985
Hila Gonene35e4d22012-06-27 17:19:42 +03006986 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02006987 out:
6988 wdev_unlock(wdev);
6989 return err;
6990}
6991
Johannes Berg28946da2011-11-04 11:18:12 +01006992static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6993 struct genl_info *info)
6994{
6995 struct net_device *dev = info->user_ptr[1];
6996 struct wireless_dev *wdev = dev->ieee80211_ptr;
6997
6998 if (wdev->iftype != NL80211_IFTYPE_AP &&
6999 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7000 return -EINVAL;
7001
Eric W. Biederman15e47302012-09-07 20:12:54 +00007002 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007003 return -EBUSY;
7004
Eric W. Biederman15e47302012-09-07 20:12:54 +00007005 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007006 return 0;
7007}
7008
Johannes Berg7f6cf312011-11-04 11:18:15 +01007009static int nl80211_probe_client(struct sk_buff *skb,
7010 struct genl_info *info)
7011{
7012 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7013 struct net_device *dev = info->user_ptr[1];
7014 struct wireless_dev *wdev = dev->ieee80211_ptr;
7015 struct sk_buff *msg;
7016 void *hdr;
7017 const u8 *addr;
7018 u64 cookie;
7019 int err;
7020
7021 if (wdev->iftype != NL80211_IFTYPE_AP &&
7022 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7023 return -EOPNOTSUPP;
7024
7025 if (!info->attrs[NL80211_ATTR_MAC])
7026 return -EINVAL;
7027
7028 if (!rdev->ops->probe_client)
7029 return -EOPNOTSUPP;
7030
7031 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7032 if (!msg)
7033 return -ENOMEM;
7034
Eric W. Biederman15e47302012-09-07 20:12:54 +00007035 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007036 NL80211_CMD_PROBE_CLIENT);
7037
7038 if (IS_ERR(hdr)) {
7039 err = PTR_ERR(hdr);
7040 goto free_msg;
7041 }
7042
7043 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7044
Hila Gonene35e4d22012-06-27 17:19:42 +03007045 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007046 if (err)
7047 goto free_msg;
7048
David S. Miller9360ffd2012-03-29 04:41:26 -04007049 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7050 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007051
7052 genlmsg_end(msg, hdr);
7053
7054 return genlmsg_reply(msg, info);
7055
7056 nla_put_failure:
7057 err = -ENOBUFS;
7058 free_msg:
7059 nlmsg_free(msg);
7060 return err;
7061}
7062
Johannes Berg5e760232011-11-04 11:18:17 +01007063static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7064{
7065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007066 struct cfg80211_beacon_registration *reg, *nreg;
7067 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007068
7069 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7070 return -EOPNOTSUPP;
7071
Ben Greear37c73b52012-10-26 14:49:25 -07007072 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7073 if (!nreg)
7074 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007075
Ben Greear37c73b52012-10-26 14:49:25 -07007076 /* First, check if already registered. */
7077 spin_lock_bh(&rdev->beacon_registrations_lock);
7078 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7079 if (reg->nlportid == info->snd_portid) {
7080 rv = -EALREADY;
7081 goto out_err;
7082 }
7083 }
7084 /* Add it to the list */
7085 nreg->nlportid = info->snd_portid;
7086 list_add(&nreg->list, &rdev->beacon_registrations);
7087
7088 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007089
7090 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007091out_err:
7092 spin_unlock_bh(&rdev->beacon_registrations_lock);
7093 kfree(nreg);
7094 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007095}
7096
Johannes Berg98104fde2012-06-16 00:19:54 +02007097static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7098{
7099 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7100 struct wireless_dev *wdev = info->user_ptr[1];
7101 int err;
7102
7103 if (!rdev->ops->start_p2p_device)
7104 return -EOPNOTSUPP;
7105
7106 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7107 return -EOPNOTSUPP;
7108
7109 if (wdev->p2p_started)
7110 return 0;
7111
7112 mutex_lock(&rdev->devlist_mtx);
7113 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7114 mutex_unlock(&rdev->devlist_mtx);
7115 if (err)
7116 return err;
7117
Johannes Bergeeb126e2012-10-23 15:16:50 +02007118 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007119 if (err)
7120 return err;
7121
7122 wdev->p2p_started = true;
7123 mutex_lock(&rdev->devlist_mtx);
7124 rdev->opencount++;
7125 mutex_unlock(&rdev->devlist_mtx);
7126
7127 return 0;
7128}
7129
7130static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7131{
7132 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7133 struct wireless_dev *wdev = info->user_ptr[1];
7134
7135 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7136 return -EOPNOTSUPP;
7137
7138 if (!rdev->ops->stop_p2p_device)
7139 return -EOPNOTSUPP;
7140
7141 if (!wdev->p2p_started)
7142 return 0;
7143
Johannes Bergeeb126e2012-10-23 15:16:50 +02007144 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007145 wdev->p2p_started = false;
7146
7147 mutex_lock(&rdev->devlist_mtx);
7148 rdev->opencount--;
7149 mutex_unlock(&rdev->devlist_mtx);
7150
7151 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7152 rdev->scan_req->aborted = true;
7153 ___cfg80211_scan_done(rdev, true);
7154 }
7155
7156 return 0;
7157}
7158
Johannes Berg4c476992010-10-04 21:36:35 +02007159#define NL80211_FLAG_NEED_WIPHY 0x01
7160#define NL80211_FLAG_NEED_NETDEV 0x02
7161#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007162#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7163#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7164 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007165#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007166/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007167#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7168 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007169
7170static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7171 struct genl_info *info)
7172{
7173 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007174 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007175 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007176 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7177
7178 if (rtnl)
7179 rtnl_lock();
7180
7181 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007182 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007183 if (IS_ERR(rdev)) {
7184 if (rtnl)
7185 rtnl_unlock();
7186 return PTR_ERR(rdev);
7187 }
7188 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007189 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7190 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007191 mutex_lock(&cfg80211_mutex);
7192 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7193 info->attrs);
7194 if (IS_ERR(wdev)) {
7195 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007196 if (rtnl)
7197 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007198 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007199 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007200
Johannes Berg89a54e42012-06-15 14:33:17 +02007201 dev = wdev->netdev;
7202 rdev = wiphy_to_dev(wdev->wiphy);
7203
Johannes Berg1bf614e2012-06-15 15:23:36 +02007204 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7205 if (!dev) {
7206 mutex_unlock(&cfg80211_mutex);
7207 if (rtnl)
7208 rtnl_unlock();
7209 return -EINVAL;
7210 }
7211
7212 info->user_ptr[1] = dev;
7213 } else {
7214 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007215 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007216
Johannes Berg1bf614e2012-06-15 15:23:36 +02007217 if (dev) {
7218 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7219 !netif_running(dev)) {
7220 mutex_unlock(&cfg80211_mutex);
7221 if (rtnl)
7222 rtnl_unlock();
7223 return -ENETDOWN;
7224 }
7225
7226 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007227 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7228 if (!wdev->p2p_started) {
7229 mutex_unlock(&cfg80211_mutex);
7230 if (rtnl)
7231 rtnl_unlock();
7232 return -ENETDOWN;
7233 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007234 }
7235
Johannes Berg89a54e42012-06-15 14:33:17 +02007236 cfg80211_lock_rdev(rdev);
7237
7238 mutex_unlock(&cfg80211_mutex);
7239
Johannes Berg4c476992010-10-04 21:36:35 +02007240 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007241 }
7242
7243 return 0;
7244}
7245
7246static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7247 struct genl_info *info)
7248{
7249 if (info->user_ptr[0])
7250 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007251 if (info->user_ptr[1]) {
7252 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7253 struct wireless_dev *wdev = info->user_ptr[1];
7254
7255 if (wdev->netdev)
7256 dev_put(wdev->netdev);
7257 } else {
7258 dev_put(info->user_ptr[1]);
7259 }
7260 }
Johannes Berg4c476992010-10-04 21:36:35 +02007261 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7262 rtnl_unlock();
7263}
7264
Johannes Berg55682962007-09-20 13:09:35 -04007265static struct genl_ops nl80211_ops[] = {
7266 {
7267 .cmd = NL80211_CMD_GET_WIPHY,
7268 .doit = nl80211_get_wiphy,
7269 .dumpit = nl80211_dump_wiphy,
7270 .policy = nl80211_policy,
7271 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007272 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007273 },
7274 {
7275 .cmd = NL80211_CMD_SET_WIPHY,
7276 .doit = nl80211_set_wiphy,
7277 .policy = nl80211_policy,
7278 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007279 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007280 },
7281 {
7282 .cmd = NL80211_CMD_GET_INTERFACE,
7283 .doit = nl80211_get_interface,
7284 .dumpit = nl80211_dump_interface,
7285 .policy = nl80211_policy,
7286 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007287 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007288 },
7289 {
7290 .cmd = NL80211_CMD_SET_INTERFACE,
7291 .doit = nl80211_set_interface,
7292 .policy = nl80211_policy,
7293 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007294 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7295 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007296 },
7297 {
7298 .cmd = NL80211_CMD_NEW_INTERFACE,
7299 .doit = nl80211_new_interface,
7300 .policy = nl80211_policy,
7301 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007302 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7303 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007304 },
7305 {
7306 .cmd = NL80211_CMD_DEL_INTERFACE,
7307 .doit = nl80211_del_interface,
7308 .policy = nl80211_policy,
7309 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007310 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007311 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007312 },
Johannes Berg41ade002007-12-19 02:03:29 +01007313 {
7314 .cmd = NL80211_CMD_GET_KEY,
7315 .doit = nl80211_get_key,
7316 .policy = nl80211_policy,
7317 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007318 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007319 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007320 },
7321 {
7322 .cmd = NL80211_CMD_SET_KEY,
7323 .doit = nl80211_set_key,
7324 .policy = nl80211_policy,
7325 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007326 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007327 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007328 },
7329 {
7330 .cmd = NL80211_CMD_NEW_KEY,
7331 .doit = nl80211_new_key,
7332 .policy = nl80211_policy,
7333 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007334 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007335 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007336 },
7337 {
7338 .cmd = NL80211_CMD_DEL_KEY,
7339 .doit = nl80211_del_key,
7340 .policy = nl80211_policy,
7341 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007342 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007343 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007344 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007345 {
7346 .cmd = NL80211_CMD_SET_BEACON,
7347 .policy = nl80211_policy,
7348 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007349 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007350 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007351 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007352 },
7353 {
Johannes Berg88600202012-02-13 15:17:18 +01007354 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007355 .policy = nl80211_policy,
7356 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007357 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007358 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007359 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007360 },
7361 {
Johannes Berg88600202012-02-13 15:17:18 +01007362 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007363 .policy = nl80211_policy,
7364 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007365 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007366 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007367 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007368 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007369 {
7370 .cmd = NL80211_CMD_GET_STATION,
7371 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007372 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007373 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007374 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7375 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007376 },
7377 {
7378 .cmd = NL80211_CMD_SET_STATION,
7379 .doit = nl80211_set_station,
7380 .policy = nl80211_policy,
7381 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007382 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007383 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007384 },
7385 {
7386 .cmd = NL80211_CMD_NEW_STATION,
7387 .doit = nl80211_new_station,
7388 .policy = nl80211_policy,
7389 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007390 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007391 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007392 },
7393 {
7394 .cmd = NL80211_CMD_DEL_STATION,
7395 .doit = nl80211_del_station,
7396 .policy = nl80211_policy,
7397 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007398 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007399 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007400 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007401 {
7402 .cmd = NL80211_CMD_GET_MPATH,
7403 .doit = nl80211_get_mpath,
7404 .dumpit = nl80211_dump_mpath,
7405 .policy = nl80211_policy,
7406 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007407 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007408 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007409 },
7410 {
7411 .cmd = NL80211_CMD_SET_MPATH,
7412 .doit = nl80211_set_mpath,
7413 .policy = nl80211_policy,
7414 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007415 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007416 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007417 },
7418 {
7419 .cmd = NL80211_CMD_NEW_MPATH,
7420 .doit = nl80211_new_mpath,
7421 .policy = nl80211_policy,
7422 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007423 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007424 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007425 },
7426 {
7427 .cmd = NL80211_CMD_DEL_MPATH,
7428 .doit = nl80211_del_mpath,
7429 .policy = nl80211_policy,
7430 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007431 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007432 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007433 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007434 {
7435 .cmd = NL80211_CMD_SET_BSS,
7436 .doit = nl80211_set_bss,
7437 .policy = nl80211_policy,
7438 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007439 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007440 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007441 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007442 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007443 .cmd = NL80211_CMD_GET_REG,
7444 .doit = nl80211_get_reg,
7445 .policy = nl80211_policy,
7446 /* can be retrieved by unprivileged users */
7447 },
7448 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007449 .cmd = NL80211_CMD_SET_REG,
7450 .doit = nl80211_set_reg,
7451 .policy = nl80211_policy,
7452 .flags = GENL_ADMIN_PERM,
7453 },
7454 {
7455 .cmd = NL80211_CMD_REQ_SET_REG,
7456 .doit = nl80211_req_set_reg,
7457 .policy = nl80211_policy,
7458 .flags = GENL_ADMIN_PERM,
7459 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007460 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007461 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7462 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007463 .policy = nl80211_policy,
7464 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007465 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007466 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007467 },
7468 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007469 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7470 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007471 .policy = nl80211_policy,
7472 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007473 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007474 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007475 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007476 {
Johannes Berg2a519312009-02-10 21:25:55 +01007477 .cmd = NL80211_CMD_TRIGGER_SCAN,
7478 .doit = nl80211_trigger_scan,
7479 .policy = nl80211_policy,
7480 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007481 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007482 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007483 },
7484 {
7485 .cmd = NL80211_CMD_GET_SCAN,
7486 .policy = nl80211_policy,
7487 .dumpit = nl80211_dump_scan,
7488 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007489 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007490 .cmd = NL80211_CMD_START_SCHED_SCAN,
7491 .doit = nl80211_start_sched_scan,
7492 .policy = nl80211_policy,
7493 .flags = GENL_ADMIN_PERM,
7494 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7495 NL80211_FLAG_NEED_RTNL,
7496 },
7497 {
7498 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7499 .doit = nl80211_stop_sched_scan,
7500 .policy = nl80211_policy,
7501 .flags = GENL_ADMIN_PERM,
7502 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7503 NL80211_FLAG_NEED_RTNL,
7504 },
7505 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007506 .cmd = NL80211_CMD_AUTHENTICATE,
7507 .doit = nl80211_authenticate,
7508 .policy = nl80211_policy,
7509 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007510 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007511 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007512 },
7513 {
7514 .cmd = NL80211_CMD_ASSOCIATE,
7515 .doit = nl80211_associate,
7516 .policy = nl80211_policy,
7517 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007518 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007519 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007520 },
7521 {
7522 .cmd = NL80211_CMD_DEAUTHENTICATE,
7523 .doit = nl80211_deauthenticate,
7524 .policy = nl80211_policy,
7525 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007526 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007527 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007528 },
7529 {
7530 .cmd = NL80211_CMD_DISASSOCIATE,
7531 .doit = nl80211_disassociate,
7532 .policy = nl80211_policy,
7533 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007534 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007535 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007536 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007537 {
7538 .cmd = NL80211_CMD_JOIN_IBSS,
7539 .doit = nl80211_join_ibss,
7540 .policy = nl80211_policy,
7541 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007543 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007544 },
7545 {
7546 .cmd = NL80211_CMD_LEAVE_IBSS,
7547 .doit = nl80211_leave_ibss,
7548 .policy = nl80211_policy,
7549 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007551 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007552 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007553#ifdef CONFIG_NL80211_TESTMODE
7554 {
7555 .cmd = NL80211_CMD_TESTMODE,
7556 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007557 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007558 .policy = nl80211_policy,
7559 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007560 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7561 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007562 },
7563#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007564 {
7565 .cmd = NL80211_CMD_CONNECT,
7566 .doit = nl80211_connect,
7567 .policy = nl80211_policy,
7568 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007569 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007570 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007571 },
7572 {
7573 .cmd = NL80211_CMD_DISCONNECT,
7574 .doit = nl80211_disconnect,
7575 .policy = nl80211_policy,
7576 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007577 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007578 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007579 },
Johannes Berg463d0182009-07-14 00:33:35 +02007580 {
7581 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7582 .doit = nl80211_wiphy_netns,
7583 .policy = nl80211_policy,
7584 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007585 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7586 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007587 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007588 {
7589 .cmd = NL80211_CMD_GET_SURVEY,
7590 .policy = nl80211_policy,
7591 .dumpit = nl80211_dump_survey,
7592 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007593 {
7594 .cmd = NL80211_CMD_SET_PMKSA,
7595 .doit = nl80211_setdel_pmksa,
7596 .policy = nl80211_policy,
7597 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007598 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007599 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007600 },
7601 {
7602 .cmd = NL80211_CMD_DEL_PMKSA,
7603 .doit = nl80211_setdel_pmksa,
7604 .policy = nl80211_policy,
7605 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007606 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007607 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007608 },
7609 {
7610 .cmd = NL80211_CMD_FLUSH_PMKSA,
7611 .doit = nl80211_flush_pmksa,
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,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007616 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007617 {
7618 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7619 .doit = nl80211_remain_on_channel,
7620 .policy = nl80211_policy,
7621 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007622 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007623 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007624 },
7625 {
7626 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7627 .doit = nl80211_cancel_remain_on_channel,
7628 .policy = nl80211_policy,
7629 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007630 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007631 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007632 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007633 {
7634 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7635 .doit = nl80211_set_tx_bitrate_mask,
7636 .policy = nl80211_policy,
7637 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007638 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7639 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007640 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007641 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007642 .cmd = NL80211_CMD_REGISTER_FRAME,
7643 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007644 .policy = nl80211_policy,
7645 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007646 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007647 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007648 },
7649 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007650 .cmd = NL80211_CMD_FRAME,
7651 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007652 .policy = nl80211_policy,
7653 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007654 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007655 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007656 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007657 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007658 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7659 .doit = nl80211_tx_mgmt_cancel_wait,
7660 .policy = nl80211_policy,
7661 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007662 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007663 NL80211_FLAG_NEED_RTNL,
7664 },
7665 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007666 .cmd = NL80211_CMD_SET_POWER_SAVE,
7667 .doit = nl80211_set_power_save,
7668 .policy = nl80211_policy,
7669 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007670 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7671 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007672 },
7673 {
7674 .cmd = NL80211_CMD_GET_POWER_SAVE,
7675 .doit = nl80211_get_power_save,
7676 .policy = nl80211_policy,
7677 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007678 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7679 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007680 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007681 {
7682 .cmd = NL80211_CMD_SET_CQM,
7683 .doit = nl80211_set_cqm,
7684 .policy = nl80211_policy,
7685 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007686 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7687 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007688 },
Johannes Bergf444de02010-05-05 15:25:02 +02007689 {
7690 .cmd = NL80211_CMD_SET_CHANNEL,
7691 .doit = nl80211_set_channel,
7692 .policy = nl80211_policy,
7693 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007694 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7695 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007696 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007697 {
7698 .cmd = NL80211_CMD_SET_WDS_PEER,
7699 .doit = nl80211_set_wds_peer,
7700 .policy = nl80211_policy,
7701 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007702 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7703 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007704 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007705 {
7706 .cmd = NL80211_CMD_JOIN_MESH,
7707 .doit = nl80211_join_mesh,
7708 .policy = nl80211_policy,
7709 .flags = GENL_ADMIN_PERM,
7710 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7711 NL80211_FLAG_NEED_RTNL,
7712 },
7713 {
7714 .cmd = NL80211_CMD_LEAVE_MESH,
7715 .doit = nl80211_leave_mesh,
7716 .policy = nl80211_policy,
7717 .flags = GENL_ADMIN_PERM,
7718 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7719 NL80211_FLAG_NEED_RTNL,
7720 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007721#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007722 {
7723 .cmd = NL80211_CMD_GET_WOWLAN,
7724 .doit = nl80211_get_wowlan,
7725 .policy = nl80211_policy,
7726 /* can be retrieved by unprivileged users */
7727 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7728 NL80211_FLAG_NEED_RTNL,
7729 },
7730 {
7731 .cmd = NL80211_CMD_SET_WOWLAN,
7732 .doit = nl80211_set_wowlan,
7733 .policy = nl80211_policy,
7734 .flags = GENL_ADMIN_PERM,
7735 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7736 NL80211_FLAG_NEED_RTNL,
7737 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007738#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007739 {
7740 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7741 .doit = nl80211_set_rekey_data,
7742 .policy = nl80211_policy,
7743 .flags = GENL_ADMIN_PERM,
7744 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7745 NL80211_FLAG_NEED_RTNL,
7746 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007747 {
7748 .cmd = NL80211_CMD_TDLS_MGMT,
7749 .doit = nl80211_tdls_mgmt,
7750 .policy = nl80211_policy,
7751 .flags = GENL_ADMIN_PERM,
7752 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7753 NL80211_FLAG_NEED_RTNL,
7754 },
7755 {
7756 .cmd = NL80211_CMD_TDLS_OPER,
7757 .doit = nl80211_tdls_oper,
7758 .policy = nl80211_policy,
7759 .flags = GENL_ADMIN_PERM,
7760 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7761 NL80211_FLAG_NEED_RTNL,
7762 },
Johannes Berg28946da2011-11-04 11:18:12 +01007763 {
7764 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7765 .doit = nl80211_register_unexpected_frame,
7766 .policy = nl80211_policy,
7767 .flags = GENL_ADMIN_PERM,
7768 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7769 NL80211_FLAG_NEED_RTNL,
7770 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007771 {
7772 .cmd = NL80211_CMD_PROBE_CLIENT,
7773 .doit = nl80211_probe_client,
7774 .policy = nl80211_policy,
7775 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007776 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007777 NL80211_FLAG_NEED_RTNL,
7778 },
Johannes Berg5e760232011-11-04 11:18:17 +01007779 {
7780 .cmd = NL80211_CMD_REGISTER_BEACONS,
7781 .doit = nl80211_register_beacons,
7782 .policy = nl80211_policy,
7783 .flags = GENL_ADMIN_PERM,
7784 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7785 NL80211_FLAG_NEED_RTNL,
7786 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007787 {
7788 .cmd = NL80211_CMD_SET_NOACK_MAP,
7789 .doit = nl80211_set_noack_map,
7790 .policy = nl80211_policy,
7791 .flags = GENL_ADMIN_PERM,
7792 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7793 NL80211_FLAG_NEED_RTNL,
7794 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007795 {
7796 .cmd = NL80211_CMD_START_P2P_DEVICE,
7797 .doit = nl80211_start_p2p_device,
7798 .policy = nl80211_policy,
7799 .flags = GENL_ADMIN_PERM,
7800 .internal_flags = NL80211_FLAG_NEED_WDEV |
7801 NL80211_FLAG_NEED_RTNL,
7802 },
7803 {
7804 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7805 .doit = nl80211_stop_p2p_device,
7806 .policy = nl80211_policy,
7807 .flags = GENL_ADMIN_PERM,
7808 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7809 NL80211_FLAG_NEED_RTNL,
7810 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007811 {
7812 .cmd = NL80211_CMD_SET_MCAST_RATE,
7813 .doit = nl80211_set_mcast_rate,
7814 .policy = nl80211_policy,
7815 .flags = GENL_ADMIN_PERM,
7816 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7817 NL80211_FLAG_NEED_RTNL,
7818 },
Johannes Berg55682962007-09-20 13:09:35 -04007819};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007820
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007821static struct genl_multicast_group nl80211_mlme_mcgrp = {
7822 .name = "mlme",
7823};
Johannes Berg55682962007-09-20 13:09:35 -04007824
7825/* multicast groups */
7826static struct genl_multicast_group nl80211_config_mcgrp = {
7827 .name = "config",
7828};
Johannes Berg2a519312009-02-10 21:25:55 +01007829static struct genl_multicast_group nl80211_scan_mcgrp = {
7830 .name = "scan",
7831};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007832static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7833 .name = "regulatory",
7834};
Johannes Berg55682962007-09-20 13:09:35 -04007835
7836/* notification functions */
7837
7838void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7839{
7840 struct sk_buff *msg;
7841
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007842 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007843 if (!msg)
7844 return;
7845
7846 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7847 nlmsg_free(msg);
7848 return;
7849 }
7850
Johannes Berg463d0182009-07-14 00:33:35 +02007851 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7852 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007853}
7854
Johannes Berg362a4152009-05-24 16:43:15 +02007855static int nl80211_add_scan_req(struct sk_buff *msg,
7856 struct cfg80211_registered_device *rdev)
7857{
7858 struct cfg80211_scan_request *req = rdev->scan_req;
7859 struct nlattr *nest;
7860 int i;
7861
Johannes Berg667503d2009-07-07 03:56:11 +02007862 ASSERT_RDEV_LOCK(rdev);
7863
Johannes Berg362a4152009-05-24 16:43:15 +02007864 if (WARN_ON(!req))
7865 return 0;
7866
7867 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7868 if (!nest)
7869 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007870 for (i = 0; i < req->n_ssids; i++) {
7871 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7872 goto nla_put_failure;
7873 }
Johannes Berg362a4152009-05-24 16:43:15 +02007874 nla_nest_end(msg, nest);
7875
7876 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7877 if (!nest)
7878 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007879 for (i = 0; i < req->n_channels; i++) {
7880 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7881 goto nla_put_failure;
7882 }
Johannes Berg362a4152009-05-24 16:43:15 +02007883 nla_nest_end(msg, nest);
7884
David S. Miller9360ffd2012-03-29 04:41:26 -04007885 if (req->ie &&
7886 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7887 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007888
Sam Lefflered4737712012-10-11 21:03:31 -07007889 if (req->flags)
7890 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7891
Johannes Berg362a4152009-05-24 16:43:15 +02007892 return 0;
7893 nla_put_failure:
7894 return -ENOBUFS;
7895}
7896
Johannes Berga538e2d2009-06-16 19:56:42 +02007897static int nl80211_send_scan_msg(struct sk_buff *msg,
7898 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007899 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007900 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007901 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007902{
7903 void *hdr;
7904
Eric W. Biederman15e47302012-09-07 20:12:54 +00007905 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007906 if (!hdr)
7907 return -1;
7908
David S. Miller9360ffd2012-03-29 04:41:26 -04007909 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007910 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7911 wdev->netdev->ifindex)) ||
7912 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007913 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007914
Johannes Berg362a4152009-05-24 16:43:15 +02007915 /* ignore errors and send incomplete event anyway */
7916 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007917
7918 return genlmsg_end(msg, hdr);
7919
7920 nla_put_failure:
7921 genlmsg_cancel(msg, hdr);
7922 return -EMSGSIZE;
7923}
7924
Luciano Coelho807f8a82011-05-11 17:09:35 +03007925static int
7926nl80211_send_sched_scan_msg(struct sk_buff *msg,
7927 struct cfg80211_registered_device *rdev,
7928 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007929 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007930{
7931 void *hdr;
7932
Eric W. Biederman15e47302012-09-07 20:12:54 +00007933 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007934 if (!hdr)
7935 return -1;
7936
David S. Miller9360ffd2012-03-29 04:41:26 -04007937 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7938 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7939 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007940
7941 return genlmsg_end(msg, hdr);
7942
7943 nla_put_failure:
7944 genlmsg_cancel(msg, hdr);
7945 return -EMSGSIZE;
7946}
7947
Johannes Berga538e2d2009-06-16 19:56:42 +02007948void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007949 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007950{
7951 struct sk_buff *msg;
7952
Thomas Graf58050fc2012-06-28 03:57:45 +00007953 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007954 if (!msg)
7955 return;
7956
Johannes Bergfd014282012-06-18 19:17:03 +02007957 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007958 NL80211_CMD_TRIGGER_SCAN) < 0) {
7959 nlmsg_free(msg);
7960 return;
7961 }
7962
Johannes Berg463d0182009-07-14 00:33:35 +02007963 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7964 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007965}
7966
Johannes Berg2a519312009-02-10 21:25:55 +01007967void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007968 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007969{
7970 struct sk_buff *msg;
7971
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007972 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007973 if (!msg)
7974 return;
7975
Johannes Bergfd014282012-06-18 19:17:03 +02007976 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007977 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007978 nlmsg_free(msg);
7979 return;
7980 }
7981
Johannes Berg463d0182009-07-14 00:33:35 +02007982 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7983 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007984}
7985
7986void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007987 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007988{
7989 struct sk_buff *msg;
7990
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007991 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007992 if (!msg)
7993 return;
7994
Johannes Bergfd014282012-06-18 19:17:03 +02007995 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007996 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007997 nlmsg_free(msg);
7998 return;
7999 }
8000
Johannes Berg463d0182009-07-14 00:33:35 +02008001 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8002 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008003}
8004
Luciano Coelho807f8a82011-05-11 17:09:35 +03008005void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8006 struct net_device *netdev)
8007{
8008 struct sk_buff *msg;
8009
8010 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8011 if (!msg)
8012 return;
8013
8014 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8015 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8016 nlmsg_free(msg);
8017 return;
8018 }
8019
8020 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8021 nl80211_scan_mcgrp.id, GFP_KERNEL);
8022}
8023
8024void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8025 struct net_device *netdev, u32 cmd)
8026{
8027 struct sk_buff *msg;
8028
Thomas Graf58050fc2012-06-28 03:57:45 +00008029 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008030 if (!msg)
8031 return;
8032
8033 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8034 nlmsg_free(msg);
8035 return;
8036 }
8037
8038 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8039 nl80211_scan_mcgrp.id, GFP_KERNEL);
8040}
8041
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008042/*
8043 * This can happen on global regulatory changes or device specific settings
8044 * based on custom world regulatory domains.
8045 */
8046void nl80211_send_reg_change_event(struct regulatory_request *request)
8047{
8048 struct sk_buff *msg;
8049 void *hdr;
8050
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008051 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008052 if (!msg)
8053 return;
8054
8055 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8056 if (!hdr) {
8057 nlmsg_free(msg);
8058 return;
8059 }
8060
8061 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008062 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8063 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008064
David S. Miller9360ffd2012-03-29 04:41:26 -04008065 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8066 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8067 NL80211_REGDOM_TYPE_WORLD))
8068 goto nla_put_failure;
8069 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8070 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8071 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8072 goto nla_put_failure;
8073 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8074 request->intersect) {
8075 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8076 NL80211_REGDOM_TYPE_INTERSECTION))
8077 goto nla_put_failure;
8078 } else {
8079 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8080 NL80211_REGDOM_TYPE_COUNTRY) ||
8081 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8082 request->alpha2))
8083 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008084 }
8085
Johannes Bergf4173762012-12-03 18:23:37 +01008086 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008087 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8088 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008089
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008090 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008091
Johannes Bergbc43b282009-07-25 10:54:13 +02008092 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008093 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008094 GFP_ATOMIC);
8095 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008096
8097 return;
8098
8099nla_put_failure:
8100 genlmsg_cancel(msg, hdr);
8101 nlmsg_free(msg);
8102}
8103
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008104static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8105 struct net_device *netdev,
8106 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008107 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008108{
8109 struct sk_buff *msg;
8110 void *hdr;
8111
Johannes Berge6d6e342009-07-01 21:26:47 +02008112 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008113 if (!msg)
8114 return;
8115
8116 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8117 if (!hdr) {
8118 nlmsg_free(msg);
8119 return;
8120 }
8121
David S. Miller9360ffd2012-03-29 04:41:26 -04008122 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8123 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8124 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8125 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008126
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008127 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008128
Johannes Berg463d0182009-07-14 00:33:35 +02008129 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8130 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008131 return;
8132
8133 nla_put_failure:
8134 genlmsg_cancel(msg, hdr);
8135 nlmsg_free(msg);
8136}
8137
8138void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008139 struct net_device *netdev, const u8 *buf,
8140 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008141{
8142 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008143 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008144}
8145
8146void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8147 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008148 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008149{
Johannes Berge6d6e342009-07-01 21:26:47 +02008150 nl80211_send_mlme_event(rdev, netdev, buf, len,
8151 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008152}
8153
Jouni Malinen53b46b82009-03-27 20:53:56 +02008154void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008155 struct net_device *netdev, const u8 *buf,
8156 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008157{
8158 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008159 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008160}
8161
Jouni Malinen53b46b82009-03-27 20:53:56 +02008162void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8163 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008164 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008165{
8166 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008167 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008168}
8169
Jouni Malinencf4e5942010-12-16 00:52:40 +02008170void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8171 struct net_device *netdev, const u8 *buf,
8172 size_t len, gfp_t gfp)
8173{
8174 nl80211_send_mlme_event(rdev, netdev, buf, len,
8175 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8176}
8177
8178void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8179 struct net_device *netdev, const u8 *buf,
8180 size_t len, gfp_t gfp)
8181{
8182 nl80211_send_mlme_event(rdev, netdev, buf, len,
8183 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8184}
8185
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008186static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8187 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008188 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008189{
8190 struct sk_buff *msg;
8191 void *hdr;
8192
Johannes Berge6d6e342009-07-01 21:26:47 +02008193 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008194 if (!msg)
8195 return;
8196
8197 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8198 if (!hdr) {
8199 nlmsg_free(msg);
8200 return;
8201 }
8202
David S. Miller9360ffd2012-03-29 04:41:26 -04008203 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8204 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8205 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8206 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8207 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008208
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008209 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008210
Johannes Berg463d0182009-07-14 00:33:35 +02008211 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8212 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008213 return;
8214
8215 nla_put_failure:
8216 genlmsg_cancel(msg, hdr);
8217 nlmsg_free(msg);
8218}
8219
8220void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008221 struct net_device *netdev, const u8 *addr,
8222 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008223{
8224 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008225 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008226}
8227
8228void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008229 struct net_device *netdev, const u8 *addr,
8230 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008231{
Johannes Berge6d6e342009-07-01 21:26:47 +02008232 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8233 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008234}
8235
Samuel Ortizb23aa672009-07-01 21:26:54 +02008236void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8237 struct net_device *netdev, const u8 *bssid,
8238 const u8 *req_ie, size_t req_ie_len,
8239 const u8 *resp_ie, size_t resp_ie_len,
8240 u16 status, gfp_t gfp)
8241{
8242 struct sk_buff *msg;
8243 void *hdr;
8244
Thomas Graf58050fc2012-06-28 03:57:45 +00008245 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008246 if (!msg)
8247 return;
8248
8249 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8250 if (!hdr) {
8251 nlmsg_free(msg);
8252 return;
8253 }
8254
David S. Miller9360ffd2012-03-29 04:41:26 -04008255 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8256 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8257 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8258 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8259 (req_ie &&
8260 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8261 (resp_ie &&
8262 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8263 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008264
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008265 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008266
Johannes Berg463d0182009-07-14 00:33:35 +02008267 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8268 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008269 return;
8270
8271 nla_put_failure:
8272 genlmsg_cancel(msg, hdr);
8273 nlmsg_free(msg);
8274
8275}
8276
8277void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8278 struct net_device *netdev, const u8 *bssid,
8279 const u8 *req_ie, size_t req_ie_len,
8280 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8281{
8282 struct sk_buff *msg;
8283 void *hdr;
8284
Thomas Graf58050fc2012-06-28 03:57:45 +00008285 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008286 if (!msg)
8287 return;
8288
8289 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8290 if (!hdr) {
8291 nlmsg_free(msg);
8292 return;
8293 }
8294
David S. Miller9360ffd2012-03-29 04:41:26 -04008295 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8296 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8297 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8298 (req_ie &&
8299 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8300 (resp_ie &&
8301 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8302 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008303
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008304 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008305
Johannes Berg463d0182009-07-14 00:33:35 +02008306 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8307 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008308 return;
8309
8310 nla_put_failure:
8311 genlmsg_cancel(msg, hdr);
8312 nlmsg_free(msg);
8313
8314}
8315
8316void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8317 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02008318 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008319{
8320 struct sk_buff *msg;
8321 void *hdr;
8322
Thomas Graf58050fc2012-06-28 03:57:45 +00008323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008324 if (!msg)
8325 return;
8326
8327 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8328 if (!hdr) {
8329 nlmsg_free(msg);
8330 return;
8331 }
8332
David S. Miller9360ffd2012-03-29 04:41:26 -04008333 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8334 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8335 (from_ap && reason &&
8336 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8337 (from_ap &&
8338 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8339 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8340 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008341
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008342 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008343
Johannes Berg463d0182009-07-14 00:33:35 +02008344 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8345 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008346 return;
8347
8348 nla_put_failure:
8349 genlmsg_cancel(msg, hdr);
8350 nlmsg_free(msg);
8351
8352}
8353
Johannes Berg04a773a2009-04-19 21:24:32 +02008354void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8355 struct net_device *netdev, const u8 *bssid,
8356 gfp_t gfp)
8357{
8358 struct sk_buff *msg;
8359 void *hdr;
8360
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008361 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008362 if (!msg)
8363 return;
8364
8365 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8366 if (!hdr) {
8367 nlmsg_free(msg);
8368 return;
8369 }
8370
David S. Miller9360ffd2012-03-29 04:41:26 -04008371 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8372 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8373 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8374 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008375
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008376 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008377
Johannes Berg463d0182009-07-14 00:33:35 +02008378 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8379 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008380 return;
8381
8382 nla_put_failure:
8383 genlmsg_cancel(msg, hdr);
8384 nlmsg_free(msg);
8385}
8386
Javier Cardonac93b5e72011-04-07 15:08:34 -07008387void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8388 struct net_device *netdev,
8389 const u8 *macaddr, const u8* ie, u8 ie_len,
8390 gfp_t gfp)
8391{
8392 struct sk_buff *msg;
8393 void *hdr;
8394
8395 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8396 if (!msg)
8397 return;
8398
8399 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8400 if (!hdr) {
8401 nlmsg_free(msg);
8402 return;
8403 }
8404
David S. Miller9360ffd2012-03-29 04:41:26 -04008405 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8406 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8407 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8408 (ie_len && ie &&
8409 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8410 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008411
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008412 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008413
8414 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8415 nl80211_mlme_mcgrp.id, gfp);
8416 return;
8417
8418 nla_put_failure:
8419 genlmsg_cancel(msg, hdr);
8420 nlmsg_free(msg);
8421}
8422
Jouni Malinena3b8b052009-03-27 21:59:49 +02008423void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8424 struct net_device *netdev, const u8 *addr,
8425 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008426 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008427{
8428 struct sk_buff *msg;
8429 void *hdr;
8430
Johannes Berge6d6e342009-07-01 21:26:47 +02008431 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008432 if (!msg)
8433 return;
8434
8435 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8436 if (!hdr) {
8437 nlmsg_free(msg);
8438 return;
8439 }
8440
David S. Miller9360ffd2012-03-29 04:41:26 -04008441 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8442 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8443 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8444 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8445 (key_id != -1 &&
8446 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8447 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8448 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008449
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008450 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008451
Johannes Berg463d0182009-07-14 00:33:35 +02008452 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8453 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008454 return;
8455
8456 nla_put_failure:
8457 genlmsg_cancel(msg, hdr);
8458 nlmsg_free(msg);
8459}
8460
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008461void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8462 struct ieee80211_channel *channel_before,
8463 struct ieee80211_channel *channel_after)
8464{
8465 struct sk_buff *msg;
8466 void *hdr;
8467 struct nlattr *nl_freq;
8468
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008469 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008470 if (!msg)
8471 return;
8472
8473 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8474 if (!hdr) {
8475 nlmsg_free(msg);
8476 return;
8477 }
8478
8479 /*
8480 * Since we are applying the beacon hint to a wiphy we know its
8481 * wiphy_idx is valid
8482 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008483 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8484 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008485
8486 /* Before */
8487 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8488 if (!nl_freq)
8489 goto nla_put_failure;
8490 if (nl80211_msg_put_channel(msg, channel_before))
8491 goto nla_put_failure;
8492 nla_nest_end(msg, nl_freq);
8493
8494 /* After */
8495 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8496 if (!nl_freq)
8497 goto nla_put_failure;
8498 if (nl80211_msg_put_channel(msg, channel_after))
8499 goto nla_put_failure;
8500 nla_nest_end(msg, nl_freq);
8501
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008502 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008503
Johannes Berg463d0182009-07-14 00:33:35 +02008504 rcu_read_lock();
8505 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8506 GFP_ATOMIC);
8507 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008508
8509 return;
8510
8511nla_put_failure:
8512 genlmsg_cancel(msg, hdr);
8513 nlmsg_free(msg);
8514}
8515
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008516static void nl80211_send_remain_on_chan_event(
8517 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008518 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008519 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008520 unsigned int duration, gfp_t gfp)
8521{
8522 struct sk_buff *msg;
8523 void *hdr;
8524
8525 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8526 if (!msg)
8527 return;
8528
8529 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8530 if (!hdr) {
8531 nlmsg_free(msg);
8532 return;
8533 }
8534
David S. Miller9360ffd2012-03-29 04:41:26 -04008535 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008536 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8537 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008538 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008539 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01008540 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
8541 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008542 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8543 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008544
David S. Miller9360ffd2012-03-29 04:41:26 -04008545 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8546 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8547 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008548
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008549 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008550
8551 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8552 nl80211_mlme_mcgrp.id, gfp);
8553 return;
8554
8555 nla_put_failure:
8556 genlmsg_cancel(msg, hdr);
8557 nlmsg_free(msg);
8558}
8559
8560void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008561 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008562 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008563 unsigned int duration, gfp_t gfp)
8564{
8565 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008566 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01008567 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008568}
8569
8570void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008571 struct cfg80211_registered_device *rdev,
8572 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01008573 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008574{
8575 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01008576 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008577}
8578
Johannes Berg98b62182009-12-23 13:15:44 +01008579void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8580 struct net_device *dev, const u8 *mac_addr,
8581 struct station_info *sinfo, gfp_t gfp)
8582{
8583 struct sk_buff *msg;
8584
Thomas Graf58050fc2012-06-28 03:57:45 +00008585 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008586 if (!msg)
8587 return;
8588
John W. Linville66266b32012-03-15 13:25:41 -04008589 if (nl80211_send_station(msg, 0, 0, 0,
8590 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008591 nlmsg_free(msg);
8592 return;
8593 }
8594
8595 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8596 nl80211_mlme_mcgrp.id, gfp);
8597}
8598
Jouni Malinenec15e682011-03-23 15:29:52 +02008599void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8600 struct net_device *dev, const u8 *mac_addr,
8601 gfp_t gfp)
8602{
8603 struct sk_buff *msg;
8604 void *hdr;
8605
Thomas Graf58050fc2012-06-28 03:57:45 +00008606 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008607 if (!msg)
8608 return;
8609
8610 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8611 if (!hdr) {
8612 nlmsg_free(msg);
8613 return;
8614 }
8615
David S. Miller9360ffd2012-03-29 04:41:26 -04008616 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8617 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8618 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008619
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008620 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008621
8622 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8623 nl80211_mlme_mcgrp.id, gfp);
8624 return;
8625
8626 nla_put_failure:
8627 genlmsg_cancel(msg, hdr);
8628 nlmsg_free(msg);
8629}
8630
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308631void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8632 struct net_device *dev, const u8 *mac_addr,
8633 enum nl80211_connect_failed_reason reason,
8634 gfp_t gfp)
8635{
8636 struct sk_buff *msg;
8637 void *hdr;
8638
8639 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8640 if (!msg)
8641 return;
8642
8643 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8644 if (!hdr) {
8645 nlmsg_free(msg);
8646 return;
8647 }
8648
8649 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8650 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8651 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8652 goto nla_put_failure;
8653
8654 genlmsg_end(msg, hdr);
8655
8656 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8657 nl80211_mlme_mcgrp.id, gfp);
8658 return;
8659
8660 nla_put_failure:
8661 genlmsg_cancel(msg, hdr);
8662 nlmsg_free(msg);
8663}
8664
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008665static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8666 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008667{
8668 struct wireless_dev *wdev = dev->ieee80211_ptr;
8669 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8670 struct sk_buff *msg;
8671 void *hdr;
8672 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008673 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008674
Eric W. Biederman15e47302012-09-07 20:12:54 +00008675 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008676 return false;
8677
8678 msg = nlmsg_new(100, gfp);
8679 if (!msg)
8680 return true;
8681
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008682 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008683 if (!hdr) {
8684 nlmsg_free(msg);
8685 return true;
8686 }
8687
David S. Miller9360ffd2012-03-29 04:41:26 -04008688 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8689 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8690 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8691 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008692
8693 err = genlmsg_end(msg, hdr);
8694 if (err < 0) {
8695 nlmsg_free(msg);
8696 return true;
8697 }
8698
Eric W. Biederman15e47302012-09-07 20:12:54 +00008699 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008700 return true;
8701
8702 nla_put_failure:
8703 genlmsg_cancel(msg, hdr);
8704 nlmsg_free(msg);
8705 return true;
8706}
8707
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008708bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8709{
8710 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8711 addr, gfp);
8712}
8713
8714bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8715 const u8 *addr, gfp_t gfp)
8716{
8717 return __nl80211_unexpected_frame(dev,
8718 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8719 addr, gfp);
8720}
8721
Johannes Berg2e161f72010-08-12 15:38:38 +02008722int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008723 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008724 int freq, int sig_dbm,
8725 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008726{
Johannes Berg71bbc992012-06-15 15:30:18 +02008727 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008728 struct sk_buff *msg;
8729 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008730
8731 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8732 if (!msg)
8733 return -ENOMEM;
8734
Johannes Berg2e161f72010-08-12 15:38:38 +02008735 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008736 if (!hdr) {
8737 nlmsg_free(msg);
8738 return -ENOMEM;
8739 }
8740
David S. Miller9360ffd2012-03-29 04:41:26 -04008741 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008742 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8743 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008744 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8745 (sig_dbm &&
8746 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8747 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8748 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008749
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008750 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008751
Eric W. Biederman15e47302012-09-07 20:12:54 +00008752 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008753
8754 nla_put_failure:
8755 genlmsg_cancel(msg, hdr);
8756 nlmsg_free(msg);
8757 return -ENOBUFS;
8758}
8759
Johannes Berg2e161f72010-08-12 15:38:38 +02008760void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008761 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008762 const u8 *buf, size_t len, bool ack,
8763 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008764{
Johannes Berg71bbc992012-06-15 15:30:18 +02008765 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008766 struct sk_buff *msg;
8767 void *hdr;
8768
8769 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8770 if (!msg)
8771 return;
8772
Johannes Berg2e161f72010-08-12 15:38:38 +02008773 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008774 if (!hdr) {
8775 nlmsg_free(msg);
8776 return;
8777 }
8778
David S. Miller9360ffd2012-03-29 04:41:26 -04008779 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008780 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8781 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008782 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8783 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8784 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8785 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008786
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008787 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008788
8789 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8790 return;
8791
8792 nla_put_failure:
8793 genlmsg_cancel(msg, hdr);
8794 nlmsg_free(msg);
8795}
8796
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008797void
8798nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8799 struct net_device *netdev,
8800 enum nl80211_cqm_rssi_threshold_event rssi_event,
8801 gfp_t gfp)
8802{
8803 struct sk_buff *msg;
8804 struct nlattr *pinfoattr;
8805 void *hdr;
8806
Thomas Graf58050fc2012-06-28 03:57:45 +00008807 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008808 if (!msg)
8809 return;
8810
8811 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8812 if (!hdr) {
8813 nlmsg_free(msg);
8814 return;
8815 }
8816
David S. Miller9360ffd2012-03-29 04:41:26 -04008817 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8818 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8819 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008820
8821 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8822 if (!pinfoattr)
8823 goto nla_put_failure;
8824
David S. Miller9360ffd2012-03-29 04:41:26 -04008825 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8826 rssi_event))
8827 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008828
8829 nla_nest_end(msg, pinfoattr);
8830
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008831 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008832
8833 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8834 nl80211_mlme_mcgrp.id, gfp);
8835 return;
8836
8837 nla_put_failure:
8838 genlmsg_cancel(msg, hdr);
8839 nlmsg_free(msg);
8840}
8841
Johannes Berge5497d72011-07-05 16:35:40 +02008842void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8843 struct net_device *netdev, const u8 *bssid,
8844 const u8 *replay_ctr, gfp_t gfp)
8845{
8846 struct sk_buff *msg;
8847 struct nlattr *rekey_attr;
8848 void *hdr;
8849
Thomas Graf58050fc2012-06-28 03:57:45 +00008850 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008851 if (!msg)
8852 return;
8853
8854 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8855 if (!hdr) {
8856 nlmsg_free(msg);
8857 return;
8858 }
8859
David S. Miller9360ffd2012-03-29 04:41:26 -04008860 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8861 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8862 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8863 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008864
8865 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8866 if (!rekey_attr)
8867 goto nla_put_failure;
8868
David S. Miller9360ffd2012-03-29 04:41:26 -04008869 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8870 NL80211_REPLAY_CTR_LEN, replay_ctr))
8871 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008872
8873 nla_nest_end(msg, rekey_attr);
8874
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008875 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008876
8877 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8878 nl80211_mlme_mcgrp.id, gfp);
8879 return;
8880
8881 nla_put_failure:
8882 genlmsg_cancel(msg, hdr);
8883 nlmsg_free(msg);
8884}
8885
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008886void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8887 struct net_device *netdev, int index,
8888 const u8 *bssid, bool preauth, gfp_t gfp)
8889{
8890 struct sk_buff *msg;
8891 struct nlattr *attr;
8892 void *hdr;
8893
Thomas Graf58050fc2012-06-28 03:57:45 +00008894 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008895 if (!msg)
8896 return;
8897
8898 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8899 if (!hdr) {
8900 nlmsg_free(msg);
8901 return;
8902 }
8903
David S. Miller9360ffd2012-03-29 04:41:26 -04008904 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8905 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8906 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008907
8908 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8909 if (!attr)
8910 goto nla_put_failure;
8911
David S. Miller9360ffd2012-03-29 04:41:26 -04008912 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8913 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8914 (preauth &&
8915 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8916 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008917
8918 nla_nest_end(msg, attr);
8919
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008920 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008921
8922 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8923 nl80211_mlme_mcgrp.id, gfp);
8924 return;
8925
8926 nla_put_failure:
8927 genlmsg_cancel(msg, hdr);
8928 nlmsg_free(msg);
8929}
8930
Thomas Pedersen53145262012-04-06 13:35:47 -07008931void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01008932 struct net_device *netdev,
8933 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07008934{
8935 struct sk_buff *msg;
8936 void *hdr;
8937
Thomas Graf58050fc2012-06-28 03:57:45 +00008938 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008939 if (!msg)
8940 return;
8941
8942 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8943 if (!hdr) {
8944 nlmsg_free(msg);
8945 return;
8946 }
8947
Johannes Berg683b6d32012-11-08 21:25:48 +01008948 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8949 goto nla_put_failure;
8950
8951 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04008952 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008953
8954 genlmsg_end(msg, hdr);
8955
8956 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8957 nl80211_mlme_mcgrp.id, gfp);
8958 return;
8959
8960 nla_put_failure:
8961 genlmsg_cancel(msg, hdr);
8962 nlmsg_free(msg);
8963}
8964
Johannes Bergc063dbf2010-11-24 08:10:05 +01008965void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008966nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8967 struct net_device *netdev, const u8 *peer,
8968 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8969{
8970 struct sk_buff *msg;
8971 struct nlattr *pinfoattr;
8972 void *hdr;
8973
8974 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8975 if (!msg)
8976 return;
8977
8978 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8979 if (!hdr) {
8980 nlmsg_free(msg);
8981 return;
8982 }
8983
8984 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8985 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8986 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8987 goto nla_put_failure;
8988
8989 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8990 if (!pinfoattr)
8991 goto nla_put_failure;
8992
8993 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8994 goto nla_put_failure;
8995
8996 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8997 goto nla_put_failure;
8998
8999 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9000 goto nla_put_failure;
9001
9002 nla_nest_end(msg, pinfoattr);
9003
9004 genlmsg_end(msg, hdr);
9005
9006 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9007 nl80211_mlme_mcgrp.id, gfp);
9008 return;
9009
9010 nla_put_failure:
9011 genlmsg_cancel(msg, hdr);
9012 nlmsg_free(msg);
9013}
9014
9015void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009016nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9017 struct net_device *netdev, const u8 *peer,
9018 u32 num_packets, gfp_t gfp)
9019{
9020 struct sk_buff *msg;
9021 struct nlattr *pinfoattr;
9022 void *hdr;
9023
Thomas Graf58050fc2012-06-28 03:57:45 +00009024 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009025 if (!msg)
9026 return;
9027
9028 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9029 if (!hdr) {
9030 nlmsg_free(msg);
9031 return;
9032 }
9033
David S. Miller9360ffd2012-03-29 04:41:26 -04009034 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9035 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9036 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9037 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009038
9039 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9040 if (!pinfoattr)
9041 goto nla_put_failure;
9042
David S. Miller9360ffd2012-03-29 04:41:26 -04009043 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9044 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009045
9046 nla_nest_end(msg, pinfoattr);
9047
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009048 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009049
9050 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9051 nl80211_mlme_mcgrp.id, gfp);
9052 return;
9053
9054 nla_put_failure:
9055 genlmsg_cancel(msg, hdr);
9056 nlmsg_free(msg);
9057}
9058
Johannes Berg7f6cf312011-11-04 11:18:15 +01009059void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9060 u64 cookie, bool acked, gfp_t gfp)
9061{
9062 struct wireless_dev *wdev = dev->ieee80211_ptr;
9063 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9064 struct sk_buff *msg;
9065 void *hdr;
9066 int err;
9067
Beni Lev4ee3e062012-08-27 12:49:39 +03009068 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9069
Thomas Graf58050fc2012-06-28 03:57:45 +00009070 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009071
Johannes Berg7f6cf312011-11-04 11:18:15 +01009072 if (!msg)
9073 return;
9074
9075 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9076 if (!hdr) {
9077 nlmsg_free(msg);
9078 return;
9079 }
9080
David S. Miller9360ffd2012-03-29 04:41:26 -04009081 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9082 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9083 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9084 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9085 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9086 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009087
9088 err = genlmsg_end(msg, hdr);
9089 if (err < 0) {
9090 nlmsg_free(msg);
9091 return;
9092 }
9093
9094 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9095 nl80211_mlme_mcgrp.id, gfp);
9096 return;
9097
9098 nla_put_failure:
9099 genlmsg_cancel(msg, hdr);
9100 nlmsg_free(msg);
9101}
9102EXPORT_SYMBOL(cfg80211_probe_status);
9103
Johannes Berg5e760232011-11-04 11:18:17 +01009104void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9105 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009106 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009107{
9108 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9109 struct sk_buff *msg;
9110 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009111 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009112
Beni Lev4ee3e062012-08-27 12:49:39 +03009113 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9114
Ben Greear37c73b52012-10-26 14:49:25 -07009115 spin_lock_bh(&rdev->beacon_registrations_lock);
9116 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9117 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9118 if (!msg) {
9119 spin_unlock_bh(&rdev->beacon_registrations_lock);
9120 return;
9121 }
Johannes Berg5e760232011-11-04 11:18:17 +01009122
Ben Greear37c73b52012-10-26 14:49:25 -07009123 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9124 if (!hdr)
9125 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009126
Ben Greear37c73b52012-10-26 14:49:25 -07009127 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9128 (freq &&
9129 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9130 (sig_dbm &&
9131 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9132 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9133 goto nla_put_failure;
9134
9135 genlmsg_end(msg, hdr);
9136
9137 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009138 }
Ben Greear37c73b52012-10-26 14:49:25 -07009139 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009140 return;
9141
9142 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009143 spin_unlock_bh(&rdev->beacon_registrations_lock);
9144 if (hdr)
9145 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009146 nlmsg_free(msg);
9147}
9148EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9149
Jouni Malinen3475b092012-11-16 22:49:57 +02009150void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9151 enum nl80211_tdls_operation oper,
9152 u16 reason_code, gfp_t gfp)
9153{
9154 struct wireless_dev *wdev = dev->ieee80211_ptr;
9155 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9156 struct sk_buff *msg;
9157 void *hdr;
9158 int err;
9159
9160 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9161 reason_code);
9162
9163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9164 if (!msg)
9165 return;
9166
9167 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9168 if (!hdr) {
9169 nlmsg_free(msg);
9170 return;
9171 }
9172
9173 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9174 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9175 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9176 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9177 (reason_code > 0 &&
9178 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9179 goto nla_put_failure;
9180
9181 err = genlmsg_end(msg, hdr);
9182 if (err < 0) {
9183 nlmsg_free(msg);
9184 return;
9185 }
9186
9187 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9188 nl80211_mlme_mcgrp.id, gfp);
9189 return;
9190
9191 nla_put_failure:
9192 genlmsg_cancel(msg, hdr);
9193 nlmsg_free(msg);
9194}
9195EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9196
Jouni Malinen026331c2010-02-15 12:53:10 +02009197static int nl80211_netlink_notify(struct notifier_block * nb,
9198 unsigned long state,
9199 void *_notify)
9200{
9201 struct netlink_notify *notify = _notify;
9202 struct cfg80211_registered_device *rdev;
9203 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009204 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009205
9206 if (state != NETLINK_URELEASE)
9207 return NOTIFY_DONE;
9208
9209 rcu_read_lock();
9210
Johannes Berg5e760232011-11-04 11:18:17 +01009211 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009212 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009213 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009214
9215 spin_lock_bh(&rdev->beacon_registrations_lock);
9216 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9217 list) {
9218 if (reg->nlportid == notify->portid) {
9219 list_del(&reg->list);
9220 kfree(reg);
9221 break;
9222 }
9223 }
9224 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009225 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009226
9227 rcu_read_unlock();
9228
9229 return NOTIFY_DONE;
9230}
9231
9232static struct notifier_block nl80211_netlink_notifier = {
9233 .notifier_call = nl80211_netlink_notify,
9234};
9235
Johannes Berg55682962007-09-20 13:09:35 -04009236/* initialisation/exit functions */
9237
9238int nl80211_init(void)
9239{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009240 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009241
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009242 err = genl_register_family_with_ops(&nl80211_fam,
9243 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009244 if (err)
9245 return err;
9246
Johannes Berg55682962007-09-20 13:09:35 -04009247 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9248 if (err)
9249 goto err_out;
9250
Johannes Berg2a519312009-02-10 21:25:55 +01009251 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9252 if (err)
9253 goto err_out;
9254
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009255 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9256 if (err)
9257 goto err_out;
9258
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009259 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9260 if (err)
9261 goto err_out;
9262
Johannes Bergaff89a92009-07-01 21:26:51 +02009263#ifdef CONFIG_NL80211_TESTMODE
9264 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9265 if (err)
9266 goto err_out;
9267#endif
9268
Jouni Malinen026331c2010-02-15 12:53:10 +02009269 err = netlink_register_notifier(&nl80211_netlink_notifier);
9270 if (err)
9271 goto err_out;
9272
Johannes Berg55682962007-09-20 13:09:35 -04009273 return 0;
9274 err_out:
9275 genl_unregister_family(&nl80211_fam);
9276 return err;
9277}
9278
9279void nl80211_exit(void)
9280{
Jouni Malinen026331c2010-02-15 12:53:10 +02009281 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009282 genl_unregister_family(&nl80211_fam);
9283}