blob: 15158a3d64a3df958d22e87e151c57c74e7471ec [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 Berg55682962007-09-20 13:09:35 -0400366};
367
Johannes Berge31b8212010-10-05 19:39:30 +0200368/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000369static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200370 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200371 [NL80211_KEY_IDX] = { .type = NLA_U8 },
372 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200373 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200374 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
375 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200376 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100377 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
378};
379
380/* policy for the key default flags */
381static const struct nla_policy
382nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
383 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
384 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200385};
386
Johannes Bergff1b6e62011-05-04 15:37:28 +0200387/* policy for WoWLAN attributes */
388static const struct nla_policy
389nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
390 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
391 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
392 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
393 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200394 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
395 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
396 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
397 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200398};
399
Johannes Berge5497d72011-07-05 16:35:40 +0200400/* policy for GTK rekey offload attributes */
401static const struct nla_policy
402nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
403 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
404 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
405 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
406};
407
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300408static const struct nla_policy
409nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200410 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300411 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700412 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300413};
414
Holger Schuriga0438972009-11-11 11:30:02 +0100415/* ifidx get helper */
416static int nl80211_get_ifidx(struct netlink_callback *cb)
417{
418 int res;
419
420 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
421 nl80211_fam.attrbuf, nl80211_fam.maxattr,
422 nl80211_policy);
423 if (res)
424 return res;
425
426 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
427 return -EINVAL;
428
429 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
430 if (!res)
431 return -EINVAL;
432 return res;
433}
434
Johannes Berg67748892010-10-04 21:14:06 +0200435static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
436 struct netlink_callback *cb,
437 struct cfg80211_registered_device **rdev,
438 struct net_device **dev)
439{
440 int ifidx = cb->args[0];
441 int err;
442
443 if (!ifidx)
444 ifidx = nl80211_get_ifidx(cb);
445 if (ifidx < 0)
446 return ifidx;
447
448 cb->args[0] = ifidx;
449
450 rtnl_lock();
451
452 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
453 if (!*dev) {
454 err = -ENODEV;
455 goto out_rtnl;
456 }
457
458 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100459 if (IS_ERR(*rdev)) {
460 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200461 goto out_rtnl;
462 }
463
464 return 0;
465 out_rtnl:
466 rtnl_unlock();
467 return err;
468}
469
470static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
471{
472 cfg80211_unlock_rdev(rdev);
473 rtnl_unlock();
474}
475
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100476/* IE validation */
477static bool is_valid_ie_attr(const struct nlattr *attr)
478{
479 const u8 *pos;
480 int len;
481
482 if (!attr)
483 return true;
484
485 pos = nla_data(attr);
486 len = nla_len(attr);
487
488 while (len) {
489 u8 elemlen;
490
491 if (len < 2)
492 return false;
493 len -= 2;
494
495 elemlen = pos[1];
496 if (elemlen > len)
497 return false;
498
499 len -= elemlen;
500 pos += 2 + elemlen;
501 }
502
503 return true;
504}
505
Johannes Berg55682962007-09-20 13:09:35 -0400506/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000507static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400508 int flags, u8 cmd)
509{
510 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000511 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400512}
513
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400514static int nl80211_msg_put_channel(struct sk_buff *msg,
515 struct ieee80211_channel *chan)
516{
David S. Miller9360ffd2012-03-29 04:41:26 -0400517 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
518 chan->center_freq))
519 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400520
David S. Miller9360ffd2012-03-29 04:41:26 -0400521 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
522 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
523 goto nla_put_failure;
524 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
525 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
526 goto nla_put_failure;
527 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
528 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
529 goto nla_put_failure;
530 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
531 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
532 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400533
David S. Miller9360ffd2012-03-29 04:41:26 -0400534 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
535 DBM_TO_MBM(chan->max_power)))
536 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400537
538 return 0;
539
540 nla_put_failure:
541 return -ENOBUFS;
542}
543
Johannes Berg55682962007-09-20 13:09:35 -0400544/* netlink command implementations */
545
Johannes Bergb9454e82009-07-08 13:29:08 +0200546struct key_parse {
547 struct key_params p;
548 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200549 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200550 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100551 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200552};
553
554static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
555{
556 struct nlattr *tb[NL80211_KEY_MAX + 1];
557 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
558 nl80211_key_policy);
559 if (err)
560 return err;
561
562 k->def = !!tb[NL80211_KEY_DEFAULT];
563 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
564
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100565 if (k->def) {
566 k->def_uni = true;
567 k->def_multi = true;
568 }
569 if (k->defmgmt)
570 k->def_multi = true;
571
Johannes Bergb9454e82009-07-08 13:29:08 +0200572 if (tb[NL80211_KEY_IDX])
573 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
574
575 if (tb[NL80211_KEY_DATA]) {
576 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
577 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
578 }
579
580 if (tb[NL80211_KEY_SEQ]) {
581 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
582 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
583 }
584
585 if (tb[NL80211_KEY_CIPHER])
586 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
587
Johannes Berge31b8212010-10-05 19:39:30 +0200588 if (tb[NL80211_KEY_TYPE]) {
589 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
590 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
591 return -EINVAL;
592 }
593
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100594 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
595 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100596 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
597 tb[NL80211_KEY_DEFAULT_TYPES],
598 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100599 if (err)
600 return err;
601
602 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
603 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
604 }
605
Johannes Bergb9454e82009-07-08 13:29:08 +0200606 return 0;
607}
608
609static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
610{
611 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
612 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
613 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
614 }
615
616 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
617 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
618 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
619 }
620
621 if (info->attrs[NL80211_ATTR_KEY_IDX])
622 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
623
624 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
625 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
626
627 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
628 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
629
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100630 if (k->def) {
631 k->def_uni = true;
632 k->def_multi = true;
633 }
634 if (k->defmgmt)
635 k->def_multi = true;
636
Johannes Berge31b8212010-10-05 19:39:30 +0200637 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
638 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
639 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
640 return -EINVAL;
641 }
642
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100643 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
644 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
645 int err = nla_parse_nested(
646 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
647 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
648 nl80211_key_default_policy);
649 if (err)
650 return err;
651
652 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
653 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
654 }
655
Johannes Bergb9454e82009-07-08 13:29:08 +0200656 return 0;
657}
658
659static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
660{
661 int err;
662
663 memset(k, 0, sizeof(*k));
664 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200665 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200666
667 if (info->attrs[NL80211_ATTR_KEY])
668 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
669 else
670 err = nl80211_parse_key_old(info, k);
671
672 if (err)
673 return err;
674
675 if (k->def && k->defmgmt)
676 return -EINVAL;
677
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100678 if (k->defmgmt) {
679 if (k->def_uni || !k->def_multi)
680 return -EINVAL;
681 }
682
Johannes Bergb9454e82009-07-08 13:29:08 +0200683 if (k->idx != -1) {
684 if (k->defmgmt) {
685 if (k->idx < 4 || k->idx > 5)
686 return -EINVAL;
687 } else if (k->def) {
688 if (k->idx < 0 || k->idx > 3)
689 return -EINVAL;
690 } else {
691 if (k->idx < 0 || k->idx > 5)
692 return -EINVAL;
693 }
694 }
695
696 return 0;
697}
698
Johannes Bergfffd0932009-07-08 14:22:54 +0200699static struct cfg80211_cached_keys *
700nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530701 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200702{
703 struct key_parse parse;
704 struct nlattr *key;
705 struct cfg80211_cached_keys *result;
706 int rem, err, def = 0;
707
708 result = kzalloc(sizeof(*result), GFP_KERNEL);
709 if (!result)
710 return ERR_PTR(-ENOMEM);
711
712 result->def = -1;
713 result->defmgmt = -1;
714
715 nla_for_each_nested(key, keys, rem) {
716 memset(&parse, 0, sizeof(parse));
717 parse.idx = -1;
718
719 err = nl80211_parse_key_new(key, &parse);
720 if (err)
721 goto error;
722 err = -EINVAL;
723 if (!parse.p.key)
724 goto error;
725 if (parse.idx < 0 || parse.idx > 4)
726 goto error;
727 if (parse.def) {
728 if (def)
729 goto error;
730 def = 1;
731 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100732 if (!parse.def_uni || !parse.def_multi)
733 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200734 } else if (parse.defmgmt)
735 goto error;
736 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200737 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200738 if (err)
739 goto error;
740 result->params[parse.idx].cipher = parse.p.cipher;
741 result->params[parse.idx].key_len = parse.p.key_len;
742 result->params[parse.idx].key = result->data[parse.idx];
743 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530744
745 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
746 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
747 if (no_ht)
748 *no_ht = true;
749 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200750 }
751
752 return result;
753 error:
754 kfree(result);
755 return ERR_PTR(err);
756}
757
758static int nl80211_key_allowed(struct wireless_dev *wdev)
759{
760 ASSERT_WDEV_LOCK(wdev);
761
Johannes Bergfffd0932009-07-08 14:22:54 +0200762 switch (wdev->iftype) {
763 case NL80211_IFTYPE_AP:
764 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200765 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700766 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200767 break;
768 case NL80211_IFTYPE_ADHOC:
769 if (!wdev->current_bss)
770 return -ENOLINK;
771 break;
772 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200773 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200774 if (wdev->sme_state != CFG80211_SME_CONNECTED)
775 return -ENOLINK;
776 break;
777 default:
778 return -EINVAL;
779 }
780
781 return 0;
782}
783
Johannes Berg7527a782011-05-13 10:58:57 +0200784static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
785{
786 struct nlattr *nl_modes = nla_nest_start(msg, attr);
787 int i;
788
789 if (!nl_modes)
790 goto nla_put_failure;
791
792 i = 0;
793 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400794 if ((ifmodes & 1) && nla_put_flag(msg, i))
795 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200796 ifmodes >>= 1;
797 i++;
798 }
799
800 nla_nest_end(msg, nl_modes);
801 return 0;
802
803nla_put_failure:
804 return -ENOBUFS;
805}
806
807static int nl80211_put_iface_combinations(struct wiphy *wiphy,
808 struct sk_buff *msg)
809{
810 struct nlattr *nl_combis;
811 int i, j;
812
813 nl_combis = nla_nest_start(msg,
814 NL80211_ATTR_INTERFACE_COMBINATIONS);
815 if (!nl_combis)
816 goto nla_put_failure;
817
818 for (i = 0; i < wiphy->n_iface_combinations; i++) {
819 const struct ieee80211_iface_combination *c;
820 struct nlattr *nl_combi, *nl_limits;
821
822 c = &wiphy->iface_combinations[i];
823
824 nl_combi = nla_nest_start(msg, i + 1);
825 if (!nl_combi)
826 goto nla_put_failure;
827
828 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
829 if (!nl_limits)
830 goto nla_put_failure;
831
832 for (j = 0; j < c->n_limits; j++) {
833 struct nlattr *nl_limit;
834
835 nl_limit = nla_nest_start(msg, j + 1);
836 if (!nl_limit)
837 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400838 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
839 c->limits[j].max))
840 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200841 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
842 c->limits[j].types))
843 goto nla_put_failure;
844 nla_nest_end(msg, nl_limit);
845 }
846
847 nla_nest_end(msg, nl_limits);
848
David S. Miller9360ffd2012-03-29 04:41:26 -0400849 if (c->beacon_int_infra_match &&
850 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
851 goto nla_put_failure;
852 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
853 c->num_different_channels) ||
854 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
855 c->max_interfaces))
856 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200857
858 nla_nest_end(msg, nl_combi);
859 }
860
861 nla_nest_end(msg, nl_combis);
862
863 return 0;
864nla_put_failure:
865 return -ENOBUFS;
866}
867
Eric W. Biederman15e47302012-09-07 20:12:54 +0000868static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400869 struct cfg80211_registered_device *dev)
870{
871 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100872 struct nlattr *nl_bands, *nl_band;
873 struct nlattr *nl_freqs, *nl_freq;
874 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100875 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100876 enum ieee80211_band band;
877 struct ieee80211_channel *chan;
878 struct ieee80211_rate *rate;
879 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200880 const struct ieee80211_txrx_stypes *mgmt_stypes =
881 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400882
Eric W. Biederman15e47302012-09-07 20:12:54 +0000883 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400884 if (!hdr)
885 return -1;
886
David S. Miller9360ffd2012-03-29 04:41:26 -0400887 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
888 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
889 nla_put_u32(msg, NL80211_ATTR_GENERATION,
890 cfg80211_rdev_list_generation) ||
891 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
892 dev->wiphy.retry_short) ||
893 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
894 dev->wiphy.retry_long) ||
895 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
896 dev->wiphy.frag_threshold) ||
897 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
898 dev->wiphy.rts_threshold) ||
899 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
900 dev->wiphy.coverage_class) ||
901 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
902 dev->wiphy.max_scan_ssids) ||
903 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
904 dev->wiphy.max_sched_scan_ssids) ||
905 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
906 dev->wiphy.max_scan_ie_len) ||
907 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
908 dev->wiphy.max_sched_scan_ie_len) ||
909 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
910 dev->wiphy.max_match_sets))
911 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200912
David S. Miller9360ffd2012-03-29 04:41:26 -0400913 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
914 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
915 goto nla_put_failure;
916 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
917 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
918 goto nla_put_failure;
919 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
920 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
921 goto nla_put_failure;
922 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
923 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
924 goto nla_put_failure;
925 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
926 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
927 goto nla_put_failure;
928 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
929 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
930 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200931
David S. Miller9360ffd2012-03-29 04:41:26 -0400932 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
933 sizeof(u32) * dev->wiphy.n_cipher_suites,
934 dev->wiphy.cipher_suites))
935 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100936
David S. Miller9360ffd2012-03-29 04:41:26 -0400937 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
938 dev->wiphy.max_num_pmkids))
939 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530940
David S. Miller9360ffd2012-03-29 04:41:26 -0400941 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
942 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
943 goto nla_put_failure;
Johannes Berg25e47c182009-04-02 20:14:06 +0200944
David S. Miller9360ffd2012-03-29 04:41:26 -0400945 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
946 dev->wiphy.available_antennas_tx) ||
947 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
948 dev->wiphy.available_antennas_rx))
949 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100950
David S. Miller9360ffd2012-03-29 04:41:26 -0400951 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
952 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
953 dev->wiphy.probe_resp_offload))
954 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200955
Bruno Randolf7f531e02010-12-16 11:30:22 +0900956 if ((dev->wiphy.available_antennas_tx ||
957 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900958 u32 tx_ant = 0, rx_ant = 0;
959 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300960 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900961 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400962 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
963 tx_ant) ||
964 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
965 rx_ant))
966 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900967 }
968 }
969
Johannes Berg7527a782011-05-13 10:58:57 +0200970 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
971 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700972 goto nla_put_failure;
973
Johannes Bergee688b002008-01-24 19:38:39 +0100974 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
975 if (!nl_bands)
976 goto nla_put_failure;
977
978 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
979 if (!dev->wiphy.bands[band])
980 continue;
981
982 nl_band = nla_nest_start(msg, band);
983 if (!nl_band)
984 goto nla_put_failure;
985
Johannes Bergd51626d2008-10-09 12:20:13 +0200986 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400987 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
988 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
989 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
990 &dev->wiphy.bands[band]->ht_cap.mcs) ||
991 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
992 dev->wiphy.bands[band]->ht_cap.cap) ||
993 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
994 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
995 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
996 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
997 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200998
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000999 /* add VHT info */
1000 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1001 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1002 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1003 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1004 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1005 dev->wiphy.bands[band]->vht_cap.cap)))
1006 goto nla_put_failure;
1007
Johannes Bergee688b002008-01-24 19:38:39 +01001008 /* add frequencies */
1009 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1010 if (!nl_freqs)
1011 goto nla_put_failure;
1012
1013 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1014 nl_freq = nla_nest_start(msg, i);
1015 if (!nl_freq)
1016 goto nla_put_failure;
1017
1018 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001019
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001020 if (nl80211_msg_put_channel(msg, chan))
1021 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001022
Johannes Bergee688b002008-01-24 19:38:39 +01001023 nla_nest_end(msg, nl_freq);
1024 }
1025
1026 nla_nest_end(msg, nl_freqs);
1027
1028 /* add bitrates */
1029 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1030 if (!nl_rates)
1031 goto nla_put_failure;
1032
1033 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1034 nl_rate = nla_nest_start(msg, i);
1035 if (!nl_rate)
1036 goto nla_put_failure;
1037
1038 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001039 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1040 rate->bitrate))
1041 goto nla_put_failure;
1042 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1043 nla_put_flag(msg,
1044 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1045 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001046
1047 nla_nest_end(msg, nl_rate);
1048 }
1049
1050 nla_nest_end(msg, nl_rates);
1051
1052 nla_nest_end(msg, nl_band);
1053 }
1054 nla_nest_end(msg, nl_bands);
1055
Johannes Berg8fdc6212009-03-14 09:34:01 +01001056 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1057 if (!nl_cmds)
1058 goto nla_put_failure;
1059
1060 i = 0;
1061#define CMD(op, n) \
1062 do { \
1063 if (dev->ops->op) { \
1064 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001065 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1066 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001067 } \
1068 } while (0)
1069
1070 CMD(add_virtual_intf, NEW_INTERFACE);
1071 CMD(change_virtual_intf, SET_INTERFACE);
1072 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001073 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001074 CMD(add_station, NEW_STATION);
1075 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001076 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001077 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001078 CMD(auth, AUTHENTICATE);
1079 CMD(assoc, ASSOCIATE);
1080 CMD(deauth, DEAUTHENTICATE);
1081 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001082 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001083 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001084 CMD(set_pmksa, SET_PMKSA);
1085 CMD(del_pmksa, DEL_PMKSA);
1086 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001087 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1088 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001089 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001090 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001091 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001092 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001093 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001094 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1095 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001096 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001097 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001098 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001099 i++;
1100 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1101 goto nla_put_failure;
1102 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001103 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001104 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1105 CMD(tdls_mgmt, TDLS_MGMT);
1106 CMD(tdls_oper, TDLS_OPER);
1107 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001108 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1109 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001110 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001111 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001112 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1113 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001114 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1115 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001116 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001117 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001118 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001119
Kalle Valo4745fc02011-11-17 19:06:10 +02001120#ifdef CONFIG_NL80211_TESTMODE
1121 CMD(testmode_cmd, TESTMODE);
1122#endif
1123
Johannes Berg8fdc6212009-03-14 09:34:01 +01001124#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001125
Johannes Berg6829c8782009-07-02 09:13:27 +02001126 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001127 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001128 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1129 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001130 }
1131
Johannes Berg6829c8782009-07-02 09:13:27 +02001132 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001133 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001134 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1135 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001136 }
1137
Johannes Berg8fdc6212009-03-14 09:34:01 +01001138 nla_nest_end(msg, nl_cmds);
1139
Johannes Berg7c4ef712011-11-18 15:33:48 +01001140 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001141 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1142 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1143 dev->wiphy.max_remain_on_channel_duration))
1144 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001145
David S. Miller9360ffd2012-03-29 04:41:26 -04001146 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1147 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1148 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001149
Johannes Berg2e161f72010-08-12 15:38:38 +02001150 if (mgmt_stypes) {
1151 u16 stypes;
1152 struct nlattr *nl_ftypes, *nl_ifs;
1153 enum nl80211_iftype ift;
1154
1155 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1156 if (!nl_ifs)
1157 goto nla_put_failure;
1158
1159 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1160 nl_ftypes = nla_nest_start(msg, ift);
1161 if (!nl_ftypes)
1162 goto nla_put_failure;
1163 i = 0;
1164 stypes = mgmt_stypes[ift].tx;
1165 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001166 if ((stypes & 1) &&
1167 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1168 (i << 4) | IEEE80211_FTYPE_MGMT))
1169 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001170 stypes >>= 1;
1171 i++;
1172 }
1173 nla_nest_end(msg, nl_ftypes);
1174 }
1175
Johannes Berg74b70a42010-08-24 12:15:53 +02001176 nla_nest_end(msg, nl_ifs);
1177
Johannes Berg2e161f72010-08-12 15:38:38 +02001178 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1179 if (!nl_ifs)
1180 goto nla_put_failure;
1181
1182 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1183 nl_ftypes = nla_nest_start(msg, ift);
1184 if (!nl_ftypes)
1185 goto nla_put_failure;
1186 i = 0;
1187 stypes = mgmt_stypes[ift].rx;
1188 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001189 if ((stypes & 1) &&
1190 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1191 (i << 4) | IEEE80211_FTYPE_MGMT))
1192 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001193 stypes >>= 1;
1194 i++;
1195 }
1196 nla_nest_end(msg, nl_ftypes);
1197 }
1198 nla_nest_end(msg, nl_ifs);
1199 }
1200
Johannes Bergdfb89c52012-06-27 09:23:48 +02001201#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001202 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1203 struct nlattr *nl_wowlan;
1204
1205 nl_wowlan = nla_nest_start(msg,
1206 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1207 if (!nl_wowlan)
1208 goto nla_put_failure;
1209
David S. Miller9360ffd2012-03-29 04:41:26 -04001210 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1211 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1212 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1213 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1214 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1215 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1216 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1217 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1218 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1219 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1220 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1221 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1222 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1223 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1224 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1226 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001227 if (dev->wiphy.wowlan.n_patterns) {
1228 struct nl80211_wowlan_pattern_support pat = {
1229 .max_patterns = dev->wiphy.wowlan.n_patterns,
1230 .min_pattern_len =
1231 dev->wiphy.wowlan.pattern_min_len,
1232 .max_pattern_len =
1233 dev->wiphy.wowlan.pattern_max_len,
1234 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001235 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1236 sizeof(pat), &pat))
1237 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001238 }
1239
1240 nla_nest_end(msg, nl_wowlan);
1241 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001242#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001243
Johannes Berg7527a782011-05-13 10:58:57 +02001244 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1245 dev->wiphy.software_iftypes))
1246 goto nla_put_failure;
1247
1248 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1249 goto nla_put_failure;
1250
David S. Miller9360ffd2012-03-29 04:41:26 -04001251 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1252 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1253 dev->wiphy.ap_sme_capa))
1254 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001255
David S. Miller9360ffd2012-03-29 04:41:26 -04001256 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1257 dev->wiphy.features))
1258 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001259
David S. Miller9360ffd2012-03-29 04:41:26 -04001260 if (dev->wiphy.ht_capa_mod_mask &&
1261 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1262 sizeof(*dev->wiphy.ht_capa_mod_mask),
1263 dev->wiphy.ht_capa_mod_mask))
1264 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001265
Johannes Berg55682962007-09-20 13:09:35 -04001266 return genlmsg_end(msg, hdr);
1267
1268 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001269 genlmsg_cancel(msg, hdr);
1270 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001271}
1272
1273static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1274{
1275 int idx = 0;
1276 int start = cb->args[0];
1277 struct cfg80211_registered_device *dev;
1278
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001279 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001280 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001281 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1282 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001283 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001284 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001285 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001286 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001287 dev) < 0) {
1288 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001289 break;
Julius Volzb4637272008-07-08 14:02:19 +02001290 }
Johannes Berg55682962007-09-20 13:09:35 -04001291 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001292 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001293
1294 cb->args[0] = idx;
1295
1296 return skb->len;
1297}
1298
1299static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1300{
1301 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001302 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001303
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001305 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001306 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001307
Eric W. Biederman15e47302012-09-07 20:12:54 +00001308 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001309 nlmsg_free(msg);
1310 return -ENOBUFS;
1311 }
Johannes Berg55682962007-09-20 13:09:35 -04001312
Johannes Berg134e6372009-07-10 09:51:34 +00001313 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001314}
1315
Jouni Malinen31888482008-10-30 16:59:24 +02001316static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1317 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1318 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1319 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1320 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1321 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1322};
1323
1324static int parse_txq_params(struct nlattr *tb[],
1325 struct ieee80211_txq_params *txq_params)
1326{
Johannes Berga3304b02012-03-28 11:04:24 +02001327 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001328 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1329 !tb[NL80211_TXQ_ATTR_AIFS])
1330 return -EINVAL;
1331
Johannes Berga3304b02012-03-28 11:04:24 +02001332 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001333 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1334 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1335 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1336 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1337
Johannes Berga3304b02012-03-28 11:04:24 +02001338 if (txq_params->ac >= NL80211_NUM_ACS)
1339 return -EINVAL;
1340
Jouni Malinen31888482008-10-30 16:59:24 +02001341 return 0;
1342}
1343
Johannes Bergf444de02010-05-05 15:25:02 +02001344static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1345{
1346 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001347 * You can only set the channel explicitly for WDS interfaces,
1348 * all others have their channel managed via their respective
1349 * "establish a connection" command (connect, join, ...)
1350 *
1351 * For AP/GO and mesh mode, the channel can be set with the
1352 * channel userspace API, but is only stored and passed to the
1353 * low-level driver when the AP starts or the mesh is joined.
1354 * This is for backward compatibility, userspace can also give
1355 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001356 *
1357 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001358 * whatever else is going on, so they have their own special
1359 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001360 */
1361 return !wdev ||
1362 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001363 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001364 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1365 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001366}
1367
Johannes Berg683b6d32012-11-08 21:25:48 +01001368static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1369 struct genl_info *info,
1370 struct cfg80211_chan_def *chandef)
1371{
1372 struct ieee80211_sta_ht_cap *ht_cap;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001373 struct ieee80211_sta_vht_cap *vht_cap;
1374 u32 control_freq, width;
Johannes Berg683b6d32012-11-08 21:25:48 +01001375
1376 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1377 return -EINVAL;
1378
1379 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1380
1381 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001382 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1383 chandef->center_freq1 = control_freq;
1384 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001385
1386 /* Primary channel not allowed */
1387 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1388 return -EINVAL;
1389
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001390 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1391 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001392
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001393 chantype = nla_get_u32(
1394 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1395
1396 switch (chantype) {
1397 case NL80211_CHAN_NO_HT:
1398 case NL80211_CHAN_HT20:
1399 case NL80211_CHAN_HT40PLUS:
1400 case NL80211_CHAN_HT40MINUS:
1401 cfg80211_chandef_create(chandef, chandef->chan,
1402 chantype);
1403 break;
1404 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001405 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001406 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001407 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1408 chandef->width =
1409 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1410 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1411 chandef->center_freq1 =
1412 nla_get_u32(
1413 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1414 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1415 chandef->center_freq2 =
1416 nla_get_u32(
1417 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1418 }
1419
1420 ht_cap = &rdev->wiphy.bands[chandef->chan->band]->ht_cap;
1421 vht_cap = &rdev->wiphy.bands[chandef->chan->band]->vht_cap;
1422
1423 if (!cfg80211_chan_def_valid(chandef))
1424 return -EINVAL;
1425
1426 switch (chandef->width) {
1427 case NL80211_CHAN_WIDTH_20:
1428 if (!ht_cap->ht_supported)
1429 return -EINVAL;
1430 case NL80211_CHAN_WIDTH_20_NOHT:
1431 width = 20;
1432 break;
1433 case NL80211_CHAN_WIDTH_40:
1434 width = 40;
1435 /* quick early regulatory check */
1436 if (chandef->center_freq1 < control_freq &&
1437 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
1438 return -EINVAL;
1439 if (chandef->center_freq1 > control_freq &&
1440 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
1441 return -EINVAL;
1442 if (!ht_cap->ht_supported)
1443 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001444 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
1445 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
1446 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001447 break;
1448 case NL80211_CHAN_WIDTH_80:
1449 width = 80;
1450 if (!vht_cap->vht_supported)
Johannes Berg683b6d32012-11-08 21:25:48 +01001451 return -EINVAL;
1452 break;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001453 case NL80211_CHAN_WIDTH_80P80:
1454 width = 80;
1455 if (!vht_cap->vht_supported)
1456 return -EINVAL;
1457 if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))
1458 return -EINVAL;
1459 break;
1460 case NL80211_CHAN_WIDTH_160:
1461 width = 160;
1462 if (!vht_cap->vht_supported)
1463 return -EINVAL;
1464 if (!(vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ))
1465 return -EINVAL;
1466 break;
1467 default:
1468 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001469 }
1470
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001471 if (!cfg80211_secondary_chans_ok(&rdev->wiphy, chandef->center_freq1,
1472 width, IEEE80211_CHAN_DISABLED))
1473 return -EINVAL;
1474 if (chandef->center_freq2 &&
1475 !cfg80211_secondary_chans_ok(&rdev->wiphy, chandef->center_freq2,
1476 width, IEEE80211_CHAN_DISABLED))
1477 return -EINVAL;
1478
1479 /* TODO: missing regulatory check on bandwidth */
1480
Johannes Berg683b6d32012-11-08 21:25:48 +01001481 return 0;
1482}
1483
Johannes Bergf444de02010-05-05 15:25:02 +02001484static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1485 struct wireless_dev *wdev,
1486 struct genl_info *info)
1487{
Johannes Berg683b6d32012-11-08 21:25:48 +01001488 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001489 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001490 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1491
1492 if (wdev)
1493 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001494
Johannes Bergf444de02010-05-05 15:25:02 +02001495 if (!nl80211_can_set_dev_channel(wdev))
1496 return -EOPNOTSUPP;
1497
Johannes Berg683b6d32012-11-08 21:25:48 +01001498 result = nl80211_parse_chandef(rdev, info, &chandef);
1499 if (result)
1500 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001501
1502 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001503 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001504 case NL80211_IFTYPE_AP:
1505 case NL80211_IFTYPE_P2P_GO:
1506 if (wdev->beacon_interval) {
1507 result = -EBUSY;
1508 break;
1509 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001510 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001511 result = -EINVAL;
1512 break;
1513 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001514 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001515 result = 0;
1516 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001517 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001518 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001519 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001520 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001521 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001522 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001523 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001524 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001525 }
1526 mutex_unlock(&rdev->devlist_mtx);
1527
1528 return result;
1529}
1530
1531static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1532{
Johannes Berg4c476992010-10-04 21:36:35 +02001533 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1534 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001535
Johannes Berg4c476992010-10-04 21:36:35 +02001536 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001537}
1538
Bill Jordane8347eb2010-10-01 13:54:28 -04001539static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1540{
Johannes Berg43b19952010-10-07 13:10:30 +02001541 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1542 struct net_device *dev = info->user_ptr[1];
1543 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001544 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001545
1546 if (!info->attrs[NL80211_ATTR_MAC])
1547 return -EINVAL;
1548
Johannes Berg43b19952010-10-07 13:10:30 +02001549 if (netif_running(dev))
1550 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001551
Johannes Berg43b19952010-10-07 13:10:30 +02001552 if (!rdev->ops->set_wds_peer)
1553 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001554
Johannes Berg43b19952010-10-07 13:10:30 +02001555 if (wdev->iftype != NL80211_IFTYPE_WDS)
1556 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001557
1558 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001559 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001560}
1561
1562
Johannes Berg55682962007-09-20 13:09:35 -04001563static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1564{
1565 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001566 struct net_device *netdev = NULL;
1567 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001568 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001569 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001570 u32 changed;
1571 u8 retry_short = 0, retry_long = 0;
1572 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001573 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001574
Johannes Bergf444de02010-05-05 15:25:02 +02001575 /*
1576 * Try to find the wiphy and netdev. Normally this
1577 * function shouldn't need the netdev, but this is
1578 * done for backward compatibility -- previously
1579 * setting the channel was done per wiphy, but now
1580 * it is per netdev. Previous userland like hostapd
1581 * also passed a netdev to set_wiphy, so that it is
1582 * possible to let that go to the right netdev!
1583 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001584 mutex_lock(&cfg80211_mutex);
1585
Johannes Bergf444de02010-05-05 15:25:02 +02001586 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1587 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1588
1589 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1590 if (netdev && netdev->ieee80211_ptr) {
1591 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1592 mutex_lock(&rdev->mtx);
1593 } else
1594 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001595 }
1596
Johannes Bergf444de02010-05-05 15:25:02 +02001597 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001598 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1599 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001600 if (IS_ERR(rdev)) {
1601 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001602 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001603 }
1604 wdev = NULL;
1605 netdev = NULL;
1606 result = 0;
1607
1608 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001609 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001610 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001611
1612 /*
1613 * end workaround code, by now the rdev is available
1614 * and locked, and wdev may or may not be NULL.
1615 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001616
1617 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001618 result = cfg80211_dev_rename(
1619 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001620
1621 mutex_unlock(&cfg80211_mutex);
1622
1623 if (result)
1624 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001625
Jouni Malinen31888482008-10-30 16:59:24 +02001626 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1627 struct ieee80211_txq_params txq_params;
1628 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1629
1630 if (!rdev->ops->set_txq_params) {
1631 result = -EOPNOTSUPP;
1632 goto bad_res;
1633 }
1634
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001635 if (!netdev) {
1636 result = -EINVAL;
1637 goto bad_res;
1638 }
1639
Johannes Berg133a3ff2011-11-03 14:50:13 +01001640 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1641 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1642 result = -EINVAL;
1643 goto bad_res;
1644 }
1645
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001646 if (!netif_running(netdev)) {
1647 result = -ENETDOWN;
1648 goto bad_res;
1649 }
1650
Jouni Malinen31888482008-10-30 16:59:24 +02001651 nla_for_each_nested(nl_txq_params,
1652 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1653 rem_txq_params) {
1654 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1655 nla_data(nl_txq_params),
1656 nla_len(nl_txq_params),
1657 txq_params_policy);
1658 result = parse_txq_params(tb, &txq_params);
1659 if (result)
1660 goto bad_res;
1661
Hila Gonene35e4d22012-06-27 17:19:42 +03001662 result = rdev_set_txq_params(rdev, netdev,
1663 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001664 if (result)
1665 goto bad_res;
1666 }
1667 }
1668
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001669 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001670 result = __nl80211_set_channel(rdev,
1671 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1672 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001673 if (result)
1674 goto bad_res;
1675 }
1676
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001677 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001678 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001679 enum nl80211_tx_power_setting type;
1680 int idx, mbm = 0;
1681
Johannes Bergc8442112012-10-24 10:17:18 +02001682 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1683 txp_wdev = NULL;
1684
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001685 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001686 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001687 goto bad_res;
1688 }
1689
1690 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1691 type = nla_get_u32(info->attrs[idx]);
1692
1693 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1694 (type != NL80211_TX_POWER_AUTOMATIC)) {
1695 result = -EINVAL;
1696 goto bad_res;
1697 }
1698
1699 if (type != NL80211_TX_POWER_AUTOMATIC) {
1700 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1701 mbm = nla_get_u32(info->attrs[idx]);
1702 }
1703
Johannes Bergc8442112012-10-24 10:17:18 +02001704 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001705 if (result)
1706 goto bad_res;
1707 }
1708
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001709 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1710 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1711 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001712 if ((!rdev->wiphy.available_antennas_tx &&
1713 !rdev->wiphy.available_antennas_rx) ||
1714 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001715 result = -EOPNOTSUPP;
1716 goto bad_res;
1717 }
1718
1719 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1720 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1721
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001722 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001723 * available antenna masks, except for the "all" mask */
1724 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1725 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001726 result = -EINVAL;
1727 goto bad_res;
1728 }
1729
Bruno Randolf7f531e02010-12-16 11:30:22 +09001730 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1731 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001732
Hila Gonene35e4d22012-06-27 17:19:42 +03001733 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001734 if (result)
1735 goto bad_res;
1736 }
1737
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001738 changed = 0;
1739
1740 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1741 retry_short = nla_get_u8(
1742 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1743 if (retry_short == 0) {
1744 result = -EINVAL;
1745 goto bad_res;
1746 }
1747 changed |= WIPHY_PARAM_RETRY_SHORT;
1748 }
1749
1750 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1751 retry_long = nla_get_u8(
1752 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1753 if (retry_long == 0) {
1754 result = -EINVAL;
1755 goto bad_res;
1756 }
1757 changed |= WIPHY_PARAM_RETRY_LONG;
1758 }
1759
1760 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1761 frag_threshold = nla_get_u32(
1762 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1763 if (frag_threshold < 256) {
1764 result = -EINVAL;
1765 goto bad_res;
1766 }
1767 if (frag_threshold != (u32) -1) {
1768 /*
1769 * Fragments (apart from the last one) are required to
1770 * have even length. Make the fragmentation code
1771 * simpler by stripping LSB should someone try to use
1772 * odd threshold value.
1773 */
1774 frag_threshold &= ~0x1;
1775 }
1776 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1777 }
1778
1779 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1780 rts_threshold = nla_get_u32(
1781 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1782 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1783 }
1784
Lukáš Turek81077e82009-12-21 22:50:47 +01001785 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1786 coverage_class = nla_get_u8(
1787 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1788 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1789 }
1790
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001791 if (changed) {
1792 u8 old_retry_short, old_retry_long;
1793 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001794 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001795
1796 if (!rdev->ops->set_wiphy_params) {
1797 result = -EOPNOTSUPP;
1798 goto bad_res;
1799 }
1800
1801 old_retry_short = rdev->wiphy.retry_short;
1802 old_retry_long = rdev->wiphy.retry_long;
1803 old_frag_threshold = rdev->wiphy.frag_threshold;
1804 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001805 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001806
1807 if (changed & WIPHY_PARAM_RETRY_SHORT)
1808 rdev->wiphy.retry_short = retry_short;
1809 if (changed & WIPHY_PARAM_RETRY_LONG)
1810 rdev->wiphy.retry_long = retry_long;
1811 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1812 rdev->wiphy.frag_threshold = frag_threshold;
1813 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1814 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001815 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1816 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001817
Hila Gonene35e4d22012-06-27 17:19:42 +03001818 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001819 if (result) {
1820 rdev->wiphy.retry_short = old_retry_short;
1821 rdev->wiphy.retry_long = old_retry_long;
1822 rdev->wiphy.frag_threshold = old_frag_threshold;
1823 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001824 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001825 }
1826 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001827
Johannes Berg306d6112008-12-08 12:39:04 +01001828 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001829 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001830 if (netdev)
1831 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001832 return result;
1833}
1834
Johannes Berg71bbc992012-06-15 15:30:18 +02001835static inline u64 wdev_id(struct wireless_dev *wdev)
1836{
1837 return (u64)wdev->identifier |
1838 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1839}
Johannes Berg55682962007-09-20 13:09:35 -04001840
Johannes Berg683b6d32012-11-08 21:25:48 +01001841static int nl80211_send_chandef(struct sk_buff *msg,
1842 struct cfg80211_chan_def *chandef)
1843{
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001844 WARN_ON(!cfg80211_chan_def_valid(chandef));
1845
Johannes Berg683b6d32012-11-08 21:25:48 +01001846 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1847 chandef->chan->center_freq))
1848 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001849 switch (chandef->width) {
1850 case NL80211_CHAN_WIDTH_20_NOHT:
1851 case NL80211_CHAN_WIDTH_20:
1852 case NL80211_CHAN_WIDTH_40:
1853 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1854 cfg80211_get_chandef_type(chandef)))
1855 return -ENOBUFS;
1856 break;
1857 default:
1858 break;
1859 }
1860 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1861 return -ENOBUFS;
1862 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1863 return -ENOBUFS;
1864 if (chandef->center_freq2 &&
1865 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001866 return -ENOBUFS;
1867 return 0;
1868}
1869
Eric W. Biederman15e47302012-09-07 20:12:54 +00001870static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001871 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001872 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001873{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001874 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001875 void *hdr;
1876
Eric W. Biederman15e47302012-09-07 20:12:54 +00001877 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001878 if (!hdr)
1879 return -1;
1880
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001881 if (dev &&
1882 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001883 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001884 goto nla_put_failure;
1885
1886 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1887 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001888 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001889 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001890 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1891 rdev->devlist_generation ^
1892 (cfg80211_rdev_list_generation << 2)))
1893 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001894
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001895 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001896 int ret;
1897 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001898
Johannes Berg683b6d32012-11-08 21:25:48 +01001899 ret = rdev_get_channel(rdev, wdev, &chandef);
1900 if (ret == 0) {
1901 if (nl80211_send_chandef(msg, &chandef))
1902 goto nla_put_failure;
1903 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001904 }
1905
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001906 if (wdev->ssid_len) {
1907 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1908 goto nla_put_failure;
1909 }
1910
Johannes Berg55682962007-09-20 13:09:35 -04001911 return genlmsg_end(msg, hdr);
1912
1913 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001914 genlmsg_cancel(msg, hdr);
1915 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001916}
1917
1918static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1919{
1920 int wp_idx = 0;
1921 int if_idx = 0;
1922 int wp_start = cb->args[0];
1923 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001924 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001925 struct wireless_dev *wdev;
1926
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001927 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001928 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1929 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001930 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001931 if (wp_idx < wp_start) {
1932 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001933 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001934 }
Johannes Berg55682962007-09-20 13:09:35 -04001935 if_idx = 0;
1936
Johannes Bergf5ea9122009-08-07 16:17:38 +02001937 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001938 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001939 if (if_idx < if_start) {
1940 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001941 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001942 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001943 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001944 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001945 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001946 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001947 goto out;
1948 }
1949 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001950 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001951 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001952
1953 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001954 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001955 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001956 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001957
1958 cb->args[0] = wp_idx;
1959 cb->args[1] = if_idx;
1960
1961 return skb->len;
1962}
1963
1964static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1965{
1966 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001967 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001968 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001969
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001970 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001971 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001972 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001973
Eric W. Biederman15e47302012-09-07 20:12:54 +00001974 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001975 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001976 nlmsg_free(msg);
1977 return -ENOBUFS;
1978 }
Johannes Berg55682962007-09-20 13:09:35 -04001979
Johannes Berg134e6372009-07-10 09:51:34 +00001980 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001981}
1982
Michael Wu66f7ac52008-01-31 19:48:22 +01001983static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1984 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1985 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1986 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1987 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1988 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1989};
1990
1991static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1992{
1993 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1994 int flag;
1995
1996 *mntrflags = 0;
1997
1998 if (!nla)
1999 return -EINVAL;
2000
2001 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2002 nla, mntr_flags_policy))
2003 return -EINVAL;
2004
2005 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2006 if (flags[flag])
2007 *mntrflags |= (1<<flag);
2008
2009 return 0;
2010}
2011
Johannes Berg9bc383d2009-11-19 11:55:19 +01002012static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002013 struct net_device *netdev, u8 use_4addr,
2014 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002015{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002016 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002017 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002018 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002019 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002020 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002021
2022 switch (iftype) {
2023 case NL80211_IFTYPE_AP_VLAN:
2024 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2025 return 0;
2026 break;
2027 case NL80211_IFTYPE_STATION:
2028 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2029 return 0;
2030 break;
2031 default:
2032 break;
2033 }
2034
2035 return -EOPNOTSUPP;
2036}
2037
Johannes Berg55682962007-09-20 13:09:35 -04002038static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2039{
Johannes Berg4c476992010-10-04 21:36:35 +02002040 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002041 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002042 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002043 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002044 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002045 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002046 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002047
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002048 memset(&params, 0, sizeof(params));
2049
Johannes Berg04a773a2009-04-19 21:24:32 +02002050 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002051
Johannes Berg723b0382008-09-16 20:22:09 +02002052 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002053 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002054 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002055 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002056 if (ntype > NL80211_IFTYPE_MAX)
2057 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002058 }
2059
Johannes Berg92ffe052008-09-16 20:39:36 +02002060 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002061 struct wireless_dev *wdev = dev->ieee80211_ptr;
2062
Johannes Berg4c476992010-10-04 21:36:35 +02002063 if (ntype != NL80211_IFTYPE_MESH_POINT)
2064 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002065 if (netif_running(dev))
2066 return -EBUSY;
2067
2068 wdev_lock(wdev);
2069 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2070 IEEE80211_MAX_MESH_ID_LEN);
2071 wdev->mesh_id_up_len =
2072 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2073 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2074 wdev->mesh_id_up_len);
2075 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002076 }
2077
Felix Fietkau8b787642009-11-10 18:53:10 +01002078 if (info->attrs[NL80211_ATTR_4ADDR]) {
2079 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2080 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002081 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002082 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002083 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002084 } else {
2085 params.use_4addr = -1;
2086 }
2087
Johannes Berg92ffe052008-09-16 20:39:36 +02002088 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002089 if (ntype != NL80211_IFTYPE_MONITOR)
2090 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002091 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2092 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002093 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002094 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002095
2096 flags = &_flags;
2097 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002098 }
Johannes Berg3b858752009-03-12 09:55:09 +01002099
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002100 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002101 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002102 else
2103 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002104
Johannes Berg9bc383d2009-11-19 11:55:19 +01002105 if (!err && params.use_4addr != -1)
2106 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2107
Johannes Berg55682962007-09-20 13:09:35 -04002108 return err;
2109}
2110
2111static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2112{
Johannes Berg4c476992010-10-04 21:36:35 +02002113 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002114 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002115 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002116 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002117 int err;
2118 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002119 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002120
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002121 memset(&params, 0, sizeof(params));
2122
Johannes Berg55682962007-09-20 13:09:35 -04002123 if (!info->attrs[NL80211_ATTR_IFNAME])
2124 return -EINVAL;
2125
2126 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2127 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2128 if (type > NL80211_IFTYPE_MAX)
2129 return -EINVAL;
2130 }
2131
Johannes Berg79c97e92009-07-07 03:56:12 +02002132 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002133 !(rdev->wiphy.interface_modes & (1 << type)))
2134 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002135
Johannes Berg9bc383d2009-11-19 11:55:19 +01002136 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002137 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002138 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002139 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002140 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002141 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002142
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002143 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2144 if (!msg)
2145 return -ENOMEM;
2146
Michael Wu66f7ac52008-01-31 19:48:22 +01002147 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2148 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2149 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002150 wdev = rdev_add_virtual_intf(rdev,
2151 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2152 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002153 if (IS_ERR(wdev)) {
2154 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002155 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002156 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002157
Johannes Berg98104fde2012-06-16 00:19:54 +02002158 switch (type) {
2159 case NL80211_IFTYPE_MESH_POINT:
2160 if (!info->attrs[NL80211_ATTR_MESH_ID])
2161 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002162 wdev_lock(wdev);
2163 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2164 IEEE80211_MAX_MESH_ID_LEN);
2165 wdev->mesh_id_up_len =
2166 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2167 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2168 wdev->mesh_id_up_len);
2169 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002170 break;
2171 case NL80211_IFTYPE_P2P_DEVICE:
2172 /*
2173 * P2P Device doesn't have a netdev, so doesn't go
2174 * through the netdev notifier and must be added here
2175 */
2176 mutex_init(&wdev->mtx);
2177 INIT_LIST_HEAD(&wdev->event_list);
2178 spin_lock_init(&wdev->event_lock);
2179 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2180 spin_lock_init(&wdev->mgmt_registrations_lock);
2181
2182 mutex_lock(&rdev->devlist_mtx);
2183 wdev->identifier = ++rdev->wdev_id;
2184 list_add_rcu(&wdev->list, &rdev->wdev_list);
2185 rdev->devlist_generation++;
2186 mutex_unlock(&rdev->devlist_mtx);
2187 break;
2188 default:
2189 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002190 }
2191
Eric W. Biederman15e47302012-09-07 20:12:54 +00002192 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002193 rdev, wdev) < 0) {
2194 nlmsg_free(msg);
2195 return -ENOBUFS;
2196 }
2197
2198 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002199}
2200
2201static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2202{
Johannes Berg4c476992010-10-04 21:36:35 +02002203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002204 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002205
Johannes Berg4c476992010-10-04 21:36:35 +02002206 if (!rdev->ops->del_virtual_intf)
2207 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002208
Johannes Berg84efbb82012-06-16 00:00:26 +02002209 /*
2210 * If we remove a wireless device without a netdev then clear
2211 * user_ptr[1] so that nl80211_post_doit won't dereference it
2212 * to check if it needs to do dev_put(). Otherwise it crashes
2213 * since the wdev has been freed, unlike with a netdev where
2214 * we need the dev_put() for the netdev to really be freed.
2215 */
2216 if (!wdev->netdev)
2217 info->user_ptr[1] = NULL;
2218
Hila Gonene35e4d22012-06-27 17:19:42 +03002219 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002220}
2221
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002222static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2223{
2224 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2225 struct net_device *dev = info->user_ptr[1];
2226 u16 noack_map;
2227
2228 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2229 return -EINVAL;
2230
2231 if (!rdev->ops->set_noack_map)
2232 return -EOPNOTSUPP;
2233
2234 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2235
Hila Gonene35e4d22012-06-27 17:19:42 +03002236 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002237}
2238
Johannes Berg41ade002007-12-19 02:03:29 +01002239struct get_key_cookie {
2240 struct sk_buff *msg;
2241 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002242 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002243};
2244
2245static void get_key_callback(void *c, struct key_params *params)
2246{
Johannes Bergb9454e82009-07-08 13:29:08 +02002247 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002248 struct get_key_cookie *cookie = c;
2249
David S. Miller9360ffd2012-03-29 04:41:26 -04002250 if ((params->key &&
2251 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2252 params->key_len, params->key)) ||
2253 (params->seq &&
2254 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2255 params->seq_len, params->seq)) ||
2256 (params->cipher &&
2257 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2258 params->cipher)))
2259 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002260
Johannes Bergb9454e82009-07-08 13:29:08 +02002261 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2262 if (!key)
2263 goto nla_put_failure;
2264
David S. Miller9360ffd2012-03-29 04:41:26 -04002265 if ((params->key &&
2266 nla_put(cookie->msg, NL80211_KEY_DATA,
2267 params->key_len, params->key)) ||
2268 (params->seq &&
2269 nla_put(cookie->msg, NL80211_KEY_SEQ,
2270 params->seq_len, params->seq)) ||
2271 (params->cipher &&
2272 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2273 params->cipher)))
2274 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002275
David S. Miller9360ffd2012-03-29 04:41:26 -04002276 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2277 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002278
2279 nla_nest_end(cookie->msg, key);
2280
Johannes Berg41ade002007-12-19 02:03:29 +01002281 return;
2282 nla_put_failure:
2283 cookie->error = 1;
2284}
2285
2286static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2287{
Johannes Berg4c476992010-10-04 21:36:35 +02002288 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002289 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002290 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002291 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002292 const u8 *mac_addr = NULL;
2293 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002294 struct get_key_cookie cookie = {
2295 .error = 0,
2296 };
2297 void *hdr;
2298 struct sk_buff *msg;
2299
2300 if (info->attrs[NL80211_ATTR_KEY_IDX])
2301 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2302
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002303 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002304 return -EINVAL;
2305
2306 if (info->attrs[NL80211_ATTR_MAC])
2307 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2308
Johannes Berge31b8212010-10-05 19:39:30 +02002309 pairwise = !!mac_addr;
2310 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2311 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2312 if (kt >= NUM_NL80211_KEYTYPES)
2313 return -EINVAL;
2314 if (kt != NL80211_KEYTYPE_GROUP &&
2315 kt != NL80211_KEYTYPE_PAIRWISE)
2316 return -EINVAL;
2317 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2318 }
2319
Johannes Berg4c476992010-10-04 21:36:35 +02002320 if (!rdev->ops->get_key)
2321 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002322
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002324 if (!msg)
2325 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002326
Eric W. Biederman15e47302012-09-07 20:12:54 +00002327 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002328 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002329 if (IS_ERR(hdr))
2330 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002331
2332 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002333 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002334
David S. Miller9360ffd2012-03-29 04:41:26 -04002335 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2336 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2337 goto nla_put_failure;
2338 if (mac_addr &&
2339 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2340 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002341
Johannes Berge31b8212010-10-05 19:39:30 +02002342 if (pairwise && mac_addr &&
2343 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2344 return -ENOENT;
2345
Hila Gonene35e4d22012-06-27 17:19:42 +03002346 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2347 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002348
2349 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002350 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002351
2352 if (cookie.error)
2353 goto nla_put_failure;
2354
2355 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002356 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002357
2358 nla_put_failure:
2359 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002360 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002361 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002362 return err;
2363}
2364
2365static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2366{
Johannes Berg4c476992010-10-04 21:36:35 +02002367 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002368 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002369 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002370 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002371
Johannes Bergb9454e82009-07-08 13:29:08 +02002372 err = nl80211_parse_key(info, &key);
2373 if (err)
2374 return err;
2375
2376 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002377 return -EINVAL;
2378
Johannes Bergb9454e82009-07-08 13:29:08 +02002379 /* only support setting default key */
2380 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002381 return -EINVAL;
2382
Johannes Bergfffd0932009-07-08 14:22:54 +02002383 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002384
2385 if (key.def) {
2386 if (!rdev->ops->set_default_key) {
2387 err = -EOPNOTSUPP;
2388 goto out;
2389 }
2390
2391 err = nl80211_key_allowed(dev->ieee80211_ptr);
2392 if (err)
2393 goto out;
2394
Hila Gonene35e4d22012-06-27 17:19:42 +03002395 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002396 key.def_uni, key.def_multi);
2397
2398 if (err)
2399 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002400
Johannes Berg3d23e342009-09-29 23:27:28 +02002401#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002402 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002403#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002404 } else {
2405 if (key.def_uni || !key.def_multi) {
2406 err = -EINVAL;
2407 goto out;
2408 }
2409
2410 if (!rdev->ops->set_default_mgmt_key) {
2411 err = -EOPNOTSUPP;
2412 goto out;
2413 }
2414
2415 err = nl80211_key_allowed(dev->ieee80211_ptr);
2416 if (err)
2417 goto out;
2418
Hila Gonene35e4d22012-06-27 17:19:42 +03002419 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002420 if (err)
2421 goto out;
2422
2423#ifdef CONFIG_CFG80211_WEXT
2424 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2425#endif
2426 }
2427
2428 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002429 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002430
Johannes Berg41ade002007-12-19 02:03:29 +01002431 return err;
2432}
2433
2434static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2435{
Johannes Berg4c476992010-10-04 21:36:35 +02002436 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002437 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002438 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002439 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002440 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002441
Johannes Bergb9454e82009-07-08 13:29:08 +02002442 err = nl80211_parse_key(info, &key);
2443 if (err)
2444 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002445
Johannes Bergb9454e82009-07-08 13:29:08 +02002446 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002447 return -EINVAL;
2448
Johannes Berg41ade002007-12-19 02:03:29 +01002449 if (info->attrs[NL80211_ATTR_MAC])
2450 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2451
Johannes Berge31b8212010-10-05 19:39:30 +02002452 if (key.type == -1) {
2453 if (mac_addr)
2454 key.type = NL80211_KEYTYPE_PAIRWISE;
2455 else
2456 key.type = NL80211_KEYTYPE_GROUP;
2457 }
2458
2459 /* for now */
2460 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2461 key.type != NL80211_KEYTYPE_GROUP)
2462 return -EINVAL;
2463
Johannes Berg4c476992010-10-04 21:36:35 +02002464 if (!rdev->ops->add_key)
2465 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002466
Johannes Berge31b8212010-10-05 19:39:30 +02002467 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2468 key.type == NL80211_KEYTYPE_PAIRWISE,
2469 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002470 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002471
2472 wdev_lock(dev->ieee80211_ptr);
2473 err = nl80211_key_allowed(dev->ieee80211_ptr);
2474 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002475 err = rdev_add_key(rdev, dev, key.idx,
2476 key.type == NL80211_KEYTYPE_PAIRWISE,
2477 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002478 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002479
Johannes Berg41ade002007-12-19 02:03:29 +01002480 return err;
2481}
2482
2483static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2484{
Johannes Berg4c476992010-10-04 21:36:35 +02002485 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002486 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002487 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002488 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002489 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002490
Johannes Bergb9454e82009-07-08 13:29:08 +02002491 err = nl80211_parse_key(info, &key);
2492 if (err)
2493 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002494
2495 if (info->attrs[NL80211_ATTR_MAC])
2496 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2497
Johannes Berge31b8212010-10-05 19:39:30 +02002498 if (key.type == -1) {
2499 if (mac_addr)
2500 key.type = NL80211_KEYTYPE_PAIRWISE;
2501 else
2502 key.type = NL80211_KEYTYPE_GROUP;
2503 }
2504
2505 /* for now */
2506 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2507 key.type != NL80211_KEYTYPE_GROUP)
2508 return -EINVAL;
2509
Johannes Berg4c476992010-10-04 21:36:35 +02002510 if (!rdev->ops->del_key)
2511 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002512
Johannes Bergfffd0932009-07-08 14:22:54 +02002513 wdev_lock(dev->ieee80211_ptr);
2514 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002515
2516 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2517 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2518 err = -ENOENT;
2519
Johannes Bergfffd0932009-07-08 14:22:54 +02002520 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002521 err = rdev_del_key(rdev, dev, key.idx,
2522 key.type == NL80211_KEYTYPE_PAIRWISE,
2523 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002524
Johannes Berg3d23e342009-09-29 23:27:28 +02002525#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002526 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002527 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002528 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002529 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002530 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2531 }
2532#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002533 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002534
Johannes Berg41ade002007-12-19 02:03:29 +01002535 return err;
2536}
2537
Johannes Berg88600202012-02-13 15:17:18 +01002538static int nl80211_parse_beacon(struct genl_info *info,
2539 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002540{
Johannes Berg88600202012-02-13 15:17:18 +01002541 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002542
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002543 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2544 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2545 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2546 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002547 return -EINVAL;
2548
Johannes Berg88600202012-02-13 15:17:18 +01002549 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002550
Johannes Berged1b6cc2007-12-19 02:03:32 +01002551 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002552 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2553 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2554 if (!bcn->head_len)
2555 return -EINVAL;
2556 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002557 }
2558
2559 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002560 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2561 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002562 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002563 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002564 }
2565
Johannes Berg4c476992010-10-04 21:36:35 +02002566 if (!haveinfo)
2567 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002568
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002569 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002570 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2571 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002572 }
2573
2574 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002575 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002576 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002577 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002578 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2579 }
2580
2581 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002582 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002583 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002584 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002585 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2586 }
2587
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002588 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002589 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002590 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002591 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002592 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2593 }
2594
Johannes Berg88600202012-02-13 15:17:18 +01002595 return 0;
2596}
2597
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002598static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2599 struct cfg80211_ap_settings *params)
2600{
2601 struct wireless_dev *wdev;
2602 bool ret = false;
2603
2604 mutex_lock(&rdev->devlist_mtx);
2605
Johannes Berg89a54e42012-06-15 14:33:17 +02002606 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002607 if (wdev->iftype != NL80211_IFTYPE_AP &&
2608 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2609 continue;
2610
Johannes Berg683b6d32012-11-08 21:25:48 +01002611 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002612 continue;
2613
Johannes Berg683b6d32012-11-08 21:25:48 +01002614 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002615 ret = true;
2616 break;
2617 }
2618
2619 mutex_unlock(&rdev->devlist_mtx);
2620
2621 return ret;
2622}
2623
Jouni Malinene39e5b52012-09-30 19:29:39 +03002624static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2625 enum nl80211_auth_type auth_type,
2626 enum nl80211_commands cmd)
2627{
2628 if (auth_type > NL80211_AUTHTYPE_MAX)
2629 return false;
2630
2631 switch (cmd) {
2632 case NL80211_CMD_AUTHENTICATE:
2633 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2634 auth_type == NL80211_AUTHTYPE_SAE)
2635 return false;
2636 return true;
2637 case NL80211_CMD_CONNECT:
2638 case NL80211_CMD_START_AP:
2639 /* SAE not supported yet */
2640 if (auth_type == NL80211_AUTHTYPE_SAE)
2641 return false;
2642 return true;
2643 default:
2644 return false;
2645 }
2646}
2647
Johannes Berg88600202012-02-13 15:17:18 +01002648static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2649{
2650 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2651 struct net_device *dev = info->user_ptr[1];
2652 struct wireless_dev *wdev = dev->ieee80211_ptr;
2653 struct cfg80211_ap_settings params;
2654 int err;
2655
2656 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2657 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2658 return -EOPNOTSUPP;
2659
2660 if (!rdev->ops->start_ap)
2661 return -EOPNOTSUPP;
2662
2663 if (wdev->beacon_interval)
2664 return -EALREADY;
2665
2666 memset(&params, 0, sizeof(params));
2667
2668 /* these are required for START_AP */
2669 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2670 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2671 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2672 return -EINVAL;
2673
2674 err = nl80211_parse_beacon(info, &params.beacon);
2675 if (err)
2676 return err;
2677
2678 params.beacon_interval =
2679 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2680 params.dtim_period =
2681 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2682
2683 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2684 if (err)
2685 return err;
2686
2687 /*
2688 * In theory, some of these attributes should be required here
2689 * but since they were not used when the command was originally
2690 * added, keep them optional for old user space programs to let
2691 * them continue to work with drivers that do not need the
2692 * additional information -- drivers must check!
2693 */
2694 if (info->attrs[NL80211_ATTR_SSID]) {
2695 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2696 params.ssid_len =
2697 nla_len(info->attrs[NL80211_ATTR_SSID]);
2698 if (params.ssid_len == 0 ||
2699 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2700 return -EINVAL;
2701 }
2702
2703 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2704 params.hidden_ssid = nla_get_u32(
2705 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2706 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2707 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2708 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2709 return -EINVAL;
2710 }
2711
2712 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2713
2714 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2715 params.auth_type = nla_get_u32(
2716 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002717 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2718 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002719 return -EINVAL;
2720 } else
2721 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2722
2723 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2724 NL80211_MAX_NR_CIPHER_SUITES);
2725 if (err)
2726 return err;
2727
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302728 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2729 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2730 return -EOPNOTSUPP;
2731 params.inactivity_timeout = nla_get_u16(
2732 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2733 }
2734
Johannes Bergaa430da2012-05-16 23:50:18 +02002735 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002736 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2737 if (err)
2738 return err;
2739 } else if (wdev->preset_chandef.chan) {
2740 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002741 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002742 return -EINVAL;
2743
Johannes Berg683b6d32012-11-08 21:25:48 +01002744 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002745 return -EINVAL;
2746
Michal Kaziore4e32452012-06-29 12:47:08 +02002747 mutex_lock(&rdev->devlist_mtx);
Johannes Berg683b6d32012-11-08 21:25:48 +01002748 err = cfg80211_can_use_chan(rdev, wdev, params.chandef.chan,
Michal Kaziore4e32452012-06-29 12:47:08 +02002749 CHAN_MODE_SHARED);
2750 mutex_unlock(&rdev->devlist_mtx);
2751
2752 if (err)
2753 return err;
2754
Hila Gonene35e4d22012-06-27 17:19:42 +03002755 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002756 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002757 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002758 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002759 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002760 wdev->ssid_len = params.ssid_len;
2761 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002762 }
Johannes Berg56d18932011-05-09 18:41:15 +02002763 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002764}
2765
Johannes Berg88600202012-02-13 15:17:18 +01002766static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2767{
2768 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2769 struct net_device *dev = info->user_ptr[1];
2770 struct wireless_dev *wdev = dev->ieee80211_ptr;
2771 struct cfg80211_beacon_data params;
2772 int err;
2773
2774 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2775 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2776 return -EOPNOTSUPP;
2777
2778 if (!rdev->ops->change_beacon)
2779 return -EOPNOTSUPP;
2780
2781 if (!wdev->beacon_interval)
2782 return -EINVAL;
2783
2784 err = nl80211_parse_beacon(info, &params);
2785 if (err)
2786 return err;
2787
Hila Gonene35e4d22012-06-27 17:19:42 +03002788 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002789}
2790
2791static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002792{
Johannes Berg4c476992010-10-04 21:36:35 +02002793 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2794 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002795
Michal Kazior60771782012-06-29 12:46:56 +02002796 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002797}
2798
Johannes Berg5727ef12007-12-19 02:03:34 +01002799static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2800 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2801 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2802 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002803 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002804 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002805 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002806};
2807
Johannes Bergeccb8e82009-05-11 21:57:56 +03002808static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002809 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002810 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002811{
2812 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002813 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002814 int flag;
2815
Johannes Bergeccb8e82009-05-11 21:57:56 +03002816 /*
2817 * Try parsing the new attribute first so userspace
2818 * can specify both for older kernels.
2819 */
2820 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2821 if (nla) {
2822 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002823
Johannes Bergeccb8e82009-05-11 21:57:56 +03002824 sta_flags = nla_data(nla);
2825 params->sta_flags_mask = sta_flags->mask;
2826 params->sta_flags_set = sta_flags->set;
2827 if ((params->sta_flags_mask |
2828 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2829 return -EINVAL;
2830 return 0;
2831 }
2832
2833 /* if present, parse the old attribute */
2834
2835 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002836 if (!nla)
2837 return 0;
2838
2839 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2840 nla, sta_flags_policy))
2841 return -EINVAL;
2842
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002843 /*
2844 * Only allow certain flags for interface types so that
2845 * other attributes are silently ignored. Remember that
2846 * this is backward compatibility code with old userspace
2847 * and shouldn't be hit in other cases anyway.
2848 */
2849 switch (iftype) {
2850 case NL80211_IFTYPE_AP:
2851 case NL80211_IFTYPE_AP_VLAN:
2852 case NL80211_IFTYPE_P2P_GO:
2853 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2854 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2855 BIT(NL80211_STA_FLAG_WME) |
2856 BIT(NL80211_STA_FLAG_MFP);
2857 break;
2858 case NL80211_IFTYPE_P2P_CLIENT:
2859 case NL80211_IFTYPE_STATION:
2860 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2861 BIT(NL80211_STA_FLAG_TDLS_PEER);
2862 break;
2863 case NL80211_IFTYPE_MESH_POINT:
2864 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2865 BIT(NL80211_STA_FLAG_MFP) |
2866 BIT(NL80211_STA_FLAG_AUTHORIZED);
2867 default:
2868 return -EINVAL;
2869 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002870
Johannes Berg3383b5a2012-05-10 20:14:43 +02002871 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2872 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002873 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002874
Johannes Berg3383b5a2012-05-10 20:14:43 +02002875 /* no longer support new API additions in old API */
2876 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2877 return -EINVAL;
2878 }
2879 }
2880
Johannes Berg5727ef12007-12-19 02:03:34 +01002881 return 0;
2882}
2883
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002884static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2885 int attr)
2886{
2887 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002888 u32 bitrate;
2889 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002890
2891 rate = nla_nest_start(msg, attr);
2892 if (!rate)
2893 goto nla_put_failure;
2894
2895 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2896 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002897 /* report 16-bit bitrate only if we can */
2898 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002899 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002900 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2901 (bitrate_compat > 0 &&
2902 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002903 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2904 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2905 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2906 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2907 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2908 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2909 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002910
2911 nla_nest_end(msg, rate);
2912 return true;
2913
2914nla_put_failure:
2915 return false;
2916}
2917
Eric W. Biederman15e47302012-09-07 20:12:54 +00002918static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002919 int flags,
2920 struct cfg80211_registered_device *rdev,
2921 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002922 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002923{
2924 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002925 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002926
Eric W. Biederman15e47302012-09-07 20:12:54 +00002927 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002928 if (!hdr)
2929 return -1;
2930
David S. Miller9360ffd2012-03-29 04:41:26 -04002931 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2932 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2933 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2934 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002935
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002936 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2937 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002938 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002939 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2940 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2941 sinfo->connected_time))
2942 goto nla_put_failure;
2943 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2944 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2945 sinfo->inactive_time))
2946 goto nla_put_failure;
2947 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2948 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2949 sinfo->rx_bytes))
2950 goto nla_put_failure;
2951 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2952 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2953 sinfo->tx_bytes))
2954 goto nla_put_failure;
2955 if ((sinfo->filled & STATION_INFO_LLID) &&
2956 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2957 goto nla_put_failure;
2958 if ((sinfo->filled & STATION_INFO_PLID) &&
2959 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2960 goto nla_put_failure;
2961 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2962 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2963 sinfo->plink_state))
2964 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002965 switch (rdev->wiphy.signal_type) {
2966 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002967 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2968 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2969 sinfo->signal))
2970 goto nla_put_failure;
2971 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2972 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2973 sinfo->signal_avg))
2974 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002975 break;
2976 default:
2977 break;
2978 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002979 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002980 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2981 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002982 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002983 }
2984 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2985 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2986 NL80211_STA_INFO_RX_BITRATE))
2987 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002988 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002989 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2990 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2991 sinfo->rx_packets))
2992 goto nla_put_failure;
2993 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2994 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2995 sinfo->tx_packets))
2996 goto nla_put_failure;
2997 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2998 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2999 sinfo->tx_retries))
3000 goto nla_put_failure;
3001 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3002 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3003 sinfo->tx_failed))
3004 goto nla_put_failure;
3005 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3006 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3007 sinfo->beacon_loss_count))
3008 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003009 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3010 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3011 if (!bss_param)
3012 goto nla_put_failure;
3013
David S. Miller9360ffd2012-03-29 04:41:26 -04003014 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3015 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3016 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3017 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3018 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3019 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3020 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3021 sinfo->bss_param.dtim_period) ||
3022 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3023 sinfo->bss_param.beacon_interval))
3024 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003025
3026 nla_nest_end(msg, bss_param);
3027 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003028 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3029 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3030 sizeof(struct nl80211_sta_flag_update),
3031 &sinfo->sta_flags))
3032 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003033 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3034 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3035 sinfo->t_offset))
3036 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003037 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003038
David S. Miller9360ffd2012-03-29 04:41:26 -04003039 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3040 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3041 sinfo->assoc_req_ies))
3042 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003043
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003044 return genlmsg_end(msg, hdr);
3045
3046 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003047 genlmsg_cancel(msg, hdr);
3048 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003049}
3050
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003051static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003052 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003053{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003054 struct station_info sinfo;
3055 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003056 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003057 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003058 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003059 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003060
Johannes Berg67748892010-10-04 21:14:06 +02003061 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3062 if (err)
3063 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003064
3065 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003066 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003067 goto out_err;
3068 }
3069
Johannes Bergbba95fe2008-07-29 13:22:51 +02003070 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003071 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003072 err = rdev_dump_station(dev, netdev, sta_idx,
3073 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003074 if (err == -ENOENT)
3075 break;
3076 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003077 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003078
3079 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003080 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003081 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003082 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003083 &sinfo) < 0)
3084 goto out;
3085
3086 sta_idx++;
3087 }
3088
3089
3090 out:
3091 cb->args[1] = sta_idx;
3092 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003093 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003094 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003095
3096 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003097}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003098
Johannes Berg5727ef12007-12-19 02:03:34 +01003099static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3100{
Johannes Berg4c476992010-10-04 21:36:35 +02003101 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3102 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003103 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003104 struct sk_buff *msg;
3105 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003106 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003107
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003108 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003109
3110 if (!info->attrs[NL80211_ATTR_MAC])
3111 return -EINVAL;
3112
3113 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3114
Johannes Berg4c476992010-10-04 21:36:35 +02003115 if (!rdev->ops->get_station)
3116 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003117
Hila Gonene35e4d22012-06-27 17:19:42 +03003118 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003119 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003120 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003121
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003123 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003124 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003125
Eric W. Biederman15e47302012-09-07 20:12:54 +00003126 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003127 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003128 nlmsg_free(msg);
3129 return -ENOBUFS;
3130 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003131
Johannes Berg4c476992010-10-04 21:36:35 +02003132 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003133}
3134
3135/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003136 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003137 */
Johannes Berg80b99892011-11-18 16:23:01 +01003138static struct net_device *get_vlan(struct genl_info *info,
3139 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003140{
Johannes Berg463d0182009-07-14 00:33:35 +02003141 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003142 struct net_device *v;
3143 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003144
Johannes Berg80b99892011-11-18 16:23:01 +01003145 if (!vlanattr)
3146 return NULL;
3147
3148 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3149 if (!v)
3150 return ERR_PTR(-ENODEV);
3151
3152 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3153 ret = -EINVAL;
3154 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003155 }
Johannes Berg80b99892011-11-18 16:23:01 +01003156
3157 if (!netif_running(v)) {
3158 ret = -ENETDOWN;
3159 goto error;
3160 }
3161
3162 return v;
3163 error:
3164 dev_put(v);
3165 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003166}
3167
3168static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3169{
Johannes Berg4c476992010-10-04 21:36:35 +02003170 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003171 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003172 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003173 struct station_parameters params;
3174 u8 *mac_addr = NULL;
3175
3176 memset(&params, 0, sizeof(params));
3177
3178 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003179 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003180
3181 if (info->attrs[NL80211_ATTR_STA_AID])
3182 return -EINVAL;
3183
3184 if (!info->attrs[NL80211_ATTR_MAC])
3185 return -EINVAL;
3186
3187 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3188
3189 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3190 params.supported_rates =
3191 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3192 params.supported_rates_len =
3193 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3194 }
3195
3196 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3197 params.listen_interval =
3198 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3199
Jouni Malinen36aedc92008-08-25 11:58:58 +03003200 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3201 params.ht_capa =
3202 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3203
Johannes Bergbdd90d52011-12-14 12:20:27 +01003204 if (!rdev->ops->change_station)
3205 return -EOPNOTSUPP;
3206
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003207 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003208 return -EINVAL;
3209
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003210 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3211 params.plink_action =
3212 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3213
Javier Cardona9c3990a2011-05-03 16:57:11 -07003214 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3215 params.plink_state =
3216 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3217
Johannes Berga97f4422009-06-18 17:23:43 +02003218 switch (dev->ieee80211_ptr->iftype) {
3219 case NL80211_IFTYPE_AP:
3220 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003221 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003222 /* disallow mesh-specific things */
3223 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003224 return -EINVAL;
3225
3226 /* TDLS can't be set, ... */
3227 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3228 return -EINVAL;
3229 /*
3230 * ... but don't bother the driver with it. This works around
3231 * a hostapd/wpa_supplicant issue -- it always includes the
3232 * TLDS_PEER flag in the mask even for AP mode.
3233 */
3234 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3235
3236 /* accept only the listed bits */
3237 if (params.sta_flags_mask &
3238 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3239 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3240 BIT(NL80211_STA_FLAG_WME) |
3241 BIT(NL80211_STA_FLAG_MFP)))
3242 return -EINVAL;
3243
3244 /* must be last in here for error handling */
3245 params.vlan = get_vlan(info, rdev);
3246 if (IS_ERR(params.vlan))
3247 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003248 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003249 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003250 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003251 /*
3252 * Don't allow userspace to change the TDLS_PEER flag,
3253 * but silently ignore attempts to change it since we
3254 * don't have state here to verify that it doesn't try
3255 * to change the flag.
3256 */
3257 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003258 /* fall through */
3259 case NL80211_IFTYPE_ADHOC:
3260 /* disallow things sta doesn't support */
3261 if (params.plink_action)
3262 return -EINVAL;
3263 if (params.ht_capa)
3264 return -EINVAL;
3265 if (params.listen_interval >= 0)
3266 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003267 /* reject any changes other than AUTHORIZED */
3268 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3269 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003270 break;
3271 case NL80211_IFTYPE_MESH_POINT:
3272 /* disallow things mesh doesn't support */
3273 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003274 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003275 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003276 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003277 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003278 return -EINVAL;
3279 /*
3280 * No special handling for TDLS here -- the userspace
3281 * mesh code doesn't have this bug.
3282 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003283 if (params.sta_flags_mask &
3284 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003285 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003286 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003287 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003288 break;
3289 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003290 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003291 }
3292
Johannes Bergbdd90d52011-12-14 12:20:27 +01003293 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003294
Hila Gonene35e4d22012-06-27 17:19:42 +03003295 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003296
Johannes Berg5727ef12007-12-19 02:03:34 +01003297 if (params.vlan)
3298 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003299
Johannes Berg5727ef12007-12-19 02:03:34 +01003300 return err;
3301}
3302
Eliad Pellerc75786c2011-08-23 14:37:46 +03003303static struct nla_policy
3304nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3305 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3306 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3307};
3308
Johannes Berg5727ef12007-12-19 02:03:34 +01003309static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3310{
Johannes Berg4c476992010-10-04 21:36:35 +02003311 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003312 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003313 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003314 struct station_parameters params;
3315 u8 *mac_addr = NULL;
3316
3317 memset(&params, 0, sizeof(params));
3318
3319 if (!info->attrs[NL80211_ATTR_MAC])
3320 return -EINVAL;
3321
Johannes Berg5727ef12007-12-19 02:03:34 +01003322 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3323 return -EINVAL;
3324
3325 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3326 return -EINVAL;
3327
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003328 if (!info->attrs[NL80211_ATTR_STA_AID])
3329 return -EINVAL;
3330
Johannes Berg5727ef12007-12-19 02:03:34 +01003331 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3332 params.supported_rates =
3333 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3334 params.supported_rates_len =
3335 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3336 params.listen_interval =
3337 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003338
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003339 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3340 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3341 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003342
Jouni Malinen36aedc92008-08-25 11:58:58 +03003343 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3344 params.ht_capa =
3345 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003346
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003347 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3348 params.vht_capa =
3349 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3350
Javier Cardona96b78df2011-04-07 15:08:33 -07003351 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3352 params.plink_action =
3353 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3354
Johannes Bergbdd90d52011-12-14 12:20:27 +01003355 if (!rdev->ops->add_station)
3356 return -EOPNOTSUPP;
3357
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003358 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003359 return -EINVAL;
3360
Johannes Bergbdd90d52011-12-14 12:20:27 +01003361 switch (dev->ieee80211_ptr->iftype) {
3362 case NL80211_IFTYPE_AP:
3363 case NL80211_IFTYPE_AP_VLAN:
3364 case NL80211_IFTYPE_P2P_GO:
3365 /* parse WME attributes if sta is WME capable */
3366 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3367 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3368 info->attrs[NL80211_ATTR_STA_WME]) {
3369 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3370 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003371
Johannes Bergbdd90d52011-12-14 12:20:27 +01003372 nla = info->attrs[NL80211_ATTR_STA_WME];
3373 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3374 nl80211_sta_wme_policy);
3375 if (err)
3376 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003377
Johannes Bergbdd90d52011-12-14 12:20:27 +01003378 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3379 params.uapsd_queues =
3380 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3381 if (params.uapsd_queues &
3382 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3383 return -EINVAL;
3384
3385 if (tb[NL80211_STA_WME_MAX_SP])
3386 params.max_sp =
3387 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3388
3389 if (params.max_sp &
3390 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3391 return -EINVAL;
3392
3393 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3394 }
3395 /* TDLS peers cannot be added */
3396 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003397 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003398 /* but don't bother the driver with it */
3399 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003400
Johannes Bergbdd90d52011-12-14 12:20:27 +01003401 /* must be last in here for error handling */
3402 params.vlan = get_vlan(info, rdev);
3403 if (IS_ERR(params.vlan))
3404 return PTR_ERR(params.vlan);
3405 break;
3406 case NL80211_IFTYPE_MESH_POINT:
3407 /* TDLS peers cannot be added */
3408 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003409 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003410 break;
3411 case NL80211_IFTYPE_STATION:
3412 /* Only TDLS peers can be added */
3413 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3414 return -EINVAL;
3415 /* Can only add if TDLS ... */
3416 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3417 return -EOPNOTSUPP;
3418 /* ... with external setup is supported */
3419 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3420 return -EOPNOTSUPP;
3421 break;
3422 default:
3423 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003424 }
3425
Johannes Bergbdd90d52011-12-14 12:20:27 +01003426 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003427
Hila Gonene35e4d22012-06-27 17:19:42 +03003428 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003429
Johannes Berg5727ef12007-12-19 02:03:34 +01003430 if (params.vlan)
3431 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003432 return err;
3433}
3434
3435static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3436{
Johannes Berg4c476992010-10-04 21:36:35 +02003437 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3438 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003439 u8 *mac_addr = NULL;
3440
3441 if (info->attrs[NL80211_ATTR_MAC])
3442 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3443
Johannes Berge80cf852009-05-11 14:43:13 +02003444 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003445 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003446 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003447 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3448 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003449
Johannes Berg4c476992010-10-04 21:36:35 +02003450 if (!rdev->ops->del_station)
3451 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003452
Hila Gonene35e4d22012-06-27 17:19:42 +03003453 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003454}
3455
Eric W. Biederman15e47302012-09-07 20:12:54 +00003456static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003457 int flags, struct net_device *dev,
3458 u8 *dst, u8 *next_hop,
3459 struct mpath_info *pinfo)
3460{
3461 void *hdr;
3462 struct nlattr *pinfoattr;
3463
Eric W. Biederman15e47302012-09-07 20:12:54 +00003464 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003465 if (!hdr)
3466 return -1;
3467
David S. Miller9360ffd2012-03-29 04:41:26 -04003468 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3469 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3470 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3471 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3472 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003473
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003474 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3475 if (!pinfoattr)
3476 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003477 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3478 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3479 pinfo->frame_qlen))
3480 goto nla_put_failure;
3481 if (((pinfo->filled & MPATH_INFO_SN) &&
3482 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3483 ((pinfo->filled & MPATH_INFO_METRIC) &&
3484 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3485 pinfo->metric)) ||
3486 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3487 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3488 pinfo->exptime)) ||
3489 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3490 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3491 pinfo->flags)) ||
3492 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3493 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3494 pinfo->discovery_timeout)) ||
3495 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3496 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3497 pinfo->discovery_retries)))
3498 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003499
3500 nla_nest_end(msg, pinfoattr);
3501
3502 return genlmsg_end(msg, hdr);
3503
3504 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003505 genlmsg_cancel(msg, hdr);
3506 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003507}
3508
3509static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003510 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003511{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003512 struct mpath_info pinfo;
3513 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003514 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003515 u8 dst[ETH_ALEN];
3516 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003517 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003518 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003519
Johannes Berg67748892010-10-04 21:14:06 +02003520 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3521 if (err)
3522 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003523
3524 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003525 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003526 goto out_err;
3527 }
3528
Jouni Malineneec60b02009-03-20 21:21:19 +02003529 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3530 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003531 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003532 }
3533
Johannes Bergbba95fe2008-07-29 13:22:51 +02003534 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003535 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3536 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003537 if (err == -ENOENT)
3538 break;
3539 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003540 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003541
Eric W. Biederman15e47302012-09-07 20:12:54 +00003542 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003543 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3544 netdev, dst, next_hop,
3545 &pinfo) < 0)
3546 goto out;
3547
3548 path_idx++;
3549 }
3550
3551
3552 out:
3553 cb->args[1] = path_idx;
3554 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003555 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003556 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003557 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003558}
3559
3560static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3561{
Johannes Berg4c476992010-10-04 21:36:35 +02003562 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003563 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003564 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003565 struct mpath_info pinfo;
3566 struct sk_buff *msg;
3567 u8 *dst = NULL;
3568 u8 next_hop[ETH_ALEN];
3569
3570 memset(&pinfo, 0, sizeof(pinfo));
3571
3572 if (!info->attrs[NL80211_ATTR_MAC])
3573 return -EINVAL;
3574
3575 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3576
Johannes Berg4c476992010-10-04 21:36:35 +02003577 if (!rdev->ops->get_mpath)
3578 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003579
Johannes Berg4c476992010-10-04 21:36:35 +02003580 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3581 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003582
Hila Gonene35e4d22012-06-27 17:19:42 +03003583 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003584 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003585 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003586
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003587 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003588 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003589 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003590
Eric W. Biederman15e47302012-09-07 20:12:54 +00003591 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003592 dev, dst, next_hop, &pinfo) < 0) {
3593 nlmsg_free(msg);
3594 return -ENOBUFS;
3595 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003596
Johannes Berg4c476992010-10-04 21:36:35 +02003597 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003598}
3599
3600static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3601{
Johannes Berg4c476992010-10-04 21:36:35 +02003602 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3603 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003604 u8 *dst = NULL;
3605 u8 *next_hop = NULL;
3606
3607 if (!info->attrs[NL80211_ATTR_MAC])
3608 return -EINVAL;
3609
3610 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3611 return -EINVAL;
3612
3613 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3614 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3615
Johannes Berg4c476992010-10-04 21:36:35 +02003616 if (!rdev->ops->change_mpath)
3617 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003618
Johannes Berg4c476992010-10-04 21:36:35 +02003619 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3620 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003621
Hila Gonene35e4d22012-06-27 17:19:42 +03003622 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003623}
Johannes Berg4c476992010-10-04 21:36:35 +02003624
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003625static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3626{
Johannes Berg4c476992010-10-04 21:36:35 +02003627 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3628 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003629 u8 *dst = NULL;
3630 u8 *next_hop = NULL;
3631
3632 if (!info->attrs[NL80211_ATTR_MAC])
3633 return -EINVAL;
3634
3635 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3636 return -EINVAL;
3637
3638 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3639 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3640
Johannes Berg4c476992010-10-04 21:36:35 +02003641 if (!rdev->ops->add_mpath)
3642 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003643
Johannes Berg4c476992010-10-04 21:36:35 +02003644 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3645 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003646
Hila Gonene35e4d22012-06-27 17:19:42 +03003647 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003648}
3649
3650static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3651{
Johannes Berg4c476992010-10-04 21:36:35 +02003652 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3653 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003654 u8 *dst = NULL;
3655
3656 if (info->attrs[NL80211_ATTR_MAC])
3657 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3658
Johannes Berg4c476992010-10-04 21:36:35 +02003659 if (!rdev->ops->del_mpath)
3660 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003661
Hila Gonene35e4d22012-06-27 17:19:42 +03003662 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003663}
3664
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003665static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3666{
Johannes Berg4c476992010-10-04 21:36:35 +02003667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3668 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003669 struct bss_parameters params;
3670
3671 memset(&params, 0, sizeof(params));
3672 /* default to not changing parameters */
3673 params.use_cts_prot = -1;
3674 params.use_short_preamble = -1;
3675 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003676 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003677 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003678
3679 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3680 params.use_cts_prot =
3681 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3682 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3683 params.use_short_preamble =
3684 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3685 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3686 params.use_short_slot_time =
3687 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003688 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3689 params.basic_rates =
3690 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3691 params.basic_rates_len =
3692 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3693 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003694 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3695 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003696 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3697 params.ht_opmode =
3698 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003699
Johannes Berg4c476992010-10-04 21:36:35 +02003700 if (!rdev->ops->change_bss)
3701 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003702
Johannes Berg074ac8d2010-09-16 14:58:22 +02003703 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003704 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3705 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003706
Hila Gonene35e4d22012-06-27 17:19:42 +03003707 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003708}
3709
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003710static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003711 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3712 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3713 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3714 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3715 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3716 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3717};
3718
3719static int parse_reg_rule(struct nlattr *tb[],
3720 struct ieee80211_reg_rule *reg_rule)
3721{
3722 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3723 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3724
3725 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3726 return -EINVAL;
3727 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3728 return -EINVAL;
3729 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3730 return -EINVAL;
3731 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3732 return -EINVAL;
3733 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3734 return -EINVAL;
3735
3736 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3737
3738 freq_range->start_freq_khz =
3739 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3740 freq_range->end_freq_khz =
3741 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3742 freq_range->max_bandwidth_khz =
3743 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3744
3745 power_rule->max_eirp =
3746 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3747
3748 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3749 power_rule->max_antenna_gain =
3750 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3751
3752 return 0;
3753}
3754
3755static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3756{
3757 int r;
3758 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003759 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003760
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003761 /*
3762 * You should only get this when cfg80211 hasn't yet initialized
3763 * completely when built-in to the kernel right between the time
3764 * window between nl80211_init() and regulatory_init(), if that is
3765 * even possible.
3766 */
3767 mutex_lock(&cfg80211_mutex);
3768 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003769 mutex_unlock(&cfg80211_mutex);
3770 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003771 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003772 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003773
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003774 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3775 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003776
3777 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3778
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003779 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3780 user_reg_hint_type =
3781 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3782 else
3783 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3784
3785 switch (user_reg_hint_type) {
3786 case NL80211_USER_REG_HINT_USER:
3787 case NL80211_USER_REG_HINT_CELL_BASE:
3788 break;
3789 default:
3790 return -EINVAL;
3791 }
3792
3793 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003794
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003795 return r;
3796}
3797
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003798static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003799 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003800{
Johannes Berg4c476992010-10-04 21:36:35 +02003801 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003802 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003803 struct wireless_dev *wdev = dev->ieee80211_ptr;
3804 struct mesh_config cur_params;
3805 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003806 void *hdr;
3807 struct nlattr *pinfoattr;
3808 struct sk_buff *msg;
3809
Johannes Berg29cbe682010-12-03 09:20:44 +01003810 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3811 return -EOPNOTSUPP;
3812
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003813 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003814 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003815
Johannes Berg29cbe682010-12-03 09:20:44 +01003816 wdev_lock(wdev);
3817 /* If not connected, get default parameters */
3818 if (!wdev->mesh_id_len)
3819 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3820 else
Hila Gonene35e4d22012-06-27 17:19:42 +03003821 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01003822 wdev_unlock(wdev);
3823
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003824 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003825 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003826
3827 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003828 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003829 if (!msg)
3830 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003831 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003832 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003833 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003834 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003835 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003836 if (!pinfoattr)
3837 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003838 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3839 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3840 cur_params.dot11MeshRetryTimeout) ||
3841 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3842 cur_params.dot11MeshConfirmTimeout) ||
3843 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3844 cur_params.dot11MeshHoldingTimeout) ||
3845 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3846 cur_params.dot11MeshMaxPeerLinks) ||
3847 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3848 cur_params.dot11MeshMaxRetries) ||
3849 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3850 cur_params.dot11MeshTTL) ||
3851 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3852 cur_params.element_ttl) ||
3853 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3854 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003855 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3856 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003857 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3858 cur_params.dot11MeshHWMPmaxPREQretries) ||
3859 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3860 cur_params.path_refresh_time) ||
3861 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3862 cur_params.min_discovery_timeout) ||
3863 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3864 cur_params.dot11MeshHWMPactivePathTimeout) ||
3865 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3866 cur_params.dot11MeshHWMPpreqMinInterval) ||
3867 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3868 cur_params.dot11MeshHWMPperrMinInterval) ||
3869 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3870 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3871 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3872 cur_params.dot11MeshHWMPRootMode) ||
3873 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3874 cur_params.dot11MeshHWMPRannInterval) ||
3875 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3876 cur_params.dot11MeshGateAnnouncementProtocol) ||
3877 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3878 cur_params.dot11MeshForwarding) ||
3879 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003880 cur_params.rssi_threshold) ||
3881 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003882 cur_params.ht_opmode) ||
3883 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3884 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3885 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003886 cur_params.dot11MeshHWMProotInterval) ||
3887 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3888 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003889 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003890 nla_nest_end(msg, pinfoattr);
3891 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003892 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003893
Johannes Berg3b858752009-03-12 09:55:09 +01003894 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003895 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003896 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003897 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003898 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003899}
3900
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003901static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003902 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3903 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3904 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3905 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3906 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3907 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003908 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003909 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003910 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003911 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3912 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3913 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3914 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3915 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003916 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003917 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003918 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003919 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003920 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003921 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003922 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3923 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003924 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3925 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003926 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003927};
3928
Javier Cardonac80d5452010-12-16 17:37:49 -08003929static const struct nla_policy
3930 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003931 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003932 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3933 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003934 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003935 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003936 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003937 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003938};
3939
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003940static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003941 struct mesh_config *cfg,
3942 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003943{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003944 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003945 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003946
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003947#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3948do {\
3949 if (table[attr_num]) {\
3950 cfg->param = nla_fn(table[attr_num]); \
3951 mask |= (1 << (attr_num - 1)); \
3952 } \
3953} while (0);\
3954
3955
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003956 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003957 return -EINVAL;
3958 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003959 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003960 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003961 return -EINVAL;
3962
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003963 /* This makes sure that there aren't more than 32 mesh config
3964 * parameters (otherwise our bitfield scheme would not work.) */
3965 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3966
3967 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003968 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003969 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3970 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003971 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003972 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3973 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003974 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003975 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3976 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003977 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003978 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3979 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003980 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003981 mask, NL80211_MESHCONF_MAX_RETRIES,
3982 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003983 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003984 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003985 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003986 mask, NL80211_MESHCONF_ELEMENT_TTL,
3987 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003988 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003989 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3990 nla_get_u8);
3991 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3992 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3993 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003994 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003995 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3996 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003997 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003998 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3999 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004000 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004001 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4002 nla_get_u16);
4003 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
4004 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4005 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004006 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004007 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4008 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004009 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004010 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4011 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004012 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004013 dot11MeshHWMPnetDiameterTraversalTime, mask,
4014 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4015 nla_get_u16);
4016 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
4017 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
4018 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
4019 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4020 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004021 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004022 dot11MeshGateAnnouncementProtocol, mask,
4023 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4024 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004025 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004026 mask, NL80211_MESHCONF_FORWARDING,
4027 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08004028 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004029 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4030 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004031 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004032 mask, NL80211_MESHCONF_HT_OPMODE,
4033 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004034 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
4035 mask,
4036 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4037 nla_get_u32);
4038 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
4039 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4040 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004041 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
4042 dot11MeshHWMPconfirmationInterval, mask,
4043 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4044 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004045 if (mask_out)
4046 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004047
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004048 return 0;
4049
4050#undef FILL_IN_MESH_PARAM_IF_SET
4051}
4052
Javier Cardonac80d5452010-12-16 17:37:49 -08004053static int nl80211_parse_mesh_setup(struct genl_info *info,
4054 struct mesh_setup *setup)
4055{
4056 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4057
4058 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4059 return -EINVAL;
4060 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4061 info->attrs[NL80211_ATTR_MESH_SETUP],
4062 nl80211_mesh_setup_params_policy))
4063 return -EINVAL;
4064
Javier Cardonad299a1f2012-03-31 11:31:33 -07004065 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4066 setup->sync_method =
4067 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4068 IEEE80211_SYNC_METHOD_VENDOR :
4069 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4070
Javier Cardonac80d5452010-12-16 17:37:49 -08004071 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4072 setup->path_sel_proto =
4073 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4074 IEEE80211_PATH_PROTOCOL_VENDOR :
4075 IEEE80211_PATH_PROTOCOL_HWMP;
4076
4077 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4078 setup->path_metric =
4079 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4080 IEEE80211_PATH_METRIC_VENDOR :
4081 IEEE80211_PATH_METRIC_AIRTIME;
4082
Javier Cardona581a8b02011-04-07 15:08:27 -07004083
4084 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004085 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004086 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004087 if (!is_valid_ie_attr(ieattr))
4088 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004089 setup->ie = nla_data(ieattr);
4090 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004091 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004092 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4093 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004094
4095 return 0;
4096}
4097
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004098static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004099 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004100{
4101 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4102 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004103 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004104 struct mesh_config cfg;
4105 u32 mask;
4106 int err;
4107
Johannes Berg29cbe682010-12-03 09:20:44 +01004108 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4109 return -EOPNOTSUPP;
4110
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004111 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004112 return -EOPNOTSUPP;
4113
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004114 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004115 if (err)
4116 return err;
4117
Johannes Berg29cbe682010-12-03 09:20:44 +01004118 wdev_lock(wdev);
4119 if (!wdev->mesh_id_len)
4120 err = -ENOLINK;
4121
4122 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004123 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004124
4125 wdev_unlock(wdev);
4126
4127 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004128}
4129
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004130static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4131{
4132 struct sk_buff *msg;
4133 void *hdr = NULL;
4134 struct nlattr *nl_reg_rules;
4135 unsigned int i;
4136 int err = -EINVAL;
4137
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004138 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004139
4140 if (!cfg80211_regdomain)
4141 goto out;
4142
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004143 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004144 if (!msg) {
4145 err = -ENOBUFS;
4146 goto out;
4147 }
4148
Eric W. Biederman15e47302012-09-07 20:12:54 +00004149 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004150 NL80211_CMD_GET_REG);
4151 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004152 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004153
David S. Miller9360ffd2012-03-29 04:41:26 -04004154 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4155 cfg80211_regdomain->alpha2) ||
4156 (cfg80211_regdomain->dfs_region &&
4157 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4158 cfg80211_regdomain->dfs_region)))
4159 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004160
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004161 if (reg_last_request_cell_base() &&
4162 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4163 NL80211_USER_REG_HINT_CELL_BASE))
4164 goto nla_put_failure;
4165
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004166 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4167 if (!nl_reg_rules)
4168 goto nla_put_failure;
4169
4170 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4171 struct nlattr *nl_reg_rule;
4172 const struct ieee80211_reg_rule *reg_rule;
4173 const struct ieee80211_freq_range *freq_range;
4174 const struct ieee80211_power_rule *power_rule;
4175
4176 reg_rule = &cfg80211_regdomain->reg_rules[i];
4177 freq_range = &reg_rule->freq_range;
4178 power_rule = &reg_rule->power_rule;
4179
4180 nl_reg_rule = nla_nest_start(msg, i);
4181 if (!nl_reg_rule)
4182 goto nla_put_failure;
4183
David S. Miller9360ffd2012-03-29 04:41:26 -04004184 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4185 reg_rule->flags) ||
4186 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4187 freq_range->start_freq_khz) ||
4188 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4189 freq_range->end_freq_khz) ||
4190 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4191 freq_range->max_bandwidth_khz) ||
4192 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4193 power_rule->max_antenna_gain) ||
4194 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4195 power_rule->max_eirp))
4196 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004197
4198 nla_nest_end(msg, nl_reg_rule);
4199 }
4200
4201 nla_nest_end(msg, nl_reg_rules);
4202
4203 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004204 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004205 goto out;
4206
4207nla_put_failure:
4208 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004209put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004210 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004211 err = -EMSGSIZE;
4212out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004213 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004214 return err;
4215}
4216
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004217static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4218{
4219 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4220 struct nlattr *nl_reg_rule;
4221 char *alpha2 = NULL;
4222 int rem_reg_rules = 0, r = 0;
4223 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004224 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004225 struct ieee80211_regdomain *rd = NULL;
4226
4227 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4228 return -EINVAL;
4229
4230 if (!info->attrs[NL80211_ATTR_REG_RULES])
4231 return -EINVAL;
4232
4233 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4234
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004235 if (info->attrs[NL80211_ATTR_DFS_REGION])
4236 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4237
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004238 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4239 rem_reg_rules) {
4240 num_rules++;
4241 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004242 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004243 }
4244
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004245 mutex_lock(&cfg80211_mutex);
4246
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004247 if (!reg_is_valid_request(alpha2)) {
4248 r = -EINVAL;
4249 goto bad_reg;
4250 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004251
4252 size_of_regd = sizeof(struct ieee80211_regdomain) +
4253 (num_rules * sizeof(struct ieee80211_reg_rule));
4254
4255 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004256 if (!rd) {
4257 r = -ENOMEM;
4258 goto bad_reg;
4259 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004260
4261 rd->n_reg_rules = num_rules;
4262 rd->alpha2[0] = alpha2[0];
4263 rd->alpha2[1] = alpha2[1];
4264
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004265 /*
4266 * Disable DFS master mode if the DFS region was
4267 * not supported or known on this kernel.
4268 */
4269 if (reg_supported_dfs_region(dfs_region))
4270 rd->dfs_region = dfs_region;
4271
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004272 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4273 rem_reg_rules) {
4274 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4275 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4276 reg_rule_policy);
4277 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4278 if (r)
4279 goto bad_reg;
4280
4281 rule_idx++;
4282
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004283 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4284 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004285 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004286 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004287 }
4288
4289 BUG_ON(rule_idx != num_rules);
4290
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004291 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004292
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004293 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004294
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004295 return r;
4296
Johannes Bergd2372b32008-10-24 20:32:20 +02004297 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004298 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004299 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004300 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004301}
4302
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004303static int validate_scan_freqs(struct nlattr *freqs)
4304{
4305 struct nlattr *attr1, *attr2;
4306 int n_channels = 0, tmp1, tmp2;
4307
4308 nla_for_each_nested(attr1, freqs, tmp1) {
4309 n_channels++;
4310 /*
4311 * Some hardware has a limited channel list for
4312 * scanning, and it is pretty much nonsensical
4313 * to scan for a channel twice, so disallow that
4314 * and don't require drivers to check that the
4315 * channel list they get isn't longer than what
4316 * they can scan, as long as they can scan all
4317 * the channels they registered at once.
4318 */
4319 nla_for_each_nested(attr2, freqs, tmp2)
4320 if (attr1 != attr2 &&
4321 nla_get_u32(attr1) == nla_get_u32(attr2))
4322 return 0;
4323 }
4324
4325 return n_channels;
4326}
4327
Johannes Berg2a519312009-02-10 21:25:55 +01004328static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4329{
Johannes Berg4c476992010-10-04 21:36:35 +02004330 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004331 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004332 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004333 struct nlattr *attr;
4334 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004335 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004336 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004337
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004338 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4339 return -EINVAL;
4340
Johannes Berg79c97e92009-07-07 03:56:12 +02004341 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004342
Johannes Berg4c476992010-10-04 21:36:35 +02004343 if (!rdev->ops->scan)
4344 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004345
Johannes Berg4c476992010-10-04 21:36:35 +02004346 if (rdev->scan_req)
4347 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004348
4349 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004350 n_channels = validate_scan_freqs(
4351 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004352 if (!n_channels)
4353 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004354 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004355 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004356 n_channels = 0;
4357
Johannes Berg2a519312009-02-10 21:25:55 +01004358 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4359 if (wiphy->bands[band])
4360 n_channels += wiphy->bands[band]->n_channels;
4361 }
4362
4363 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4364 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4365 n_ssids++;
4366
Johannes Berg4c476992010-10-04 21:36:35 +02004367 if (n_ssids > wiphy->max_scan_ssids)
4368 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004369
Jouni Malinen70692ad2009-02-16 19:39:13 +02004370 if (info->attrs[NL80211_ATTR_IE])
4371 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4372 else
4373 ie_len = 0;
4374
Johannes Berg4c476992010-10-04 21:36:35 +02004375 if (ie_len > wiphy->max_scan_ie_len)
4376 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004377
Johannes Berg2a519312009-02-10 21:25:55 +01004378 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004379 + sizeof(*request->ssids) * n_ssids
4380 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004381 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004382 if (!request)
4383 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004384
Johannes Berg2a519312009-02-10 21:25:55 +01004385 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004386 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004387 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004388 if (ie_len) {
4389 if (request->ssids)
4390 request->ie = (void *)(request->ssids + n_ssids);
4391 else
4392 request->ie = (void *)(request->channels + n_channels);
4393 }
Johannes Berg2a519312009-02-10 21:25:55 +01004394
Johannes Berg584991d2009-11-02 13:32:03 +01004395 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004396 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4397 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004398 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004399 struct ieee80211_channel *chan;
4400
4401 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4402
4403 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004404 err = -EINVAL;
4405 goto out_free;
4406 }
Johannes Berg584991d2009-11-02 13:32:03 +01004407
4408 /* ignore disabled channels */
4409 if (chan->flags & IEEE80211_CHAN_DISABLED)
4410 continue;
4411
4412 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004413 i++;
4414 }
4415 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004416 enum ieee80211_band band;
4417
Johannes Berg2a519312009-02-10 21:25:55 +01004418 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004419 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4420 int j;
4421 if (!wiphy->bands[band])
4422 continue;
4423 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004424 struct ieee80211_channel *chan;
4425
4426 chan = &wiphy->bands[band]->channels[j];
4427
4428 if (chan->flags & IEEE80211_CHAN_DISABLED)
4429 continue;
4430
4431 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004432 i++;
4433 }
4434 }
4435 }
4436
Johannes Berg584991d2009-11-02 13:32:03 +01004437 if (!i) {
4438 err = -EINVAL;
4439 goto out_free;
4440 }
4441
4442 request->n_channels = i;
4443
Johannes Berg2a519312009-02-10 21:25:55 +01004444 i = 0;
4445 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4446 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004447 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004448 err = -EINVAL;
4449 goto out_free;
4450 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004451 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004452 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004453 i++;
4454 }
4455 }
4456
Jouni Malinen70692ad2009-02-16 19:39:13 +02004457 if (info->attrs[NL80211_ATTR_IE]) {
4458 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004459 memcpy((void *)request->ie,
4460 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004461 request->ie_len);
4462 }
4463
Johannes Berg34850ab2011-07-18 18:08:35 +02004464 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004465 if (wiphy->bands[i])
4466 request->rates[i] =
4467 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004468
4469 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4470 nla_for_each_nested(attr,
4471 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4472 tmp) {
4473 enum ieee80211_band band = nla_type(attr);
4474
Dan Carpenter84404622011-07-29 11:52:18 +03004475 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004476 err = -EINVAL;
4477 goto out_free;
4478 }
4479 err = ieee80211_get_ratemask(wiphy->bands[band],
4480 nla_data(attr),
4481 nla_len(attr),
4482 &request->rates[band]);
4483 if (err)
4484 goto out_free;
4485 }
4486 }
4487
Sam Leffler46856bb2012-10-11 21:03:32 -07004488 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004489 request->flags = nla_get_u32(
4490 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004491 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4492 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4493 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4494 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004495 err = -EOPNOTSUPP;
4496 goto out_free;
4497 }
4498 }
Sam Lefflered4737712012-10-11 21:03:31 -07004499
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304500 request->no_cck =
4501 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4502
Johannes Bergfd014282012-06-18 19:17:03 +02004503 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004504 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004505 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004506
Johannes Berg79c97e92009-07-07 03:56:12 +02004507 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004508 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004509
Johannes Berg463d0182009-07-14 00:33:35 +02004510 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004511 nl80211_send_scan_start(rdev, wdev);
4512 if (wdev->netdev)
4513 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004514 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004515 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004516 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004517 kfree(request);
4518 }
Johannes Berg3b858752009-03-12 09:55:09 +01004519
Johannes Berg2a519312009-02-10 21:25:55 +01004520 return err;
4521}
4522
Luciano Coelho807f8a82011-05-11 17:09:35 +03004523static int nl80211_start_sched_scan(struct sk_buff *skb,
4524 struct genl_info *info)
4525{
4526 struct cfg80211_sched_scan_request *request;
4527 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4528 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004529 struct nlattr *attr;
4530 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004531 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004532 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004533 enum ieee80211_band band;
4534 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004535 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004536
4537 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4538 !rdev->ops->sched_scan_start)
4539 return -EOPNOTSUPP;
4540
4541 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4542 return -EINVAL;
4543
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004544 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4545 return -EINVAL;
4546
4547 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4548 if (interval == 0)
4549 return -EINVAL;
4550
Luciano Coelho807f8a82011-05-11 17:09:35 +03004551 wiphy = &rdev->wiphy;
4552
4553 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4554 n_channels = validate_scan_freqs(
4555 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4556 if (!n_channels)
4557 return -EINVAL;
4558 } else {
4559 n_channels = 0;
4560
4561 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4562 if (wiphy->bands[band])
4563 n_channels += wiphy->bands[band]->n_channels;
4564 }
4565
4566 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4567 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4568 tmp)
4569 n_ssids++;
4570
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004571 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004572 return -EINVAL;
4573
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004574 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4575 nla_for_each_nested(attr,
4576 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4577 tmp)
4578 n_match_sets++;
4579
4580 if (n_match_sets > wiphy->max_match_sets)
4581 return -EINVAL;
4582
Luciano Coelho807f8a82011-05-11 17:09:35 +03004583 if (info->attrs[NL80211_ATTR_IE])
4584 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4585 else
4586 ie_len = 0;
4587
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004588 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004589 return -EINVAL;
4590
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004591 mutex_lock(&rdev->sched_scan_mtx);
4592
4593 if (rdev->sched_scan_req) {
4594 err = -EINPROGRESS;
4595 goto out;
4596 }
4597
Luciano Coelho807f8a82011-05-11 17:09:35 +03004598 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004599 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004600 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004601 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004602 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004603 if (!request) {
4604 err = -ENOMEM;
4605 goto out;
4606 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004607
4608 if (n_ssids)
4609 request->ssids = (void *)&request->channels[n_channels];
4610 request->n_ssids = n_ssids;
4611 if (ie_len) {
4612 if (request->ssids)
4613 request->ie = (void *)(request->ssids + n_ssids);
4614 else
4615 request->ie = (void *)(request->channels + n_channels);
4616 }
4617
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004618 if (n_match_sets) {
4619 if (request->ie)
4620 request->match_sets = (void *)(request->ie + ie_len);
4621 else if (request->ssids)
4622 request->match_sets =
4623 (void *)(request->ssids + n_ssids);
4624 else
4625 request->match_sets =
4626 (void *)(request->channels + n_channels);
4627 }
4628 request->n_match_sets = n_match_sets;
4629
Luciano Coelho807f8a82011-05-11 17:09:35 +03004630 i = 0;
4631 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4632 /* user specified, bail out if channel not found */
4633 nla_for_each_nested(attr,
4634 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4635 tmp) {
4636 struct ieee80211_channel *chan;
4637
4638 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4639
4640 if (!chan) {
4641 err = -EINVAL;
4642 goto out_free;
4643 }
4644
4645 /* ignore disabled channels */
4646 if (chan->flags & IEEE80211_CHAN_DISABLED)
4647 continue;
4648
4649 request->channels[i] = chan;
4650 i++;
4651 }
4652 } else {
4653 /* all channels */
4654 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4655 int j;
4656 if (!wiphy->bands[band])
4657 continue;
4658 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4659 struct ieee80211_channel *chan;
4660
4661 chan = &wiphy->bands[band]->channels[j];
4662
4663 if (chan->flags & IEEE80211_CHAN_DISABLED)
4664 continue;
4665
4666 request->channels[i] = chan;
4667 i++;
4668 }
4669 }
4670 }
4671
4672 if (!i) {
4673 err = -EINVAL;
4674 goto out_free;
4675 }
4676
4677 request->n_channels = i;
4678
4679 i = 0;
4680 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4681 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4682 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004683 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004684 err = -EINVAL;
4685 goto out_free;
4686 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004687 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004688 memcpy(request->ssids[i].ssid, nla_data(attr),
4689 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004690 i++;
4691 }
4692 }
4693
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004694 i = 0;
4695 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4696 nla_for_each_nested(attr,
4697 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4698 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004699 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004700
4701 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4702 nla_data(attr), nla_len(attr),
4703 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004704 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004705 if (ssid) {
4706 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4707 err = -EINVAL;
4708 goto out_free;
4709 }
4710 memcpy(request->match_sets[i].ssid.ssid,
4711 nla_data(ssid), nla_len(ssid));
4712 request->match_sets[i].ssid.ssid_len =
4713 nla_len(ssid);
4714 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004715 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4716 if (rssi)
4717 request->rssi_thold = nla_get_u32(rssi);
4718 else
4719 request->rssi_thold =
4720 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004721 i++;
4722 }
4723 }
4724
Luciano Coelho807f8a82011-05-11 17:09:35 +03004725 if (info->attrs[NL80211_ATTR_IE]) {
4726 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4727 memcpy((void *)request->ie,
4728 nla_data(info->attrs[NL80211_ATTR_IE]),
4729 request->ie_len);
4730 }
4731
Sam Leffler46856bb2012-10-11 21:03:32 -07004732 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004733 request->flags = nla_get_u32(
4734 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004735 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4736 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4737 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4738 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004739 err = -EOPNOTSUPP;
4740 goto out_free;
4741 }
4742 }
Sam Lefflered4737712012-10-11 21:03:31 -07004743
Luciano Coelho807f8a82011-05-11 17:09:35 +03004744 request->dev = dev;
4745 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004746 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004747 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004748
Hila Gonene35e4d22012-06-27 17:19:42 +03004749 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004750 if (!err) {
4751 rdev->sched_scan_req = request;
4752 nl80211_send_sched_scan(rdev, dev,
4753 NL80211_CMD_START_SCHED_SCAN);
4754 goto out;
4755 }
4756
4757out_free:
4758 kfree(request);
4759out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004760 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004761 return err;
4762}
4763
4764static int nl80211_stop_sched_scan(struct sk_buff *skb,
4765 struct genl_info *info)
4766{
4767 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004768 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004769
4770 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4771 !rdev->ops->sched_scan_stop)
4772 return -EOPNOTSUPP;
4773
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004774 mutex_lock(&rdev->sched_scan_mtx);
4775 err = __cfg80211_stop_sched_scan(rdev, false);
4776 mutex_unlock(&rdev->sched_scan_mtx);
4777
4778 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004779}
4780
Johannes Berg9720bb32011-06-21 09:45:33 +02004781static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4782 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004783 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004784 struct wireless_dev *wdev,
4785 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004786{
Johannes Berg48ab9052009-07-10 18:42:31 +02004787 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004788 void *hdr;
4789 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004790
4791 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004792
Eric W. Biederman15e47302012-09-07 20:12:54 +00004793 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004794 NL80211_CMD_NEW_SCAN_RESULTS);
4795 if (!hdr)
4796 return -1;
4797
Johannes Berg9720bb32011-06-21 09:45:33 +02004798 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4799
David S. Miller9360ffd2012-03-29 04:41:26 -04004800 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4801 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4802 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004803
4804 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4805 if (!bss)
4806 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004807 if ((!is_zero_ether_addr(res->bssid) &&
4808 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4809 (res->information_elements && res->len_information_elements &&
4810 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4811 res->len_information_elements,
4812 res->information_elements)) ||
4813 (res->beacon_ies && res->len_beacon_ies &&
4814 res->beacon_ies != res->information_elements &&
4815 nla_put(msg, NL80211_BSS_BEACON_IES,
4816 res->len_beacon_ies, res->beacon_ies)))
4817 goto nla_put_failure;
4818 if (res->tsf &&
4819 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4820 goto nla_put_failure;
4821 if (res->beacon_interval &&
4822 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4823 goto nla_put_failure;
4824 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4825 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4826 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4827 jiffies_to_msecs(jiffies - intbss->ts)))
4828 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004829
Johannes Berg77965c92009-02-18 18:45:06 +01004830 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004831 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004832 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4833 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004834 break;
4835 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004836 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4837 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004838 break;
4839 default:
4840 break;
4841 }
4842
Johannes Berg48ab9052009-07-10 18:42:31 +02004843 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004844 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004845 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004846 if (intbss == wdev->current_bss &&
4847 nla_put_u32(msg, NL80211_BSS_STATUS,
4848 NL80211_BSS_STATUS_ASSOCIATED))
4849 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004850 break;
4851 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004852 if (intbss == wdev->current_bss &&
4853 nla_put_u32(msg, NL80211_BSS_STATUS,
4854 NL80211_BSS_STATUS_IBSS_JOINED))
4855 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004856 break;
4857 default:
4858 break;
4859 }
4860
Johannes Berg2a519312009-02-10 21:25:55 +01004861 nla_nest_end(msg, bss);
4862
4863 return genlmsg_end(msg, hdr);
4864
4865 nla_put_failure:
4866 genlmsg_cancel(msg, hdr);
4867 return -EMSGSIZE;
4868}
4869
4870static int nl80211_dump_scan(struct sk_buff *skb,
4871 struct netlink_callback *cb)
4872{
Johannes Berg48ab9052009-07-10 18:42:31 +02004873 struct cfg80211_registered_device *rdev;
4874 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004875 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004876 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004877 int start = cb->args[1], idx = 0;
4878 int err;
4879
Johannes Berg67748892010-10-04 21:14:06 +02004880 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4881 if (err)
4882 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004883
Johannes Berg48ab9052009-07-10 18:42:31 +02004884 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004885
Johannes Berg48ab9052009-07-10 18:42:31 +02004886 wdev_lock(wdev);
4887 spin_lock_bh(&rdev->bss_lock);
4888 cfg80211_bss_expire(rdev);
4889
Johannes Berg9720bb32011-06-21 09:45:33 +02004890 cb->seq = rdev->bss_generation;
4891
Johannes Berg48ab9052009-07-10 18:42:31 +02004892 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004893 if (++idx <= start)
4894 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004895 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004896 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004897 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004898 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004899 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004900 }
4901 }
4902
Johannes Berg48ab9052009-07-10 18:42:31 +02004903 spin_unlock_bh(&rdev->bss_lock);
4904 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004905
4906 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004907 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004908
Johannes Berg67748892010-10-04 21:14:06 +02004909 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004910}
4911
Eric W. Biederman15e47302012-09-07 20:12:54 +00004912static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004913 int flags, struct net_device *dev,
4914 struct survey_info *survey)
4915{
4916 void *hdr;
4917 struct nlattr *infoattr;
4918
Eric W. Biederman15e47302012-09-07 20:12:54 +00004919 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004920 NL80211_CMD_NEW_SURVEY_RESULTS);
4921 if (!hdr)
4922 return -ENOMEM;
4923
David S. Miller9360ffd2012-03-29 04:41:26 -04004924 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4925 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004926
4927 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4928 if (!infoattr)
4929 goto nla_put_failure;
4930
David S. Miller9360ffd2012-03-29 04:41:26 -04004931 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4932 survey->channel->center_freq))
4933 goto nla_put_failure;
4934
4935 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4936 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4937 goto nla_put_failure;
4938 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4939 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4940 goto nla_put_failure;
4941 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4942 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4943 survey->channel_time))
4944 goto nla_put_failure;
4945 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4946 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4947 survey->channel_time_busy))
4948 goto nla_put_failure;
4949 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4950 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4951 survey->channel_time_ext_busy))
4952 goto nla_put_failure;
4953 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4954 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4955 survey->channel_time_rx))
4956 goto nla_put_failure;
4957 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4958 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4959 survey->channel_time_tx))
4960 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004961
4962 nla_nest_end(msg, infoattr);
4963
4964 return genlmsg_end(msg, hdr);
4965
4966 nla_put_failure:
4967 genlmsg_cancel(msg, hdr);
4968 return -EMSGSIZE;
4969}
4970
4971static int nl80211_dump_survey(struct sk_buff *skb,
4972 struct netlink_callback *cb)
4973{
4974 struct survey_info survey;
4975 struct cfg80211_registered_device *dev;
4976 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004977 int survey_idx = cb->args[1];
4978 int res;
4979
Johannes Berg67748892010-10-04 21:14:06 +02004980 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4981 if (res)
4982 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004983
4984 if (!dev->ops->dump_survey) {
4985 res = -EOPNOTSUPP;
4986 goto out_err;
4987 }
4988
4989 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004990 struct ieee80211_channel *chan;
4991
Hila Gonene35e4d22012-06-27 17:19:42 +03004992 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01004993 if (res == -ENOENT)
4994 break;
4995 if (res)
4996 goto out_err;
4997
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004998 /* Survey without a channel doesn't make sense */
4999 if (!survey.channel) {
5000 res = -EINVAL;
5001 goto out;
5002 }
5003
5004 chan = ieee80211_get_channel(&dev->wiphy,
5005 survey.channel->center_freq);
5006 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5007 survey_idx++;
5008 continue;
5009 }
5010
Holger Schurig61fa7132009-11-11 12:25:40 +01005011 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005012 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005013 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5014 netdev,
5015 &survey) < 0)
5016 goto out;
5017 survey_idx++;
5018 }
5019
5020 out:
5021 cb->args[1] = survey_idx;
5022 res = skb->len;
5023 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005024 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005025 return res;
5026}
5027
Samuel Ortizb23aa672009-07-01 21:26:54 +02005028static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5029{
5030 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5031 NL80211_WPA_VERSION_2));
5032}
5033
Jouni Malinen636a5d32009-03-19 13:39:22 +02005034static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5035{
Johannes Berg4c476992010-10-04 21:36:35 +02005036 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5037 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005038 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005039 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5040 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005041 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005042 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005043 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005044
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005045 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5046 return -EINVAL;
5047
5048 if (!info->attrs[NL80211_ATTR_MAC])
5049 return -EINVAL;
5050
Jouni Malinen17780922009-03-27 20:52:47 +02005051 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5052 return -EINVAL;
5053
Johannes Berg19957bb2009-07-02 17:20:43 +02005054 if (!info->attrs[NL80211_ATTR_SSID])
5055 return -EINVAL;
5056
5057 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5058 return -EINVAL;
5059
Johannes Bergfffd0932009-07-08 14:22:54 +02005060 err = nl80211_parse_key(info, &key);
5061 if (err)
5062 return err;
5063
5064 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005065 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5066 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005067 if (!key.p.key || !key.p.key_len)
5068 return -EINVAL;
5069 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5070 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5071 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5072 key.p.key_len != WLAN_KEY_LEN_WEP104))
5073 return -EINVAL;
5074 if (key.idx > 4)
5075 return -EINVAL;
5076 } else {
5077 key.p.key_len = 0;
5078 key.p.key = NULL;
5079 }
5080
Johannes Bergafea0b72010-08-10 09:46:42 +02005081 if (key.idx >= 0) {
5082 int i;
5083 bool ok = false;
5084 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5085 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5086 ok = true;
5087 break;
5088 }
5089 }
Johannes Berg4c476992010-10-04 21:36:35 +02005090 if (!ok)
5091 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005092 }
5093
Johannes Berg4c476992010-10-04 21:36:35 +02005094 if (!rdev->ops->auth)
5095 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005096
Johannes Berg074ac8d2010-09-16 14:58:22 +02005097 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005098 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5099 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005100
Johannes Berg19957bb2009-07-02 17:20:43 +02005101 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005102 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005103 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005104 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5105 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005106
Johannes Berg19957bb2009-07-02 17:20:43 +02005107 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5108 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5109
5110 if (info->attrs[NL80211_ATTR_IE]) {
5111 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5112 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5113 }
5114
5115 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005116 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005117 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005118
Jouni Malinene39e5b52012-09-30 19:29:39 +03005119 if (auth_type == NL80211_AUTHTYPE_SAE &&
5120 !info->attrs[NL80211_ATTR_SAE_DATA])
5121 return -EINVAL;
5122
5123 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5124 if (auth_type != NL80211_AUTHTYPE_SAE)
5125 return -EINVAL;
5126 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5127 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5128 /* need to include at least Auth Transaction and Status Code */
5129 if (sae_data_len < 4)
5130 return -EINVAL;
5131 }
5132
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005133 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5134
Johannes Berg95de8172012-01-20 13:55:25 +01005135 /*
5136 * Since we no longer track auth state, ignore
5137 * requests to only change local state.
5138 */
5139 if (local_state_change)
5140 return 0;
5141
Johannes Berg4c476992010-10-04 21:36:35 +02005142 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5143 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005144 key.p.key, key.p.key_len, key.idx,
5145 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005146}
5147
Johannes Bergc0692b82010-08-27 14:26:53 +03005148static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5149 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005150 struct cfg80211_crypto_settings *settings,
5151 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005152{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005153 memset(settings, 0, sizeof(*settings));
5154
Samuel Ortizb23aa672009-07-01 21:26:54 +02005155 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5156
Johannes Bergc0692b82010-08-27 14:26:53 +03005157 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5158 u16 proto;
5159 proto = nla_get_u16(
5160 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5161 settings->control_port_ethertype = cpu_to_be16(proto);
5162 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5163 proto != ETH_P_PAE)
5164 return -EINVAL;
5165 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5166 settings->control_port_no_encrypt = true;
5167 } else
5168 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5169
Samuel Ortizb23aa672009-07-01 21:26:54 +02005170 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5171 void *data;
5172 int len, i;
5173
5174 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5175 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5176 settings->n_ciphers_pairwise = len / sizeof(u32);
5177
5178 if (len % sizeof(u32))
5179 return -EINVAL;
5180
Johannes Berg3dc27d22009-07-02 21:36:37 +02005181 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005182 return -EINVAL;
5183
5184 memcpy(settings->ciphers_pairwise, data, len);
5185
5186 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005187 if (!cfg80211_supported_cipher_suite(
5188 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005189 settings->ciphers_pairwise[i]))
5190 return -EINVAL;
5191 }
5192
5193 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5194 settings->cipher_group =
5195 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005196 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5197 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005198 return -EINVAL;
5199 }
5200
5201 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5202 settings->wpa_versions =
5203 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5204 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5205 return -EINVAL;
5206 }
5207
5208 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5209 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005210 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005211
5212 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5213 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5214 settings->n_akm_suites = len / sizeof(u32);
5215
5216 if (len % sizeof(u32))
5217 return -EINVAL;
5218
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005219 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5220 return -EINVAL;
5221
Samuel Ortizb23aa672009-07-01 21:26:54 +02005222 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005223 }
5224
5225 return 0;
5226}
5227
Jouni Malinen636a5d32009-03-19 13:39:22 +02005228static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5229{
Johannes Berg4c476992010-10-04 21:36:35 +02005230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5231 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005232 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005233 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005234 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005235 int err, ssid_len, ie_len = 0;
5236 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005237 u32 flags = 0;
5238 struct ieee80211_ht_cap *ht_capa = NULL;
5239 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005240
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005241 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5242 return -EINVAL;
5243
5244 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005245 !info->attrs[NL80211_ATTR_SSID] ||
5246 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005247 return -EINVAL;
5248
Johannes Berg4c476992010-10-04 21:36:35 +02005249 if (!rdev->ops->assoc)
5250 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005251
Johannes Berg074ac8d2010-09-16 14:58:22 +02005252 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005253 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5254 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005255
Johannes Berg19957bb2009-07-02 17:20:43 +02005256 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005257
Johannes Berg19957bb2009-07-02 17:20:43 +02005258 chan = ieee80211_get_channel(&rdev->wiphy,
5259 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005260 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5261 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005262
Johannes Berg19957bb2009-07-02 17:20:43 +02005263 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5264 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005265
5266 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005267 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5268 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005269 }
5270
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005271 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005272 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005273 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005274 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005275 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005276 else if (mfp != NL80211_MFP_NO)
5277 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005278 }
5279
Johannes Berg3e5d7642009-07-07 14:37:26 +02005280 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5281 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5282
Ben Greear7e7c8922011-11-18 11:31:59 -08005283 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5284 flags |= ASSOC_REQ_DISABLE_HT;
5285
5286 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5287 ht_capa_mask =
5288 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5289
5290 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5291 if (!ht_capa_mask)
5292 return -EINVAL;
5293 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5294 }
5295
Johannes Bergc0692b82010-08-27 14:26:53 +03005296 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005297 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005298 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5299 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005300 &crypto, flags, ht_capa,
5301 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005302
Jouni Malinen636a5d32009-03-19 13:39:22 +02005303 return err;
5304}
5305
5306static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5307{
Johannes Berg4c476992010-10-04 21:36:35 +02005308 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5309 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005310 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005311 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005312 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005313 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005314
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005315 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5316 return -EINVAL;
5317
5318 if (!info->attrs[NL80211_ATTR_MAC])
5319 return -EINVAL;
5320
5321 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5322 return -EINVAL;
5323
Johannes Berg4c476992010-10-04 21:36:35 +02005324 if (!rdev->ops->deauth)
5325 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005326
Johannes Berg074ac8d2010-09-16 14:58:22 +02005327 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005328 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5329 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005330
Johannes Berg19957bb2009-07-02 17:20:43 +02005331 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005332
Johannes Berg19957bb2009-07-02 17:20:43 +02005333 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5334 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005335 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005336 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005337 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005338
5339 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005340 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5341 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005342 }
5343
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005344 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5345
Johannes Berg4c476992010-10-04 21:36:35 +02005346 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5347 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005348}
5349
5350static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5351{
Johannes Berg4c476992010-10-04 21:36:35 +02005352 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5353 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005354 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005355 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005356 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005357 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005358
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005359 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5360 return -EINVAL;
5361
5362 if (!info->attrs[NL80211_ATTR_MAC])
5363 return -EINVAL;
5364
5365 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5366 return -EINVAL;
5367
Johannes Berg4c476992010-10-04 21:36:35 +02005368 if (!rdev->ops->disassoc)
5369 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005370
Johannes Berg074ac8d2010-09-16 14:58:22 +02005371 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005372 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5373 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005374
Johannes Berg19957bb2009-07-02 17:20:43 +02005375 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005376
Johannes Berg19957bb2009-07-02 17:20:43 +02005377 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5378 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005379 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005380 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005381 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005382
5383 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005384 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5385 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005386 }
5387
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005388 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5389
Johannes Berg4c476992010-10-04 21:36:35 +02005390 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5391 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005392}
5393
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005394static bool
5395nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5396 int mcast_rate[IEEE80211_NUM_BANDS],
5397 int rateval)
5398{
5399 struct wiphy *wiphy = &rdev->wiphy;
5400 bool found = false;
5401 int band, i;
5402
5403 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5404 struct ieee80211_supported_band *sband;
5405
5406 sband = wiphy->bands[band];
5407 if (!sband)
5408 continue;
5409
5410 for (i = 0; i < sband->n_bitrates; i++) {
5411 if (sband->bitrates[i].bitrate == rateval) {
5412 mcast_rate[band] = i + 1;
5413 found = true;
5414 break;
5415 }
5416 }
5417 }
5418
5419 return found;
5420}
5421
Johannes Berg04a773a2009-04-19 21:24:32 +02005422static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5423{
Johannes Berg4c476992010-10-04 21:36:35 +02005424 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5425 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005426 struct cfg80211_ibss_params ibss;
5427 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005428 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005429 int err;
5430
Johannes Berg8e30bc52009-04-22 17:45:38 +02005431 memset(&ibss, 0, sizeof(ibss));
5432
Johannes Berg04a773a2009-04-19 21:24:32 +02005433 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5434 return -EINVAL;
5435
Johannes Berg683b6d32012-11-08 21:25:48 +01005436 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005437 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5438 return -EINVAL;
5439
Johannes Berg8e30bc52009-04-22 17:45:38 +02005440 ibss.beacon_interval = 100;
5441
5442 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5443 ibss.beacon_interval =
5444 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5445 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5446 return -EINVAL;
5447 }
5448
Johannes Berg4c476992010-10-04 21:36:35 +02005449 if (!rdev->ops->join_ibss)
5450 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005451
Johannes Berg4c476992010-10-04 21:36:35 +02005452 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5453 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005454
Johannes Berg79c97e92009-07-07 03:56:12 +02005455 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005456
Johannes Berg39193492011-09-16 13:45:25 +02005457 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005458 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005459
5460 if (!is_valid_ether_addr(ibss.bssid))
5461 return -EINVAL;
5462 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005463 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5464 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5465
5466 if (info->attrs[NL80211_ATTR_IE]) {
5467 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5468 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5469 }
5470
Johannes Berg683b6d32012-11-08 21:25:48 +01005471 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5472 if (err)
5473 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005474
Johannes Berg683b6d32012-11-08 21:25:48 +01005475 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005476 return -EINVAL;
5477
Johannes Berg04a773a2009-04-19 21:24:32 +02005478 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005479 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005480
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005481 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5482 u8 *rates =
5483 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5484 int n_rates =
5485 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5486 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005487 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005488
Johannes Berg34850ab2011-07-18 18:08:35 +02005489 err = ieee80211_get_ratemask(sband, rates, n_rates,
5490 &ibss.basic_rates);
5491 if (err)
5492 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005493 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005494
5495 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5496 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5497 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5498 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005499
Johannes Berg4c476992010-10-04 21:36:35 +02005500 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305501 bool no_ht = false;
5502
Johannes Berg4c476992010-10-04 21:36:35 +02005503 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305504 info->attrs[NL80211_ATTR_KEYS],
5505 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005506 if (IS_ERR(connkeys))
5507 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305508
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005509 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5510 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305511 kfree(connkeys);
5512 return -EINVAL;
5513 }
Johannes Berg4c476992010-10-04 21:36:35 +02005514 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005515
Antonio Quartulli267335d2012-01-31 20:25:47 +01005516 ibss.control_port =
5517 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5518
Johannes Berg4c476992010-10-04 21:36:35 +02005519 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005520 if (err)
5521 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005522 return err;
5523}
5524
5525static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5526{
Johannes Berg4c476992010-10-04 21:36:35 +02005527 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5528 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005529
Johannes Berg4c476992010-10-04 21:36:35 +02005530 if (!rdev->ops->leave_ibss)
5531 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005532
Johannes Berg4c476992010-10-04 21:36:35 +02005533 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5534 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005535
Johannes Berg4c476992010-10-04 21:36:35 +02005536 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005537}
5538
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005539static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5540{
5541 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5542 struct net_device *dev = info->user_ptr[1];
5543 int mcast_rate[IEEE80211_NUM_BANDS];
5544 u32 nla_rate;
5545 int err;
5546
5547 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5548 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5549 return -EOPNOTSUPP;
5550
5551 if (!rdev->ops->set_mcast_rate)
5552 return -EOPNOTSUPP;
5553
5554 memset(mcast_rate, 0, sizeof(mcast_rate));
5555
5556 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5557 return -EINVAL;
5558
5559 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5560 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5561 return -EINVAL;
5562
5563 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5564
5565 return err;
5566}
5567
5568
Johannes Bergaff89a92009-07-01 21:26:51 +02005569#ifdef CONFIG_NL80211_TESTMODE
5570static struct genl_multicast_group nl80211_testmode_mcgrp = {
5571 .name = "testmode",
5572};
5573
5574static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5575{
Johannes Berg4c476992010-10-04 21:36:35 +02005576 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005577 int err;
5578
5579 if (!info->attrs[NL80211_ATTR_TESTDATA])
5580 return -EINVAL;
5581
Johannes Bergaff89a92009-07-01 21:26:51 +02005582 err = -EOPNOTSUPP;
5583 if (rdev->ops->testmode_cmd) {
5584 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005585 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005586 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5587 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5588 rdev->testmode_info = NULL;
5589 }
5590
Johannes Bergaff89a92009-07-01 21:26:51 +02005591 return err;
5592}
5593
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005594static int nl80211_testmode_dump(struct sk_buff *skb,
5595 struct netlink_callback *cb)
5596{
Johannes Berg00918d32011-12-13 17:22:05 +01005597 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005598 int err;
5599 long phy_idx;
5600 void *data = NULL;
5601 int data_len = 0;
5602
5603 if (cb->args[0]) {
5604 /*
5605 * 0 is a valid index, but not valid for args[0],
5606 * so we need to offset by 1.
5607 */
5608 phy_idx = cb->args[0] - 1;
5609 } else {
5610 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5611 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5612 nl80211_policy);
5613 if (err)
5614 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005615
Johannes Berg2bd7e352012-06-15 14:23:16 +02005616 mutex_lock(&cfg80211_mutex);
5617 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5618 nl80211_fam.attrbuf);
5619 if (IS_ERR(rdev)) {
5620 mutex_unlock(&cfg80211_mutex);
5621 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005622 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005623 phy_idx = rdev->wiphy_idx;
5624 rdev = NULL;
5625 mutex_unlock(&cfg80211_mutex);
5626
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005627 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5628 cb->args[1] =
5629 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5630 }
5631
5632 if (cb->args[1]) {
5633 data = nla_data((void *)cb->args[1]);
5634 data_len = nla_len((void *)cb->args[1]);
5635 }
5636
5637 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005638 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5639 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005640 mutex_unlock(&cfg80211_mutex);
5641 return -ENOENT;
5642 }
Johannes Berg00918d32011-12-13 17:22:05 +01005643 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005644 mutex_unlock(&cfg80211_mutex);
5645
Johannes Berg00918d32011-12-13 17:22:05 +01005646 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005647 err = -EOPNOTSUPP;
5648 goto out_err;
5649 }
5650
5651 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005652 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005653 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5654 NL80211_CMD_TESTMODE);
5655 struct nlattr *tmdata;
5656
David S. Miller9360ffd2012-03-29 04:41:26 -04005657 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005658 genlmsg_cancel(skb, hdr);
5659 break;
5660 }
5661
5662 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5663 if (!tmdata) {
5664 genlmsg_cancel(skb, hdr);
5665 break;
5666 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005667 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005668 nla_nest_end(skb, tmdata);
5669
5670 if (err == -ENOBUFS || err == -ENOENT) {
5671 genlmsg_cancel(skb, hdr);
5672 break;
5673 } else if (err) {
5674 genlmsg_cancel(skb, hdr);
5675 goto out_err;
5676 }
5677
5678 genlmsg_end(skb, hdr);
5679 }
5680
5681 err = skb->len;
5682 /* see above */
5683 cb->args[0] = phy_idx + 1;
5684 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005685 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005686 return err;
5687}
5688
Johannes Bergaff89a92009-07-01 21:26:51 +02005689static struct sk_buff *
5690__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005691 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005692{
5693 struct sk_buff *skb;
5694 void *hdr;
5695 struct nlattr *data;
5696
5697 skb = nlmsg_new(approxlen + 100, gfp);
5698 if (!skb)
5699 return NULL;
5700
Eric W. Biederman15e47302012-09-07 20:12:54 +00005701 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005702 if (!hdr) {
5703 kfree_skb(skb);
5704 return NULL;
5705 }
5706
David S. Miller9360ffd2012-03-29 04:41:26 -04005707 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5708 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005709 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5710
5711 ((void **)skb->cb)[0] = rdev;
5712 ((void **)skb->cb)[1] = hdr;
5713 ((void **)skb->cb)[2] = data;
5714
5715 return skb;
5716
5717 nla_put_failure:
5718 kfree_skb(skb);
5719 return NULL;
5720}
5721
5722struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5723 int approxlen)
5724{
5725 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5726
5727 if (WARN_ON(!rdev->testmode_info))
5728 return NULL;
5729
5730 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005731 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005732 rdev->testmode_info->snd_seq,
5733 GFP_KERNEL);
5734}
5735EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5736
5737int cfg80211_testmode_reply(struct sk_buff *skb)
5738{
5739 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5740 void *hdr = ((void **)skb->cb)[1];
5741 struct nlattr *data = ((void **)skb->cb)[2];
5742
5743 if (WARN_ON(!rdev->testmode_info)) {
5744 kfree_skb(skb);
5745 return -EINVAL;
5746 }
5747
5748 nla_nest_end(skb, data);
5749 genlmsg_end(skb, hdr);
5750 return genlmsg_reply(skb, rdev->testmode_info);
5751}
5752EXPORT_SYMBOL(cfg80211_testmode_reply);
5753
5754struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5755 int approxlen, gfp_t gfp)
5756{
5757 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5758
5759 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5760}
5761EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5762
5763void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5764{
5765 void *hdr = ((void **)skb->cb)[1];
5766 struct nlattr *data = ((void **)skb->cb)[2];
5767
5768 nla_nest_end(skb, data);
5769 genlmsg_end(skb, hdr);
5770 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5771}
5772EXPORT_SYMBOL(cfg80211_testmode_event);
5773#endif
5774
Samuel Ortizb23aa672009-07-01 21:26:54 +02005775static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5776{
Johannes Berg4c476992010-10-04 21:36:35 +02005777 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5778 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005779 struct cfg80211_connect_params connect;
5780 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005781 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005782 int err;
5783
5784 memset(&connect, 0, sizeof(connect));
5785
5786 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5787 return -EINVAL;
5788
5789 if (!info->attrs[NL80211_ATTR_SSID] ||
5790 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5791 return -EINVAL;
5792
5793 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5794 connect.auth_type =
5795 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005796 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5797 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005798 return -EINVAL;
5799 } else
5800 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5801
5802 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5803
Johannes Bergc0692b82010-08-27 14:26:53 +03005804 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005805 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005806 if (err)
5807 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005808
Johannes Berg074ac8d2010-09-16 14:58:22 +02005809 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005810 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5811 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005812
Johannes Berg79c97e92009-07-07 03:56:12 +02005813 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005814
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305815 connect.bg_scan_period = -1;
5816 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5817 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5818 connect.bg_scan_period =
5819 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5820 }
5821
Samuel Ortizb23aa672009-07-01 21:26:54 +02005822 if (info->attrs[NL80211_ATTR_MAC])
5823 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5824 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5825 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5826
5827 if (info->attrs[NL80211_ATTR_IE]) {
5828 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5829 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5830 }
5831
5832 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5833 connect.channel =
5834 ieee80211_get_channel(wiphy,
5835 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5836 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005837 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5838 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005839 }
5840
Johannes Bergfffd0932009-07-08 14:22:54 +02005841 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5842 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305843 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005844 if (IS_ERR(connkeys))
5845 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005846 }
5847
Ben Greear7e7c8922011-11-18 11:31:59 -08005848 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5849 connect.flags |= ASSOC_REQ_DISABLE_HT;
5850
5851 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5852 memcpy(&connect.ht_capa_mask,
5853 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5854 sizeof(connect.ht_capa_mask));
5855
5856 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005857 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5858 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005859 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005860 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005861 memcpy(&connect.ht_capa,
5862 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5863 sizeof(connect.ht_capa));
5864 }
5865
Johannes Bergfffd0932009-07-08 14:22:54 +02005866 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005867 if (err)
5868 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005869 return err;
5870}
5871
5872static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5873{
Johannes Berg4c476992010-10-04 21:36:35 +02005874 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5875 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005876 u16 reason;
5877
5878 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5879 reason = WLAN_REASON_DEAUTH_LEAVING;
5880 else
5881 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5882
5883 if (reason == 0)
5884 return -EINVAL;
5885
Johannes Berg074ac8d2010-09-16 14:58:22 +02005886 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005887 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5888 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005889
Johannes Berg4c476992010-10-04 21:36:35 +02005890 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005891}
5892
Johannes Berg463d0182009-07-14 00:33:35 +02005893static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5894{
Johannes Berg4c476992010-10-04 21:36:35 +02005895 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005896 struct net *net;
5897 int err;
5898 u32 pid;
5899
5900 if (!info->attrs[NL80211_ATTR_PID])
5901 return -EINVAL;
5902
5903 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5904
Johannes Berg463d0182009-07-14 00:33:35 +02005905 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005906 if (IS_ERR(net))
5907 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005908
5909 err = 0;
5910
5911 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005912 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5913 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005914
Johannes Berg463d0182009-07-14 00:33:35 +02005915 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005916 return err;
5917}
5918
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005919static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5920{
Johannes Berg4c476992010-10-04 21:36:35 +02005921 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005922 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5923 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005924 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005925 struct cfg80211_pmksa pmksa;
5926
5927 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5928
5929 if (!info->attrs[NL80211_ATTR_MAC])
5930 return -EINVAL;
5931
5932 if (!info->attrs[NL80211_ATTR_PMKID])
5933 return -EINVAL;
5934
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005935 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5936 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5937
Johannes Berg074ac8d2010-09-16 14:58:22 +02005938 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005939 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5940 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005941
5942 switch (info->genlhdr->cmd) {
5943 case NL80211_CMD_SET_PMKSA:
5944 rdev_ops = rdev->ops->set_pmksa;
5945 break;
5946 case NL80211_CMD_DEL_PMKSA:
5947 rdev_ops = rdev->ops->del_pmksa;
5948 break;
5949 default:
5950 WARN_ON(1);
5951 break;
5952 }
5953
Johannes Berg4c476992010-10-04 21:36:35 +02005954 if (!rdev_ops)
5955 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005956
Johannes Berg4c476992010-10-04 21:36:35 +02005957 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005958}
5959
5960static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5961{
Johannes Berg4c476992010-10-04 21:36:35 +02005962 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5963 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005964
Johannes Berg074ac8d2010-09-16 14:58:22 +02005965 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005966 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5967 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005968
Johannes Berg4c476992010-10-04 21:36:35 +02005969 if (!rdev->ops->flush_pmksa)
5970 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005971
Hila Gonene35e4d22012-06-27 17:19:42 +03005972 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005973}
5974
Arik Nemtsov109086c2011-09-28 14:12:50 +03005975static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5976{
5977 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5978 struct net_device *dev = info->user_ptr[1];
5979 u8 action_code, dialog_token;
5980 u16 status_code;
5981 u8 *peer;
5982
5983 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5984 !rdev->ops->tdls_mgmt)
5985 return -EOPNOTSUPP;
5986
5987 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5988 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5989 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5990 !info->attrs[NL80211_ATTR_IE] ||
5991 !info->attrs[NL80211_ATTR_MAC])
5992 return -EINVAL;
5993
5994 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5995 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5996 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5997 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5998
Hila Gonene35e4d22012-06-27 17:19:42 +03005999 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6000 dialog_token, status_code,
6001 nla_data(info->attrs[NL80211_ATTR_IE]),
6002 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006003}
6004
6005static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6006{
6007 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6008 struct net_device *dev = info->user_ptr[1];
6009 enum nl80211_tdls_operation operation;
6010 u8 *peer;
6011
6012 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6013 !rdev->ops->tdls_oper)
6014 return -EOPNOTSUPP;
6015
6016 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6017 !info->attrs[NL80211_ATTR_MAC])
6018 return -EINVAL;
6019
6020 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6021 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6022
Hila Gonene35e4d22012-06-27 17:19:42 +03006023 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006024}
6025
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006026static int nl80211_remain_on_channel(struct sk_buff *skb,
6027 struct genl_info *info)
6028{
Johannes Berg4c476992010-10-04 21:36:35 +02006029 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006030 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006031 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006032 struct sk_buff *msg;
6033 void *hdr;
6034 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006035 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006036 int err;
6037
6038 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6039 !info->attrs[NL80211_ATTR_DURATION])
6040 return -EINVAL;
6041
6042 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6043
Johannes Berg7c4ef712011-11-18 15:33:48 +01006044 if (!rdev->ops->remain_on_channel ||
6045 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006046 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006047
Johannes Bergebf348f2012-06-01 12:50:54 +02006048 /*
6049 * We should be on that channel for at least a minimum amount of
6050 * time (10ms) but no longer than the driver supports.
6051 */
6052 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6053 duration > rdev->wiphy.max_remain_on_channel_duration)
6054 return -EINVAL;
6055
Johannes Berg683b6d32012-11-08 21:25:48 +01006056 err = nl80211_parse_chandef(rdev, info, &chandef);
6057 if (err)
6058 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006059
6060 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006061 if (!msg)
6062 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006063
Eric W. Biederman15e47302012-09-07 20:12:54 +00006064 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006065 NL80211_CMD_REMAIN_ON_CHANNEL);
6066
6067 if (IS_ERR(hdr)) {
6068 err = PTR_ERR(hdr);
6069 goto free_msg;
6070 }
6071
Johannes Berg683b6d32012-11-08 21:25:48 +01006072 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6073 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006074
6075 if (err)
6076 goto free_msg;
6077
David S. Miller9360ffd2012-03-29 04:41:26 -04006078 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6079 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006080
6081 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006082
6083 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006084
6085 nla_put_failure:
6086 err = -ENOBUFS;
6087 free_msg:
6088 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006089 return err;
6090}
6091
6092static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6093 struct genl_info *info)
6094{
Johannes Berg4c476992010-10-04 21:36:35 +02006095 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006096 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006097 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006098
6099 if (!info->attrs[NL80211_ATTR_COOKIE])
6100 return -EINVAL;
6101
Johannes Berg4c476992010-10-04 21:36:35 +02006102 if (!rdev->ops->cancel_remain_on_channel)
6103 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006104
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006105 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6106
Hila Gonene35e4d22012-06-27 17:19:42 +03006107 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006108}
6109
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006110static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6111 u8 *rates, u8 rates_len)
6112{
6113 u8 i;
6114 u32 mask = 0;
6115
6116 for (i = 0; i < rates_len; i++) {
6117 int rate = (rates[i] & 0x7f) * 5;
6118 int ridx;
6119 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6120 struct ieee80211_rate *srate =
6121 &sband->bitrates[ridx];
6122 if (rate == srate->bitrate) {
6123 mask |= 1 << ridx;
6124 break;
6125 }
6126 }
6127 if (ridx == sband->n_bitrates)
6128 return 0; /* rate not found */
6129 }
6130
6131 return mask;
6132}
6133
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006134static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6135 u8 *rates, u8 rates_len,
6136 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6137{
6138 u8 i;
6139
6140 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6141
6142 for (i = 0; i < rates_len; i++) {
6143 int ridx, rbit;
6144
6145 ridx = rates[i] / 8;
6146 rbit = BIT(rates[i] % 8);
6147
6148 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006149 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006150 return false;
6151
6152 /* check availability */
6153 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6154 mcs[ridx] |= rbit;
6155 else
6156 return false;
6157 }
6158
6159 return true;
6160}
6161
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006162static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006163 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6164 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006165 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6166 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006167};
6168
6169static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6170 struct genl_info *info)
6171{
6172 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006173 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006174 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006175 int rem, i;
6176 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006177 struct nlattr *tx_rates;
6178 struct ieee80211_supported_band *sband;
6179
6180 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6181 return -EINVAL;
6182
Johannes Berg4c476992010-10-04 21:36:35 +02006183 if (!rdev->ops->set_bitrate_mask)
6184 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006185
6186 memset(&mask, 0, sizeof(mask));
6187 /* Default to all rates enabled */
6188 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6189 sband = rdev->wiphy.bands[i];
6190 mask.control[i].legacy =
6191 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006192 if (sband)
6193 memcpy(mask.control[i].mcs,
6194 sband->ht_cap.mcs.rx_mask,
6195 sizeof(mask.control[i].mcs));
6196 else
6197 memset(mask.control[i].mcs, 0,
6198 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006199 }
6200
6201 /*
6202 * The nested attribute uses enum nl80211_band as the index. This maps
6203 * directly to the enum ieee80211_band values used in cfg80211.
6204 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006205 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006206 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6207 {
6208 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006209 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6210 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006211 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006212 if (sband == NULL)
6213 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006214 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6215 nla_len(tx_rates), nl80211_txattr_policy);
6216 if (tb[NL80211_TXRATE_LEGACY]) {
6217 mask.control[band].legacy = rateset_to_mask(
6218 sband,
6219 nla_data(tb[NL80211_TXRATE_LEGACY]),
6220 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306221 if ((mask.control[band].legacy == 0) &&
6222 nla_len(tb[NL80211_TXRATE_LEGACY]))
6223 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006224 }
6225 if (tb[NL80211_TXRATE_MCS]) {
6226 if (!ht_rateset_to_mask(
6227 sband,
6228 nla_data(tb[NL80211_TXRATE_MCS]),
6229 nla_len(tb[NL80211_TXRATE_MCS]),
6230 mask.control[band].mcs))
6231 return -EINVAL;
6232 }
6233
6234 if (mask.control[band].legacy == 0) {
6235 /* don't allow empty legacy rates if HT
6236 * is not even supported. */
6237 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6238 return -EINVAL;
6239
6240 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6241 if (mask.control[band].mcs[i])
6242 break;
6243
6244 /* legacy and mcs rates may not be both empty */
6245 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006246 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006247 }
6248 }
6249
Hila Gonene35e4d22012-06-27 17:19:42 +03006250 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006251}
6252
Johannes Berg2e161f72010-08-12 15:38:38 +02006253static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006254{
Johannes Berg4c476992010-10-04 21:36:35 +02006255 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006256 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006257 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006258
6259 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6260 return -EINVAL;
6261
Johannes Berg2e161f72010-08-12 15:38:38 +02006262 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6263 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006264
Johannes Berg71bbc992012-06-15 15:30:18 +02006265 switch (wdev->iftype) {
6266 case NL80211_IFTYPE_STATION:
6267 case NL80211_IFTYPE_ADHOC:
6268 case NL80211_IFTYPE_P2P_CLIENT:
6269 case NL80211_IFTYPE_AP:
6270 case NL80211_IFTYPE_AP_VLAN:
6271 case NL80211_IFTYPE_MESH_POINT:
6272 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006273 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006274 break;
6275 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006276 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006277 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006278
6279 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006280 if (!rdev->ops->mgmt_tx)
6281 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006282
Eric W. Biederman15e47302012-09-07 20:12:54 +00006283 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006284 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6285 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006286}
6287
Johannes Berg2e161f72010-08-12 15:38:38 +02006288static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006289{
Johannes Berg4c476992010-10-04 21:36:35 +02006290 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006291 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006292 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006293 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006294 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006295 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006296 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006297 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006298 bool offchan, no_cck, dont_wait_for_ack;
6299
6300 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006301
Johannes Berg683b6d32012-11-08 21:25:48 +01006302 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006303 return -EINVAL;
6304
Johannes Berg4c476992010-10-04 21:36:35 +02006305 if (!rdev->ops->mgmt_tx)
6306 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006307
Johannes Berg71bbc992012-06-15 15:30:18 +02006308 switch (wdev->iftype) {
6309 case NL80211_IFTYPE_STATION:
6310 case NL80211_IFTYPE_ADHOC:
6311 case NL80211_IFTYPE_P2P_CLIENT:
6312 case NL80211_IFTYPE_AP:
6313 case NL80211_IFTYPE_AP_VLAN:
6314 case NL80211_IFTYPE_MESH_POINT:
6315 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006316 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006317 break;
6318 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006319 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006320 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006321
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006322 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006323 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006324 return -EINVAL;
6325 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006326
6327 /*
6328 * We should wait on the channel for at least a minimum amount
6329 * of time (10ms) but no longer than the driver supports.
6330 */
6331 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6332 wait > rdev->wiphy.max_remain_on_channel_duration)
6333 return -EINVAL;
6334
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006335 }
6336
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006337 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6338
Johannes Berg7c4ef712011-11-18 15:33:48 +01006339 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6340 return -EINVAL;
6341
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306342 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6343
Johannes Berg683b6d32012-11-08 21:25:48 +01006344 err = nl80211_parse_chandef(rdev, info, &chandef);
6345 if (err)
6346 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006347
Johannes Berge247bd902011-11-04 11:18:21 +01006348 if (!dont_wait_for_ack) {
6349 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6350 if (!msg)
6351 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006352
Eric W. Biederman15e47302012-09-07 20:12:54 +00006353 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006354 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006355
Johannes Berge247bd902011-11-04 11:18:21 +01006356 if (IS_ERR(hdr)) {
6357 err = PTR_ERR(hdr);
6358 goto free_msg;
6359 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006360 }
Johannes Berge247bd902011-11-04 11:18:21 +01006361
Johannes Berg683b6d32012-11-08 21:25:48 +01006362 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006363 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6364 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006365 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006366 if (err)
6367 goto free_msg;
6368
Johannes Berge247bd902011-11-04 11:18:21 +01006369 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006370 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6371 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006372
Johannes Berge247bd902011-11-04 11:18:21 +01006373 genlmsg_end(msg, hdr);
6374 return genlmsg_reply(msg, info);
6375 }
6376
6377 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006378
6379 nla_put_failure:
6380 err = -ENOBUFS;
6381 free_msg:
6382 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006383 return err;
6384}
6385
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006386static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6387{
6388 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006389 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006390 u64 cookie;
6391
6392 if (!info->attrs[NL80211_ATTR_COOKIE])
6393 return -EINVAL;
6394
6395 if (!rdev->ops->mgmt_tx_cancel_wait)
6396 return -EOPNOTSUPP;
6397
Johannes Berg71bbc992012-06-15 15:30:18 +02006398 switch (wdev->iftype) {
6399 case NL80211_IFTYPE_STATION:
6400 case NL80211_IFTYPE_ADHOC:
6401 case NL80211_IFTYPE_P2P_CLIENT:
6402 case NL80211_IFTYPE_AP:
6403 case NL80211_IFTYPE_AP_VLAN:
6404 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006405 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006406 break;
6407 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006408 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006409 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006410
6411 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6412
Hila Gonene35e4d22012-06-27 17:19:42 +03006413 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006414}
6415
Kalle Valoffb9eb32010-02-17 17:58:10 +02006416static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6417{
Johannes Berg4c476992010-10-04 21:36:35 +02006418 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006419 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006420 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006421 u8 ps_state;
6422 bool state;
6423 int err;
6424
Johannes Berg4c476992010-10-04 21:36:35 +02006425 if (!info->attrs[NL80211_ATTR_PS_STATE])
6426 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006427
6428 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6429
Johannes Berg4c476992010-10-04 21:36:35 +02006430 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6431 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006432
6433 wdev = dev->ieee80211_ptr;
6434
Johannes Berg4c476992010-10-04 21:36:35 +02006435 if (!rdev->ops->set_power_mgmt)
6436 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006437
6438 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6439
6440 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006441 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006442
Hila Gonene35e4d22012-06-27 17:19:42 +03006443 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006444 if (!err)
6445 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006446 return err;
6447}
6448
6449static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6450{
Johannes Berg4c476992010-10-04 21:36:35 +02006451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006452 enum nl80211_ps_state ps_state;
6453 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006454 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006455 struct sk_buff *msg;
6456 void *hdr;
6457 int err;
6458
Kalle Valoffb9eb32010-02-17 17:58:10 +02006459 wdev = dev->ieee80211_ptr;
6460
Johannes Berg4c476992010-10-04 21:36:35 +02006461 if (!rdev->ops->set_power_mgmt)
6462 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006463
6464 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006465 if (!msg)
6466 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006467
Eric W. Biederman15e47302012-09-07 20:12:54 +00006468 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006469 NL80211_CMD_GET_POWER_SAVE);
6470 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006471 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006472 goto free_msg;
6473 }
6474
6475 if (wdev->ps)
6476 ps_state = NL80211_PS_ENABLED;
6477 else
6478 ps_state = NL80211_PS_DISABLED;
6479
David S. Miller9360ffd2012-03-29 04:41:26 -04006480 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6481 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006482
6483 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006484 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006485
Johannes Berg4c476992010-10-04 21:36:35 +02006486 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006487 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006488 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006489 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006490 return err;
6491}
6492
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006493static struct nla_policy
6494nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6495 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6496 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6497 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006498 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6499 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6500 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006501};
6502
Thomas Pedersen84f10702012-07-12 16:17:33 -07006503static int nl80211_set_cqm_txe(struct genl_info *info,
6504 u32 rate, u32 pkts, u32 intvl)
6505{
6506 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6507 struct wireless_dev *wdev;
6508 struct net_device *dev = info->user_ptr[1];
6509
6510 if ((rate < 0 || rate > 100) ||
6511 (intvl < 0 || intvl > NL80211_CQM_TXE_MAX_INTVL))
6512 return -EINVAL;
6513
6514 wdev = dev->ieee80211_ptr;
6515
6516 if (!rdev->ops->set_cqm_txe_config)
6517 return -EOPNOTSUPP;
6518
6519 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6520 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6521 return -EOPNOTSUPP;
6522
Hila Gonene35e4d22012-06-27 17:19:42 +03006523 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006524}
6525
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006526static int nl80211_set_cqm_rssi(struct genl_info *info,
6527 s32 threshold, u32 hysteresis)
6528{
Johannes Berg4c476992010-10-04 21:36:35 +02006529 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006530 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006531 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006532
6533 if (threshold > 0)
6534 return -EINVAL;
6535
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006536 wdev = dev->ieee80211_ptr;
6537
Johannes Berg4c476992010-10-04 21:36:35 +02006538 if (!rdev->ops->set_cqm_rssi_config)
6539 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006540
Johannes Berg074ac8d2010-09-16 14:58:22 +02006541 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006542 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6543 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006544
Hila Gonene35e4d22012-06-27 17:19:42 +03006545 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006546}
6547
6548static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6549{
6550 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6551 struct nlattr *cqm;
6552 int err;
6553
6554 cqm = info->attrs[NL80211_ATTR_CQM];
6555 if (!cqm) {
6556 err = -EINVAL;
6557 goto out;
6558 }
6559
6560 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6561 nl80211_attr_cqm_policy);
6562 if (err)
6563 goto out;
6564
6565 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6566 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6567 s32 threshold;
6568 u32 hysteresis;
6569 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6570 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6571 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006572 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6573 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6574 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6575 u32 rate, pkts, intvl;
6576 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6577 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6578 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6579 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006580 } else
6581 err = -EINVAL;
6582
6583out:
6584 return err;
6585}
6586
Johannes Berg29cbe682010-12-03 09:20:44 +01006587static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6588{
6589 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6590 struct net_device *dev = info->user_ptr[1];
6591 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006592 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006593 int err;
6594
6595 /* start with default */
6596 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006597 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006598
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006599 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006600 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006601 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006602 if (err)
6603 return err;
6604 }
6605
6606 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6607 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6608 return -EINVAL;
6609
Javier Cardonac80d5452010-12-16 17:37:49 -08006610 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6611 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6612
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006613 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6614 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6615 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6616 return -EINVAL;
6617
Javier Cardonac80d5452010-12-16 17:37:49 -08006618 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6619 /* parse additional setup parameters if given */
6620 err = nl80211_parse_mesh_setup(info, &setup);
6621 if (err)
6622 return err;
6623 }
6624
Johannes Bergcc1d2802012-05-16 23:50:20 +02006625 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01006626 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
6627 if (err)
6628 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006629 } else {
6630 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01006631 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006632 }
6633
Javier Cardonac80d5452010-12-16 17:37:49 -08006634 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006635}
6636
6637static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6638{
6639 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6640 struct net_device *dev = info->user_ptr[1];
6641
6642 return cfg80211_leave_mesh(rdev, dev);
6643}
6644
Johannes Bergdfb89c52012-06-27 09:23:48 +02006645#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006646static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6647{
6648 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6649 struct sk_buff *msg;
6650 void *hdr;
6651
6652 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6653 return -EOPNOTSUPP;
6654
6655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6656 if (!msg)
6657 return -ENOMEM;
6658
Eric W. Biederman15e47302012-09-07 20:12:54 +00006659 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006660 NL80211_CMD_GET_WOWLAN);
6661 if (!hdr)
6662 goto nla_put_failure;
6663
6664 if (rdev->wowlan) {
6665 struct nlattr *nl_wowlan;
6666
6667 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6668 if (!nl_wowlan)
6669 goto nla_put_failure;
6670
David S. Miller9360ffd2012-03-29 04:41:26 -04006671 if ((rdev->wowlan->any &&
6672 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6673 (rdev->wowlan->disconnect &&
6674 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6675 (rdev->wowlan->magic_pkt &&
6676 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6677 (rdev->wowlan->gtk_rekey_failure &&
6678 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6679 (rdev->wowlan->eap_identity_req &&
6680 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6681 (rdev->wowlan->four_way_handshake &&
6682 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6683 (rdev->wowlan->rfkill_release &&
6684 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6685 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006686 if (rdev->wowlan->n_patterns) {
6687 struct nlattr *nl_pats, *nl_pat;
6688 int i, pat_len;
6689
6690 nl_pats = nla_nest_start(msg,
6691 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6692 if (!nl_pats)
6693 goto nla_put_failure;
6694
6695 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6696 nl_pat = nla_nest_start(msg, i + 1);
6697 if (!nl_pat)
6698 goto nla_put_failure;
6699 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006700 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6701 DIV_ROUND_UP(pat_len, 8),
6702 rdev->wowlan->patterns[i].mask) ||
6703 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6704 pat_len,
6705 rdev->wowlan->patterns[i].pattern))
6706 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006707 nla_nest_end(msg, nl_pat);
6708 }
6709 nla_nest_end(msg, nl_pats);
6710 }
6711
6712 nla_nest_end(msg, nl_wowlan);
6713 }
6714
6715 genlmsg_end(msg, hdr);
6716 return genlmsg_reply(msg, info);
6717
6718nla_put_failure:
6719 nlmsg_free(msg);
6720 return -ENOBUFS;
6721}
6722
6723static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6724{
6725 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6726 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006727 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006728 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006729 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6730 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006731 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006732
6733 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6734 return -EOPNOTSUPP;
6735
Johannes Bergae33bd82012-07-12 16:25:02 +02006736 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6737 cfg80211_rdev_free_wowlan(rdev);
6738 rdev->wowlan = NULL;
6739 goto set_wakeup;
6740 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006741
6742 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6743 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6744 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6745 nl80211_wowlan_policy);
6746 if (err)
6747 return err;
6748
6749 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6750 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6751 return -EINVAL;
6752 new_triggers.any = true;
6753 }
6754
6755 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6756 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6757 return -EINVAL;
6758 new_triggers.disconnect = true;
6759 }
6760
6761 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6762 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6763 return -EINVAL;
6764 new_triggers.magic_pkt = true;
6765 }
6766
Johannes Berg77dbbb12011-07-13 10:48:55 +02006767 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6768 return -EINVAL;
6769
6770 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6771 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6772 return -EINVAL;
6773 new_triggers.gtk_rekey_failure = true;
6774 }
6775
6776 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6777 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6778 return -EINVAL;
6779 new_triggers.eap_identity_req = true;
6780 }
6781
6782 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6783 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6784 return -EINVAL;
6785 new_triggers.four_way_handshake = true;
6786 }
6787
6788 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6789 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6790 return -EINVAL;
6791 new_triggers.rfkill_release = true;
6792 }
6793
Johannes Bergff1b6e62011-05-04 15:37:28 +02006794 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6795 struct nlattr *pat;
6796 int n_patterns = 0;
6797 int rem, pat_len, mask_len;
6798 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6799
6800 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6801 rem)
6802 n_patterns++;
6803 if (n_patterns > wowlan->n_patterns)
6804 return -EINVAL;
6805
6806 new_triggers.patterns = kcalloc(n_patterns,
6807 sizeof(new_triggers.patterns[0]),
6808 GFP_KERNEL);
6809 if (!new_triggers.patterns)
6810 return -ENOMEM;
6811
6812 new_triggers.n_patterns = n_patterns;
6813 i = 0;
6814
6815 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6816 rem) {
6817 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6818 nla_data(pat), nla_len(pat), NULL);
6819 err = -EINVAL;
6820 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6821 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6822 goto error;
6823 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6824 mask_len = DIV_ROUND_UP(pat_len, 8);
6825 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6826 mask_len)
6827 goto error;
6828 if (pat_len > wowlan->pattern_max_len ||
6829 pat_len < wowlan->pattern_min_len)
6830 goto error;
6831
6832 new_triggers.patterns[i].mask =
6833 kmalloc(mask_len + pat_len, GFP_KERNEL);
6834 if (!new_triggers.patterns[i].mask) {
6835 err = -ENOMEM;
6836 goto error;
6837 }
6838 new_triggers.patterns[i].pattern =
6839 new_triggers.patterns[i].mask + mask_len;
6840 memcpy(new_triggers.patterns[i].mask,
6841 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6842 mask_len);
6843 new_triggers.patterns[i].pattern_len = pat_len;
6844 memcpy(new_triggers.patterns[i].pattern,
6845 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6846 pat_len);
6847 i++;
6848 }
6849 }
6850
Johannes Bergae33bd82012-07-12 16:25:02 +02006851 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6852 if (!ntrig) {
6853 err = -ENOMEM;
6854 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006855 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006856 cfg80211_rdev_free_wowlan(rdev);
6857 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006858
Johannes Bergae33bd82012-07-12 16:25:02 +02006859 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006860 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03006861 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02006862
Johannes Bergff1b6e62011-05-04 15:37:28 +02006863 return 0;
6864 error:
6865 for (i = 0; i < new_triggers.n_patterns; i++)
6866 kfree(new_triggers.patterns[i].mask);
6867 kfree(new_triggers.patterns);
6868 return err;
6869}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006870#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006871
Johannes Berge5497d72011-07-05 16:35:40 +02006872static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6873{
6874 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6875 struct net_device *dev = info->user_ptr[1];
6876 struct wireless_dev *wdev = dev->ieee80211_ptr;
6877 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6878 struct cfg80211_gtk_rekey_data rekey_data;
6879 int err;
6880
6881 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6882 return -EINVAL;
6883
6884 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6885 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6886 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6887 nl80211_rekey_policy);
6888 if (err)
6889 return err;
6890
6891 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6892 return -ERANGE;
6893 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6894 return -ERANGE;
6895 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6896 return -ERANGE;
6897
6898 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6899 NL80211_KEK_LEN);
6900 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6901 NL80211_KCK_LEN);
6902 memcpy(rekey_data.replay_ctr,
6903 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6904 NL80211_REPLAY_CTR_LEN);
6905
6906 wdev_lock(wdev);
6907 if (!wdev->current_bss) {
6908 err = -ENOTCONN;
6909 goto out;
6910 }
6911
6912 if (!rdev->ops->set_rekey_data) {
6913 err = -EOPNOTSUPP;
6914 goto out;
6915 }
6916
Hila Gonene35e4d22012-06-27 17:19:42 +03006917 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02006918 out:
6919 wdev_unlock(wdev);
6920 return err;
6921}
6922
Johannes Berg28946da2011-11-04 11:18:12 +01006923static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6924 struct genl_info *info)
6925{
6926 struct net_device *dev = info->user_ptr[1];
6927 struct wireless_dev *wdev = dev->ieee80211_ptr;
6928
6929 if (wdev->iftype != NL80211_IFTYPE_AP &&
6930 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6931 return -EINVAL;
6932
Eric W. Biederman15e47302012-09-07 20:12:54 +00006933 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006934 return -EBUSY;
6935
Eric W. Biederman15e47302012-09-07 20:12:54 +00006936 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006937 return 0;
6938}
6939
Johannes Berg7f6cf312011-11-04 11:18:15 +01006940static int nl80211_probe_client(struct sk_buff *skb,
6941 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 sk_buff *msg;
6947 void *hdr;
6948 const u8 *addr;
6949 u64 cookie;
6950 int err;
6951
6952 if (wdev->iftype != NL80211_IFTYPE_AP &&
6953 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6954 return -EOPNOTSUPP;
6955
6956 if (!info->attrs[NL80211_ATTR_MAC])
6957 return -EINVAL;
6958
6959 if (!rdev->ops->probe_client)
6960 return -EOPNOTSUPP;
6961
6962 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6963 if (!msg)
6964 return -ENOMEM;
6965
Eric W. Biederman15e47302012-09-07 20:12:54 +00006966 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01006967 NL80211_CMD_PROBE_CLIENT);
6968
6969 if (IS_ERR(hdr)) {
6970 err = PTR_ERR(hdr);
6971 goto free_msg;
6972 }
6973
6974 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6975
Hila Gonene35e4d22012-06-27 17:19:42 +03006976 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01006977 if (err)
6978 goto free_msg;
6979
David S. Miller9360ffd2012-03-29 04:41:26 -04006980 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6981 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006982
6983 genlmsg_end(msg, hdr);
6984
6985 return genlmsg_reply(msg, info);
6986
6987 nla_put_failure:
6988 err = -ENOBUFS;
6989 free_msg:
6990 nlmsg_free(msg);
6991 return err;
6992}
6993
Johannes Berg5e760232011-11-04 11:18:17 +01006994static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6995{
6996 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07006997 struct cfg80211_beacon_registration *reg, *nreg;
6998 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01006999
7000 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7001 return -EOPNOTSUPP;
7002
Ben Greear37c73b52012-10-26 14:49:25 -07007003 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7004 if (!nreg)
7005 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007006
Ben Greear37c73b52012-10-26 14:49:25 -07007007 /* First, check if already registered. */
7008 spin_lock_bh(&rdev->beacon_registrations_lock);
7009 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7010 if (reg->nlportid == info->snd_portid) {
7011 rv = -EALREADY;
7012 goto out_err;
7013 }
7014 }
7015 /* Add it to the list */
7016 nreg->nlportid = info->snd_portid;
7017 list_add(&nreg->list, &rdev->beacon_registrations);
7018
7019 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007020
7021 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007022out_err:
7023 spin_unlock_bh(&rdev->beacon_registrations_lock);
7024 kfree(nreg);
7025 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007026}
7027
Johannes Berg98104fde2012-06-16 00:19:54 +02007028static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7029{
7030 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7031 struct wireless_dev *wdev = info->user_ptr[1];
7032 int err;
7033
7034 if (!rdev->ops->start_p2p_device)
7035 return -EOPNOTSUPP;
7036
7037 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7038 return -EOPNOTSUPP;
7039
7040 if (wdev->p2p_started)
7041 return 0;
7042
7043 mutex_lock(&rdev->devlist_mtx);
7044 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7045 mutex_unlock(&rdev->devlist_mtx);
7046 if (err)
7047 return err;
7048
Johannes Bergeeb126e2012-10-23 15:16:50 +02007049 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007050 if (err)
7051 return err;
7052
7053 wdev->p2p_started = true;
7054 mutex_lock(&rdev->devlist_mtx);
7055 rdev->opencount++;
7056 mutex_unlock(&rdev->devlist_mtx);
7057
7058 return 0;
7059}
7060
7061static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7062{
7063 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7064 struct wireless_dev *wdev = info->user_ptr[1];
7065
7066 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7067 return -EOPNOTSUPP;
7068
7069 if (!rdev->ops->stop_p2p_device)
7070 return -EOPNOTSUPP;
7071
7072 if (!wdev->p2p_started)
7073 return 0;
7074
Johannes Bergeeb126e2012-10-23 15:16:50 +02007075 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007076 wdev->p2p_started = false;
7077
7078 mutex_lock(&rdev->devlist_mtx);
7079 rdev->opencount--;
7080 mutex_unlock(&rdev->devlist_mtx);
7081
7082 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7083 rdev->scan_req->aborted = true;
7084 ___cfg80211_scan_done(rdev, true);
7085 }
7086
7087 return 0;
7088}
7089
Johannes Berg4c476992010-10-04 21:36:35 +02007090#define NL80211_FLAG_NEED_WIPHY 0x01
7091#define NL80211_FLAG_NEED_NETDEV 0x02
7092#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007093#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7094#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7095 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007096#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007097/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007098#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7099 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007100
7101static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7102 struct genl_info *info)
7103{
7104 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007105 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007106 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007107 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7108
7109 if (rtnl)
7110 rtnl_lock();
7111
7112 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007113 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007114 if (IS_ERR(rdev)) {
7115 if (rtnl)
7116 rtnl_unlock();
7117 return PTR_ERR(rdev);
7118 }
7119 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007120 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7121 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007122 mutex_lock(&cfg80211_mutex);
7123 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7124 info->attrs);
7125 if (IS_ERR(wdev)) {
7126 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007127 if (rtnl)
7128 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007129 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007130 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007131
Johannes Berg89a54e42012-06-15 14:33:17 +02007132 dev = wdev->netdev;
7133 rdev = wiphy_to_dev(wdev->wiphy);
7134
Johannes Berg1bf614e2012-06-15 15:23:36 +02007135 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7136 if (!dev) {
7137 mutex_unlock(&cfg80211_mutex);
7138 if (rtnl)
7139 rtnl_unlock();
7140 return -EINVAL;
7141 }
7142
7143 info->user_ptr[1] = dev;
7144 } else {
7145 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007146 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007147
Johannes Berg1bf614e2012-06-15 15:23:36 +02007148 if (dev) {
7149 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7150 !netif_running(dev)) {
7151 mutex_unlock(&cfg80211_mutex);
7152 if (rtnl)
7153 rtnl_unlock();
7154 return -ENETDOWN;
7155 }
7156
7157 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007158 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7159 if (!wdev->p2p_started) {
7160 mutex_unlock(&cfg80211_mutex);
7161 if (rtnl)
7162 rtnl_unlock();
7163 return -ENETDOWN;
7164 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007165 }
7166
Johannes Berg89a54e42012-06-15 14:33:17 +02007167 cfg80211_lock_rdev(rdev);
7168
7169 mutex_unlock(&cfg80211_mutex);
7170
Johannes Berg4c476992010-10-04 21:36:35 +02007171 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007172 }
7173
7174 return 0;
7175}
7176
7177static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7178 struct genl_info *info)
7179{
7180 if (info->user_ptr[0])
7181 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007182 if (info->user_ptr[1]) {
7183 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7184 struct wireless_dev *wdev = info->user_ptr[1];
7185
7186 if (wdev->netdev)
7187 dev_put(wdev->netdev);
7188 } else {
7189 dev_put(info->user_ptr[1]);
7190 }
7191 }
Johannes Berg4c476992010-10-04 21:36:35 +02007192 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7193 rtnl_unlock();
7194}
7195
Johannes Berg55682962007-09-20 13:09:35 -04007196static struct genl_ops nl80211_ops[] = {
7197 {
7198 .cmd = NL80211_CMD_GET_WIPHY,
7199 .doit = nl80211_get_wiphy,
7200 .dumpit = nl80211_dump_wiphy,
7201 .policy = nl80211_policy,
7202 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007203 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007204 },
7205 {
7206 .cmd = NL80211_CMD_SET_WIPHY,
7207 .doit = nl80211_set_wiphy,
7208 .policy = nl80211_policy,
7209 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007210 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007211 },
7212 {
7213 .cmd = NL80211_CMD_GET_INTERFACE,
7214 .doit = nl80211_get_interface,
7215 .dumpit = nl80211_dump_interface,
7216 .policy = nl80211_policy,
7217 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007218 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007219 },
7220 {
7221 .cmd = NL80211_CMD_SET_INTERFACE,
7222 .doit = nl80211_set_interface,
7223 .policy = nl80211_policy,
7224 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007225 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7226 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007227 },
7228 {
7229 .cmd = NL80211_CMD_NEW_INTERFACE,
7230 .doit = nl80211_new_interface,
7231 .policy = nl80211_policy,
7232 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007233 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7234 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007235 },
7236 {
7237 .cmd = NL80211_CMD_DEL_INTERFACE,
7238 .doit = nl80211_del_interface,
7239 .policy = nl80211_policy,
7240 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007241 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007242 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007243 },
Johannes Berg41ade002007-12-19 02:03:29 +01007244 {
7245 .cmd = NL80211_CMD_GET_KEY,
7246 .doit = nl80211_get_key,
7247 .policy = nl80211_policy,
7248 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007249 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007250 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007251 },
7252 {
7253 .cmd = NL80211_CMD_SET_KEY,
7254 .doit = nl80211_set_key,
7255 .policy = nl80211_policy,
7256 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007257 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007258 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007259 },
7260 {
7261 .cmd = NL80211_CMD_NEW_KEY,
7262 .doit = nl80211_new_key,
7263 .policy = nl80211_policy,
7264 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007265 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007266 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007267 },
7268 {
7269 .cmd = NL80211_CMD_DEL_KEY,
7270 .doit = nl80211_del_key,
7271 .policy = nl80211_policy,
7272 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007273 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007274 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007275 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007276 {
7277 .cmd = NL80211_CMD_SET_BEACON,
7278 .policy = nl80211_policy,
7279 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007280 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007281 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007282 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007283 },
7284 {
Johannes Berg88600202012-02-13 15:17:18 +01007285 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007286 .policy = nl80211_policy,
7287 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007288 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007289 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007290 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007291 },
7292 {
Johannes Berg88600202012-02-13 15:17:18 +01007293 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007294 .policy = nl80211_policy,
7295 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007296 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007297 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007298 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007299 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007300 {
7301 .cmd = NL80211_CMD_GET_STATION,
7302 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007303 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007304 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007305 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7306 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007307 },
7308 {
7309 .cmd = NL80211_CMD_SET_STATION,
7310 .doit = nl80211_set_station,
7311 .policy = nl80211_policy,
7312 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007313 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007314 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007315 },
7316 {
7317 .cmd = NL80211_CMD_NEW_STATION,
7318 .doit = nl80211_new_station,
7319 .policy = nl80211_policy,
7320 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007321 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007322 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007323 },
7324 {
7325 .cmd = NL80211_CMD_DEL_STATION,
7326 .doit = nl80211_del_station,
7327 .policy = nl80211_policy,
7328 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007329 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007330 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007331 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007332 {
7333 .cmd = NL80211_CMD_GET_MPATH,
7334 .doit = nl80211_get_mpath,
7335 .dumpit = nl80211_dump_mpath,
7336 .policy = nl80211_policy,
7337 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007338 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007339 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007340 },
7341 {
7342 .cmd = NL80211_CMD_SET_MPATH,
7343 .doit = nl80211_set_mpath,
7344 .policy = nl80211_policy,
7345 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007346 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007347 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007348 },
7349 {
7350 .cmd = NL80211_CMD_NEW_MPATH,
7351 .doit = nl80211_new_mpath,
7352 .policy = nl80211_policy,
7353 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007354 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007355 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007356 },
7357 {
7358 .cmd = NL80211_CMD_DEL_MPATH,
7359 .doit = nl80211_del_mpath,
7360 .policy = nl80211_policy,
7361 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007362 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007363 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007364 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007365 {
7366 .cmd = NL80211_CMD_SET_BSS,
7367 .doit = nl80211_set_bss,
7368 .policy = nl80211_policy,
7369 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007370 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007371 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007372 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007373 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007374 .cmd = NL80211_CMD_GET_REG,
7375 .doit = nl80211_get_reg,
7376 .policy = nl80211_policy,
7377 /* can be retrieved by unprivileged users */
7378 },
7379 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007380 .cmd = NL80211_CMD_SET_REG,
7381 .doit = nl80211_set_reg,
7382 .policy = nl80211_policy,
7383 .flags = GENL_ADMIN_PERM,
7384 },
7385 {
7386 .cmd = NL80211_CMD_REQ_SET_REG,
7387 .doit = nl80211_req_set_reg,
7388 .policy = nl80211_policy,
7389 .flags = GENL_ADMIN_PERM,
7390 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007391 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007392 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7393 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007394 .policy = nl80211_policy,
7395 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007396 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007397 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007398 },
7399 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007400 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7401 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007402 .policy = nl80211_policy,
7403 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007404 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007405 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007406 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007407 {
Johannes Berg2a519312009-02-10 21:25:55 +01007408 .cmd = NL80211_CMD_TRIGGER_SCAN,
7409 .doit = nl80211_trigger_scan,
7410 .policy = nl80211_policy,
7411 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007412 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007413 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007414 },
7415 {
7416 .cmd = NL80211_CMD_GET_SCAN,
7417 .policy = nl80211_policy,
7418 .dumpit = nl80211_dump_scan,
7419 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007420 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007421 .cmd = NL80211_CMD_START_SCHED_SCAN,
7422 .doit = nl80211_start_sched_scan,
7423 .policy = nl80211_policy,
7424 .flags = GENL_ADMIN_PERM,
7425 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7426 NL80211_FLAG_NEED_RTNL,
7427 },
7428 {
7429 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7430 .doit = nl80211_stop_sched_scan,
7431 .policy = nl80211_policy,
7432 .flags = GENL_ADMIN_PERM,
7433 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7434 NL80211_FLAG_NEED_RTNL,
7435 },
7436 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007437 .cmd = NL80211_CMD_AUTHENTICATE,
7438 .doit = nl80211_authenticate,
7439 .policy = nl80211_policy,
7440 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007441 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007442 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007443 },
7444 {
7445 .cmd = NL80211_CMD_ASSOCIATE,
7446 .doit = nl80211_associate,
7447 .policy = nl80211_policy,
7448 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007449 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007450 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007451 },
7452 {
7453 .cmd = NL80211_CMD_DEAUTHENTICATE,
7454 .doit = nl80211_deauthenticate,
7455 .policy = nl80211_policy,
7456 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007457 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007458 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007459 },
7460 {
7461 .cmd = NL80211_CMD_DISASSOCIATE,
7462 .doit = nl80211_disassociate,
7463 .policy = nl80211_policy,
7464 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007465 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007466 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007467 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007468 {
7469 .cmd = NL80211_CMD_JOIN_IBSS,
7470 .doit = nl80211_join_ibss,
7471 .policy = nl80211_policy,
7472 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007473 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007474 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007475 },
7476 {
7477 .cmd = NL80211_CMD_LEAVE_IBSS,
7478 .doit = nl80211_leave_ibss,
7479 .policy = nl80211_policy,
7480 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007481 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007482 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007483 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007484#ifdef CONFIG_NL80211_TESTMODE
7485 {
7486 .cmd = NL80211_CMD_TESTMODE,
7487 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007488 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007489 .policy = nl80211_policy,
7490 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007491 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7492 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007493 },
7494#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007495 {
7496 .cmd = NL80211_CMD_CONNECT,
7497 .doit = nl80211_connect,
7498 .policy = nl80211_policy,
7499 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007500 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007501 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007502 },
7503 {
7504 .cmd = NL80211_CMD_DISCONNECT,
7505 .doit = nl80211_disconnect,
7506 .policy = nl80211_policy,
7507 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007508 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007509 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007510 },
Johannes Berg463d0182009-07-14 00:33:35 +02007511 {
7512 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7513 .doit = nl80211_wiphy_netns,
7514 .policy = nl80211_policy,
7515 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007516 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7517 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007518 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007519 {
7520 .cmd = NL80211_CMD_GET_SURVEY,
7521 .policy = nl80211_policy,
7522 .dumpit = nl80211_dump_survey,
7523 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007524 {
7525 .cmd = NL80211_CMD_SET_PMKSA,
7526 .doit = nl80211_setdel_pmksa,
7527 .policy = nl80211_policy,
7528 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007529 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007530 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007531 },
7532 {
7533 .cmd = NL80211_CMD_DEL_PMKSA,
7534 .doit = nl80211_setdel_pmksa,
7535 .policy = nl80211_policy,
7536 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007537 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007538 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007539 },
7540 {
7541 .cmd = NL80211_CMD_FLUSH_PMKSA,
7542 .doit = nl80211_flush_pmksa,
7543 .policy = nl80211_policy,
7544 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007545 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007546 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007547 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007548 {
7549 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7550 .doit = nl80211_remain_on_channel,
7551 .policy = nl80211_policy,
7552 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007553 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007554 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007555 },
7556 {
7557 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7558 .doit = nl80211_cancel_remain_on_channel,
7559 .policy = nl80211_policy,
7560 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007561 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007562 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007563 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007564 {
7565 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7566 .doit = nl80211_set_tx_bitrate_mask,
7567 .policy = nl80211_policy,
7568 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007569 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7570 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007571 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007572 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007573 .cmd = NL80211_CMD_REGISTER_FRAME,
7574 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007575 .policy = nl80211_policy,
7576 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007577 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007578 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007579 },
7580 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007581 .cmd = NL80211_CMD_FRAME,
7582 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007583 .policy = nl80211_policy,
7584 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007585 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007586 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007587 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007588 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007589 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7590 .doit = nl80211_tx_mgmt_cancel_wait,
7591 .policy = nl80211_policy,
7592 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007593 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007594 NL80211_FLAG_NEED_RTNL,
7595 },
7596 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007597 .cmd = NL80211_CMD_SET_POWER_SAVE,
7598 .doit = nl80211_set_power_save,
7599 .policy = nl80211_policy,
7600 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007601 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7602 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007603 },
7604 {
7605 .cmd = NL80211_CMD_GET_POWER_SAVE,
7606 .doit = nl80211_get_power_save,
7607 .policy = nl80211_policy,
7608 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007609 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7610 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007611 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007612 {
7613 .cmd = NL80211_CMD_SET_CQM,
7614 .doit = nl80211_set_cqm,
7615 .policy = nl80211_policy,
7616 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007617 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7618 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007619 },
Johannes Bergf444de02010-05-05 15:25:02 +02007620 {
7621 .cmd = NL80211_CMD_SET_CHANNEL,
7622 .doit = nl80211_set_channel,
7623 .policy = nl80211_policy,
7624 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007625 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7626 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007627 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007628 {
7629 .cmd = NL80211_CMD_SET_WDS_PEER,
7630 .doit = nl80211_set_wds_peer,
7631 .policy = nl80211_policy,
7632 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007633 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7634 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007635 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007636 {
7637 .cmd = NL80211_CMD_JOIN_MESH,
7638 .doit = nl80211_join_mesh,
7639 .policy = nl80211_policy,
7640 .flags = GENL_ADMIN_PERM,
7641 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7642 NL80211_FLAG_NEED_RTNL,
7643 },
7644 {
7645 .cmd = NL80211_CMD_LEAVE_MESH,
7646 .doit = nl80211_leave_mesh,
7647 .policy = nl80211_policy,
7648 .flags = GENL_ADMIN_PERM,
7649 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7650 NL80211_FLAG_NEED_RTNL,
7651 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007652#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007653 {
7654 .cmd = NL80211_CMD_GET_WOWLAN,
7655 .doit = nl80211_get_wowlan,
7656 .policy = nl80211_policy,
7657 /* can be retrieved by unprivileged users */
7658 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7659 NL80211_FLAG_NEED_RTNL,
7660 },
7661 {
7662 .cmd = NL80211_CMD_SET_WOWLAN,
7663 .doit = nl80211_set_wowlan,
7664 .policy = nl80211_policy,
7665 .flags = GENL_ADMIN_PERM,
7666 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7667 NL80211_FLAG_NEED_RTNL,
7668 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007669#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007670 {
7671 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7672 .doit = nl80211_set_rekey_data,
7673 .policy = nl80211_policy,
7674 .flags = GENL_ADMIN_PERM,
7675 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7676 NL80211_FLAG_NEED_RTNL,
7677 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007678 {
7679 .cmd = NL80211_CMD_TDLS_MGMT,
7680 .doit = nl80211_tdls_mgmt,
7681 .policy = nl80211_policy,
7682 .flags = GENL_ADMIN_PERM,
7683 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7684 NL80211_FLAG_NEED_RTNL,
7685 },
7686 {
7687 .cmd = NL80211_CMD_TDLS_OPER,
7688 .doit = nl80211_tdls_oper,
7689 .policy = nl80211_policy,
7690 .flags = GENL_ADMIN_PERM,
7691 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7692 NL80211_FLAG_NEED_RTNL,
7693 },
Johannes Berg28946da2011-11-04 11:18:12 +01007694 {
7695 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7696 .doit = nl80211_register_unexpected_frame,
7697 .policy = nl80211_policy,
7698 .flags = GENL_ADMIN_PERM,
7699 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7700 NL80211_FLAG_NEED_RTNL,
7701 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007702 {
7703 .cmd = NL80211_CMD_PROBE_CLIENT,
7704 .doit = nl80211_probe_client,
7705 .policy = nl80211_policy,
7706 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007707 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007708 NL80211_FLAG_NEED_RTNL,
7709 },
Johannes Berg5e760232011-11-04 11:18:17 +01007710 {
7711 .cmd = NL80211_CMD_REGISTER_BEACONS,
7712 .doit = nl80211_register_beacons,
7713 .policy = nl80211_policy,
7714 .flags = GENL_ADMIN_PERM,
7715 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7716 NL80211_FLAG_NEED_RTNL,
7717 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007718 {
7719 .cmd = NL80211_CMD_SET_NOACK_MAP,
7720 .doit = nl80211_set_noack_map,
7721 .policy = nl80211_policy,
7722 .flags = GENL_ADMIN_PERM,
7723 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7724 NL80211_FLAG_NEED_RTNL,
7725 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007726 {
7727 .cmd = NL80211_CMD_START_P2P_DEVICE,
7728 .doit = nl80211_start_p2p_device,
7729 .policy = nl80211_policy,
7730 .flags = GENL_ADMIN_PERM,
7731 .internal_flags = NL80211_FLAG_NEED_WDEV |
7732 NL80211_FLAG_NEED_RTNL,
7733 },
7734 {
7735 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7736 .doit = nl80211_stop_p2p_device,
7737 .policy = nl80211_policy,
7738 .flags = GENL_ADMIN_PERM,
7739 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7740 NL80211_FLAG_NEED_RTNL,
7741 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007742 {
7743 .cmd = NL80211_CMD_SET_MCAST_RATE,
7744 .doit = nl80211_set_mcast_rate,
7745 .policy = nl80211_policy,
7746 .flags = GENL_ADMIN_PERM,
7747 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7748 NL80211_FLAG_NEED_RTNL,
7749 },
Johannes Berg55682962007-09-20 13:09:35 -04007750};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007751
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007752static struct genl_multicast_group nl80211_mlme_mcgrp = {
7753 .name = "mlme",
7754};
Johannes Berg55682962007-09-20 13:09:35 -04007755
7756/* multicast groups */
7757static struct genl_multicast_group nl80211_config_mcgrp = {
7758 .name = "config",
7759};
Johannes Berg2a519312009-02-10 21:25:55 +01007760static struct genl_multicast_group nl80211_scan_mcgrp = {
7761 .name = "scan",
7762};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007763static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7764 .name = "regulatory",
7765};
Johannes Berg55682962007-09-20 13:09:35 -04007766
7767/* notification functions */
7768
7769void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7770{
7771 struct sk_buff *msg;
7772
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007773 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007774 if (!msg)
7775 return;
7776
7777 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7778 nlmsg_free(msg);
7779 return;
7780 }
7781
Johannes Berg463d0182009-07-14 00:33:35 +02007782 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7783 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007784}
7785
Johannes Berg362a4152009-05-24 16:43:15 +02007786static int nl80211_add_scan_req(struct sk_buff *msg,
7787 struct cfg80211_registered_device *rdev)
7788{
7789 struct cfg80211_scan_request *req = rdev->scan_req;
7790 struct nlattr *nest;
7791 int i;
7792
Johannes Berg667503d2009-07-07 03:56:11 +02007793 ASSERT_RDEV_LOCK(rdev);
7794
Johannes Berg362a4152009-05-24 16:43:15 +02007795 if (WARN_ON(!req))
7796 return 0;
7797
7798 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7799 if (!nest)
7800 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007801 for (i = 0; i < req->n_ssids; i++) {
7802 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7803 goto nla_put_failure;
7804 }
Johannes Berg362a4152009-05-24 16:43:15 +02007805 nla_nest_end(msg, nest);
7806
7807 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7808 if (!nest)
7809 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007810 for (i = 0; i < req->n_channels; i++) {
7811 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7812 goto nla_put_failure;
7813 }
Johannes Berg362a4152009-05-24 16:43:15 +02007814 nla_nest_end(msg, nest);
7815
David S. Miller9360ffd2012-03-29 04:41:26 -04007816 if (req->ie &&
7817 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7818 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007819
Sam Lefflered4737712012-10-11 21:03:31 -07007820 if (req->flags)
7821 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7822
Johannes Berg362a4152009-05-24 16:43:15 +02007823 return 0;
7824 nla_put_failure:
7825 return -ENOBUFS;
7826}
7827
Johannes Berga538e2d2009-06-16 19:56:42 +02007828static int nl80211_send_scan_msg(struct sk_buff *msg,
7829 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007830 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007831 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007832 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007833{
7834 void *hdr;
7835
Eric W. Biederman15e47302012-09-07 20:12:54 +00007836 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007837 if (!hdr)
7838 return -1;
7839
David S. Miller9360ffd2012-03-29 04:41:26 -04007840 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007841 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7842 wdev->netdev->ifindex)) ||
7843 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007844 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007845
Johannes Berg362a4152009-05-24 16:43:15 +02007846 /* ignore errors and send incomplete event anyway */
7847 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007848
7849 return genlmsg_end(msg, hdr);
7850
7851 nla_put_failure:
7852 genlmsg_cancel(msg, hdr);
7853 return -EMSGSIZE;
7854}
7855
Luciano Coelho807f8a82011-05-11 17:09:35 +03007856static int
7857nl80211_send_sched_scan_msg(struct sk_buff *msg,
7858 struct cfg80211_registered_device *rdev,
7859 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007860 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007861{
7862 void *hdr;
7863
Eric W. Biederman15e47302012-09-07 20:12:54 +00007864 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007865 if (!hdr)
7866 return -1;
7867
David S. Miller9360ffd2012-03-29 04:41:26 -04007868 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7869 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7870 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007871
7872 return genlmsg_end(msg, hdr);
7873
7874 nla_put_failure:
7875 genlmsg_cancel(msg, hdr);
7876 return -EMSGSIZE;
7877}
7878
Johannes Berga538e2d2009-06-16 19:56:42 +02007879void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007880 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007881{
7882 struct sk_buff *msg;
7883
Thomas Graf58050fc2012-06-28 03:57:45 +00007884 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007885 if (!msg)
7886 return;
7887
Johannes Bergfd014282012-06-18 19:17:03 +02007888 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007889 NL80211_CMD_TRIGGER_SCAN) < 0) {
7890 nlmsg_free(msg);
7891 return;
7892 }
7893
Johannes Berg463d0182009-07-14 00:33:35 +02007894 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7895 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007896}
7897
Johannes Berg2a519312009-02-10 21:25:55 +01007898void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007899 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007900{
7901 struct sk_buff *msg;
7902
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007903 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007904 if (!msg)
7905 return;
7906
Johannes Bergfd014282012-06-18 19:17:03 +02007907 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007908 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007909 nlmsg_free(msg);
7910 return;
7911 }
7912
Johannes Berg463d0182009-07-14 00:33:35 +02007913 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7914 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007915}
7916
7917void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007918 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007919{
7920 struct sk_buff *msg;
7921
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007922 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007923 if (!msg)
7924 return;
7925
Johannes Bergfd014282012-06-18 19:17:03 +02007926 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007927 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007928 nlmsg_free(msg);
7929 return;
7930 }
7931
Johannes Berg463d0182009-07-14 00:33:35 +02007932 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7933 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007934}
7935
Luciano Coelho807f8a82011-05-11 17:09:35 +03007936void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7937 struct net_device *netdev)
7938{
7939 struct sk_buff *msg;
7940
7941 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7942 if (!msg)
7943 return;
7944
7945 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7946 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7947 nlmsg_free(msg);
7948 return;
7949 }
7950
7951 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7952 nl80211_scan_mcgrp.id, GFP_KERNEL);
7953}
7954
7955void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7956 struct net_device *netdev, u32 cmd)
7957{
7958 struct sk_buff *msg;
7959
Thomas Graf58050fc2012-06-28 03:57:45 +00007960 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007961 if (!msg)
7962 return;
7963
7964 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7965 nlmsg_free(msg);
7966 return;
7967 }
7968
7969 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7970 nl80211_scan_mcgrp.id, GFP_KERNEL);
7971}
7972
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007973/*
7974 * This can happen on global regulatory changes or device specific settings
7975 * based on custom world regulatory domains.
7976 */
7977void nl80211_send_reg_change_event(struct regulatory_request *request)
7978{
7979 struct sk_buff *msg;
7980 void *hdr;
7981
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007982 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007983 if (!msg)
7984 return;
7985
7986 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7987 if (!hdr) {
7988 nlmsg_free(msg);
7989 return;
7990 }
7991
7992 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007993 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7994 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007995
David S. Miller9360ffd2012-03-29 04:41:26 -04007996 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7997 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7998 NL80211_REGDOM_TYPE_WORLD))
7999 goto nla_put_failure;
8000 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8001 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8002 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8003 goto nla_put_failure;
8004 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8005 request->intersect) {
8006 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8007 NL80211_REGDOM_TYPE_INTERSECTION))
8008 goto nla_put_failure;
8009 } else {
8010 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8011 NL80211_REGDOM_TYPE_COUNTRY) ||
8012 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8013 request->alpha2))
8014 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008015 }
8016
David S. Miller9360ffd2012-03-29 04:41:26 -04008017 if (wiphy_idx_valid(request->wiphy_idx) &&
8018 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8019 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008020
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008021 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008022
Johannes Bergbc43b282009-07-25 10:54:13 +02008023 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008024 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008025 GFP_ATOMIC);
8026 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008027
8028 return;
8029
8030nla_put_failure:
8031 genlmsg_cancel(msg, hdr);
8032 nlmsg_free(msg);
8033}
8034
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008035static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8036 struct net_device *netdev,
8037 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008038 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008039{
8040 struct sk_buff *msg;
8041 void *hdr;
8042
Johannes Berge6d6e342009-07-01 21:26:47 +02008043 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008044 if (!msg)
8045 return;
8046
8047 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8048 if (!hdr) {
8049 nlmsg_free(msg);
8050 return;
8051 }
8052
David S. Miller9360ffd2012-03-29 04:41:26 -04008053 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8054 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8055 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8056 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008057
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008058 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008059
Johannes Berg463d0182009-07-14 00:33:35 +02008060 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8061 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008062 return;
8063
8064 nla_put_failure:
8065 genlmsg_cancel(msg, hdr);
8066 nlmsg_free(msg);
8067}
8068
8069void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008070 struct net_device *netdev, const u8 *buf,
8071 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008072{
8073 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008074 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008075}
8076
8077void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8078 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008079 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008080{
Johannes Berge6d6e342009-07-01 21:26:47 +02008081 nl80211_send_mlme_event(rdev, netdev, buf, len,
8082 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008083}
8084
Jouni Malinen53b46b82009-03-27 20:53:56 +02008085void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008086 struct net_device *netdev, const u8 *buf,
8087 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008088{
8089 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008090 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008091}
8092
Jouni Malinen53b46b82009-03-27 20:53:56 +02008093void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8094 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008095 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008096{
8097 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008098 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008099}
8100
Jouni Malinencf4e5942010-12-16 00:52:40 +02008101void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8102 struct net_device *netdev, const u8 *buf,
8103 size_t len, gfp_t gfp)
8104{
8105 nl80211_send_mlme_event(rdev, netdev, buf, len,
8106 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8107}
8108
8109void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8110 struct net_device *netdev, const u8 *buf,
8111 size_t len, gfp_t gfp)
8112{
8113 nl80211_send_mlme_event(rdev, netdev, buf, len,
8114 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8115}
8116
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008117static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8118 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008119 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008120{
8121 struct sk_buff *msg;
8122 void *hdr;
8123
Johannes Berge6d6e342009-07-01 21:26:47 +02008124 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008125 if (!msg)
8126 return;
8127
8128 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8129 if (!hdr) {
8130 nlmsg_free(msg);
8131 return;
8132 }
8133
David S. Miller9360ffd2012-03-29 04:41:26 -04008134 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8135 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8136 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8137 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8138 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008139
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008140 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008141
Johannes Berg463d0182009-07-14 00:33:35 +02008142 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8143 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008144 return;
8145
8146 nla_put_failure:
8147 genlmsg_cancel(msg, hdr);
8148 nlmsg_free(msg);
8149}
8150
8151void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008152 struct net_device *netdev, const u8 *addr,
8153 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008154{
8155 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008156 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008157}
8158
8159void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008160 struct net_device *netdev, const u8 *addr,
8161 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008162{
Johannes Berge6d6e342009-07-01 21:26:47 +02008163 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8164 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008165}
8166
Samuel Ortizb23aa672009-07-01 21:26:54 +02008167void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8168 struct net_device *netdev, const u8 *bssid,
8169 const u8 *req_ie, size_t req_ie_len,
8170 const u8 *resp_ie, size_t resp_ie_len,
8171 u16 status, gfp_t gfp)
8172{
8173 struct sk_buff *msg;
8174 void *hdr;
8175
Thomas Graf58050fc2012-06-28 03:57:45 +00008176 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008177 if (!msg)
8178 return;
8179
8180 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8181 if (!hdr) {
8182 nlmsg_free(msg);
8183 return;
8184 }
8185
David S. Miller9360ffd2012-03-29 04:41:26 -04008186 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8187 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8188 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8189 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8190 (req_ie &&
8191 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8192 (resp_ie &&
8193 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8194 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008195
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008196 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008197
Johannes Berg463d0182009-07-14 00:33:35 +02008198 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8199 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008200 return;
8201
8202 nla_put_failure:
8203 genlmsg_cancel(msg, hdr);
8204 nlmsg_free(msg);
8205
8206}
8207
8208void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8209 struct net_device *netdev, const u8 *bssid,
8210 const u8 *req_ie, size_t req_ie_len,
8211 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8212{
8213 struct sk_buff *msg;
8214 void *hdr;
8215
Thomas Graf58050fc2012-06-28 03:57:45 +00008216 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008217 if (!msg)
8218 return;
8219
8220 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8221 if (!hdr) {
8222 nlmsg_free(msg);
8223 return;
8224 }
8225
David S. Miller9360ffd2012-03-29 04:41:26 -04008226 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8227 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8228 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8229 (req_ie &&
8230 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8231 (resp_ie &&
8232 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8233 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008234
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008235 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008236
Johannes Berg463d0182009-07-14 00:33:35 +02008237 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8238 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008239 return;
8240
8241 nla_put_failure:
8242 genlmsg_cancel(msg, hdr);
8243 nlmsg_free(msg);
8244
8245}
8246
8247void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8248 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02008249 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008250{
8251 struct sk_buff *msg;
8252 void *hdr;
8253
Thomas Graf58050fc2012-06-28 03:57:45 +00008254 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008255 if (!msg)
8256 return;
8257
8258 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8259 if (!hdr) {
8260 nlmsg_free(msg);
8261 return;
8262 }
8263
David S. Miller9360ffd2012-03-29 04:41:26 -04008264 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8265 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8266 (from_ap && reason &&
8267 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8268 (from_ap &&
8269 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8270 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8271 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008272
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008273 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008274
Johannes Berg463d0182009-07-14 00:33:35 +02008275 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8276 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008277 return;
8278
8279 nla_put_failure:
8280 genlmsg_cancel(msg, hdr);
8281 nlmsg_free(msg);
8282
8283}
8284
Johannes Berg04a773a2009-04-19 21:24:32 +02008285void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8286 struct net_device *netdev, const u8 *bssid,
8287 gfp_t gfp)
8288{
8289 struct sk_buff *msg;
8290 void *hdr;
8291
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008292 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008293 if (!msg)
8294 return;
8295
8296 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8297 if (!hdr) {
8298 nlmsg_free(msg);
8299 return;
8300 }
8301
David S. Miller9360ffd2012-03-29 04:41:26 -04008302 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8303 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8304 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8305 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008306
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008307 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008308
Johannes Berg463d0182009-07-14 00:33:35 +02008309 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8310 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008311 return;
8312
8313 nla_put_failure:
8314 genlmsg_cancel(msg, hdr);
8315 nlmsg_free(msg);
8316}
8317
Javier Cardonac93b5e72011-04-07 15:08:34 -07008318void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8319 struct net_device *netdev,
8320 const u8 *macaddr, const u8* ie, u8 ie_len,
8321 gfp_t gfp)
8322{
8323 struct sk_buff *msg;
8324 void *hdr;
8325
8326 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8327 if (!msg)
8328 return;
8329
8330 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8331 if (!hdr) {
8332 nlmsg_free(msg);
8333 return;
8334 }
8335
David S. Miller9360ffd2012-03-29 04:41:26 -04008336 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8337 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8338 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8339 (ie_len && ie &&
8340 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8341 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008342
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008343 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008344
8345 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8346 nl80211_mlme_mcgrp.id, gfp);
8347 return;
8348
8349 nla_put_failure:
8350 genlmsg_cancel(msg, hdr);
8351 nlmsg_free(msg);
8352}
8353
Jouni Malinena3b8b052009-03-27 21:59:49 +02008354void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8355 struct net_device *netdev, const u8 *addr,
8356 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008357 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008358{
8359 struct sk_buff *msg;
8360 void *hdr;
8361
Johannes Berge6d6e342009-07-01 21:26:47 +02008362 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008363 if (!msg)
8364 return;
8365
8366 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8367 if (!hdr) {
8368 nlmsg_free(msg);
8369 return;
8370 }
8371
David S. Miller9360ffd2012-03-29 04:41:26 -04008372 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8373 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8374 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8375 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8376 (key_id != -1 &&
8377 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8378 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8379 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008380
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008381 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008382
Johannes Berg463d0182009-07-14 00:33:35 +02008383 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8384 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008385 return;
8386
8387 nla_put_failure:
8388 genlmsg_cancel(msg, hdr);
8389 nlmsg_free(msg);
8390}
8391
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008392void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8393 struct ieee80211_channel *channel_before,
8394 struct ieee80211_channel *channel_after)
8395{
8396 struct sk_buff *msg;
8397 void *hdr;
8398 struct nlattr *nl_freq;
8399
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008400 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008401 if (!msg)
8402 return;
8403
8404 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8405 if (!hdr) {
8406 nlmsg_free(msg);
8407 return;
8408 }
8409
8410 /*
8411 * Since we are applying the beacon hint to a wiphy we know its
8412 * wiphy_idx is valid
8413 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008414 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8415 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008416
8417 /* Before */
8418 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8419 if (!nl_freq)
8420 goto nla_put_failure;
8421 if (nl80211_msg_put_channel(msg, channel_before))
8422 goto nla_put_failure;
8423 nla_nest_end(msg, nl_freq);
8424
8425 /* After */
8426 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8427 if (!nl_freq)
8428 goto nla_put_failure;
8429 if (nl80211_msg_put_channel(msg, channel_after))
8430 goto nla_put_failure;
8431 nla_nest_end(msg, nl_freq);
8432
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008433 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008434
Johannes Berg463d0182009-07-14 00:33:35 +02008435 rcu_read_lock();
8436 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8437 GFP_ATOMIC);
8438 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008439
8440 return;
8441
8442nla_put_failure:
8443 genlmsg_cancel(msg, hdr);
8444 nlmsg_free(msg);
8445}
8446
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008447static void nl80211_send_remain_on_chan_event(
8448 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008449 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008450 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008451 unsigned int duration, gfp_t gfp)
8452{
8453 struct sk_buff *msg;
8454 void *hdr;
8455
8456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8457 if (!msg)
8458 return;
8459
8460 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8461 if (!hdr) {
8462 nlmsg_free(msg);
8463 return;
8464 }
8465
David S. Miller9360ffd2012-03-29 04:41:26 -04008466 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008467 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8468 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008469 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008470 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01008471 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
8472 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008473 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8474 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008475
David S. Miller9360ffd2012-03-29 04:41:26 -04008476 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8477 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8478 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008479
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008480 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008481
8482 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8483 nl80211_mlme_mcgrp.id, gfp);
8484 return;
8485
8486 nla_put_failure:
8487 genlmsg_cancel(msg, hdr);
8488 nlmsg_free(msg);
8489}
8490
8491void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008492 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008493 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008494 unsigned int duration, gfp_t gfp)
8495{
8496 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008497 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01008498 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008499}
8500
8501void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008502 struct cfg80211_registered_device *rdev,
8503 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01008504 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008505{
8506 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01008507 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008508}
8509
Johannes Berg98b62182009-12-23 13:15:44 +01008510void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8511 struct net_device *dev, const u8 *mac_addr,
8512 struct station_info *sinfo, gfp_t gfp)
8513{
8514 struct sk_buff *msg;
8515
Thomas Graf58050fc2012-06-28 03:57:45 +00008516 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008517 if (!msg)
8518 return;
8519
John W. Linville66266b32012-03-15 13:25:41 -04008520 if (nl80211_send_station(msg, 0, 0, 0,
8521 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008522 nlmsg_free(msg);
8523 return;
8524 }
8525
8526 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8527 nl80211_mlme_mcgrp.id, gfp);
8528}
8529
Jouni Malinenec15e682011-03-23 15:29:52 +02008530void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8531 struct net_device *dev, const u8 *mac_addr,
8532 gfp_t gfp)
8533{
8534 struct sk_buff *msg;
8535 void *hdr;
8536
Thomas Graf58050fc2012-06-28 03:57:45 +00008537 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008538 if (!msg)
8539 return;
8540
8541 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8542 if (!hdr) {
8543 nlmsg_free(msg);
8544 return;
8545 }
8546
David S. Miller9360ffd2012-03-29 04:41:26 -04008547 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8548 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8549 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008550
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008551 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008552
8553 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8554 nl80211_mlme_mcgrp.id, gfp);
8555 return;
8556
8557 nla_put_failure:
8558 genlmsg_cancel(msg, hdr);
8559 nlmsg_free(msg);
8560}
8561
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308562void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8563 struct net_device *dev, const u8 *mac_addr,
8564 enum nl80211_connect_failed_reason reason,
8565 gfp_t gfp)
8566{
8567 struct sk_buff *msg;
8568 void *hdr;
8569
8570 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8571 if (!msg)
8572 return;
8573
8574 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8575 if (!hdr) {
8576 nlmsg_free(msg);
8577 return;
8578 }
8579
8580 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8581 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8582 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8583 goto nla_put_failure;
8584
8585 genlmsg_end(msg, hdr);
8586
8587 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8588 nl80211_mlme_mcgrp.id, gfp);
8589 return;
8590
8591 nla_put_failure:
8592 genlmsg_cancel(msg, hdr);
8593 nlmsg_free(msg);
8594}
8595
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008596static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8597 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008598{
8599 struct wireless_dev *wdev = dev->ieee80211_ptr;
8600 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8601 struct sk_buff *msg;
8602 void *hdr;
8603 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008604 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008605
Eric W. Biederman15e47302012-09-07 20:12:54 +00008606 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008607 return false;
8608
8609 msg = nlmsg_new(100, gfp);
8610 if (!msg)
8611 return true;
8612
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008613 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008614 if (!hdr) {
8615 nlmsg_free(msg);
8616 return true;
8617 }
8618
David S. Miller9360ffd2012-03-29 04:41:26 -04008619 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8620 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8621 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8622 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008623
8624 err = genlmsg_end(msg, hdr);
8625 if (err < 0) {
8626 nlmsg_free(msg);
8627 return true;
8628 }
8629
Eric W. Biederman15e47302012-09-07 20:12:54 +00008630 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008631 return true;
8632
8633 nla_put_failure:
8634 genlmsg_cancel(msg, hdr);
8635 nlmsg_free(msg);
8636 return true;
8637}
8638
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008639bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8640{
8641 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8642 addr, gfp);
8643}
8644
8645bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8646 const u8 *addr, gfp_t gfp)
8647{
8648 return __nl80211_unexpected_frame(dev,
8649 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8650 addr, gfp);
8651}
8652
Johannes Berg2e161f72010-08-12 15:38:38 +02008653int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008654 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008655 int freq, int sig_dbm,
8656 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008657{
Johannes Berg71bbc992012-06-15 15:30:18 +02008658 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008659 struct sk_buff *msg;
8660 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008661
8662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8663 if (!msg)
8664 return -ENOMEM;
8665
Johannes Berg2e161f72010-08-12 15:38:38 +02008666 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008667 if (!hdr) {
8668 nlmsg_free(msg);
8669 return -ENOMEM;
8670 }
8671
David S. Miller9360ffd2012-03-29 04:41:26 -04008672 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008673 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8674 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008675 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8676 (sig_dbm &&
8677 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8678 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8679 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008680
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008681 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008682
Eric W. Biederman15e47302012-09-07 20:12:54 +00008683 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008684
8685 nla_put_failure:
8686 genlmsg_cancel(msg, hdr);
8687 nlmsg_free(msg);
8688 return -ENOBUFS;
8689}
8690
Johannes Berg2e161f72010-08-12 15:38:38 +02008691void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008692 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008693 const u8 *buf, size_t len, bool ack,
8694 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008695{
Johannes Berg71bbc992012-06-15 15:30:18 +02008696 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008697 struct sk_buff *msg;
8698 void *hdr;
8699
8700 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8701 if (!msg)
8702 return;
8703
Johannes Berg2e161f72010-08-12 15:38:38 +02008704 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008705 if (!hdr) {
8706 nlmsg_free(msg);
8707 return;
8708 }
8709
David S. Miller9360ffd2012-03-29 04:41:26 -04008710 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008711 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8712 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008713 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8714 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8715 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8716 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008717
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008718 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008719
8720 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8721 return;
8722
8723 nla_put_failure:
8724 genlmsg_cancel(msg, hdr);
8725 nlmsg_free(msg);
8726}
8727
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008728void
8729nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8730 struct net_device *netdev,
8731 enum nl80211_cqm_rssi_threshold_event rssi_event,
8732 gfp_t gfp)
8733{
8734 struct sk_buff *msg;
8735 struct nlattr *pinfoattr;
8736 void *hdr;
8737
Thomas Graf58050fc2012-06-28 03:57:45 +00008738 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008739 if (!msg)
8740 return;
8741
8742 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8743 if (!hdr) {
8744 nlmsg_free(msg);
8745 return;
8746 }
8747
David S. Miller9360ffd2012-03-29 04:41:26 -04008748 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8749 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8750 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008751
8752 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8753 if (!pinfoattr)
8754 goto nla_put_failure;
8755
David S. Miller9360ffd2012-03-29 04:41:26 -04008756 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8757 rssi_event))
8758 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008759
8760 nla_nest_end(msg, pinfoattr);
8761
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008762 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008763
8764 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8765 nl80211_mlme_mcgrp.id, gfp);
8766 return;
8767
8768 nla_put_failure:
8769 genlmsg_cancel(msg, hdr);
8770 nlmsg_free(msg);
8771}
8772
Johannes Berge5497d72011-07-05 16:35:40 +02008773void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8774 struct net_device *netdev, const u8 *bssid,
8775 const u8 *replay_ctr, gfp_t gfp)
8776{
8777 struct sk_buff *msg;
8778 struct nlattr *rekey_attr;
8779 void *hdr;
8780
Thomas Graf58050fc2012-06-28 03:57:45 +00008781 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008782 if (!msg)
8783 return;
8784
8785 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8786 if (!hdr) {
8787 nlmsg_free(msg);
8788 return;
8789 }
8790
David S. Miller9360ffd2012-03-29 04:41:26 -04008791 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8792 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8793 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8794 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008795
8796 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8797 if (!rekey_attr)
8798 goto nla_put_failure;
8799
David S. Miller9360ffd2012-03-29 04:41:26 -04008800 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8801 NL80211_REPLAY_CTR_LEN, replay_ctr))
8802 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008803
8804 nla_nest_end(msg, rekey_attr);
8805
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008806 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008807
8808 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8809 nl80211_mlme_mcgrp.id, gfp);
8810 return;
8811
8812 nla_put_failure:
8813 genlmsg_cancel(msg, hdr);
8814 nlmsg_free(msg);
8815}
8816
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008817void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8818 struct net_device *netdev, int index,
8819 const u8 *bssid, bool preauth, gfp_t gfp)
8820{
8821 struct sk_buff *msg;
8822 struct nlattr *attr;
8823 void *hdr;
8824
Thomas Graf58050fc2012-06-28 03:57:45 +00008825 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008826 if (!msg)
8827 return;
8828
8829 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8830 if (!hdr) {
8831 nlmsg_free(msg);
8832 return;
8833 }
8834
David S. Miller9360ffd2012-03-29 04:41:26 -04008835 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8836 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8837 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008838
8839 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8840 if (!attr)
8841 goto nla_put_failure;
8842
David S. Miller9360ffd2012-03-29 04:41:26 -04008843 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8844 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8845 (preauth &&
8846 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8847 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008848
8849 nla_nest_end(msg, attr);
8850
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008851 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008852
8853 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8854 nl80211_mlme_mcgrp.id, gfp);
8855 return;
8856
8857 nla_put_failure:
8858 genlmsg_cancel(msg, hdr);
8859 nlmsg_free(msg);
8860}
8861
Thomas Pedersen53145262012-04-06 13:35:47 -07008862void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01008863 struct net_device *netdev,
8864 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07008865{
8866 struct sk_buff *msg;
8867 void *hdr;
8868
Thomas Graf58050fc2012-06-28 03:57:45 +00008869 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008870 if (!msg)
8871 return;
8872
8873 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8874 if (!hdr) {
8875 nlmsg_free(msg);
8876 return;
8877 }
8878
Johannes Berg683b6d32012-11-08 21:25:48 +01008879 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8880 goto nla_put_failure;
8881
8882 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04008883 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008884
8885 genlmsg_end(msg, hdr);
8886
8887 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8888 nl80211_mlme_mcgrp.id, gfp);
8889 return;
8890
8891 nla_put_failure:
8892 genlmsg_cancel(msg, hdr);
8893 nlmsg_free(msg);
8894}
8895
Johannes Bergc063dbf2010-11-24 08:10:05 +01008896void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008897nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8898 struct net_device *netdev, const u8 *peer,
8899 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8900{
8901 struct sk_buff *msg;
8902 struct nlattr *pinfoattr;
8903 void *hdr;
8904
8905 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8906 if (!msg)
8907 return;
8908
8909 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8910 if (!hdr) {
8911 nlmsg_free(msg);
8912 return;
8913 }
8914
8915 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8916 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8917 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8918 goto nla_put_failure;
8919
8920 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8921 if (!pinfoattr)
8922 goto nla_put_failure;
8923
8924 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8925 goto nla_put_failure;
8926
8927 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8928 goto nla_put_failure;
8929
8930 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8931 goto nla_put_failure;
8932
8933 nla_nest_end(msg, pinfoattr);
8934
8935 genlmsg_end(msg, hdr);
8936
8937 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8938 nl80211_mlme_mcgrp.id, gfp);
8939 return;
8940
8941 nla_put_failure:
8942 genlmsg_cancel(msg, hdr);
8943 nlmsg_free(msg);
8944}
8945
8946void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008947nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8948 struct net_device *netdev, const u8 *peer,
8949 u32 num_packets, gfp_t gfp)
8950{
8951 struct sk_buff *msg;
8952 struct nlattr *pinfoattr;
8953 void *hdr;
8954
Thomas Graf58050fc2012-06-28 03:57:45 +00008955 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008956 if (!msg)
8957 return;
8958
8959 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8960 if (!hdr) {
8961 nlmsg_free(msg);
8962 return;
8963 }
8964
David S. Miller9360ffd2012-03-29 04:41:26 -04008965 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8966 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8967 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8968 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008969
8970 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8971 if (!pinfoattr)
8972 goto nla_put_failure;
8973
David S. Miller9360ffd2012-03-29 04:41:26 -04008974 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8975 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008976
8977 nla_nest_end(msg, pinfoattr);
8978
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008979 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008980
8981 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8982 nl80211_mlme_mcgrp.id, gfp);
8983 return;
8984
8985 nla_put_failure:
8986 genlmsg_cancel(msg, hdr);
8987 nlmsg_free(msg);
8988}
8989
Johannes Berg7f6cf312011-11-04 11:18:15 +01008990void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8991 u64 cookie, bool acked, gfp_t gfp)
8992{
8993 struct wireless_dev *wdev = dev->ieee80211_ptr;
8994 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8995 struct sk_buff *msg;
8996 void *hdr;
8997 int err;
8998
Beni Lev4ee3e062012-08-27 12:49:39 +03008999 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9000
Thomas Graf58050fc2012-06-28 03:57:45 +00009001 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009002
Johannes Berg7f6cf312011-11-04 11:18:15 +01009003 if (!msg)
9004 return;
9005
9006 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9007 if (!hdr) {
9008 nlmsg_free(msg);
9009 return;
9010 }
9011
David S. Miller9360ffd2012-03-29 04:41:26 -04009012 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9013 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9014 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9015 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9016 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9017 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009018
9019 err = genlmsg_end(msg, hdr);
9020 if (err < 0) {
9021 nlmsg_free(msg);
9022 return;
9023 }
9024
9025 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9026 nl80211_mlme_mcgrp.id, gfp);
9027 return;
9028
9029 nla_put_failure:
9030 genlmsg_cancel(msg, hdr);
9031 nlmsg_free(msg);
9032}
9033EXPORT_SYMBOL(cfg80211_probe_status);
9034
Johannes Berg5e760232011-11-04 11:18:17 +01009035void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9036 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009037 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009038{
9039 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9040 struct sk_buff *msg;
9041 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009042 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009043
Beni Lev4ee3e062012-08-27 12:49:39 +03009044 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9045
Ben Greear37c73b52012-10-26 14:49:25 -07009046 spin_lock_bh(&rdev->beacon_registrations_lock);
9047 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9048 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9049 if (!msg) {
9050 spin_unlock_bh(&rdev->beacon_registrations_lock);
9051 return;
9052 }
Johannes Berg5e760232011-11-04 11:18:17 +01009053
Ben Greear37c73b52012-10-26 14:49:25 -07009054 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9055 if (!hdr)
9056 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009057
Ben Greear37c73b52012-10-26 14:49:25 -07009058 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9059 (freq &&
9060 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9061 (sig_dbm &&
9062 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9063 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9064 goto nla_put_failure;
9065
9066 genlmsg_end(msg, hdr);
9067
9068 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009069 }
Ben Greear37c73b52012-10-26 14:49:25 -07009070 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009071 return;
9072
9073 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009074 spin_unlock_bh(&rdev->beacon_registrations_lock);
9075 if (hdr)
9076 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009077 nlmsg_free(msg);
9078}
9079EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9080
Jouni Malinen3475b092012-11-16 22:49:57 +02009081void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9082 enum nl80211_tdls_operation oper,
9083 u16 reason_code, gfp_t gfp)
9084{
9085 struct wireless_dev *wdev = dev->ieee80211_ptr;
9086 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9087 struct sk_buff *msg;
9088 void *hdr;
9089 int err;
9090
9091 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9092 reason_code);
9093
9094 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9095 if (!msg)
9096 return;
9097
9098 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9099 if (!hdr) {
9100 nlmsg_free(msg);
9101 return;
9102 }
9103
9104 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9105 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9106 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9107 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9108 (reason_code > 0 &&
9109 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9110 goto nla_put_failure;
9111
9112 err = genlmsg_end(msg, hdr);
9113 if (err < 0) {
9114 nlmsg_free(msg);
9115 return;
9116 }
9117
9118 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9119 nl80211_mlme_mcgrp.id, gfp);
9120 return;
9121
9122 nla_put_failure:
9123 genlmsg_cancel(msg, hdr);
9124 nlmsg_free(msg);
9125}
9126EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9127
Jouni Malinen026331c2010-02-15 12:53:10 +02009128static int nl80211_netlink_notify(struct notifier_block * nb,
9129 unsigned long state,
9130 void *_notify)
9131{
9132 struct netlink_notify *notify = _notify;
9133 struct cfg80211_registered_device *rdev;
9134 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009135 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009136
9137 if (state != NETLINK_URELEASE)
9138 return NOTIFY_DONE;
9139
9140 rcu_read_lock();
9141
Johannes Berg5e760232011-11-04 11:18:17 +01009142 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009143 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009144 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009145
9146 spin_lock_bh(&rdev->beacon_registrations_lock);
9147 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9148 list) {
9149 if (reg->nlportid == notify->portid) {
9150 list_del(&reg->list);
9151 kfree(reg);
9152 break;
9153 }
9154 }
9155 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009156 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009157
9158 rcu_read_unlock();
9159
9160 return NOTIFY_DONE;
9161}
9162
9163static struct notifier_block nl80211_netlink_notifier = {
9164 .notifier_call = nl80211_netlink_notify,
9165};
9166
Johannes Berg55682962007-09-20 13:09:35 -04009167/* initialisation/exit functions */
9168
9169int nl80211_init(void)
9170{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009171 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009172
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009173 err = genl_register_family_with_ops(&nl80211_fam,
9174 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009175 if (err)
9176 return err;
9177
Johannes Berg55682962007-09-20 13:09:35 -04009178 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9179 if (err)
9180 goto err_out;
9181
Johannes Berg2a519312009-02-10 21:25:55 +01009182 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9183 if (err)
9184 goto err_out;
9185
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009186 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9187 if (err)
9188 goto err_out;
9189
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009190 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9191 if (err)
9192 goto err_out;
9193
Johannes Bergaff89a92009-07-01 21:26:51 +02009194#ifdef CONFIG_NL80211_TESTMODE
9195 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9196 if (err)
9197 goto err_out;
9198#endif
9199
Jouni Malinen026331c2010-02-15 12:53:10 +02009200 err = netlink_register_notifier(&nl80211_netlink_notifier);
9201 if (err)
9202 goto err_out;
9203
Johannes Berg55682962007-09-20 13:09:35 -04009204 return 0;
9205 err_out:
9206 genl_unregister_family(&nl80211_fam);
9207 return err;
9208}
9209
9210void nl80211_exit(void)
9211{
Jouni Malinen026331c2010-02-15 12:53:10 +02009212 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009213 genl_unregister_family(&nl80211_fam);
9214}