blob: 4a719770eaaf432c841e25799df3de48600e178e [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030025#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040026
Jouni Malinen5fb628e2011-08-10 23:54:35 +030027static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg89a54e42012-06-15 14:33:17 +020049/* returns ERR_PTR values */
50static struct wireless_dev *
51__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040052{
Johannes Berg89a54e42012-06-15 14:33:17 +020053 struct cfg80211_registered_device *rdev;
54 struct wireless_dev *result = NULL;
55 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
56 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
57 u64 wdev_id;
58 int wiphy_idx = -1;
59 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040060
Johannes Berg89a54e42012-06-15 14:33:17 +020061 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040062
Johannes Berg89a54e42012-06-15 14:33:17 +020063 if (!have_ifidx && !have_wdev_id)
64 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040065
Johannes Berg89a54e42012-06-15 14:33:17 +020066 if (have_ifidx)
67 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
68 if (have_wdev_id) {
69 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
70 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040071 }
72
Johannes Berg89a54e42012-06-15 14:33:17 +020073 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
74 struct wireless_dev *wdev;
75
76 if (wiphy_net(&rdev->wiphy) != netns)
77 continue;
78
79 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
80 continue;
81
82 mutex_lock(&rdev->devlist_mtx);
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
88 }
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
92 }
93 }
94 mutex_unlock(&rdev->devlist_mtx);
95
96 if (result)
97 break;
98 }
99
100 if (result)
101 return result;
102 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400103}
104
Johannes Berga9455402012-06-15 13:32:49 +0200105static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200106__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200107{
Johannes Berg7fee4772012-06-15 14:09:58 +0200108 struct cfg80211_registered_device *rdev = NULL, *tmp;
109 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200110
111 assert_cfg80211_lock();
112
Johannes Berg878d9ec2012-06-15 14:18:32 +0200113 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200114 !attrs[NL80211_ATTR_IFINDEX] &&
115 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200116 return ERR_PTR(-EINVAL);
117
Johannes Berg878d9ec2012-06-15 14:18:32 +0200118 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200119 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200120 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200121
Johannes Berg89a54e42012-06-15 14:33:17 +0200122 if (attrs[NL80211_ATTR_WDEV]) {
123 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
124 struct wireless_dev *wdev;
125 bool found = false;
126
127 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
128 if (tmp) {
129 /* make sure wdev exists */
130 mutex_lock(&tmp->devlist_mtx);
131 list_for_each_entry(wdev, &tmp->wdev_list, list) {
132 if (wdev->identifier != (u32)wdev_id)
133 continue;
134 found = true;
135 break;
136 }
137 mutex_unlock(&tmp->devlist_mtx);
138
139 if (!found)
140 tmp = NULL;
141
142 if (rdev && tmp != rdev)
143 return ERR_PTR(-EINVAL);
144 rdev = tmp;
145 }
146 }
147
Johannes Berg878d9ec2012-06-15 14:18:32 +0200148 if (attrs[NL80211_ATTR_IFINDEX]) {
149 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200150 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200151 if (netdev) {
152 if (netdev->ieee80211_ptr)
153 tmp = wiphy_to_dev(
154 netdev->ieee80211_ptr->wiphy);
155 else
156 tmp = NULL;
157
158 dev_put(netdev);
159
160 /* not wireless device -- return error */
161 if (!tmp)
162 return ERR_PTR(-EINVAL);
163
164 /* mismatch -- return error */
165 if (rdev && tmp != rdev)
166 return ERR_PTR(-EINVAL);
167
168 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200169 }
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
171
Johannes Berg4f7eff12012-06-15 14:14:22 +0200172 if (!rdev)
173 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200174
Johannes Berg4f7eff12012-06-15 14:14:22 +0200175 if (netns != wiphy_net(&rdev->wiphy))
176 return ERR_PTR(-ENODEV);
177
178 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200179}
180
181/*
182 * This function returns a pointer to the driver
183 * that the genl_info item that is passed refers to.
184 * If successful, it returns non-NULL and also locks
185 * the driver's mutex!
186 *
187 * This means that you need to call cfg80211_unlock_rdev()
188 * before being allowed to acquire &cfg80211_mutex!
189 *
190 * This is necessary because we need to lock the global
191 * mutex to get an item off the list safely, and then
192 * we lock the rdev mutex so it doesn't go away under us.
193 *
194 * We don't want to keep cfg80211_mutex locked
195 * for all the time in order to allow requests on
196 * other interfaces to go through at the same time.
197 *
198 * The result of this can be a PTR_ERR and hence must
199 * be checked with IS_ERR() for errors.
200 */
201static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200202cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200203{
204 struct cfg80211_registered_device *rdev;
205
206 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200207 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208
209 /* if it is not an error we grab the lock on
210 * it to assure it won't be going away while
211 * we operate on it */
212 if (!IS_ERR(rdev))
213 mutex_lock(&rdev->mtx);
214
215 mutex_unlock(&cfg80211_mutex);
216
217 return rdev;
218}
219
Johannes Berg55682962007-09-20 13:09:35 -0400220/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000221static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400222 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700224 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200225 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100226
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200227 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530228 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100229 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
230 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
232
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200233 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
234 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
236 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100237 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400238
239 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
240 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100242
Eliad Pellere007b852011-11-24 18:13:56 +0200243 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
244 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100245
Johannes Bergb9454e82009-07-08 13:29:08 +0200246 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100247 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
248 .len = WLAN_MAX_KEY_LEN },
249 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
250 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
251 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200252 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200253 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100254
255 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
256 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
257 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
258 .len = IEEE80211_MAX_DATA_LEN },
259 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
260 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100261 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
262 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
263 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
264 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
265 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100266 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100267 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200268 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100269 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800270 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100271 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300272
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700273 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
274 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
275
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300276 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
277 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200279 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
280 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100281 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300282
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800283 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700284 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700285
Johannes Berg6c739412011-11-03 09:27:01 +0100286 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200287
288 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
289 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100291 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
292 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200293
294 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
295 .len = IEEE80211_MAX_SSID_LEN },
296 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
297 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200298 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300299 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300300 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300301 [NL80211_ATTR_STA_FLAGS2] = {
302 .len = sizeof(struct nl80211_sta_flag_update),
303 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300304 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300305 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
306 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200307 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
308 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
309 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200310 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100311 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100312 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
313 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100314 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
315 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200316 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200317 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
318 .len = IEEE80211_MAX_DATA_LEN },
319 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200320 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200321 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300322 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200323 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300324 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
325 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200326 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900327 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
328 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100329 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100330 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100331 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200332 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700333 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300334 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200335 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200336 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300337 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300338 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
339 .len = IEEE80211_MAX_DATA_LEN },
340 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
341 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530342 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300343 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530344 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300345 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
346 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
349 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100350 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200351 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
352 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700353 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800354 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
355 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
356 .len = NL80211_HT_CAPABILITY_LEN
357 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100358 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530359 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530360 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200361 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700362 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300363 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000364 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700365 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100366 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
367 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400368};
369
Johannes Berge31b8212010-10-05 19:39:30 +0200370/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000371static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200372 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200373 [NL80211_KEY_IDX] = { .type = NLA_U8 },
374 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200375 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200376 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
377 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200378 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100379 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
380};
381
382/* policy for the key default flags */
383static const struct nla_policy
384nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
385 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
386 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200387};
388
Johannes Bergff1b6e62011-05-04 15:37:28 +0200389/* policy for WoWLAN attributes */
390static const struct nla_policy
391nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
392 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
393 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
394 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
395 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200396 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
397 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
398 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200400};
401
Johannes Berge5497d72011-07-05 16:35:40 +0200402/* policy for GTK rekey offload attributes */
403static const struct nla_policy
404nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
405 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
406 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
407 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
408};
409
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300410static const struct nla_policy
411nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200412 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300413 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700414 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300415};
416
Holger Schuriga0438972009-11-11 11:30:02 +0100417/* ifidx get helper */
418static int nl80211_get_ifidx(struct netlink_callback *cb)
419{
420 int res;
421
422 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
423 nl80211_fam.attrbuf, nl80211_fam.maxattr,
424 nl80211_policy);
425 if (res)
426 return res;
427
428 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
429 return -EINVAL;
430
431 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
432 if (!res)
433 return -EINVAL;
434 return res;
435}
436
Johannes Berg67748892010-10-04 21:14:06 +0200437static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
438 struct netlink_callback *cb,
439 struct cfg80211_registered_device **rdev,
440 struct net_device **dev)
441{
442 int ifidx = cb->args[0];
443 int err;
444
445 if (!ifidx)
446 ifidx = nl80211_get_ifidx(cb);
447 if (ifidx < 0)
448 return ifidx;
449
450 cb->args[0] = ifidx;
451
452 rtnl_lock();
453
454 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
455 if (!*dev) {
456 err = -ENODEV;
457 goto out_rtnl;
458 }
459
460 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100461 if (IS_ERR(*rdev)) {
462 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200463 goto out_rtnl;
464 }
465
466 return 0;
467 out_rtnl:
468 rtnl_unlock();
469 return err;
470}
471
472static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
473{
474 cfg80211_unlock_rdev(rdev);
475 rtnl_unlock();
476}
477
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100478/* IE validation */
479static bool is_valid_ie_attr(const struct nlattr *attr)
480{
481 const u8 *pos;
482 int len;
483
484 if (!attr)
485 return true;
486
487 pos = nla_data(attr);
488 len = nla_len(attr);
489
490 while (len) {
491 u8 elemlen;
492
493 if (len < 2)
494 return false;
495 len -= 2;
496
497 elemlen = pos[1];
498 if (elemlen > len)
499 return false;
500
501 len -= elemlen;
502 pos += 2 + elemlen;
503 }
504
505 return true;
506}
507
Johannes Berg55682962007-09-20 13:09:35 -0400508/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000509static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400510 int flags, u8 cmd)
511{
512 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000513 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400514}
515
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400516static int nl80211_msg_put_channel(struct sk_buff *msg,
517 struct ieee80211_channel *chan)
518{
David S. Miller9360ffd2012-03-29 04:41:26 -0400519 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
520 chan->center_freq))
521 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400522
David S. Miller9360ffd2012-03-29 04:41:26 -0400523 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
524 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
525 goto nla_put_failure;
526 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
527 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
528 goto nla_put_failure;
529 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
530 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
531 goto nla_put_failure;
532 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
533 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
534 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400535
David S. Miller9360ffd2012-03-29 04:41:26 -0400536 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
537 DBM_TO_MBM(chan->max_power)))
538 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400539
540 return 0;
541
542 nla_put_failure:
543 return -ENOBUFS;
544}
545
Johannes Berg55682962007-09-20 13:09:35 -0400546/* netlink command implementations */
547
Johannes Bergb9454e82009-07-08 13:29:08 +0200548struct key_parse {
549 struct key_params p;
550 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200551 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200552 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100553 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200554};
555
556static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
557{
558 struct nlattr *tb[NL80211_KEY_MAX + 1];
559 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
560 nl80211_key_policy);
561 if (err)
562 return err;
563
564 k->def = !!tb[NL80211_KEY_DEFAULT];
565 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
566
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100567 if (k->def) {
568 k->def_uni = true;
569 k->def_multi = true;
570 }
571 if (k->defmgmt)
572 k->def_multi = true;
573
Johannes Bergb9454e82009-07-08 13:29:08 +0200574 if (tb[NL80211_KEY_IDX])
575 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
576
577 if (tb[NL80211_KEY_DATA]) {
578 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
579 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
580 }
581
582 if (tb[NL80211_KEY_SEQ]) {
583 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
584 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
585 }
586
587 if (tb[NL80211_KEY_CIPHER])
588 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
589
Johannes Berge31b8212010-10-05 19:39:30 +0200590 if (tb[NL80211_KEY_TYPE]) {
591 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
592 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
593 return -EINVAL;
594 }
595
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100596 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
597 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100598 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
599 tb[NL80211_KEY_DEFAULT_TYPES],
600 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100601 if (err)
602 return err;
603
604 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
605 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
606 }
607
Johannes Bergb9454e82009-07-08 13:29:08 +0200608 return 0;
609}
610
611static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
612{
613 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
614 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
615 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
616 }
617
618 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
619 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
620 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
621 }
622
623 if (info->attrs[NL80211_ATTR_KEY_IDX])
624 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
625
626 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
627 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
628
629 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
630 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
631
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100632 if (k->def) {
633 k->def_uni = true;
634 k->def_multi = true;
635 }
636 if (k->defmgmt)
637 k->def_multi = true;
638
Johannes Berge31b8212010-10-05 19:39:30 +0200639 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
640 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
641 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
642 return -EINVAL;
643 }
644
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100645 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
646 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
647 int err = nla_parse_nested(
648 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
649 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
650 nl80211_key_default_policy);
651 if (err)
652 return err;
653
654 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
655 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
656 }
657
Johannes Bergb9454e82009-07-08 13:29:08 +0200658 return 0;
659}
660
661static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
662{
663 int err;
664
665 memset(k, 0, sizeof(*k));
666 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200667 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200668
669 if (info->attrs[NL80211_ATTR_KEY])
670 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
671 else
672 err = nl80211_parse_key_old(info, k);
673
674 if (err)
675 return err;
676
677 if (k->def && k->defmgmt)
678 return -EINVAL;
679
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100680 if (k->defmgmt) {
681 if (k->def_uni || !k->def_multi)
682 return -EINVAL;
683 }
684
Johannes Bergb9454e82009-07-08 13:29:08 +0200685 if (k->idx != -1) {
686 if (k->defmgmt) {
687 if (k->idx < 4 || k->idx > 5)
688 return -EINVAL;
689 } else if (k->def) {
690 if (k->idx < 0 || k->idx > 3)
691 return -EINVAL;
692 } else {
693 if (k->idx < 0 || k->idx > 5)
694 return -EINVAL;
695 }
696 }
697
698 return 0;
699}
700
Johannes Bergfffd0932009-07-08 14:22:54 +0200701static struct cfg80211_cached_keys *
702nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530703 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200704{
705 struct key_parse parse;
706 struct nlattr *key;
707 struct cfg80211_cached_keys *result;
708 int rem, err, def = 0;
709
710 result = kzalloc(sizeof(*result), GFP_KERNEL);
711 if (!result)
712 return ERR_PTR(-ENOMEM);
713
714 result->def = -1;
715 result->defmgmt = -1;
716
717 nla_for_each_nested(key, keys, rem) {
718 memset(&parse, 0, sizeof(parse));
719 parse.idx = -1;
720
721 err = nl80211_parse_key_new(key, &parse);
722 if (err)
723 goto error;
724 err = -EINVAL;
725 if (!parse.p.key)
726 goto error;
727 if (parse.idx < 0 || parse.idx > 4)
728 goto error;
729 if (parse.def) {
730 if (def)
731 goto error;
732 def = 1;
733 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100734 if (!parse.def_uni || !parse.def_multi)
735 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200736 } else if (parse.defmgmt)
737 goto error;
738 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200739 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200740 if (err)
741 goto error;
742 result->params[parse.idx].cipher = parse.p.cipher;
743 result->params[parse.idx].key_len = parse.p.key_len;
744 result->params[parse.idx].key = result->data[parse.idx];
745 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530746
747 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
748 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
749 if (no_ht)
750 *no_ht = true;
751 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 }
753
754 return result;
755 error:
756 kfree(result);
757 return ERR_PTR(err);
758}
759
760static int nl80211_key_allowed(struct wireless_dev *wdev)
761{
762 ASSERT_WDEV_LOCK(wdev);
763
Johannes Bergfffd0932009-07-08 14:22:54 +0200764 switch (wdev->iftype) {
765 case NL80211_IFTYPE_AP:
766 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200767 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700768 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200769 break;
770 case NL80211_IFTYPE_ADHOC:
771 if (!wdev->current_bss)
772 return -ENOLINK;
773 break;
774 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200775 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200776 if (wdev->sme_state != CFG80211_SME_CONNECTED)
777 return -ENOLINK;
778 break;
779 default:
780 return -EINVAL;
781 }
782
783 return 0;
784}
785
Johannes Berg7527a782011-05-13 10:58:57 +0200786static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
787{
788 struct nlattr *nl_modes = nla_nest_start(msg, attr);
789 int i;
790
791 if (!nl_modes)
792 goto nla_put_failure;
793
794 i = 0;
795 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400796 if ((ifmodes & 1) && nla_put_flag(msg, i))
797 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200798 ifmodes >>= 1;
799 i++;
800 }
801
802 nla_nest_end(msg, nl_modes);
803 return 0;
804
805nla_put_failure:
806 return -ENOBUFS;
807}
808
809static int nl80211_put_iface_combinations(struct wiphy *wiphy,
810 struct sk_buff *msg)
811{
812 struct nlattr *nl_combis;
813 int i, j;
814
815 nl_combis = nla_nest_start(msg,
816 NL80211_ATTR_INTERFACE_COMBINATIONS);
817 if (!nl_combis)
818 goto nla_put_failure;
819
820 for (i = 0; i < wiphy->n_iface_combinations; i++) {
821 const struct ieee80211_iface_combination *c;
822 struct nlattr *nl_combi, *nl_limits;
823
824 c = &wiphy->iface_combinations[i];
825
826 nl_combi = nla_nest_start(msg, i + 1);
827 if (!nl_combi)
828 goto nla_put_failure;
829
830 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
831 if (!nl_limits)
832 goto nla_put_failure;
833
834 for (j = 0; j < c->n_limits; j++) {
835 struct nlattr *nl_limit;
836
837 nl_limit = nla_nest_start(msg, j + 1);
838 if (!nl_limit)
839 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400840 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
841 c->limits[j].max))
842 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200843 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
844 c->limits[j].types))
845 goto nla_put_failure;
846 nla_nest_end(msg, nl_limit);
847 }
848
849 nla_nest_end(msg, nl_limits);
850
David S. Miller9360ffd2012-03-29 04:41:26 -0400851 if (c->beacon_int_infra_match &&
852 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
853 goto nla_put_failure;
854 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
855 c->num_different_channels) ||
856 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
857 c->max_interfaces))
858 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200859
860 nla_nest_end(msg, nl_combi);
861 }
862
863 nla_nest_end(msg, nl_combis);
864
865 return 0;
866nla_put_failure:
867 return -ENOBUFS;
868}
869
Eric W. Biederman15e47302012-09-07 20:12:54 +0000870static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400871 struct cfg80211_registered_device *dev)
872{
873 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100874 struct nlattr *nl_bands, *nl_band;
875 struct nlattr *nl_freqs, *nl_freq;
876 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100877 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100878 enum ieee80211_band band;
879 struct ieee80211_channel *chan;
880 struct ieee80211_rate *rate;
881 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200882 const struct ieee80211_txrx_stypes *mgmt_stypes =
883 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400884
Eric W. Biederman15e47302012-09-07 20:12:54 +0000885 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400886 if (!hdr)
887 return -1;
888
David S. Miller9360ffd2012-03-29 04:41:26 -0400889 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
890 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
891 nla_put_u32(msg, NL80211_ATTR_GENERATION,
892 cfg80211_rdev_list_generation) ||
893 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
894 dev->wiphy.retry_short) ||
895 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
896 dev->wiphy.retry_long) ||
897 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
898 dev->wiphy.frag_threshold) ||
899 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
900 dev->wiphy.rts_threshold) ||
901 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
902 dev->wiphy.coverage_class) ||
903 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
904 dev->wiphy.max_scan_ssids) ||
905 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
906 dev->wiphy.max_sched_scan_ssids) ||
907 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
908 dev->wiphy.max_scan_ie_len) ||
909 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
910 dev->wiphy.max_sched_scan_ie_len) ||
911 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
912 dev->wiphy.max_match_sets))
913 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200914
David S. Miller9360ffd2012-03-29 04:41:26 -0400915 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
916 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
917 goto nla_put_failure;
918 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
919 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
920 goto nla_put_failure;
921 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
922 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
923 goto nla_put_failure;
924 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
925 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
926 goto nla_put_failure;
927 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
928 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
929 goto nla_put_failure;
930 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
931 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
932 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200933
David S. Miller9360ffd2012-03-29 04:41:26 -0400934 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
935 sizeof(u32) * dev->wiphy.n_cipher_suites,
936 dev->wiphy.cipher_suites))
937 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100938
David S. Miller9360ffd2012-03-29 04:41:26 -0400939 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
940 dev->wiphy.max_num_pmkids))
941 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530942
David S. Miller9360ffd2012-03-29 04:41:26 -0400943 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
944 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
945 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200946
David S. Miller9360ffd2012-03-29 04:41:26 -0400947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
948 dev->wiphy.available_antennas_tx) ||
949 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
950 dev->wiphy.available_antennas_rx))
951 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100952
David S. Miller9360ffd2012-03-29 04:41:26 -0400953 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
954 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
955 dev->wiphy.probe_resp_offload))
956 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200957
Bruno Randolf7f531e02010-12-16 11:30:22 +0900958 if ((dev->wiphy.available_antennas_tx ||
959 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900960 u32 tx_ant = 0, rx_ant = 0;
961 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300962 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900963 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400964 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
965 tx_ant) ||
966 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
967 rx_ant))
968 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900969 }
970 }
971
Johannes Berg7527a782011-05-13 10:58:57 +0200972 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
973 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700974 goto nla_put_failure;
975
Johannes Bergee688b002008-01-24 19:38:39 +0100976 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
977 if (!nl_bands)
978 goto nla_put_failure;
979
980 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
981 if (!dev->wiphy.bands[band])
982 continue;
983
984 nl_band = nla_nest_start(msg, band);
985 if (!nl_band)
986 goto nla_put_failure;
987
Johannes Bergd51626d2008-10-09 12:20:13 +0200988 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400989 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
990 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
991 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
992 &dev->wiphy.bands[band]->ht_cap.mcs) ||
993 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
994 dev->wiphy.bands[band]->ht_cap.cap) ||
995 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
996 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
997 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
998 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
999 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001000
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001001 /* add VHT info */
1002 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1003 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1004 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1005 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1006 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1007 dev->wiphy.bands[band]->vht_cap.cap)))
1008 goto nla_put_failure;
1009
Johannes Bergee688b002008-01-24 19:38:39 +01001010 /* add frequencies */
1011 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1012 if (!nl_freqs)
1013 goto nla_put_failure;
1014
1015 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1016 nl_freq = nla_nest_start(msg, i);
1017 if (!nl_freq)
1018 goto nla_put_failure;
1019
1020 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001021
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001022 if (nl80211_msg_put_channel(msg, chan))
1023 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001024
Johannes Bergee688b002008-01-24 19:38:39 +01001025 nla_nest_end(msg, nl_freq);
1026 }
1027
1028 nla_nest_end(msg, nl_freqs);
1029
1030 /* add bitrates */
1031 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1032 if (!nl_rates)
1033 goto nla_put_failure;
1034
1035 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1036 nl_rate = nla_nest_start(msg, i);
1037 if (!nl_rate)
1038 goto nla_put_failure;
1039
1040 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001041 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1042 rate->bitrate))
1043 goto nla_put_failure;
1044 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1045 nla_put_flag(msg,
1046 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1047 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001048
1049 nla_nest_end(msg, nl_rate);
1050 }
1051
1052 nla_nest_end(msg, nl_rates);
1053
1054 nla_nest_end(msg, nl_band);
1055 }
1056 nla_nest_end(msg, nl_bands);
1057
Johannes Berg8fdc6212009-03-14 09:34:01 +01001058 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1059 if (!nl_cmds)
1060 goto nla_put_failure;
1061
1062 i = 0;
1063#define CMD(op, n) \
1064 do { \
1065 if (dev->ops->op) { \
1066 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001067 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1068 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001069 } \
1070 } while (0)
1071
1072 CMD(add_virtual_intf, NEW_INTERFACE);
1073 CMD(change_virtual_intf, SET_INTERFACE);
1074 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001075 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001076 CMD(add_station, NEW_STATION);
1077 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001078 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001079 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001080 CMD(auth, AUTHENTICATE);
1081 CMD(assoc, ASSOCIATE);
1082 CMD(deauth, DEAUTHENTICATE);
1083 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001084 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001085 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001086 CMD(set_pmksa, SET_PMKSA);
1087 CMD(del_pmksa, DEL_PMKSA);
1088 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001089 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1090 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001091 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001092 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001093 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001094 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001095 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001096 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1097 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001098 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001099 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001100 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001101 i++;
1102 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1103 goto nla_put_failure;
1104 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001105 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001106 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1107 CMD(tdls_mgmt, TDLS_MGMT);
1108 CMD(tdls_oper, TDLS_OPER);
1109 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001110 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1111 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001112 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001113 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001114 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1115 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001116 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1117 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001118 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001119 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001120 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001121
Kalle Valo4745fc02011-11-17 19:06:10 +02001122#ifdef CONFIG_NL80211_TESTMODE
1123 CMD(testmode_cmd, TESTMODE);
1124#endif
1125
Johannes Berg8fdc6212009-03-14 09:34:01 +01001126#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001127
Johannes Berg6829c872009-07-02 09:13:27 +02001128 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001129 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001130 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1131 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001132 }
1133
Johannes Berg6829c872009-07-02 09:13:27 +02001134 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001135 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001136 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1137 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001138 }
1139
Johannes Berg8fdc6212009-03-14 09:34:01 +01001140 nla_nest_end(msg, nl_cmds);
1141
Johannes Berg7c4ef712011-11-18 15:33:48 +01001142 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001143 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1144 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1145 dev->wiphy.max_remain_on_channel_duration))
1146 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001147
David S. Miller9360ffd2012-03-29 04:41:26 -04001148 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1149 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1150 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001151
Johannes Berg2e161f72010-08-12 15:38:38 +02001152 if (mgmt_stypes) {
1153 u16 stypes;
1154 struct nlattr *nl_ftypes, *nl_ifs;
1155 enum nl80211_iftype ift;
1156
1157 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1158 if (!nl_ifs)
1159 goto nla_put_failure;
1160
1161 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1162 nl_ftypes = nla_nest_start(msg, ift);
1163 if (!nl_ftypes)
1164 goto nla_put_failure;
1165 i = 0;
1166 stypes = mgmt_stypes[ift].tx;
1167 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001168 if ((stypes & 1) &&
1169 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1170 (i << 4) | IEEE80211_FTYPE_MGMT))
1171 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001172 stypes >>= 1;
1173 i++;
1174 }
1175 nla_nest_end(msg, nl_ftypes);
1176 }
1177
Johannes Berg74b70a42010-08-24 12:15:53 +02001178 nla_nest_end(msg, nl_ifs);
1179
Johannes Berg2e161f72010-08-12 15:38:38 +02001180 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1181 if (!nl_ifs)
1182 goto nla_put_failure;
1183
1184 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1185 nl_ftypes = nla_nest_start(msg, ift);
1186 if (!nl_ftypes)
1187 goto nla_put_failure;
1188 i = 0;
1189 stypes = mgmt_stypes[ift].rx;
1190 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001191 if ((stypes & 1) &&
1192 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1193 (i << 4) | IEEE80211_FTYPE_MGMT))
1194 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001195 stypes >>= 1;
1196 i++;
1197 }
1198 nla_nest_end(msg, nl_ftypes);
1199 }
1200 nla_nest_end(msg, nl_ifs);
1201 }
1202
Johannes Bergdfb89c52012-06-27 09:23:48 +02001203#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001204 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1205 struct nlattr *nl_wowlan;
1206
1207 nl_wowlan = nla_nest_start(msg,
1208 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1209 if (!nl_wowlan)
1210 goto nla_put_failure;
1211
David S. Miller9360ffd2012-03-29 04:41:26 -04001212 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1213 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1214 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1215 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1216 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1217 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1218 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1219 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1220 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1221 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1222 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1223 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1224 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1226 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1227 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1228 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001229 if (dev->wiphy.wowlan.n_patterns) {
1230 struct nl80211_wowlan_pattern_support pat = {
1231 .max_patterns = dev->wiphy.wowlan.n_patterns,
1232 .min_pattern_len =
1233 dev->wiphy.wowlan.pattern_min_len,
1234 .max_pattern_len =
1235 dev->wiphy.wowlan.pattern_max_len,
1236 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1238 sizeof(pat), &pat))
1239 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001240 }
1241
1242 nla_nest_end(msg, nl_wowlan);
1243 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001244#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001245
Johannes Berg7527a782011-05-13 10:58:57 +02001246 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1247 dev->wiphy.software_iftypes))
1248 goto nla_put_failure;
1249
1250 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1251 goto nla_put_failure;
1252
David S. Miller9360ffd2012-03-29 04:41:26 -04001253 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1254 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1255 dev->wiphy.ap_sme_capa))
1256 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001257
David S. Miller9360ffd2012-03-29 04:41:26 -04001258 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1259 dev->wiphy.features))
1260 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001261
David S. Miller9360ffd2012-03-29 04:41:26 -04001262 if (dev->wiphy.ht_capa_mod_mask &&
1263 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1264 sizeof(*dev->wiphy.ht_capa_mod_mask),
1265 dev->wiphy.ht_capa_mod_mask))
1266 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001267
Johannes Berg55682962007-09-20 13:09:35 -04001268 return genlmsg_end(msg, hdr);
1269
1270 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001271 genlmsg_cancel(msg, hdr);
1272 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001273}
1274
1275static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1276{
1277 int idx = 0;
1278 int start = cb->args[0];
1279 struct cfg80211_registered_device *dev;
1280
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001281 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001282 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001283 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1284 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001285 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001286 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001287 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001288 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001289 dev) < 0) {
1290 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001291 break;
Julius Volzb4637272008-07-08 14:02:19 +02001292 }
Johannes Berg55682962007-09-20 13:09:35 -04001293 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001294 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001295
1296 cb->args[0] = idx;
1297
1298 return skb->len;
1299}
1300
1301static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1302{
1303 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001304 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001305
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001306 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001307 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001308 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001309
Eric W. Biederman15e47302012-09-07 20:12:54 +00001310 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001311 nlmsg_free(msg);
1312 return -ENOBUFS;
1313 }
Johannes Berg55682962007-09-20 13:09:35 -04001314
Johannes Berg134e6372009-07-10 09:51:34 +00001315 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001316}
1317
Jouni Malinen31888482008-10-30 16:59:24 +02001318static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1319 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1320 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1321 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1322 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1323 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1324};
1325
1326static int parse_txq_params(struct nlattr *tb[],
1327 struct ieee80211_txq_params *txq_params)
1328{
Johannes Berga3304b02012-03-28 11:04:24 +02001329 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001330 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1331 !tb[NL80211_TXQ_ATTR_AIFS])
1332 return -EINVAL;
1333
Johannes Berga3304b02012-03-28 11:04:24 +02001334 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001335 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1336 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1337 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1338 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1339
Johannes Berga3304b02012-03-28 11:04:24 +02001340 if (txq_params->ac >= NL80211_NUM_ACS)
1341 return -EINVAL;
1342
Jouni Malinen31888482008-10-30 16:59:24 +02001343 return 0;
1344}
1345
Johannes Bergf444de02010-05-05 15:25:02 +02001346static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1347{
1348 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001349 * You can only set the channel explicitly for WDS interfaces,
1350 * all others have their channel managed via their respective
1351 * "establish a connection" command (connect, join, ...)
1352 *
1353 * For AP/GO and mesh mode, the channel can be set with the
1354 * channel userspace API, but is only stored and passed to the
1355 * low-level driver when the AP starts or the mesh is joined.
1356 * This is for backward compatibility, userspace can also give
1357 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001358 *
1359 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001360 * whatever else is going on, so they have their own special
1361 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001362 */
1363 return !wdev ||
1364 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001365 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001366 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1367 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001368}
1369
Johannes Berg683b6d32012-11-08 21:25:48 +01001370static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1371 struct genl_info *info,
1372 struct cfg80211_chan_def *chandef)
1373{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301374 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001375
1376 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1377 return -EINVAL;
1378
1379 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1380
1381 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001382 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1383 chandef->center_freq1 = control_freq;
1384 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001385
1386 /* Primary channel not allowed */
1387 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1388 return -EINVAL;
1389
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001390 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1391 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001392
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001393 chantype = nla_get_u32(
1394 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1395
1396 switch (chantype) {
1397 case NL80211_CHAN_NO_HT:
1398 case NL80211_CHAN_HT20:
1399 case NL80211_CHAN_HT40PLUS:
1400 case NL80211_CHAN_HT40MINUS:
1401 cfg80211_chandef_create(chandef, chandef->chan,
1402 chantype);
1403 break;
1404 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001405 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001406 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001407 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1408 chandef->width =
1409 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1410 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1411 chandef->center_freq1 =
1412 nla_get_u32(
1413 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1414 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1415 chandef->center_freq2 =
1416 nla_get_u32(
1417 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1418 }
1419
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001420 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001421 return -EINVAL;
1422
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001423 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1424 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001425 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001426
Johannes Berg683b6d32012-11-08 21:25:48 +01001427 return 0;
1428}
1429
Johannes Bergf444de02010-05-05 15:25:02 +02001430static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1431 struct wireless_dev *wdev,
1432 struct genl_info *info)
1433{
Johannes Berg683b6d32012-11-08 21:25:48 +01001434 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001435 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001436 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1437
1438 if (wdev)
1439 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001440
Johannes Bergf444de02010-05-05 15:25:02 +02001441 if (!nl80211_can_set_dev_channel(wdev))
1442 return -EOPNOTSUPP;
1443
Johannes Berg683b6d32012-11-08 21:25:48 +01001444 result = nl80211_parse_chandef(rdev, info, &chandef);
1445 if (result)
1446 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001447
1448 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001449 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001450 case NL80211_IFTYPE_AP:
1451 case NL80211_IFTYPE_P2P_GO:
1452 if (wdev->beacon_interval) {
1453 result = -EBUSY;
1454 break;
1455 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001456 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001457 result = -EINVAL;
1458 break;
1459 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001460 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001461 result = 0;
1462 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001463 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001464 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001465 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001466 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001467 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001468 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001469 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001470 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001471 }
1472 mutex_unlock(&rdev->devlist_mtx);
1473
1474 return result;
1475}
1476
1477static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1478{
Johannes Berg4c476992010-10-04 21:36:35 +02001479 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1480 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001481
Johannes Berg4c476992010-10-04 21:36:35 +02001482 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001483}
1484
Bill Jordane8347eb2010-10-01 13:54:28 -04001485static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1486{
Johannes Berg43b19952010-10-07 13:10:30 +02001487 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1488 struct net_device *dev = info->user_ptr[1];
1489 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001490 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001491
1492 if (!info->attrs[NL80211_ATTR_MAC])
1493 return -EINVAL;
1494
Johannes Berg43b19952010-10-07 13:10:30 +02001495 if (netif_running(dev))
1496 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001497
Johannes Berg43b19952010-10-07 13:10:30 +02001498 if (!rdev->ops->set_wds_peer)
1499 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001500
Johannes Berg43b19952010-10-07 13:10:30 +02001501 if (wdev->iftype != NL80211_IFTYPE_WDS)
1502 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001503
1504 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001505 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001506}
1507
1508
Johannes Berg55682962007-09-20 13:09:35 -04001509static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1510{
1511 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001512 struct net_device *netdev = NULL;
1513 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001514 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001515 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001516 u32 changed;
1517 u8 retry_short = 0, retry_long = 0;
1518 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001519 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001520
Johannes Bergf444de02010-05-05 15:25:02 +02001521 /*
1522 * Try to find the wiphy and netdev. Normally this
1523 * function shouldn't need the netdev, but this is
1524 * done for backward compatibility -- previously
1525 * setting the channel was done per wiphy, but now
1526 * it is per netdev. Previous userland like hostapd
1527 * also passed a netdev to set_wiphy, so that it is
1528 * possible to let that go to the right netdev!
1529 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001530 mutex_lock(&cfg80211_mutex);
1531
Johannes Bergf444de02010-05-05 15:25:02 +02001532 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1533 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1534
1535 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1536 if (netdev && netdev->ieee80211_ptr) {
1537 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1538 mutex_lock(&rdev->mtx);
1539 } else
1540 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001541 }
1542
Johannes Bergf444de02010-05-05 15:25:02 +02001543 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001544 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1545 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001546 if (IS_ERR(rdev)) {
1547 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001548 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001549 }
1550 wdev = NULL;
1551 netdev = NULL;
1552 result = 0;
1553
1554 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001555 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001556 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001557
1558 /*
1559 * end workaround code, by now the rdev is available
1560 * and locked, and wdev may or may not be NULL.
1561 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001562
1563 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001564 result = cfg80211_dev_rename(
1565 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001566
1567 mutex_unlock(&cfg80211_mutex);
1568
1569 if (result)
1570 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001571
Jouni Malinen31888482008-10-30 16:59:24 +02001572 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1573 struct ieee80211_txq_params txq_params;
1574 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1575
1576 if (!rdev->ops->set_txq_params) {
1577 result = -EOPNOTSUPP;
1578 goto bad_res;
1579 }
1580
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001581 if (!netdev) {
1582 result = -EINVAL;
1583 goto bad_res;
1584 }
1585
Johannes Berg133a3ff2011-11-03 14:50:13 +01001586 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1587 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1588 result = -EINVAL;
1589 goto bad_res;
1590 }
1591
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001592 if (!netif_running(netdev)) {
1593 result = -ENETDOWN;
1594 goto bad_res;
1595 }
1596
Jouni Malinen31888482008-10-30 16:59:24 +02001597 nla_for_each_nested(nl_txq_params,
1598 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1599 rem_txq_params) {
1600 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1601 nla_data(nl_txq_params),
1602 nla_len(nl_txq_params),
1603 txq_params_policy);
1604 result = parse_txq_params(tb, &txq_params);
1605 if (result)
1606 goto bad_res;
1607
Hila Gonene35e4d22012-06-27 17:19:42 +03001608 result = rdev_set_txq_params(rdev, netdev,
1609 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001610 if (result)
1611 goto bad_res;
1612 }
1613 }
1614
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001615 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001616 result = __nl80211_set_channel(rdev,
1617 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1618 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001619 if (result)
1620 goto bad_res;
1621 }
1622
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001623 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001624 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001625 enum nl80211_tx_power_setting type;
1626 int idx, mbm = 0;
1627
Johannes Bergc8442112012-10-24 10:17:18 +02001628 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1629 txp_wdev = NULL;
1630
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001631 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001632 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001633 goto bad_res;
1634 }
1635
1636 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1637 type = nla_get_u32(info->attrs[idx]);
1638
1639 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1640 (type != NL80211_TX_POWER_AUTOMATIC)) {
1641 result = -EINVAL;
1642 goto bad_res;
1643 }
1644
1645 if (type != NL80211_TX_POWER_AUTOMATIC) {
1646 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1647 mbm = nla_get_u32(info->attrs[idx]);
1648 }
1649
Johannes Bergc8442112012-10-24 10:17:18 +02001650 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001651 if (result)
1652 goto bad_res;
1653 }
1654
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001655 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1656 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1657 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001658 if ((!rdev->wiphy.available_antennas_tx &&
1659 !rdev->wiphy.available_antennas_rx) ||
1660 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001661 result = -EOPNOTSUPP;
1662 goto bad_res;
1663 }
1664
1665 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1666 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1667
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001668 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001669 * available antenna masks, except for the "all" mask */
1670 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1671 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001672 result = -EINVAL;
1673 goto bad_res;
1674 }
1675
Bruno Randolf7f531e02010-12-16 11:30:22 +09001676 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1677 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001678
Hila Gonene35e4d22012-06-27 17:19:42 +03001679 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001680 if (result)
1681 goto bad_res;
1682 }
1683
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001684 changed = 0;
1685
1686 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1687 retry_short = nla_get_u8(
1688 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1689 if (retry_short == 0) {
1690 result = -EINVAL;
1691 goto bad_res;
1692 }
1693 changed |= WIPHY_PARAM_RETRY_SHORT;
1694 }
1695
1696 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1697 retry_long = nla_get_u8(
1698 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1699 if (retry_long == 0) {
1700 result = -EINVAL;
1701 goto bad_res;
1702 }
1703 changed |= WIPHY_PARAM_RETRY_LONG;
1704 }
1705
1706 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1707 frag_threshold = nla_get_u32(
1708 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1709 if (frag_threshold < 256) {
1710 result = -EINVAL;
1711 goto bad_res;
1712 }
1713 if (frag_threshold != (u32) -1) {
1714 /*
1715 * Fragments (apart from the last one) are required to
1716 * have even length. Make the fragmentation code
1717 * simpler by stripping LSB should someone try to use
1718 * odd threshold value.
1719 */
1720 frag_threshold &= ~0x1;
1721 }
1722 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1723 }
1724
1725 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1726 rts_threshold = nla_get_u32(
1727 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1728 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1729 }
1730
Lukáš Turek81077e82009-12-21 22:50:47 +01001731 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1732 coverage_class = nla_get_u8(
1733 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1734 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1735 }
1736
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001737 if (changed) {
1738 u8 old_retry_short, old_retry_long;
1739 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001740 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001741
1742 if (!rdev->ops->set_wiphy_params) {
1743 result = -EOPNOTSUPP;
1744 goto bad_res;
1745 }
1746
1747 old_retry_short = rdev->wiphy.retry_short;
1748 old_retry_long = rdev->wiphy.retry_long;
1749 old_frag_threshold = rdev->wiphy.frag_threshold;
1750 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001751 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001752
1753 if (changed & WIPHY_PARAM_RETRY_SHORT)
1754 rdev->wiphy.retry_short = retry_short;
1755 if (changed & WIPHY_PARAM_RETRY_LONG)
1756 rdev->wiphy.retry_long = retry_long;
1757 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1758 rdev->wiphy.frag_threshold = frag_threshold;
1759 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1760 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001761 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1762 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001763
Hila Gonene35e4d22012-06-27 17:19:42 +03001764 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001765 if (result) {
1766 rdev->wiphy.retry_short = old_retry_short;
1767 rdev->wiphy.retry_long = old_retry_long;
1768 rdev->wiphy.frag_threshold = old_frag_threshold;
1769 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001770 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001771 }
1772 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001773
Johannes Berg306d6112008-12-08 12:39:04 +01001774 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001775 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001776 if (netdev)
1777 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001778 return result;
1779}
1780
Johannes Berg71bbc992012-06-15 15:30:18 +02001781static inline u64 wdev_id(struct wireless_dev *wdev)
1782{
1783 return (u64)wdev->identifier |
1784 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1785}
Johannes Berg55682962007-09-20 13:09:35 -04001786
Johannes Berg683b6d32012-11-08 21:25:48 +01001787static int nl80211_send_chandef(struct sk_buff *msg,
1788 struct cfg80211_chan_def *chandef)
1789{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001790 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001791
Johannes Berg683b6d32012-11-08 21:25:48 +01001792 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1793 chandef->chan->center_freq))
1794 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001795 switch (chandef->width) {
1796 case NL80211_CHAN_WIDTH_20_NOHT:
1797 case NL80211_CHAN_WIDTH_20:
1798 case NL80211_CHAN_WIDTH_40:
1799 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1800 cfg80211_get_chandef_type(chandef)))
1801 return -ENOBUFS;
1802 break;
1803 default:
1804 break;
1805 }
1806 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1807 return -ENOBUFS;
1808 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1809 return -ENOBUFS;
1810 if (chandef->center_freq2 &&
1811 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001812 return -ENOBUFS;
1813 return 0;
1814}
1815
Eric W. Biederman15e47302012-09-07 20:12:54 +00001816static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001817 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001818 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001819{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001820 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001821 void *hdr;
1822
Eric W. Biederman15e47302012-09-07 20:12:54 +00001823 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001824 if (!hdr)
1825 return -1;
1826
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001827 if (dev &&
1828 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001829 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001830 goto nla_put_failure;
1831
1832 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1833 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001834 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001835 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001836 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1837 rdev->devlist_generation ^
1838 (cfg80211_rdev_list_generation << 2)))
1839 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001840
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001841 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001842 int ret;
1843 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001844
Johannes Berg683b6d32012-11-08 21:25:48 +01001845 ret = rdev_get_channel(rdev, wdev, &chandef);
1846 if (ret == 0) {
1847 if (nl80211_send_chandef(msg, &chandef))
1848 goto nla_put_failure;
1849 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001850 }
1851
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001852 if (wdev->ssid_len) {
1853 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1854 goto nla_put_failure;
1855 }
1856
Johannes Berg55682962007-09-20 13:09:35 -04001857 return genlmsg_end(msg, hdr);
1858
1859 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001860 genlmsg_cancel(msg, hdr);
1861 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001862}
1863
1864static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1865{
1866 int wp_idx = 0;
1867 int if_idx = 0;
1868 int wp_start = cb->args[0];
1869 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001870 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001871 struct wireless_dev *wdev;
1872
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001873 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001874 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1875 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001876 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001877 if (wp_idx < wp_start) {
1878 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001879 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001880 }
Johannes Berg55682962007-09-20 13:09:35 -04001881 if_idx = 0;
1882
Johannes Bergf5ea9122009-08-07 16:17:38 +02001883 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001884 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001885 if (if_idx < if_start) {
1886 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001887 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001888 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001889 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001890 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001891 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001892 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001893 goto out;
1894 }
1895 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001896 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001897 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001898
1899 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001900 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001901 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001902 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001903
1904 cb->args[0] = wp_idx;
1905 cb->args[1] = if_idx;
1906
1907 return skb->len;
1908}
1909
1910static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1911{
1912 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001913 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001914 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001915
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001916 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001917 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001918 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001919
Eric W. Biederman15e47302012-09-07 20:12:54 +00001920 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001921 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001922 nlmsg_free(msg);
1923 return -ENOBUFS;
1924 }
Johannes Berg55682962007-09-20 13:09:35 -04001925
Johannes Berg134e6372009-07-10 09:51:34 +00001926 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001927}
1928
Michael Wu66f7ac52008-01-31 19:48:22 +01001929static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1930 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1931 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1932 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1933 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1934 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1935};
1936
1937static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1938{
1939 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1940 int flag;
1941
1942 *mntrflags = 0;
1943
1944 if (!nla)
1945 return -EINVAL;
1946
1947 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1948 nla, mntr_flags_policy))
1949 return -EINVAL;
1950
1951 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1952 if (flags[flag])
1953 *mntrflags |= (1<<flag);
1954
1955 return 0;
1956}
1957
Johannes Berg9bc383d2009-11-19 11:55:19 +01001958static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001959 struct net_device *netdev, u8 use_4addr,
1960 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001961{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001962 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001963 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001964 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001965 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001966 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001967
1968 switch (iftype) {
1969 case NL80211_IFTYPE_AP_VLAN:
1970 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1971 return 0;
1972 break;
1973 case NL80211_IFTYPE_STATION:
1974 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1975 return 0;
1976 break;
1977 default:
1978 break;
1979 }
1980
1981 return -EOPNOTSUPP;
1982}
1983
Johannes Berg55682962007-09-20 13:09:35 -04001984static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1985{
Johannes Berg4c476992010-10-04 21:36:35 +02001986 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001987 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001988 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001989 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001990 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001991 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001992 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001993
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001994 memset(&params, 0, sizeof(params));
1995
Johannes Berg04a773a2009-04-19 21:24:32 +02001996 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001997
Johannes Berg723b0382008-09-16 20:22:09 +02001998 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001999 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002000 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002001 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002002 if (ntype > NL80211_IFTYPE_MAX)
2003 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002004 }
2005
Johannes Berg92ffe052008-09-16 20:39:36 +02002006 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002007 struct wireless_dev *wdev = dev->ieee80211_ptr;
2008
Johannes Berg4c476992010-10-04 21:36:35 +02002009 if (ntype != NL80211_IFTYPE_MESH_POINT)
2010 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002011 if (netif_running(dev))
2012 return -EBUSY;
2013
2014 wdev_lock(wdev);
2015 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2016 IEEE80211_MAX_MESH_ID_LEN);
2017 wdev->mesh_id_up_len =
2018 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2019 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2020 wdev->mesh_id_up_len);
2021 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002022 }
2023
Felix Fietkau8b787642009-11-10 18:53:10 +01002024 if (info->attrs[NL80211_ATTR_4ADDR]) {
2025 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2026 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002027 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002028 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002029 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002030 } else {
2031 params.use_4addr = -1;
2032 }
2033
Johannes Berg92ffe052008-09-16 20:39:36 +02002034 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002035 if (ntype != NL80211_IFTYPE_MONITOR)
2036 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002037 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2038 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002039 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002040 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002041
2042 flags = &_flags;
2043 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002044 }
Johannes Berg3b858752009-03-12 09:55:09 +01002045
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002046 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002047 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002048 else
2049 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002050
Johannes Berg9bc383d2009-11-19 11:55:19 +01002051 if (!err && params.use_4addr != -1)
2052 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2053
Johannes Berg55682962007-09-20 13:09:35 -04002054 return err;
2055}
2056
2057static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2058{
Johannes Berg4c476992010-10-04 21:36:35 +02002059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002060 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002061 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002062 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002063 int err;
2064 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002065 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002066
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002067 memset(&params, 0, sizeof(params));
2068
Johannes Berg55682962007-09-20 13:09:35 -04002069 if (!info->attrs[NL80211_ATTR_IFNAME])
2070 return -EINVAL;
2071
2072 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2073 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2074 if (type > NL80211_IFTYPE_MAX)
2075 return -EINVAL;
2076 }
2077
Johannes Berg79c97e92009-07-07 03:56:12 +02002078 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002079 !(rdev->wiphy.interface_modes & (1 << type)))
2080 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002081
Johannes Berg9bc383d2009-11-19 11:55:19 +01002082 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002083 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002084 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002085 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002086 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002087 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002088
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002089 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2090 if (!msg)
2091 return -ENOMEM;
2092
Michael Wu66f7ac52008-01-31 19:48:22 +01002093 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2094 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2095 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002096 wdev = rdev_add_virtual_intf(rdev,
2097 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2098 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002099 if (IS_ERR(wdev)) {
2100 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002101 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002102 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002103
Johannes Berg98104fde2012-06-16 00:19:54 +02002104 switch (type) {
2105 case NL80211_IFTYPE_MESH_POINT:
2106 if (!info->attrs[NL80211_ATTR_MESH_ID])
2107 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002108 wdev_lock(wdev);
2109 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2110 IEEE80211_MAX_MESH_ID_LEN);
2111 wdev->mesh_id_up_len =
2112 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2113 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2114 wdev->mesh_id_up_len);
2115 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002116 break;
2117 case NL80211_IFTYPE_P2P_DEVICE:
2118 /*
2119 * P2P Device doesn't have a netdev, so doesn't go
2120 * through the netdev notifier and must be added here
2121 */
2122 mutex_init(&wdev->mtx);
2123 INIT_LIST_HEAD(&wdev->event_list);
2124 spin_lock_init(&wdev->event_lock);
2125 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2126 spin_lock_init(&wdev->mgmt_registrations_lock);
2127
2128 mutex_lock(&rdev->devlist_mtx);
2129 wdev->identifier = ++rdev->wdev_id;
2130 list_add_rcu(&wdev->list, &rdev->wdev_list);
2131 rdev->devlist_generation++;
2132 mutex_unlock(&rdev->devlist_mtx);
2133 break;
2134 default:
2135 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002136 }
2137
Eric W. Biederman15e47302012-09-07 20:12:54 +00002138 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002139 rdev, wdev) < 0) {
2140 nlmsg_free(msg);
2141 return -ENOBUFS;
2142 }
2143
2144 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002145}
2146
2147static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2148{
Johannes Berg4c476992010-10-04 21:36:35 +02002149 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002150 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002151
Johannes Berg4c476992010-10-04 21:36:35 +02002152 if (!rdev->ops->del_virtual_intf)
2153 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002154
Johannes Berg84efbb82012-06-16 00:00:26 +02002155 /*
2156 * If we remove a wireless device without a netdev then clear
2157 * user_ptr[1] so that nl80211_post_doit won't dereference it
2158 * to check if it needs to do dev_put(). Otherwise it crashes
2159 * since the wdev has been freed, unlike with a netdev where
2160 * we need the dev_put() for the netdev to really be freed.
2161 */
2162 if (!wdev->netdev)
2163 info->user_ptr[1] = NULL;
2164
Hila Gonene35e4d22012-06-27 17:19:42 +03002165 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002166}
2167
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002168static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2169{
2170 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2171 struct net_device *dev = info->user_ptr[1];
2172 u16 noack_map;
2173
2174 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2175 return -EINVAL;
2176
2177 if (!rdev->ops->set_noack_map)
2178 return -EOPNOTSUPP;
2179
2180 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2181
Hila Gonene35e4d22012-06-27 17:19:42 +03002182 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002183}
2184
Johannes Berg41ade002007-12-19 02:03:29 +01002185struct get_key_cookie {
2186 struct sk_buff *msg;
2187 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002188 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002189};
2190
2191static void get_key_callback(void *c, struct key_params *params)
2192{
Johannes Bergb9454e82009-07-08 13:29:08 +02002193 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002194 struct get_key_cookie *cookie = c;
2195
David S. Miller9360ffd2012-03-29 04:41:26 -04002196 if ((params->key &&
2197 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2198 params->key_len, params->key)) ||
2199 (params->seq &&
2200 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2201 params->seq_len, params->seq)) ||
2202 (params->cipher &&
2203 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2204 params->cipher)))
2205 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002206
Johannes Bergb9454e82009-07-08 13:29:08 +02002207 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2208 if (!key)
2209 goto nla_put_failure;
2210
David S. Miller9360ffd2012-03-29 04:41:26 -04002211 if ((params->key &&
2212 nla_put(cookie->msg, NL80211_KEY_DATA,
2213 params->key_len, params->key)) ||
2214 (params->seq &&
2215 nla_put(cookie->msg, NL80211_KEY_SEQ,
2216 params->seq_len, params->seq)) ||
2217 (params->cipher &&
2218 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2219 params->cipher)))
2220 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002221
David S. Miller9360ffd2012-03-29 04:41:26 -04002222 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2223 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002224
2225 nla_nest_end(cookie->msg, key);
2226
Johannes Berg41ade002007-12-19 02:03:29 +01002227 return;
2228 nla_put_failure:
2229 cookie->error = 1;
2230}
2231
2232static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2233{
Johannes Berg4c476992010-10-04 21:36:35 +02002234 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002235 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002236 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002237 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002238 const u8 *mac_addr = NULL;
2239 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002240 struct get_key_cookie cookie = {
2241 .error = 0,
2242 };
2243 void *hdr;
2244 struct sk_buff *msg;
2245
2246 if (info->attrs[NL80211_ATTR_KEY_IDX])
2247 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2248
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002249 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002250 return -EINVAL;
2251
2252 if (info->attrs[NL80211_ATTR_MAC])
2253 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2254
Johannes Berge31b8212010-10-05 19:39:30 +02002255 pairwise = !!mac_addr;
2256 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2257 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2258 if (kt >= NUM_NL80211_KEYTYPES)
2259 return -EINVAL;
2260 if (kt != NL80211_KEYTYPE_GROUP &&
2261 kt != NL80211_KEYTYPE_PAIRWISE)
2262 return -EINVAL;
2263 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2264 }
2265
Johannes Berg4c476992010-10-04 21:36:35 +02002266 if (!rdev->ops->get_key)
2267 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002268
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002269 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002270 if (!msg)
2271 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002272
Eric W. Biederman15e47302012-09-07 20:12:54 +00002273 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002274 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002275 if (IS_ERR(hdr))
2276 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002277
2278 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002279 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002280
David S. Miller9360ffd2012-03-29 04:41:26 -04002281 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2282 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2283 goto nla_put_failure;
2284 if (mac_addr &&
2285 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2286 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002287
Johannes Berge31b8212010-10-05 19:39:30 +02002288 if (pairwise && mac_addr &&
2289 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2290 return -ENOENT;
2291
Hila Gonene35e4d22012-06-27 17:19:42 +03002292 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2293 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002294
2295 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002296 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002297
2298 if (cookie.error)
2299 goto nla_put_failure;
2300
2301 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002302 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002303
2304 nla_put_failure:
2305 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002306 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002307 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002308 return err;
2309}
2310
2311static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2312{
Johannes Berg4c476992010-10-04 21:36:35 +02002313 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002314 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002315 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002316 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002317
Johannes Bergb9454e82009-07-08 13:29:08 +02002318 err = nl80211_parse_key(info, &key);
2319 if (err)
2320 return err;
2321
2322 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002323 return -EINVAL;
2324
Johannes Bergb9454e82009-07-08 13:29:08 +02002325 /* only support setting default key */
2326 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002327 return -EINVAL;
2328
Johannes Bergfffd0932009-07-08 14:22:54 +02002329 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002330
2331 if (key.def) {
2332 if (!rdev->ops->set_default_key) {
2333 err = -EOPNOTSUPP;
2334 goto out;
2335 }
2336
2337 err = nl80211_key_allowed(dev->ieee80211_ptr);
2338 if (err)
2339 goto out;
2340
Hila Gonene35e4d22012-06-27 17:19:42 +03002341 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002342 key.def_uni, key.def_multi);
2343
2344 if (err)
2345 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002346
Johannes Berg3d23e342009-09-29 23:27:28 +02002347#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002348 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002349#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002350 } else {
2351 if (key.def_uni || !key.def_multi) {
2352 err = -EINVAL;
2353 goto out;
2354 }
2355
2356 if (!rdev->ops->set_default_mgmt_key) {
2357 err = -EOPNOTSUPP;
2358 goto out;
2359 }
2360
2361 err = nl80211_key_allowed(dev->ieee80211_ptr);
2362 if (err)
2363 goto out;
2364
Hila Gonene35e4d22012-06-27 17:19:42 +03002365 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002366 if (err)
2367 goto out;
2368
2369#ifdef CONFIG_CFG80211_WEXT
2370 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2371#endif
2372 }
2373
2374 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002375 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002376
Johannes Berg41ade002007-12-19 02:03:29 +01002377 return err;
2378}
2379
2380static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2381{
Johannes Berg4c476992010-10-04 21:36:35 +02002382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002383 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002384 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002385 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002386 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002387
Johannes Bergb9454e82009-07-08 13:29:08 +02002388 err = nl80211_parse_key(info, &key);
2389 if (err)
2390 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002391
Johannes Bergb9454e82009-07-08 13:29:08 +02002392 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002393 return -EINVAL;
2394
Johannes Berg41ade002007-12-19 02:03:29 +01002395 if (info->attrs[NL80211_ATTR_MAC])
2396 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2397
Johannes Berge31b8212010-10-05 19:39:30 +02002398 if (key.type == -1) {
2399 if (mac_addr)
2400 key.type = NL80211_KEYTYPE_PAIRWISE;
2401 else
2402 key.type = NL80211_KEYTYPE_GROUP;
2403 }
2404
2405 /* for now */
2406 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2407 key.type != NL80211_KEYTYPE_GROUP)
2408 return -EINVAL;
2409
Johannes Berg4c476992010-10-04 21:36:35 +02002410 if (!rdev->ops->add_key)
2411 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002412
Johannes Berge31b8212010-10-05 19:39:30 +02002413 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2414 key.type == NL80211_KEYTYPE_PAIRWISE,
2415 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002416 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002417
2418 wdev_lock(dev->ieee80211_ptr);
2419 err = nl80211_key_allowed(dev->ieee80211_ptr);
2420 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002421 err = rdev_add_key(rdev, dev, key.idx,
2422 key.type == NL80211_KEYTYPE_PAIRWISE,
2423 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002424 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002425
Johannes Berg41ade002007-12-19 02:03:29 +01002426 return err;
2427}
2428
2429static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2430{
Johannes Berg4c476992010-10-04 21:36:35 +02002431 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002432 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002433 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002434 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002435 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002436
Johannes Bergb9454e82009-07-08 13:29:08 +02002437 err = nl80211_parse_key(info, &key);
2438 if (err)
2439 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002440
2441 if (info->attrs[NL80211_ATTR_MAC])
2442 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2443
Johannes Berge31b8212010-10-05 19:39:30 +02002444 if (key.type == -1) {
2445 if (mac_addr)
2446 key.type = NL80211_KEYTYPE_PAIRWISE;
2447 else
2448 key.type = NL80211_KEYTYPE_GROUP;
2449 }
2450
2451 /* for now */
2452 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2453 key.type != NL80211_KEYTYPE_GROUP)
2454 return -EINVAL;
2455
Johannes Berg4c476992010-10-04 21:36:35 +02002456 if (!rdev->ops->del_key)
2457 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002458
Johannes Bergfffd0932009-07-08 14:22:54 +02002459 wdev_lock(dev->ieee80211_ptr);
2460 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002461
2462 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2463 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2464 err = -ENOENT;
2465
Johannes Bergfffd0932009-07-08 14:22:54 +02002466 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002467 err = rdev_del_key(rdev, dev, key.idx,
2468 key.type == NL80211_KEYTYPE_PAIRWISE,
2469 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002470
Johannes Berg3d23e342009-09-29 23:27:28 +02002471#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002472 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002473 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002474 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002475 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002476 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2477 }
2478#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002479 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002480
Johannes Berg41ade002007-12-19 02:03:29 +01002481 return err;
2482}
2483
Johannes Berg88600202012-02-13 15:17:18 +01002484static int nl80211_parse_beacon(struct genl_info *info,
2485 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002486{
Johannes Berg88600202012-02-13 15:17:18 +01002487 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002488
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002489 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2490 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2491 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2492 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002493 return -EINVAL;
2494
Johannes Berg88600202012-02-13 15:17:18 +01002495 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002496
Johannes Berged1b6cc2007-12-19 02:03:32 +01002497 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002498 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2499 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2500 if (!bcn->head_len)
2501 return -EINVAL;
2502 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002503 }
2504
2505 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002506 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2507 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002508 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002509 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002510 }
2511
Johannes Berg4c476992010-10-04 21:36:35 +02002512 if (!haveinfo)
2513 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002514
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002515 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002516 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2517 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002518 }
2519
2520 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002521 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002522 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002523 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002524 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2525 }
2526
2527 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002528 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002529 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002530 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002531 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2532 }
2533
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002534 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002535 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002536 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002537 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002538 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2539 }
2540
Johannes Berg88600202012-02-13 15:17:18 +01002541 return 0;
2542}
2543
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002544static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2545 struct cfg80211_ap_settings *params)
2546{
2547 struct wireless_dev *wdev;
2548 bool ret = false;
2549
2550 mutex_lock(&rdev->devlist_mtx);
2551
Johannes Berg89a54e42012-06-15 14:33:17 +02002552 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002553 if (wdev->iftype != NL80211_IFTYPE_AP &&
2554 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2555 continue;
2556
Johannes Berg683b6d32012-11-08 21:25:48 +01002557 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002558 continue;
2559
Johannes Berg683b6d32012-11-08 21:25:48 +01002560 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002561 ret = true;
2562 break;
2563 }
2564
2565 mutex_unlock(&rdev->devlist_mtx);
2566
2567 return ret;
2568}
2569
Jouni Malinene39e5b52012-09-30 19:29:39 +03002570static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2571 enum nl80211_auth_type auth_type,
2572 enum nl80211_commands cmd)
2573{
2574 if (auth_type > NL80211_AUTHTYPE_MAX)
2575 return false;
2576
2577 switch (cmd) {
2578 case NL80211_CMD_AUTHENTICATE:
2579 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2580 auth_type == NL80211_AUTHTYPE_SAE)
2581 return false;
2582 return true;
2583 case NL80211_CMD_CONNECT:
2584 case NL80211_CMD_START_AP:
2585 /* SAE not supported yet */
2586 if (auth_type == NL80211_AUTHTYPE_SAE)
2587 return false;
2588 return true;
2589 default:
2590 return false;
2591 }
2592}
2593
Johannes Berg88600202012-02-13 15:17:18 +01002594static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2595{
2596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2597 struct net_device *dev = info->user_ptr[1];
2598 struct wireless_dev *wdev = dev->ieee80211_ptr;
2599 struct cfg80211_ap_settings params;
2600 int err;
2601
2602 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2603 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2604 return -EOPNOTSUPP;
2605
2606 if (!rdev->ops->start_ap)
2607 return -EOPNOTSUPP;
2608
2609 if (wdev->beacon_interval)
2610 return -EALREADY;
2611
2612 memset(&params, 0, sizeof(params));
2613
2614 /* these are required for START_AP */
2615 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2616 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2617 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2618 return -EINVAL;
2619
2620 err = nl80211_parse_beacon(info, &params.beacon);
2621 if (err)
2622 return err;
2623
2624 params.beacon_interval =
2625 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2626 params.dtim_period =
2627 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2628
2629 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2630 if (err)
2631 return err;
2632
2633 /*
2634 * In theory, some of these attributes should be required here
2635 * but since they were not used when the command was originally
2636 * added, keep them optional for old user space programs to let
2637 * them continue to work with drivers that do not need the
2638 * additional information -- drivers must check!
2639 */
2640 if (info->attrs[NL80211_ATTR_SSID]) {
2641 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2642 params.ssid_len =
2643 nla_len(info->attrs[NL80211_ATTR_SSID]);
2644 if (params.ssid_len == 0 ||
2645 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2646 return -EINVAL;
2647 }
2648
2649 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2650 params.hidden_ssid = nla_get_u32(
2651 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2652 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2653 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2654 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2655 return -EINVAL;
2656 }
2657
2658 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2659
2660 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2661 params.auth_type = nla_get_u32(
2662 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002663 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2664 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002665 return -EINVAL;
2666 } else
2667 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2668
2669 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2670 NL80211_MAX_NR_CIPHER_SUITES);
2671 if (err)
2672 return err;
2673
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302674 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2675 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2676 return -EOPNOTSUPP;
2677 params.inactivity_timeout = nla_get_u16(
2678 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2679 }
2680
Johannes Berg53cabad2012-11-14 15:17:28 +01002681 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2682 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2683 return -EINVAL;
2684 params.p2p_ctwindow =
2685 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2686 if (params.p2p_ctwindow > 127)
2687 return -EINVAL;
2688 if (params.p2p_ctwindow != 0 &&
2689 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2690 return -EINVAL;
2691 }
2692
2693 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2694 u8 tmp;
2695
2696 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2697 return -EINVAL;
2698 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2699 if (tmp > 1)
2700 return -EINVAL;
2701 params.p2p_opp_ps = tmp;
2702 if (params.p2p_opp_ps != 0 &&
2703 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2704 return -EINVAL;
2705 }
2706
Johannes Bergaa430da2012-05-16 23:50:18 +02002707 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002708 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2709 if (err)
2710 return err;
2711 } else if (wdev->preset_chandef.chan) {
2712 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002713 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002714 return -EINVAL;
2715
Johannes Berg683b6d32012-11-08 21:25:48 +01002716 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002717 return -EINVAL;
2718
Michal Kaziore4e32452012-06-29 12:47:08 +02002719 mutex_lock(&rdev->devlist_mtx);
Johannes Berg683b6d32012-11-08 21:25:48 +01002720 err = cfg80211_can_use_chan(rdev, wdev, params.chandef.chan,
Michal Kaziore4e32452012-06-29 12:47:08 +02002721 CHAN_MODE_SHARED);
2722 mutex_unlock(&rdev->devlist_mtx);
2723
2724 if (err)
2725 return err;
2726
Hila Gonene35e4d22012-06-27 17:19:42 +03002727 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002728 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002729 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002730 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002731 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002732 wdev->ssid_len = params.ssid_len;
2733 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002734 }
Johannes Berg56d18932011-05-09 18:41:15 +02002735 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002736}
2737
Johannes Berg88600202012-02-13 15:17:18 +01002738static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2739{
2740 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2741 struct net_device *dev = info->user_ptr[1];
2742 struct wireless_dev *wdev = dev->ieee80211_ptr;
2743 struct cfg80211_beacon_data params;
2744 int err;
2745
2746 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2747 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2748 return -EOPNOTSUPP;
2749
2750 if (!rdev->ops->change_beacon)
2751 return -EOPNOTSUPP;
2752
2753 if (!wdev->beacon_interval)
2754 return -EINVAL;
2755
2756 err = nl80211_parse_beacon(info, &params);
2757 if (err)
2758 return err;
2759
Hila Gonene35e4d22012-06-27 17:19:42 +03002760 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002761}
2762
2763static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002764{
Johannes Berg4c476992010-10-04 21:36:35 +02002765 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2766 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002767
Michal Kazior60771782012-06-29 12:46:56 +02002768 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002769}
2770
Johannes Berg5727ef12007-12-19 02:03:34 +01002771static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2772 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2773 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2774 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002775 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002776 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002777 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002778};
2779
Johannes Bergeccb8e82009-05-11 21:57:56 +03002780static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002781 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002782 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002783{
2784 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002785 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002786 int flag;
2787
Johannes Bergeccb8e82009-05-11 21:57:56 +03002788 /*
2789 * Try parsing the new attribute first so userspace
2790 * can specify both for older kernels.
2791 */
2792 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2793 if (nla) {
2794 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002795
Johannes Bergeccb8e82009-05-11 21:57:56 +03002796 sta_flags = nla_data(nla);
2797 params->sta_flags_mask = sta_flags->mask;
2798 params->sta_flags_set = sta_flags->set;
2799 if ((params->sta_flags_mask |
2800 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2801 return -EINVAL;
2802 return 0;
2803 }
2804
2805 /* if present, parse the old attribute */
2806
2807 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002808 if (!nla)
2809 return 0;
2810
2811 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2812 nla, sta_flags_policy))
2813 return -EINVAL;
2814
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002815 /*
2816 * Only allow certain flags for interface types so that
2817 * other attributes are silently ignored. Remember that
2818 * this is backward compatibility code with old userspace
2819 * and shouldn't be hit in other cases anyway.
2820 */
2821 switch (iftype) {
2822 case NL80211_IFTYPE_AP:
2823 case NL80211_IFTYPE_AP_VLAN:
2824 case NL80211_IFTYPE_P2P_GO:
2825 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2826 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2827 BIT(NL80211_STA_FLAG_WME) |
2828 BIT(NL80211_STA_FLAG_MFP);
2829 break;
2830 case NL80211_IFTYPE_P2P_CLIENT:
2831 case NL80211_IFTYPE_STATION:
2832 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2833 BIT(NL80211_STA_FLAG_TDLS_PEER);
2834 break;
2835 case NL80211_IFTYPE_MESH_POINT:
2836 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2837 BIT(NL80211_STA_FLAG_MFP) |
2838 BIT(NL80211_STA_FLAG_AUTHORIZED);
2839 default:
2840 return -EINVAL;
2841 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002842
Johannes Berg3383b5a2012-05-10 20:14:43 +02002843 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2844 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002845 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002846
Johannes Berg3383b5a2012-05-10 20:14:43 +02002847 /* no longer support new API additions in old API */
2848 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2849 return -EINVAL;
2850 }
2851 }
2852
Johannes Berg5727ef12007-12-19 02:03:34 +01002853 return 0;
2854}
2855
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002856static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2857 int attr)
2858{
2859 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002860 u32 bitrate;
2861 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002862
2863 rate = nla_nest_start(msg, attr);
2864 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002865 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002866
2867 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2868 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002869 /* report 16-bit bitrate only if we can */
2870 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01002871 if (bitrate > 0 &&
2872 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
2873 return false;
2874 if (bitrate_compat > 0 &&
2875 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
2876 return false;
2877
2878 if (info->flags & RATE_INFO_FLAGS_MCS) {
2879 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
2880 return false;
2881 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2882 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2883 return false;
2884 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2885 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2886 return false;
2887 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
2888 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
2889 return false;
2890 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
2891 return false;
2892 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
2893 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
2894 return false;
2895 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
2896 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
2897 return false;
2898 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
2899 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
2900 return false;
2901 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
2902 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
2903 return false;
2904 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
2905 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
2906 return false;
2907 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002908
2909 nla_nest_end(msg, rate);
2910 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002911}
2912
Eric W. Biederman15e47302012-09-07 20:12:54 +00002913static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002914 int flags,
2915 struct cfg80211_registered_device *rdev,
2916 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002917 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002918{
2919 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002920 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002921
Eric W. Biederman15e47302012-09-07 20:12:54 +00002922 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002923 if (!hdr)
2924 return -1;
2925
David S. Miller9360ffd2012-03-29 04:41:26 -04002926 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2927 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2928 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2929 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002930
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002931 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2932 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002933 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002934 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2935 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2936 sinfo->connected_time))
2937 goto nla_put_failure;
2938 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2939 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2940 sinfo->inactive_time))
2941 goto nla_put_failure;
2942 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2943 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2944 sinfo->rx_bytes))
2945 goto nla_put_failure;
2946 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2947 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2948 sinfo->tx_bytes))
2949 goto nla_put_failure;
2950 if ((sinfo->filled & STATION_INFO_LLID) &&
2951 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2952 goto nla_put_failure;
2953 if ((sinfo->filled & STATION_INFO_PLID) &&
2954 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2955 goto nla_put_failure;
2956 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2957 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2958 sinfo->plink_state))
2959 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002960 switch (rdev->wiphy.signal_type) {
2961 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002962 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2963 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2964 sinfo->signal))
2965 goto nla_put_failure;
2966 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2967 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2968 sinfo->signal_avg))
2969 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002970 break;
2971 default:
2972 break;
2973 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002974 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002975 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2976 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002977 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002978 }
2979 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2980 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2981 NL80211_STA_INFO_RX_BITRATE))
2982 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002983 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002984 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2985 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2986 sinfo->rx_packets))
2987 goto nla_put_failure;
2988 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2989 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2990 sinfo->tx_packets))
2991 goto nla_put_failure;
2992 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2993 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2994 sinfo->tx_retries))
2995 goto nla_put_failure;
2996 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2997 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2998 sinfo->tx_failed))
2999 goto nla_put_failure;
3000 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3001 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3002 sinfo->beacon_loss_count))
3003 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003004 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3005 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3006 if (!bss_param)
3007 goto nla_put_failure;
3008
David S. Miller9360ffd2012-03-29 04:41:26 -04003009 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3010 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3011 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3012 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3013 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3014 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3015 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3016 sinfo->bss_param.dtim_period) ||
3017 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3018 sinfo->bss_param.beacon_interval))
3019 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003020
3021 nla_nest_end(msg, bss_param);
3022 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003023 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3024 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3025 sizeof(struct nl80211_sta_flag_update),
3026 &sinfo->sta_flags))
3027 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003028 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3029 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3030 sinfo->t_offset))
3031 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003032 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003033
David S. Miller9360ffd2012-03-29 04:41:26 -04003034 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3035 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3036 sinfo->assoc_req_ies))
3037 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003038
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003039 return genlmsg_end(msg, hdr);
3040
3041 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003042 genlmsg_cancel(msg, hdr);
3043 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003044}
3045
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003046static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003047 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003048{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003049 struct station_info sinfo;
3050 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003051 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003052 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003053 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003054 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003055
Johannes Berg67748892010-10-04 21:14:06 +02003056 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3057 if (err)
3058 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003059
3060 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003061 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003062 goto out_err;
3063 }
3064
Johannes Bergbba95fe2008-07-29 13:22:51 +02003065 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003066 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003067 err = rdev_dump_station(dev, netdev, sta_idx,
3068 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003069 if (err == -ENOENT)
3070 break;
3071 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003072 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003073
3074 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003075 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003076 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003077 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003078 &sinfo) < 0)
3079 goto out;
3080
3081 sta_idx++;
3082 }
3083
3084
3085 out:
3086 cb->args[1] = sta_idx;
3087 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003088 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003089 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003090
3091 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003092}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003093
Johannes Berg5727ef12007-12-19 02:03:34 +01003094static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3095{
Johannes Berg4c476992010-10-04 21:36:35 +02003096 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3097 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003098 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003099 struct sk_buff *msg;
3100 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003101 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003102
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003103 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003104
3105 if (!info->attrs[NL80211_ATTR_MAC])
3106 return -EINVAL;
3107
3108 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3109
Johannes Berg4c476992010-10-04 21:36:35 +02003110 if (!rdev->ops->get_station)
3111 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003112
Hila Gonene35e4d22012-06-27 17:19:42 +03003113 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003114 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003115 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003116
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003117 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003118 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003119 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003120
Eric W. Biederman15e47302012-09-07 20:12:54 +00003121 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003122 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003123 nlmsg_free(msg);
3124 return -ENOBUFS;
3125 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003126
Johannes Berg4c476992010-10-04 21:36:35 +02003127 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003128}
3129
3130/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003131 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003132 */
Johannes Berg80b99892011-11-18 16:23:01 +01003133static struct net_device *get_vlan(struct genl_info *info,
3134 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003135{
Johannes Berg463d0182009-07-14 00:33:35 +02003136 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003137 struct net_device *v;
3138 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003139
Johannes Berg80b99892011-11-18 16:23:01 +01003140 if (!vlanattr)
3141 return NULL;
3142
3143 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3144 if (!v)
3145 return ERR_PTR(-ENODEV);
3146
3147 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3148 ret = -EINVAL;
3149 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003150 }
Johannes Berg80b99892011-11-18 16:23:01 +01003151
3152 if (!netif_running(v)) {
3153 ret = -ENETDOWN;
3154 goto error;
3155 }
3156
3157 return v;
3158 error:
3159 dev_put(v);
3160 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003161}
3162
3163static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3164{
Johannes Berg4c476992010-10-04 21:36:35 +02003165 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003166 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003167 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003168 struct station_parameters params;
3169 u8 *mac_addr = NULL;
3170
3171 memset(&params, 0, sizeof(params));
3172
3173 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003174 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003175
3176 if (info->attrs[NL80211_ATTR_STA_AID])
3177 return -EINVAL;
3178
3179 if (!info->attrs[NL80211_ATTR_MAC])
3180 return -EINVAL;
3181
3182 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3183
3184 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3185 params.supported_rates =
3186 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3187 params.supported_rates_len =
3188 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3189 }
3190
3191 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3192 params.listen_interval =
3193 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3194
Jouni Malinen36aedc92008-08-25 11:58:58 +03003195 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3196 params.ht_capa =
3197 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3198
Johannes Bergbdd90d52011-12-14 12:20:27 +01003199 if (!rdev->ops->change_station)
3200 return -EOPNOTSUPP;
3201
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003202 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003203 return -EINVAL;
3204
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003205 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3206 params.plink_action =
3207 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3208
Javier Cardona9c3990a2011-05-03 16:57:11 -07003209 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3210 params.plink_state =
3211 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3212
Johannes Berga97f4422009-06-18 17:23:43 +02003213 switch (dev->ieee80211_ptr->iftype) {
3214 case NL80211_IFTYPE_AP:
3215 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003216 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003217 /* disallow mesh-specific things */
3218 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003219 return -EINVAL;
3220
3221 /* TDLS can't be set, ... */
3222 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3223 return -EINVAL;
3224 /*
3225 * ... but don't bother the driver with it. This works around
3226 * a hostapd/wpa_supplicant issue -- it always includes the
3227 * TLDS_PEER flag in the mask even for AP mode.
3228 */
3229 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3230
3231 /* accept only the listed bits */
3232 if (params.sta_flags_mask &
3233 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3234 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3235 BIT(NL80211_STA_FLAG_WME) |
3236 BIT(NL80211_STA_FLAG_MFP)))
3237 return -EINVAL;
3238
3239 /* must be last in here for error handling */
3240 params.vlan = get_vlan(info, rdev);
3241 if (IS_ERR(params.vlan))
3242 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003243 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003244 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003245 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003246 /*
3247 * Don't allow userspace to change the TDLS_PEER flag,
3248 * but silently ignore attempts to change it since we
3249 * don't have state here to verify that it doesn't try
3250 * to change the flag.
3251 */
3252 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003253 /* fall through */
3254 case NL80211_IFTYPE_ADHOC:
3255 /* disallow things sta doesn't support */
3256 if (params.plink_action)
3257 return -EINVAL;
3258 if (params.ht_capa)
3259 return -EINVAL;
3260 if (params.listen_interval >= 0)
3261 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003262 /* reject any changes other than AUTHORIZED */
3263 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3264 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003265 break;
3266 case NL80211_IFTYPE_MESH_POINT:
3267 /* disallow things mesh doesn't support */
3268 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003269 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003270 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003271 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003272 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003273 return -EINVAL;
3274 /*
3275 * No special handling for TDLS here -- the userspace
3276 * mesh code doesn't have this bug.
3277 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003278 if (params.sta_flags_mask &
3279 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003280 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003281 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003282 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003283 break;
3284 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003285 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003286 }
3287
Johannes Bergbdd90d52011-12-14 12:20:27 +01003288 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003289
Hila Gonene35e4d22012-06-27 17:19:42 +03003290 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003291
Johannes Berg5727ef12007-12-19 02:03:34 +01003292 if (params.vlan)
3293 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003294
Johannes Berg5727ef12007-12-19 02:03:34 +01003295 return err;
3296}
3297
Eliad Pellerc75786c2011-08-23 14:37:46 +03003298static struct nla_policy
3299nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3300 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3301 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3302};
3303
Johannes Berg5727ef12007-12-19 02:03:34 +01003304static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3305{
Johannes Berg4c476992010-10-04 21:36:35 +02003306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003307 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003308 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003309 struct station_parameters params;
3310 u8 *mac_addr = NULL;
3311
3312 memset(&params, 0, sizeof(params));
3313
3314 if (!info->attrs[NL80211_ATTR_MAC])
3315 return -EINVAL;
3316
Johannes Berg5727ef12007-12-19 02:03:34 +01003317 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3318 return -EINVAL;
3319
3320 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3321 return -EINVAL;
3322
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003323 if (!info->attrs[NL80211_ATTR_STA_AID])
3324 return -EINVAL;
3325
Johannes Berg5727ef12007-12-19 02:03:34 +01003326 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3327 params.supported_rates =
3328 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3329 params.supported_rates_len =
3330 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3331 params.listen_interval =
3332 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003333
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003334 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3335 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3336 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003337
Jouni Malinen36aedc92008-08-25 11:58:58 +03003338 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3339 params.ht_capa =
3340 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003341
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003342 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3343 params.vht_capa =
3344 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3345
Javier Cardona96b78df2011-04-07 15:08:33 -07003346 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3347 params.plink_action =
3348 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3349
Johannes Bergbdd90d52011-12-14 12:20:27 +01003350 if (!rdev->ops->add_station)
3351 return -EOPNOTSUPP;
3352
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003353 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003354 return -EINVAL;
3355
Johannes Bergbdd90d52011-12-14 12:20:27 +01003356 switch (dev->ieee80211_ptr->iftype) {
3357 case NL80211_IFTYPE_AP:
3358 case NL80211_IFTYPE_AP_VLAN:
3359 case NL80211_IFTYPE_P2P_GO:
3360 /* parse WME attributes if sta is WME capable */
3361 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3362 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3363 info->attrs[NL80211_ATTR_STA_WME]) {
3364 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3365 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003366
Johannes Bergbdd90d52011-12-14 12:20:27 +01003367 nla = info->attrs[NL80211_ATTR_STA_WME];
3368 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3369 nl80211_sta_wme_policy);
3370 if (err)
3371 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003372
Johannes Bergbdd90d52011-12-14 12:20:27 +01003373 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3374 params.uapsd_queues =
3375 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3376 if (params.uapsd_queues &
3377 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3378 return -EINVAL;
3379
3380 if (tb[NL80211_STA_WME_MAX_SP])
3381 params.max_sp =
3382 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3383
3384 if (params.max_sp &
3385 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3386 return -EINVAL;
3387
3388 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3389 }
3390 /* TDLS peers cannot be added */
3391 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003392 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003393 /* but don't bother the driver with it */
3394 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003395
Johannes Bergbdd90d52011-12-14 12:20:27 +01003396 /* must be last in here for error handling */
3397 params.vlan = get_vlan(info, rdev);
3398 if (IS_ERR(params.vlan))
3399 return PTR_ERR(params.vlan);
3400 break;
3401 case NL80211_IFTYPE_MESH_POINT:
3402 /* TDLS peers cannot be added */
3403 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003404 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003405 break;
3406 case NL80211_IFTYPE_STATION:
3407 /* Only TDLS peers can be added */
3408 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3409 return -EINVAL;
3410 /* Can only add if TDLS ... */
3411 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3412 return -EOPNOTSUPP;
3413 /* ... with external setup is supported */
3414 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3415 return -EOPNOTSUPP;
3416 break;
3417 default:
3418 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003419 }
3420
Johannes Bergbdd90d52011-12-14 12:20:27 +01003421 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003422
Hila Gonene35e4d22012-06-27 17:19:42 +03003423 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003424
Johannes Berg5727ef12007-12-19 02:03:34 +01003425 if (params.vlan)
3426 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003427 return err;
3428}
3429
3430static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3431{
Johannes Berg4c476992010-10-04 21:36:35 +02003432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3433 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003434 u8 *mac_addr = NULL;
3435
3436 if (info->attrs[NL80211_ATTR_MAC])
3437 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3438
Johannes Berge80cf852009-05-11 14:43:13 +02003439 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003440 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003441 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003442 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3443 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003444
Johannes Berg4c476992010-10-04 21:36:35 +02003445 if (!rdev->ops->del_station)
3446 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003447
Hila Gonene35e4d22012-06-27 17:19:42 +03003448 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003449}
3450
Eric W. Biederman15e47302012-09-07 20:12:54 +00003451static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003452 int flags, struct net_device *dev,
3453 u8 *dst, u8 *next_hop,
3454 struct mpath_info *pinfo)
3455{
3456 void *hdr;
3457 struct nlattr *pinfoattr;
3458
Eric W. Biederman15e47302012-09-07 20:12:54 +00003459 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003460 if (!hdr)
3461 return -1;
3462
David S. Miller9360ffd2012-03-29 04:41:26 -04003463 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3464 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3465 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3466 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3467 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003468
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003469 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3470 if (!pinfoattr)
3471 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003472 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3473 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3474 pinfo->frame_qlen))
3475 goto nla_put_failure;
3476 if (((pinfo->filled & MPATH_INFO_SN) &&
3477 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3478 ((pinfo->filled & MPATH_INFO_METRIC) &&
3479 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3480 pinfo->metric)) ||
3481 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3482 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3483 pinfo->exptime)) ||
3484 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3485 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3486 pinfo->flags)) ||
3487 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3488 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3489 pinfo->discovery_timeout)) ||
3490 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3491 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3492 pinfo->discovery_retries)))
3493 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003494
3495 nla_nest_end(msg, pinfoattr);
3496
3497 return genlmsg_end(msg, hdr);
3498
3499 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003500 genlmsg_cancel(msg, hdr);
3501 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003502}
3503
3504static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003505 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003506{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003507 struct mpath_info pinfo;
3508 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003509 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003510 u8 dst[ETH_ALEN];
3511 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003512 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003513 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003514
Johannes Berg67748892010-10-04 21:14:06 +02003515 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3516 if (err)
3517 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003518
3519 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003520 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003521 goto out_err;
3522 }
3523
Jouni Malineneec60b02009-03-20 21:21:19 +02003524 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3525 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003526 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003527 }
3528
Johannes Bergbba95fe2008-07-29 13:22:51 +02003529 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003530 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3531 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003532 if (err == -ENOENT)
3533 break;
3534 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003535 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003536
Eric W. Biederman15e47302012-09-07 20:12:54 +00003537 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003538 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3539 netdev, dst, next_hop,
3540 &pinfo) < 0)
3541 goto out;
3542
3543 path_idx++;
3544 }
3545
3546
3547 out:
3548 cb->args[1] = path_idx;
3549 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003550 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003551 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003552 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003553}
3554
3555static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3556{
Johannes Berg4c476992010-10-04 21:36:35 +02003557 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003558 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003559 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003560 struct mpath_info pinfo;
3561 struct sk_buff *msg;
3562 u8 *dst = NULL;
3563 u8 next_hop[ETH_ALEN];
3564
3565 memset(&pinfo, 0, sizeof(pinfo));
3566
3567 if (!info->attrs[NL80211_ATTR_MAC])
3568 return -EINVAL;
3569
3570 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3571
Johannes Berg4c476992010-10-04 21:36:35 +02003572 if (!rdev->ops->get_mpath)
3573 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003574
Johannes Berg4c476992010-10-04 21:36:35 +02003575 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3576 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003577
Hila Gonene35e4d22012-06-27 17:19:42 +03003578 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003579 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003580 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003581
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003582 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003583 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003584 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003585
Eric W. Biederman15e47302012-09-07 20:12:54 +00003586 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003587 dev, dst, next_hop, &pinfo) < 0) {
3588 nlmsg_free(msg);
3589 return -ENOBUFS;
3590 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003591
Johannes Berg4c476992010-10-04 21:36:35 +02003592 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003593}
3594
3595static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3596{
Johannes Berg4c476992010-10-04 21:36:35 +02003597 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3598 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003599 u8 *dst = NULL;
3600 u8 *next_hop = NULL;
3601
3602 if (!info->attrs[NL80211_ATTR_MAC])
3603 return -EINVAL;
3604
3605 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3606 return -EINVAL;
3607
3608 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3609 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3610
Johannes Berg4c476992010-10-04 21:36:35 +02003611 if (!rdev->ops->change_mpath)
3612 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003613
Johannes Berg4c476992010-10-04 21:36:35 +02003614 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3615 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003616
Hila Gonene35e4d22012-06-27 17:19:42 +03003617 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003618}
Johannes Berg4c476992010-10-04 21:36:35 +02003619
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003620static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3621{
Johannes Berg4c476992010-10-04 21:36:35 +02003622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3623 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003624 u8 *dst = NULL;
3625 u8 *next_hop = NULL;
3626
3627 if (!info->attrs[NL80211_ATTR_MAC])
3628 return -EINVAL;
3629
3630 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3631 return -EINVAL;
3632
3633 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3634 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3635
Johannes Berg4c476992010-10-04 21:36:35 +02003636 if (!rdev->ops->add_mpath)
3637 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003638
Johannes Berg4c476992010-10-04 21:36:35 +02003639 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3640 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003641
Hila Gonene35e4d22012-06-27 17:19:42 +03003642 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003643}
3644
3645static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3646{
Johannes Berg4c476992010-10-04 21:36:35 +02003647 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3648 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003649 u8 *dst = NULL;
3650
3651 if (info->attrs[NL80211_ATTR_MAC])
3652 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3653
Johannes Berg4c476992010-10-04 21:36:35 +02003654 if (!rdev->ops->del_mpath)
3655 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003656
Hila Gonene35e4d22012-06-27 17:19:42 +03003657 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003658}
3659
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003660static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3661{
Johannes Berg4c476992010-10-04 21:36:35 +02003662 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3663 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003664 struct bss_parameters params;
3665
3666 memset(&params, 0, sizeof(params));
3667 /* default to not changing parameters */
3668 params.use_cts_prot = -1;
3669 params.use_short_preamble = -1;
3670 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003671 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003672 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01003673 params.p2p_ctwindow = -1;
3674 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003675
3676 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3677 params.use_cts_prot =
3678 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3679 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3680 params.use_short_preamble =
3681 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3682 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3683 params.use_short_slot_time =
3684 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003685 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3686 params.basic_rates =
3687 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3688 params.basic_rates_len =
3689 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3690 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003691 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3692 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003693 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3694 params.ht_opmode =
3695 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003696
Johannes Berg53cabad2012-11-14 15:17:28 +01003697 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3698 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3699 return -EINVAL;
3700 params.p2p_ctwindow =
3701 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3702 if (params.p2p_ctwindow < 0)
3703 return -EINVAL;
3704 if (params.p2p_ctwindow != 0 &&
3705 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3706 return -EINVAL;
3707 }
3708
3709 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3710 u8 tmp;
3711
3712 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3713 return -EINVAL;
3714 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3715 if (tmp > 1)
3716 return -EINVAL;
3717 params.p2p_opp_ps = tmp;
3718 if (params.p2p_opp_ps &&
3719 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3720 return -EINVAL;
3721 }
3722
Johannes Berg4c476992010-10-04 21:36:35 +02003723 if (!rdev->ops->change_bss)
3724 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003725
Johannes Berg074ac8d2010-09-16 14:58:22 +02003726 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003727 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3728 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003729
Hila Gonene35e4d22012-06-27 17:19:42 +03003730 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003731}
3732
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003733static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003734 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3735 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3736 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3737 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3738 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3739 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3740};
3741
3742static int parse_reg_rule(struct nlattr *tb[],
3743 struct ieee80211_reg_rule *reg_rule)
3744{
3745 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3746 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3747
3748 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3749 return -EINVAL;
3750 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3751 return -EINVAL;
3752 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3753 return -EINVAL;
3754 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3755 return -EINVAL;
3756 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3757 return -EINVAL;
3758
3759 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3760
3761 freq_range->start_freq_khz =
3762 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3763 freq_range->end_freq_khz =
3764 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3765 freq_range->max_bandwidth_khz =
3766 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3767
3768 power_rule->max_eirp =
3769 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3770
3771 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3772 power_rule->max_antenna_gain =
3773 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3774
3775 return 0;
3776}
3777
3778static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3779{
3780 int r;
3781 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003782 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003783
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003784 /*
3785 * You should only get this when cfg80211 hasn't yet initialized
3786 * completely when built-in to the kernel right between the time
3787 * window between nl80211_init() and regulatory_init(), if that is
3788 * even possible.
3789 */
3790 mutex_lock(&cfg80211_mutex);
3791 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003792 mutex_unlock(&cfg80211_mutex);
3793 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003794 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003795 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003796
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003797 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3798 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003799
3800 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3801
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003802 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3803 user_reg_hint_type =
3804 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3805 else
3806 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3807
3808 switch (user_reg_hint_type) {
3809 case NL80211_USER_REG_HINT_USER:
3810 case NL80211_USER_REG_HINT_CELL_BASE:
3811 break;
3812 default:
3813 return -EINVAL;
3814 }
3815
3816 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003817
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003818 return r;
3819}
3820
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003821static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003822 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003823{
Johannes Berg4c476992010-10-04 21:36:35 +02003824 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003825 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003826 struct wireless_dev *wdev = dev->ieee80211_ptr;
3827 struct mesh_config cur_params;
3828 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003829 void *hdr;
3830 struct nlattr *pinfoattr;
3831 struct sk_buff *msg;
3832
Johannes Berg29cbe682010-12-03 09:20:44 +01003833 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3834 return -EOPNOTSUPP;
3835
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003836 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003837 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003838
Johannes Berg29cbe682010-12-03 09:20:44 +01003839 wdev_lock(wdev);
3840 /* If not connected, get default parameters */
3841 if (!wdev->mesh_id_len)
3842 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3843 else
Hila Gonene35e4d22012-06-27 17:19:42 +03003844 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01003845 wdev_unlock(wdev);
3846
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003847 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003848 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003849
3850 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003851 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003852 if (!msg)
3853 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003854 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003855 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003856 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003857 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003858 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003859 if (!pinfoattr)
3860 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003861 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3862 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3863 cur_params.dot11MeshRetryTimeout) ||
3864 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3865 cur_params.dot11MeshConfirmTimeout) ||
3866 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3867 cur_params.dot11MeshHoldingTimeout) ||
3868 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3869 cur_params.dot11MeshMaxPeerLinks) ||
3870 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3871 cur_params.dot11MeshMaxRetries) ||
3872 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3873 cur_params.dot11MeshTTL) ||
3874 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3875 cur_params.element_ttl) ||
3876 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3877 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003878 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3879 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003880 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3881 cur_params.dot11MeshHWMPmaxPREQretries) ||
3882 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3883 cur_params.path_refresh_time) ||
3884 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3885 cur_params.min_discovery_timeout) ||
3886 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3887 cur_params.dot11MeshHWMPactivePathTimeout) ||
3888 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3889 cur_params.dot11MeshHWMPpreqMinInterval) ||
3890 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3891 cur_params.dot11MeshHWMPperrMinInterval) ||
3892 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3893 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3894 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3895 cur_params.dot11MeshHWMPRootMode) ||
3896 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3897 cur_params.dot11MeshHWMPRannInterval) ||
3898 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3899 cur_params.dot11MeshGateAnnouncementProtocol) ||
3900 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3901 cur_params.dot11MeshForwarding) ||
3902 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003903 cur_params.rssi_threshold) ||
3904 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003905 cur_params.ht_opmode) ||
3906 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3907 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3908 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003909 cur_params.dot11MeshHWMProotInterval) ||
3910 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3911 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003912 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003913 nla_nest_end(msg, pinfoattr);
3914 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003915 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003916
Johannes Berg3b858752009-03-12 09:55:09 +01003917 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003918 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003919 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003920 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003921 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003922}
3923
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003924static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003925 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3926 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3927 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3928 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3929 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3930 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003931 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003932 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003933 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003934 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3935 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3936 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3937 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3938 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003939 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003940 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003941 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003942 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003943 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003944 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003945 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3946 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003947 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3948 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003949 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003950};
3951
Javier Cardonac80d5452010-12-16 17:37:49 -08003952static const struct nla_policy
3953 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003954 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003955 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3956 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003957 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003958 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003959 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003960 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003961};
3962
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003963static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003964 struct mesh_config *cfg,
3965 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003966{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003967 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003968 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003969
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003970#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3971do {\
3972 if (table[attr_num]) {\
3973 cfg->param = nla_fn(table[attr_num]); \
3974 mask |= (1 << (attr_num - 1)); \
3975 } \
3976} while (0);\
3977
3978
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003979 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003980 return -EINVAL;
3981 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003982 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003983 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003984 return -EINVAL;
3985
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003986 /* This makes sure that there aren't more than 32 mesh config
3987 * parameters (otherwise our bitfield scheme would not work.) */
3988 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3989
3990 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003991 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003992 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3993 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003994 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003995 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3996 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003997 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003998 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3999 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004000 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004001 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4002 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004003 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004004 mask, NL80211_MESHCONF_MAX_RETRIES,
4005 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004006 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004007 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01004008 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004009 mask, NL80211_MESHCONF_ELEMENT_TTL,
4010 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004011 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004012 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4013 nla_get_u8);
4014 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
4015 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4016 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004017 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004018 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4019 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004020 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004021 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4022 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004023 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004024 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4025 nla_get_u16);
4026 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
4027 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4028 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004029 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004030 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4031 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004032 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004033 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4034 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004035 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004036 dot11MeshHWMPnetDiameterTraversalTime, mask,
4037 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4038 nla_get_u16);
4039 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
4040 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
4041 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
4042 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4043 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004044 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004045 dot11MeshGateAnnouncementProtocol, mask,
4046 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4047 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004048 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004049 mask, NL80211_MESHCONF_FORWARDING,
4050 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08004051 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004052 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4053 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004054 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004055 mask, NL80211_MESHCONF_HT_OPMODE,
4056 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004057 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
4058 mask,
4059 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4060 nla_get_u32);
4061 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
4062 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4063 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004064 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
4065 dot11MeshHWMPconfirmationInterval, mask,
4066 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4067 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004068 if (mask_out)
4069 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004070
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004071 return 0;
4072
4073#undef FILL_IN_MESH_PARAM_IF_SET
4074}
4075
Javier Cardonac80d5452010-12-16 17:37:49 -08004076static int nl80211_parse_mesh_setup(struct genl_info *info,
4077 struct mesh_setup *setup)
4078{
4079 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4080
4081 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4082 return -EINVAL;
4083 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4084 info->attrs[NL80211_ATTR_MESH_SETUP],
4085 nl80211_mesh_setup_params_policy))
4086 return -EINVAL;
4087
Javier Cardonad299a1f2012-03-31 11:31:33 -07004088 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4089 setup->sync_method =
4090 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4091 IEEE80211_SYNC_METHOD_VENDOR :
4092 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4093
Javier Cardonac80d5452010-12-16 17:37:49 -08004094 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4095 setup->path_sel_proto =
4096 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4097 IEEE80211_PATH_PROTOCOL_VENDOR :
4098 IEEE80211_PATH_PROTOCOL_HWMP;
4099
4100 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4101 setup->path_metric =
4102 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4103 IEEE80211_PATH_METRIC_VENDOR :
4104 IEEE80211_PATH_METRIC_AIRTIME;
4105
Javier Cardona581a8b02011-04-07 15:08:27 -07004106
4107 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004108 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004109 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004110 if (!is_valid_ie_attr(ieattr))
4111 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004112 setup->ie = nla_data(ieattr);
4113 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004114 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004115 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4116 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004117
4118 return 0;
4119}
4120
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004121static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004122 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004123{
4124 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4125 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004126 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004127 struct mesh_config cfg;
4128 u32 mask;
4129 int err;
4130
Johannes Berg29cbe682010-12-03 09:20:44 +01004131 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4132 return -EOPNOTSUPP;
4133
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004134 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004135 return -EOPNOTSUPP;
4136
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004137 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004138 if (err)
4139 return err;
4140
Johannes Berg29cbe682010-12-03 09:20:44 +01004141 wdev_lock(wdev);
4142 if (!wdev->mesh_id_len)
4143 err = -ENOLINK;
4144
4145 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004146 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004147
4148 wdev_unlock(wdev);
4149
4150 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004151}
4152
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004153static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4154{
4155 struct sk_buff *msg;
4156 void *hdr = NULL;
4157 struct nlattr *nl_reg_rules;
4158 unsigned int i;
4159 int err = -EINVAL;
4160
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004161 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004162
4163 if (!cfg80211_regdomain)
4164 goto out;
4165
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004166 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004167 if (!msg) {
4168 err = -ENOBUFS;
4169 goto out;
4170 }
4171
Eric W. Biederman15e47302012-09-07 20:12:54 +00004172 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004173 NL80211_CMD_GET_REG);
4174 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004175 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004176
David S. Miller9360ffd2012-03-29 04:41:26 -04004177 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4178 cfg80211_regdomain->alpha2) ||
4179 (cfg80211_regdomain->dfs_region &&
4180 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4181 cfg80211_regdomain->dfs_region)))
4182 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004183
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004184 if (reg_last_request_cell_base() &&
4185 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4186 NL80211_USER_REG_HINT_CELL_BASE))
4187 goto nla_put_failure;
4188
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004189 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4190 if (!nl_reg_rules)
4191 goto nla_put_failure;
4192
4193 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4194 struct nlattr *nl_reg_rule;
4195 const struct ieee80211_reg_rule *reg_rule;
4196 const struct ieee80211_freq_range *freq_range;
4197 const struct ieee80211_power_rule *power_rule;
4198
4199 reg_rule = &cfg80211_regdomain->reg_rules[i];
4200 freq_range = &reg_rule->freq_range;
4201 power_rule = &reg_rule->power_rule;
4202
4203 nl_reg_rule = nla_nest_start(msg, i);
4204 if (!nl_reg_rule)
4205 goto nla_put_failure;
4206
David S. Miller9360ffd2012-03-29 04:41:26 -04004207 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4208 reg_rule->flags) ||
4209 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4210 freq_range->start_freq_khz) ||
4211 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4212 freq_range->end_freq_khz) ||
4213 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4214 freq_range->max_bandwidth_khz) ||
4215 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4216 power_rule->max_antenna_gain) ||
4217 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4218 power_rule->max_eirp))
4219 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004220
4221 nla_nest_end(msg, nl_reg_rule);
4222 }
4223
4224 nla_nest_end(msg, nl_reg_rules);
4225
4226 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004227 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004228 goto out;
4229
4230nla_put_failure:
4231 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004232put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004233 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004234 err = -EMSGSIZE;
4235out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004236 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004237 return err;
4238}
4239
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004240static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4241{
4242 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4243 struct nlattr *nl_reg_rule;
4244 char *alpha2 = NULL;
4245 int rem_reg_rules = 0, r = 0;
4246 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004247 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004248 struct ieee80211_regdomain *rd = NULL;
4249
4250 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4251 return -EINVAL;
4252
4253 if (!info->attrs[NL80211_ATTR_REG_RULES])
4254 return -EINVAL;
4255
4256 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4257
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004258 if (info->attrs[NL80211_ATTR_DFS_REGION])
4259 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4260
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004261 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4262 rem_reg_rules) {
4263 num_rules++;
4264 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004265 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004266 }
4267
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004268 mutex_lock(&cfg80211_mutex);
4269
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004270 if (!reg_is_valid_request(alpha2)) {
4271 r = -EINVAL;
4272 goto bad_reg;
4273 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004274
4275 size_of_regd = sizeof(struct ieee80211_regdomain) +
4276 (num_rules * sizeof(struct ieee80211_reg_rule));
4277
4278 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004279 if (!rd) {
4280 r = -ENOMEM;
4281 goto bad_reg;
4282 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004283
4284 rd->n_reg_rules = num_rules;
4285 rd->alpha2[0] = alpha2[0];
4286 rd->alpha2[1] = alpha2[1];
4287
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004288 /*
4289 * Disable DFS master mode if the DFS region was
4290 * not supported or known on this kernel.
4291 */
4292 if (reg_supported_dfs_region(dfs_region))
4293 rd->dfs_region = dfs_region;
4294
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004295 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4296 rem_reg_rules) {
4297 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4298 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4299 reg_rule_policy);
4300 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4301 if (r)
4302 goto bad_reg;
4303
4304 rule_idx++;
4305
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004306 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4307 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004308 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004309 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004310 }
4311
4312 BUG_ON(rule_idx != num_rules);
4313
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004314 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004315
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004316 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004317
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004318 return r;
4319
Johannes Bergd2372b32008-10-24 20:32:20 +02004320 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004321 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004322 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004323 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004324}
4325
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004326static int validate_scan_freqs(struct nlattr *freqs)
4327{
4328 struct nlattr *attr1, *attr2;
4329 int n_channels = 0, tmp1, tmp2;
4330
4331 nla_for_each_nested(attr1, freqs, tmp1) {
4332 n_channels++;
4333 /*
4334 * Some hardware has a limited channel list for
4335 * scanning, and it is pretty much nonsensical
4336 * to scan for a channel twice, so disallow that
4337 * and don't require drivers to check that the
4338 * channel list they get isn't longer than what
4339 * they can scan, as long as they can scan all
4340 * the channels they registered at once.
4341 */
4342 nla_for_each_nested(attr2, freqs, tmp2)
4343 if (attr1 != attr2 &&
4344 nla_get_u32(attr1) == nla_get_u32(attr2))
4345 return 0;
4346 }
4347
4348 return n_channels;
4349}
4350
Johannes Berg2a519312009-02-10 21:25:55 +01004351static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4352{
Johannes Berg4c476992010-10-04 21:36:35 +02004353 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004354 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004355 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004356 struct nlattr *attr;
4357 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004358 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004359 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004360
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004361 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4362 return -EINVAL;
4363
Johannes Berg79c97e92009-07-07 03:56:12 +02004364 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004365
Johannes Berg4c476992010-10-04 21:36:35 +02004366 if (!rdev->ops->scan)
4367 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004368
Johannes Berg4c476992010-10-04 21:36:35 +02004369 if (rdev->scan_req)
4370 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004371
4372 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004373 n_channels = validate_scan_freqs(
4374 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004375 if (!n_channels)
4376 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004377 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004378 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004379 n_channels = 0;
4380
Johannes Berg2a519312009-02-10 21:25:55 +01004381 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4382 if (wiphy->bands[band])
4383 n_channels += wiphy->bands[band]->n_channels;
4384 }
4385
4386 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4387 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4388 n_ssids++;
4389
Johannes Berg4c476992010-10-04 21:36:35 +02004390 if (n_ssids > wiphy->max_scan_ssids)
4391 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004392
Jouni Malinen70692ad2009-02-16 19:39:13 +02004393 if (info->attrs[NL80211_ATTR_IE])
4394 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4395 else
4396 ie_len = 0;
4397
Johannes Berg4c476992010-10-04 21:36:35 +02004398 if (ie_len > wiphy->max_scan_ie_len)
4399 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004400
Johannes Berg2a519312009-02-10 21:25:55 +01004401 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004402 + sizeof(*request->ssids) * n_ssids
4403 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004404 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004405 if (!request)
4406 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004407
Johannes Berg2a519312009-02-10 21:25:55 +01004408 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004409 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004410 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004411 if (ie_len) {
4412 if (request->ssids)
4413 request->ie = (void *)(request->ssids + n_ssids);
4414 else
4415 request->ie = (void *)(request->channels + n_channels);
4416 }
Johannes Berg2a519312009-02-10 21:25:55 +01004417
Johannes Berg584991d2009-11-02 13:32:03 +01004418 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004419 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4420 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004421 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004422 struct ieee80211_channel *chan;
4423
4424 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4425
4426 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004427 err = -EINVAL;
4428 goto out_free;
4429 }
Johannes Berg584991d2009-11-02 13:32:03 +01004430
4431 /* ignore disabled channels */
4432 if (chan->flags & IEEE80211_CHAN_DISABLED)
4433 continue;
4434
4435 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004436 i++;
4437 }
4438 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004439 enum ieee80211_band band;
4440
Johannes Berg2a519312009-02-10 21:25:55 +01004441 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004442 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4443 int j;
4444 if (!wiphy->bands[band])
4445 continue;
4446 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004447 struct ieee80211_channel *chan;
4448
4449 chan = &wiphy->bands[band]->channels[j];
4450
4451 if (chan->flags & IEEE80211_CHAN_DISABLED)
4452 continue;
4453
4454 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004455 i++;
4456 }
4457 }
4458 }
4459
Johannes Berg584991d2009-11-02 13:32:03 +01004460 if (!i) {
4461 err = -EINVAL;
4462 goto out_free;
4463 }
4464
4465 request->n_channels = i;
4466
Johannes Berg2a519312009-02-10 21:25:55 +01004467 i = 0;
4468 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4469 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004470 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004471 err = -EINVAL;
4472 goto out_free;
4473 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004474 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004475 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004476 i++;
4477 }
4478 }
4479
Jouni Malinen70692ad2009-02-16 19:39:13 +02004480 if (info->attrs[NL80211_ATTR_IE]) {
4481 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004482 memcpy((void *)request->ie,
4483 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004484 request->ie_len);
4485 }
4486
Johannes Berg34850ab2011-07-18 18:08:35 +02004487 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004488 if (wiphy->bands[i])
4489 request->rates[i] =
4490 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004491
4492 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4493 nla_for_each_nested(attr,
4494 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4495 tmp) {
4496 enum ieee80211_band band = nla_type(attr);
4497
Dan Carpenter84404622011-07-29 11:52:18 +03004498 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004499 err = -EINVAL;
4500 goto out_free;
4501 }
4502 err = ieee80211_get_ratemask(wiphy->bands[band],
4503 nla_data(attr),
4504 nla_len(attr),
4505 &request->rates[band]);
4506 if (err)
4507 goto out_free;
4508 }
4509 }
4510
Sam Leffler46856bb2012-10-11 21:03:32 -07004511 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004512 request->flags = nla_get_u32(
4513 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004514 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4515 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4516 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4517 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004518 err = -EOPNOTSUPP;
4519 goto out_free;
4520 }
4521 }
Sam Lefflered4737712012-10-11 21:03:31 -07004522
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304523 request->no_cck =
4524 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4525
Johannes Bergfd014282012-06-18 19:17:03 +02004526 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004527 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004528 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004529
Johannes Berg79c97e92009-07-07 03:56:12 +02004530 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004531 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004532
Johannes Berg463d0182009-07-14 00:33:35 +02004533 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004534 nl80211_send_scan_start(rdev, wdev);
4535 if (wdev->netdev)
4536 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004537 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004538 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004539 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004540 kfree(request);
4541 }
Johannes Berg3b858752009-03-12 09:55:09 +01004542
Johannes Berg2a519312009-02-10 21:25:55 +01004543 return err;
4544}
4545
Luciano Coelho807f8a82011-05-11 17:09:35 +03004546static int nl80211_start_sched_scan(struct sk_buff *skb,
4547 struct genl_info *info)
4548{
4549 struct cfg80211_sched_scan_request *request;
4550 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4551 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004552 struct nlattr *attr;
4553 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004554 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004555 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004556 enum ieee80211_band band;
4557 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004558 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004559
4560 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4561 !rdev->ops->sched_scan_start)
4562 return -EOPNOTSUPP;
4563
4564 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4565 return -EINVAL;
4566
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004567 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4568 return -EINVAL;
4569
4570 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4571 if (interval == 0)
4572 return -EINVAL;
4573
Luciano Coelho807f8a82011-05-11 17:09:35 +03004574 wiphy = &rdev->wiphy;
4575
4576 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4577 n_channels = validate_scan_freqs(
4578 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4579 if (!n_channels)
4580 return -EINVAL;
4581 } else {
4582 n_channels = 0;
4583
4584 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4585 if (wiphy->bands[band])
4586 n_channels += wiphy->bands[band]->n_channels;
4587 }
4588
4589 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4590 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4591 tmp)
4592 n_ssids++;
4593
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004594 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004595 return -EINVAL;
4596
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004597 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4598 nla_for_each_nested(attr,
4599 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4600 tmp)
4601 n_match_sets++;
4602
4603 if (n_match_sets > wiphy->max_match_sets)
4604 return -EINVAL;
4605
Luciano Coelho807f8a82011-05-11 17:09:35 +03004606 if (info->attrs[NL80211_ATTR_IE])
4607 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4608 else
4609 ie_len = 0;
4610
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004611 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004612 return -EINVAL;
4613
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004614 mutex_lock(&rdev->sched_scan_mtx);
4615
4616 if (rdev->sched_scan_req) {
4617 err = -EINPROGRESS;
4618 goto out;
4619 }
4620
Luciano Coelho807f8a82011-05-11 17:09:35 +03004621 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004622 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004623 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004624 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004625 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004626 if (!request) {
4627 err = -ENOMEM;
4628 goto out;
4629 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004630
4631 if (n_ssids)
4632 request->ssids = (void *)&request->channels[n_channels];
4633 request->n_ssids = n_ssids;
4634 if (ie_len) {
4635 if (request->ssids)
4636 request->ie = (void *)(request->ssids + n_ssids);
4637 else
4638 request->ie = (void *)(request->channels + n_channels);
4639 }
4640
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004641 if (n_match_sets) {
4642 if (request->ie)
4643 request->match_sets = (void *)(request->ie + ie_len);
4644 else if (request->ssids)
4645 request->match_sets =
4646 (void *)(request->ssids + n_ssids);
4647 else
4648 request->match_sets =
4649 (void *)(request->channels + n_channels);
4650 }
4651 request->n_match_sets = n_match_sets;
4652
Luciano Coelho807f8a82011-05-11 17:09:35 +03004653 i = 0;
4654 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4655 /* user specified, bail out if channel not found */
4656 nla_for_each_nested(attr,
4657 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4658 tmp) {
4659 struct ieee80211_channel *chan;
4660
4661 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4662
4663 if (!chan) {
4664 err = -EINVAL;
4665 goto out_free;
4666 }
4667
4668 /* ignore disabled channels */
4669 if (chan->flags & IEEE80211_CHAN_DISABLED)
4670 continue;
4671
4672 request->channels[i] = chan;
4673 i++;
4674 }
4675 } else {
4676 /* all channels */
4677 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4678 int j;
4679 if (!wiphy->bands[band])
4680 continue;
4681 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4682 struct ieee80211_channel *chan;
4683
4684 chan = &wiphy->bands[band]->channels[j];
4685
4686 if (chan->flags & IEEE80211_CHAN_DISABLED)
4687 continue;
4688
4689 request->channels[i] = chan;
4690 i++;
4691 }
4692 }
4693 }
4694
4695 if (!i) {
4696 err = -EINVAL;
4697 goto out_free;
4698 }
4699
4700 request->n_channels = i;
4701
4702 i = 0;
4703 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4704 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4705 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004706 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004707 err = -EINVAL;
4708 goto out_free;
4709 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004710 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004711 memcpy(request->ssids[i].ssid, nla_data(attr),
4712 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004713 i++;
4714 }
4715 }
4716
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004717 i = 0;
4718 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4719 nla_for_each_nested(attr,
4720 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4721 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004722 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004723
4724 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4725 nla_data(attr), nla_len(attr),
4726 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004727 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004728 if (ssid) {
4729 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4730 err = -EINVAL;
4731 goto out_free;
4732 }
4733 memcpy(request->match_sets[i].ssid.ssid,
4734 nla_data(ssid), nla_len(ssid));
4735 request->match_sets[i].ssid.ssid_len =
4736 nla_len(ssid);
4737 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004738 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4739 if (rssi)
4740 request->rssi_thold = nla_get_u32(rssi);
4741 else
4742 request->rssi_thold =
4743 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004744 i++;
4745 }
4746 }
4747
Luciano Coelho807f8a82011-05-11 17:09:35 +03004748 if (info->attrs[NL80211_ATTR_IE]) {
4749 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4750 memcpy((void *)request->ie,
4751 nla_data(info->attrs[NL80211_ATTR_IE]),
4752 request->ie_len);
4753 }
4754
Sam Leffler46856bb2012-10-11 21:03:32 -07004755 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004756 request->flags = nla_get_u32(
4757 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004758 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4759 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4760 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4761 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004762 err = -EOPNOTSUPP;
4763 goto out_free;
4764 }
4765 }
Sam Lefflered4737712012-10-11 21:03:31 -07004766
Luciano Coelho807f8a82011-05-11 17:09:35 +03004767 request->dev = dev;
4768 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004769 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004770 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004771
Hila Gonene35e4d22012-06-27 17:19:42 +03004772 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004773 if (!err) {
4774 rdev->sched_scan_req = request;
4775 nl80211_send_sched_scan(rdev, dev,
4776 NL80211_CMD_START_SCHED_SCAN);
4777 goto out;
4778 }
4779
4780out_free:
4781 kfree(request);
4782out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004783 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004784 return err;
4785}
4786
4787static int nl80211_stop_sched_scan(struct sk_buff *skb,
4788 struct genl_info *info)
4789{
4790 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004791 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004792
4793 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4794 !rdev->ops->sched_scan_stop)
4795 return -EOPNOTSUPP;
4796
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004797 mutex_lock(&rdev->sched_scan_mtx);
4798 err = __cfg80211_stop_sched_scan(rdev, false);
4799 mutex_unlock(&rdev->sched_scan_mtx);
4800
4801 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004802}
4803
Johannes Berg9720bb32011-06-21 09:45:33 +02004804static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4805 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004806 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004807 struct wireless_dev *wdev,
4808 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004809{
Johannes Berg48ab9052009-07-10 18:42:31 +02004810 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004811 void *hdr;
4812 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004813
4814 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004815
Eric W. Biederman15e47302012-09-07 20:12:54 +00004816 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004817 NL80211_CMD_NEW_SCAN_RESULTS);
4818 if (!hdr)
4819 return -1;
4820
Johannes Berg9720bb32011-06-21 09:45:33 +02004821 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4822
David S. Miller9360ffd2012-03-29 04:41:26 -04004823 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4824 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4825 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004826
4827 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4828 if (!bss)
4829 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004830 if ((!is_zero_ether_addr(res->bssid) &&
4831 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4832 (res->information_elements && res->len_information_elements &&
4833 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4834 res->len_information_elements,
4835 res->information_elements)) ||
4836 (res->beacon_ies && res->len_beacon_ies &&
4837 res->beacon_ies != res->information_elements &&
4838 nla_put(msg, NL80211_BSS_BEACON_IES,
4839 res->len_beacon_ies, res->beacon_ies)))
4840 goto nla_put_failure;
4841 if (res->tsf &&
4842 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4843 goto nla_put_failure;
4844 if (res->beacon_interval &&
4845 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4846 goto nla_put_failure;
4847 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4848 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4849 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4850 jiffies_to_msecs(jiffies - intbss->ts)))
4851 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004852
Johannes Berg77965c92009-02-18 18:45:06 +01004853 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004854 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004855 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4856 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004857 break;
4858 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004859 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4860 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004861 break;
4862 default:
4863 break;
4864 }
4865
Johannes Berg48ab9052009-07-10 18:42:31 +02004866 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004867 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004868 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004869 if (intbss == wdev->current_bss &&
4870 nla_put_u32(msg, NL80211_BSS_STATUS,
4871 NL80211_BSS_STATUS_ASSOCIATED))
4872 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004873 break;
4874 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004875 if (intbss == wdev->current_bss &&
4876 nla_put_u32(msg, NL80211_BSS_STATUS,
4877 NL80211_BSS_STATUS_IBSS_JOINED))
4878 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004879 break;
4880 default:
4881 break;
4882 }
4883
Johannes Berg2a519312009-02-10 21:25:55 +01004884 nla_nest_end(msg, bss);
4885
4886 return genlmsg_end(msg, hdr);
4887
4888 nla_put_failure:
4889 genlmsg_cancel(msg, hdr);
4890 return -EMSGSIZE;
4891}
4892
4893static int nl80211_dump_scan(struct sk_buff *skb,
4894 struct netlink_callback *cb)
4895{
Johannes Berg48ab9052009-07-10 18:42:31 +02004896 struct cfg80211_registered_device *rdev;
4897 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004898 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004899 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004900 int start = cb->args[1], idx = 0;
4901 int err;
4902
Johannes Berg67748892010-10-04 21:14:06 +02004903 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4904 if (err)
4905 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004906
Johannes Berg48ab9052009-07-10 18:42:31 +02004907 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004908
Johannes Berg48ab9052009-07-10 18:42:31 +02004909 wdev_lock(wdev);
4910 spin_lock_bh(&rdev->bss_lock);
4911 cfg80211_bss_expire(rdev);
4912
Johannes Berg9720bb32011-06-21 09:45:33 +02004913 cb->seq = rdev->bss_generation;
4914
Johannes Berg48ab9052009-07-10 18:42:31 +02004915 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004916 if (++idx <= start)
4917 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004918 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004919 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004920 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004921 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004922 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004923 }
4924 }
4925
Johannes Berg48ab9052009-07-10 18:42:31 +02004926 spin_unlock_bh(&rdev->bss_lock);
4927 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004928
4929 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004930 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004931
Johannes Berg67748892010-10-04 21:14:06 +02004932 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004933}
4934
Eric W. Biederman15e47302012-09-07 20:12:54 +00004935static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004936 int flags, struct net_device *dev,
4937 struct survey_info *survey)
4938{
4939 void *hdr;
4940 struct nlattr *infoattr;
4941
Eric W. Biederman15e47302012-09-07 20:12:54 +00004942 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004943 NL80211_CMD_NEW_SURVEY_RESULTS);
4944 if (!hdr)
4945 return -ENOMEM;
4946
David S. Miller9360ffd2012-03-29 04:41:26 -04004947 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4948 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004949
4950 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4951 if (!infoattr)
4952 goto nla_put_failure;
4953
David S. Miller9360ffd2012-03-29 04:41:26 -04004954 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4955 survey->channel->center_freq))
4956 goto nla_put_failure;
4957
4958 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4959 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4960 goto nla_put_failure;
4961 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4962 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4963 goto nla_put_failure;
4964 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4965 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4966 survey->channel_time))
4967 goto nla_put_failure;
4968 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4969 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4970 survey->channel_time_busy))
4971 goto nla_put_failure;
4972 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4973 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4974 survey->channel_time_ext_busy))
4975 goto nla_put_failure;
4976 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4977 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4978 survey->channel_time_rx))
4979 goto nla_put_failure;
4980 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4981 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4982 survey->channel_time_tx))
4983 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004984
4985 nla_nest_end(msg, infoattr);
4986
4987 return genlmsg_end(msg, hdr);
4988
4989 nla_put_failure:
4990 genlmsg_cancel(msg, hdr);
4991 return -EMSGSIZE;
4992}
4993
4994static int nl80211_dump_survey(struct sk_buff *skb,
4995 struct netlink_callback *cb)
4996{
4997 struct survey_info survey;
4998 struct cfg80211_registered_device *dev;
4999 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005000 int survey_idx = cb->args[1];
5001 int res;
5002
Johannes Berg67748892010-10-04 21:14:06 +02005003 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5004 if (res)
5005 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005006
5007 if (!dev->ops->dump_survey) {
5008 res = -EOPNOTSUPP;
5009 goto out_err;
5010 }
5011
5012 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005013 struct ieee80211_channel *chan;
5014
Hila Gonene35e4d22012-06-27 17:19:42 +03005015 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005016 if (res == -ENOENT)
5017 break;
5018 if (res)
5019 goto out_err;
5020
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005021 /* Survey without a channel doesn't make sense */
5022 if (!survey.channel) {
5023 res = -EINVAL;
5024 goto out;
5025 }
5026
5027 chan = ieee80211_get_channel(&dev->wiphy,
5028 survey.channel->center_freq);
5029 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5030 survey_idx++;
5031 continue;
5032 }
5033
Holger Schurig61fa7132009-11-11 12:25:40 +01005034 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005035 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005036 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5037 netdev,
5038 &survey) < 0)
5039 goto out;
5040 survey_idx++;
5041 }
5042
5043 out:
5044 cb->args[1] = survey_idx;
5045 res = skb->len;
5046 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005047 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005048 return res;
5049}
5050
Samuel Ortizb23aa672009-07-01 21:26:54 +02005051static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5052{
5053 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5054 NL80211_WPA_VERSION_2));
5055}
5056
Jouni Malinen636a5d32009-03-19 13:39:22 +02005057static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5058{
Johannes Berg4c476992010-10-04 21:36:35 +02005059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5060 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005061 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005062 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5063 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005064 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005065 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005066 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005067
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005068 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5069 return -EINVAL;
5070
5071 if (!info->attrs[NL80211_ATTR_MAC])
5072 return -EINVAL;
5073
Jouni Malinen17780922009-03-27 20:52:47 +02005074 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5075 return -EINVAL;
5076
Johannes Berg19957bb2009-07-02 17:20:43 +02005077 if (!info->attrs[NL80211_ATTR_SSID])
5078 return -EINVAL;
5079
5080 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5081 return -EINVAL;
5082
Johannes Bergfffd0932009-07-08 14:22:54 +02005083 err = nl80211_parse_key(info, &key);
5084 if (err)
5085 return err;
5086
5087 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005088 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5089 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005090 if (!key.p.key || !key.p.key_len)
5091 return -EINVAL;
5092 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5093 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5094 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5095 key.p.key_len != WLAN_KEY_LEN_WEP104))
5096 return -EINVAL;
5097 if (key.idx > 4)
5098 return -EINVAL;
5099 } else {
5100 key.p.key_len = 0;
5101 key.p.key = NULL;
5102 }
5103
Johannes Bergafea0b72010-08-10 09:46:42 +02005104 if (key.idx >= 0) {
5105 int i;
5106 bool ok = false;
5107 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5108 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5109 ok = true;
5110 break;
5111 }
5112 }
Johannes Berg4c476992010-10-04 21:36:35 +02005113 if (!ok)
5114 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005115 }
5116
Johannes Berg4c476992010-10-04 21:36:35 +02005117 if (!rdev->ops->auth)
5118 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005119
Johannes Berg074ac8d2010-09-16 14:58:22 +02005120 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005121 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5122 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005123
Johannes Berg19957bb2009-07-02 17:20:43 +02005124 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005125 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005126 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005127 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5128 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005129
Johannes Berg19957bb2009-07-02 17:20:43 +02005130 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5131 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5132
5133 if (info->attrs[NL80211_ATTR_IE]) {
5134 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5135 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5136 }
5137
5138 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005139 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005140 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005141
Jouni Malinene39e5b52012-09-30 19:29:39 +03005142 if (auth_type == NL80211_AUTHTYPE_SAE &&
5143 !info->attrs[NL80211_ATTR_SAE_DATA])
5144 return -EINVAL;
5145
5146 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5147 if (auth_type != NL80211_AUTHTYPE_SAE)
5148 return -EINVAL;
5149 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5150 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5151 /* need to include at least Auth Transaction and Status Code */
5152 if (sae_data_len < 4)
5153 return -EINVAL;
5154 }
5155
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005156 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5157
Johannes Berg95de8172012-01-20 13:55:25 +01005158 /*
5159 * Since we no longer track auth state, ignore
5160 * requests to only change local state.
5161 */
5162 if (local_state_change)
5163 return 0;
5164
Johannes Berg4c476992010-10-04 21:36:35 +02005165 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5166 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005167 key.p.key, key.p.key_len, key.idx,
5168 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005169}
5170
Johannes Bergc0692b82010-08-27 14:26:53 +03005171static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5172 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005173 struct cfg80211_crypto_settings *settings,
5174 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005175{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005176 memset(settings, 0, sizeof(*settings));
5177
Samuel Ortizb23aa672009-07-01 21:26:54 +02005178 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5179
Johannes Bergc0692b82010-08-27 14:26:53 +03005180 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5181 u16 proto;
5182 proto = nla_get_u16(
5183 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5184 settings->control_port_ethertype = cpu_to_be16(proto);
5185 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5186 proto != ETH_P_PAE)
5187 return -EINVAL;
5188 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5189 settings->control_port_no_encrypt = true;
5190 } else
5191 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5192
Samuel Ortizb23aa672009-07-01 21:26:54 +02005193 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5194 void *data;
5195 int len, i;
5196
5197 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5198 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5199 settings->n_ciphers_pairwise = len / sizeof(u32);
5200
5201 if (len % sizeof(u32))
5202 return -EINVAL;
5203
Johannes Berg3dc27d22009-07-02 21:36:37 +02005204 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005205 return -EINVAL;
5206
5207 memcpy(settings->ciphers_pairwise, data, len);
5208
5209 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005210 if (!cfg80211_supported_cipher_suite(
5211 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005212 settings->ciphers_pairwise[i]))
5213 return -EINVAL;
5214 }
5215
5216 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5217 settings->cipher_group =
5218 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005219 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5220 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005221 return -EINVAL;
5222 }
5223
5224 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5225 settings->wpa_versions =
5226 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5227 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5228 return -EINVAL;
5229 }
5230
5231 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5232 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005233 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005234
5235 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5236 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5237 settings->n_akm_suites = len / sizeof(u32);
5238
5239 if (len % sizeof(u32))
5240 return -EINVAL;
5241
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005242 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5243 return -EINVAL;
5244
Samuel Ortizb23aa672009-07-01 21:26:54 +02005245 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005246 }
5247
5248 return 0;
5249}
5250
Jouni Malinen636a5d32009-03-19 13:39:22 +02005251static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5252{
Johannes Berg4c476992010-10-04 21:36:35 +02005253 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5254 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005255 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005256 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005257 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005258 int err, ssid_len, ie_len = 0;
5259 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005260 u32 flags = 0;
5261 struct ieee80211_ht_cap *ht_capa = NULL;
5262 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005263
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005264 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5265 return -EINVAL;
5266
5267 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005268 !info->attrs[NL80211_ATTR_SSID] ||
5269 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005270 return -EINVAL;
5271
Johannes Berg4c476992010-10-04 21:36:35 +02005272 if (!rdev->ops->assoc)
5273 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005274
Johannes Berg074ac8d2010-09-16 14:58:22 +02005275 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005276 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5277 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005278
Johannes Berg19957bb2009-07-02 17:20:43 +02005279 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005280
Johannes Berg19957bb2009-07-02 17:20:43 +02005281 chan = ieee80211_get_channel(&rdev->wiphy,
5282 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005283 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5284 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005285
Johannes Berg19957bb2009-07-02 17:20:43 +02005286 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5287 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005288
5289 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005290 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5291 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005292 }
5293
Jouni Malinendc6382c2009-05-06 22:09:37 +03005294 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005295 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005296 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005297 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005298 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005299 else if (mfp != NL80211_MFP_NO)
5300 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005301 }
5302
Johannes Berg3e5d7642009-07-07 14:37:26 +02005303 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5304 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5305
Ben Greear7e7c8922011-11-18 11:31:59 -08005306 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5307 flags |= ASSOC_REQ_DISABLE_HT;
5308
5309 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5310 ht_capa_mask =
5311 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5312
5313 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5314 if (!ht_capa_mask)
5315 return -EINVAL;
5316 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5317 }
5318
Johannes Bergc0692b82010-08-27 14:26:53 +03005319 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005320 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005321 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5322 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005323 &crypto, flags, ht_capa,
5324 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005325
Jouni Malinen636a5d32009-03-19 13:39:22 +02005326 return err;
5327}
5328
5329static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5330{
Johannes Berg4c476992010-10-04 21:36:35 +02005331 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5332 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005333 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005334 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005335 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005336 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005337
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005338 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5339 return -EINVAL;
5340
5341 if (!info->attrs[NL80211_ATTR_MAC])
5342 return -EINVAL;
5343
5344 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5345 return -EINVAL;
5346
Johannes Berg4c476992010-10-04 21:36:35 +02005347 if (!rdev->ops->deauth)
5348 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005349
Johannes Berg074ac8d2010-09-16 14:58:22 +02005350 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005351 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5352 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005353
Johannes Berg19957bb2009-07-02 17:20:43 +02005354 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005355
Johannes Berg19957bb2009-07-02 17:20:43 +02005356 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5357 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005358 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005359 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005360 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005361
5362 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005363 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5364 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005365 }
5366
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005367 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5368
Johannes Berg4c476992010-10-04 21:36:35 +02005369 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5370 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005371}
5372
5373static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5374{
Johannes Berg4c476992010-10-04 21:36:35 +02005375 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5376 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005377 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005378 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005379 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005380 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005381
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005382 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5383 return -EINVAL;
5384
5385 if (!info->attrs[NL80211_ATTR_MAC])
5386 return -EINVAL;
5387
5388 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5389 return -EINVAL;
5390
Johannes Berg4c476992010-10-04 21:36:35 +02005391 if (!rdev->ops->disassoc)
5392 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005393
Johannes Berg074ac8d2010-09-16 14:58:22 +02005394 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005395 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5396 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005397
Johannes Berg19957bb2009-07-02 17:20:43 +02005398 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005399
Johannes Berg19957bb2009-07-02 17:20:43 +02005400 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5401 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005402 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005403 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005404 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005405
5406 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005407 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5408 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005409 }
5410
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005411 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5412
Johannes Berg4c476992010-10-04 21:36:35 +02005413 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5414 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005415}
5416
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005417static bool
5418nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5419 int mcast_rate[IEEE80211_NUM_BANDS],
5420 int rateval)
5421{
5422 struct wiphy *wiphy = &rdev->wiphy;
5423 bool found = false;
5424 int band, i;
5425
5426 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5427 struct ieee80211_supported_band *sband;
5428
5429 sband = wiphy->bands[band];
5430 if (!sband)
5431 continue;
5432
5433 for (i = 0; i < sband->n_bitrates; i++) {
5434 if (sband->bitrates[i].bitrate == rateval) {
5435 mcast_rate[band] = i + 1;
5436 found = true;
5437 break;
5438 }
5439 }
5440 }
5441
5442 return found;
5443}
5444
Johannes Berg04a773a2009-04-19 21:24:32 +02005445static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5446{
Johannes Berg4c476992010-10-04 21:36:35 +02005447 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5448 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005449 struct cfg80211_ibss_params ibss;
5450 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005451 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005452 int err;
5453
Johannes Berg8e30bc52009-04-22 17:45:38 +02005454 memset(&ibss, 0, sizeof(ibss));
5455
Johannes Berg04a773a2009-04-19 21:24:32 +02005456 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5457 return -EINVAL;
5458
Johannes Berg683b6d32012-11-08 21:25:48 +01005459 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005460 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5461 return -EINVAL;
5462
Johannes Berg8e30bc52009-04-22 17:45:38 +02005463 ibss.beacon_interval = 100;
5464
5465 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5466 ibss.beacon_interval =
5467 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5468 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5469 return -EINVAL;
5470 }
5471
Johannes Berg4c476992010-10-04 21:36:35 +02005472 if (!rdev->ops->join_ibss)
5473 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005474
Johannes Berg4c476992010-10-04 21:36:35 +02005475 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5476 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005477
Johannes Berg79c97e92009-07-07 03:56:12 +02005478 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005479
Johannes Berg39193492011-09-16 13:45:25 +02005480 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005481 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005482
5483 if (!is_valid_ether_addr(ibss.bssid))
5484 return -EINVAL;
5485 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005486 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5487 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5488
5489 if (info->attrs[NL80211_ATTR_IE]) {
5490 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5491 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5492 }
5493
Johannes Berg683b6d32012-11-08 21:25:48 +01005494 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5495 if (err)
5496 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005497
Johannes Berg683b6d32012-11-08 21:25:48 +01005498 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005499 return -EINVAL;
5500
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005501 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5502 return -EINVAL;
5503 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5504 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005505 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005506
Johannes Berg04a773a2009-04-19 21:24:32 +02005507 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005508 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005509
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005510 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5511 u8 *rates =
5512 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5513 int n_rates =
5514 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5515 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005516 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005517
Johannes Berg34850ab2011-07-18 18:08:35 +02005518 err = ieee80211_get_ratemask(sband, rates, n_rates,
5519 &ibss.basic_rates);
5520 if (err)
5521 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005522 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005523
5524 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5525 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5526 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5527 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005528
Johannes Berg4c476992010-10-04 21:36:35 +02005529 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305530 bool no_ht = false;
5531
Johannes Berg4c476992010-10-04 21:36:35 +02005532 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305533 info->attrs[NL80211_ATTR_KEYS],
5534 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005535 if (IS_ERR(connkeys))
5536 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305537
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005538 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5539 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305540 kfree(connkeys);
5541 return -EINVAL;
5542 }
Johannes Berg4c476992010-10-04 21:36:35 +02005543 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005544
Antonio Quartulli267335d2012-01-31 20:25:47 +01005545 ibss.control_port =
5546 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5547
Johannes Berg4c476992010-10-04 21:36:35 +02005548 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005549 if (err)
5550 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005551 return err;
5552}
5553
5554static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5555{
Johannes Berg4c476992010-10-04 21:36:35 +02005556 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5557 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005558
Johannes Berg4c476992010-10-04 21:36:35 +02005559 if (!rdev->ops->leave_ibss)
5560 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005561
Johannes Berg4c476992010-10-04 21:36:35 +02005562 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5563 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005564
Johannes Berg4c476992010-10-04 21:36:35 +02005565 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005566}
5567
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005568static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5569{
5570 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5571 struct net_device *dev = info->user_ptr[1];
5572 int mcast_rate[IEEE80211_NUM_BANDS];
5573 u32 nla_rate;
5574 int err;
5575
5576 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5577 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5578 return -EOPNOTSUPP;
5579
5580 if (!rdev->ops->set_mcast_rate)
5581 return -EOPNOTSUPP;
5582
5583 memset(mcast_rate, 0, sizeof(mcast_rate));
5584
5585 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5586 return -EINVAL;
5587
5588 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5589 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5590 return -EINVAL;
5591
5592 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5593
5594 return err;
5595}
5596
5597
Johannes Bergaff89a92009-07-01 21:26:51 +02005598#ifdef CONFIG_NL80211_TESTMODE
5599static struct genl_multicast_group nl80211_testmode_mcgrp = {
5600 .name = "testmode",
5601};
5602
5603static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5604{
Johannes Berg4c476992010-10-04 21:36:35 +02005605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005606 int err;
5607
5608 if (!info->attrs[NL80211_ATTR_TESTDATA])
5609 return -EINVAL;
5610
Johannes Bergaff89a92009-07-01 21:26:51 +02005611 err = -EOPNOTSUPP;
5612 if (rdev->ops->testmode_cmd) {
5613 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005614 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005615 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5616 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5617 rdev->testmode_info = NULL;
5618 }
5619
Johannes Bergaff89a92009-07-01 21:26:51 +02005620 return err;
5621}
5622
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005623static int nl80211_testmode_dump(struct sk_buff *skb,
5624 struct netlink_callback *cb)
5625{
Johannes Berg00918d32011-12-13 17:22:05 +01005626 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005627 int err;
5628 long phy_idx;
5629 void *data = NULL;
5630 int data_len = 0;
5631
5632 if (cb->args[0]) {
5633 /*
5634 * 0 is a valid index, but not valid for args[0],
5635 * so we need to offset by 1.
5636 */
5637 phy_idx = cb->args[0] - 1;
5638 } else {
5639 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5640 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5641 nl80211_policy);
5642 if (err)
5643 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005644
Johannes Berg2bd7e352012-06-15 14:23:16 +02005645 mutex_lock(&cfg80211_mutex);
5646 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5647 nl80211_fam.attrbuf);
5648 if (IS_ERR(rdev)) {
5649 mutex_unlock(&cfg80211_mutex);
5650 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005651 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005652 phy_idx = rdev->wiphy_idx;
5653 rdev = NULL;
5654 mutex_unlock(&cfg80211_mutex);
5655
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005656 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5657 cb->args[1] =
5658 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5659 }
5660
5661 if (cb->args[1]) {
5662 data = nla_data((void *)cb->args[1]);
5663 data_len = nla_len((void *)cb->args[1]);
5664 }
5665
5666 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005667 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5668 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005669 mutex_unlock(&cfg80211_mutex);
5670 return -ENOENT;
5671 }
Johannes Berg00918d32011-12-13 17:22:05 +01005672 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005673 mutex_unlock(&cfg80211_mutex);
5674
Johannes Berg00918d32011-12-13 17:22:05 +01005675 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005676 err = -EOPNOTSUPP;
5677 goto out_err;
5678 }
5679
5680 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005681 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005682 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5683 NL80211_CMD_TESTMODE);
5684 struct nlattr *tmdata;
5685
David S. Miller9360ffd2012-03-29 04:41:26 -04005686 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005687 genlmsg_cancel(skb, hdr);
5688 break;
5689 }
5690
5691 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5692 if (!tmdata) {
5693 genlmsg_cancel(skb, hdr);
5694 break;
5695 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005696 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005697 nla_nest_end(skb, tmdata);
5698
5699 if (err == -ENOBUFS || err == -ENOENT) {
5700 genlmsg_cancel(skb, hdr);
5701 break;
5702 } else if (err) {
5703 genlmsg_cancel(skb, hdr);
5704 goto out_err;
5705 }
5706
5707 genlmsg_end(skb, hdr);
5708 }
5709
5710 err = skb->len;
5711 /* see above */
5712 cb->args[0] = phy_idx + 1;
5713 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005714 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005715 return err;
5716}
5717
Johannes Bergaff89a92009-07-01 21:26:51 +02005718static struct sk_buff *
5719__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005720 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005721{
5722 struct sk_buff *skb;
5723 void *hdr;
5724 struct nlattr *data;
5725
5726 skb = nlmsg_new(approxlen + 100, gfp);
5727 if (!skb)
5728 return NULL;
5729
Eric W. Biederman15e47302012-09-07 20:12:54 +00005730 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005731 if (!hdr) {
5732 kfree_skb(skb);
5733 return NULL;
5734 }
5735
David S. Miller9360ffd2012-03-29 04:41:26 -04005736 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5737 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005738 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5739
5740 ((void **)skb->cb)[0] = rdev;
5741 ((void **)skb->cb)[1] = hdr;
5742 ((void **)skb->cb)[2] = data;
5743
5744 return skb;
5745
5746 nla_put_failure:
5747 kfree_skb(skb);
5748 return NULL;
5749}
5750
5751struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5752 int approxlen)
5753{
5754 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5755
5756 if (WARN_ON(!rdev->testmode_info))
5757 return NULL;
5758
5759 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005760 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005761 rdev->testmode_info->snd_seq,
5762 GFP_KERNEL);
5763}
5764EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5765
5766int cfg80211_testmode_reply(struct sk_buff *skb)
5767{
5768 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5769 void *hdr = ((void **)skb->cb)[1];
5770 struct nlattr *data = ((void **)skb->cb)[2];
5771
5772 if (WARN_ON(!rdev->testmode_info)) {
5773 kfree_skb(skb);
5774 return -EINVAL;
5775 }
5776
5777 nla_nest_end(skb, data);
5778 genlmsg_end(skb, hdr);
5779 return genlmsg_reply(skb, rdev->testmode_info);
5780}
5781EXPORT_SYMBOL(cfg80211_testmode_reply);
5782
5783struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5784 int approxlen, gfp_t gfp)
5785{
5786 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5787
5788 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5789}
5790EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5791
5792void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5793{
5794 void *hdr = ((void **)skb->cb)[1];
5795 struct nlattr *data = ((void **)skb->cb)[2];
5796
5797 nla_nest_end(skb, data);
5798 genlmsg_end(skb, hdr);
5799 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5800}
5801EXPORT_SYMBOL(cfg80211_testmode_event);
5802#endif
5803
Samuel Ortizb23aa672009-07-01 21:26:54 +02005804static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5805{
Johannes Berg4c476992010-10-04 21:36:35 +02005806 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5807 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005808 struct cfg80211_connect_params connect;
5809 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005810 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005811 int err;
5812
5813 memset(&connect, 0, sizeof(connect));
5814
5815 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5816 return -EINVAL;
5817
5818 if (!info->attrs[NL80211_ATTR_SSID] ||
5819 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5820 return -EINVAL;
5821
5822 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5823 connect.auth_type =
5824 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005825 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5826 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005827 return -EINVAL;
5828 } else
5829 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5830
5831 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5832
Johannes Bergc0692b82010-08-27 14:26:53 +03005833 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005834 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005835 if (err)
5836 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005837
Johannes Berg074ac8d2010-09-16 14:58:22 +02005838 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005839 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5840 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005841
Johannes Berg79c97e92009-07-07 03:56:12 +02005842 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005843
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305844 connect.bg_scan_period = -1;
5845 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5846 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5847 connect.bg_scan_period =
5848 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5849 }
5850
Samuel Ortizb23aa672009-07-01 21:26:54 +02005851 if (info->attrs[NL80211_ATTR_MAC])
5852 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5853 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5854 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5855
5856 if (info->attrs[NL80211_ATTR_IE]) {
5857 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5858 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5859 }
5860
5861 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5862 connect.channel =
5863 ieee80211_get_channel(wiphy,
5864 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5865 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005866 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5867 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005868 }
5869
Johannes Bergfffd0932009-07-08 14:22:54 +02005870 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5871 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305872 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005873 if (IS_ERR(connkeys))
5874 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005875 }
5876
Ben Greear7e7c8922011-11-18 11:31:59 -08005877 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5878 connect.flags |= ASSOC_REQ_DISABLE_HT;
5879
5880 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5881 memcpy(&connect.ht_capa_mask,
5882 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5883 sizeof(connect.ht_capa_mask));
5884
5885 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005886 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5887 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005888 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005889 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005890 memcpy(&connect.ht_capa,
5891 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5892 sizeof(connect.ht_capa));
5893 }
5894
Johannes Bergfffd0932009-07-08 14:22:54 +02005895 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005896 if (err)
5897 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005898 return err;
5899}
5900
5901static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5902{
Johannes Berg4c476992010-10-04 21:36:35 +02005903 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5904 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005905 u16 reason;
5906
5907 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5908 reason = WLAN_REASON_DEAUTH_LEAVING;
5909 else
5910 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5911
5912 if (reason == 0)
5913 return -EINVAL;
5914
Johannes Berg074ac8d2010-09-16 14:58:22 +02005915 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005916 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5917 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005918
Johannes Berg4c476992010-10-04 21:36:35 +02005919 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005920}
5921
Johannes Berg463d0182009-07-14 00:33:35 +02005922static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5923{
Johannes Berg4c476992010-10-04 21:36:35 +02005924 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005925 struct net *net;
5926 int err;
5927 u32 pid;
5928
5929 if (!info->attrs[NL80211_ATTR_PID])
5930 return -EINVAL;
5931
5932 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5933
Johannes Berg463d0182009-07-14 00:33:35 +02005934 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005935 if (IS_ERR(net))
5936 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005937
5938 err = 0;
5939
5940 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005941 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5942 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005943
Johannes Berg463d0182009-07-14 00:33:35 +02005944 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005945 return err;
5946}
5947
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005948static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5949{
Johannes Berg4c476992010-10-04 21:36:35 +02005950 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005951 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5952 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005953 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005954 struct cfg80211_pmksa pmksa;
5955
5956 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5957
5958 if (!info->attrs[NL80211_ATTR_MAC])
5959 return -EINVAL;
5960
5961 if (!info->attrs[NL80211_ATTR_PMKID])
5962 return -EINVAL;
5963
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005964 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5965 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5966
Johannes Berg074ac8d2010-09-16 14:58:22 +02005967 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005968 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5969 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005970
5971 switch (info->genlhdr->cmd) {
5972 case NL80211_CMD_SET_PMKSA:
5973 rdev_ops = rdev->ops->set_pmksa;
5974 break;
5975 case NL80211_CMD_DEL_PMKSA:
5976 rdev_ops = rdev->ops->del_pmksa;
5977 break;
5978 default:
5979 WARN_ON(1);
5980 break;
5981 }
5982
Johannes Berg4c476992010-10-04 21:36:35 +02005983 if (!rdev_ops)
5984 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005985
Johannes Berg4c476992010-10-04 21:36:35 +02005986 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005987}
5988
5989static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5990{
Johannes Berg4c476992010-10-04 21:36:35 +02005991 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5992 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005993
Johannes Berg074ac8d2010-09-16 14:58:22 +02005994 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005995 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5996 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005997
Johannes Berg4c476992010-10-04 21:36:35 +02005998 if (!rdev->ops->flush_pmksa)
5999 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006000
Hila Gonene35e4d22012-06-27 17:19:42 +03006001 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006002}
6003
Arik Nemtsov109086c2011-09-28 14:12:50 +03006004static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6005{
6006 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6007 struct net_device *dev = info->user_ptr[1];
6008 u8 action_code, dialog_token;
6009 u16 status_code;
6010 u8 *peer;
6011
6012 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6013 !rdev->ops->tdls_mgmt)
6014 return -EOPNOTSUPP;
6015
6016 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6017 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6018 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6019 !info->attrs[NL80211_ATTR_IE] ||
6020 !info->attrs[NL80211_ATTR_MAC])
6021 return -EINVAL;
6022
6023 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6024 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6025 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6026 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6027
Hila Gonene35e4d22012-06-27 17:19:42 +03006028 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6029 dialog_token, status_code,
6030 nla_data(info->attrs[NL80211_ATTR_IE]),
6031 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006032}
6033
6034static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6035{
6036 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6037 struct net_device *dev = info->user_ptr[1];
6038 enum nl80211_tdls_operation operation;
6039 u8 *peer;
6040
6041 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6042 !rdev->ops->tdls_oper)
6043 return -EOPNOTSUPP;
6044
6045 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6046 !info->attrs[NL80211_ATTR_MAC])
6047 return -EINVAL;
6048
6049 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6050 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6051
Hila Gonene35e4d22012-06-27 17:19:42 +03006052 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006053}
6054
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006055static int nl80211_remain_on_channel(struct sk_buff *skb,
6056 struct genl_info *info)
6057{
Johannes Berg4c476992010-10-04 21:36:35 +02006058 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006059 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006060 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006061 struct sk_buff *msg;
6062 void *hdr;
6063 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006064 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006065 int err;
6066
6067 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6068 !info->attrs[NL80211_ATTR_DURATION])
6069 return -EINVAL;
6070
6071 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6072
Johannes Berg7c4ef712011-11-18 15:33:48 +01006073 if (!rdev->ops->remain_on_channel ||
6074 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006075 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006076
Johannes Bergebf348f2012-06-01 12:50:54 +02006077 /*
6078 * We should be on that channel for at least a minimum amount of
6079 * time (10ms) but no longer than the driver supports.
6080 */
6081 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6082 duration > rdev->wiphy.max_remain_on_channel_duration)
6083 return -EINVAL;
6084
Johannes Berg683b6d32012-11-08 21:25:48 +01006085 err = nl80211_parse_chandef(rdev, info, &chandef);
6086 if (err)
6087 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006088
6089 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006090 if (!msg)
6091 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006092
Eric W. Biederman15e47302012-09-07 20:12:54 +00006093 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006094 NL80211_CMD_REMAIN_ON_CHANNEL);
6095
6096 if (IS_ERR(hdr)) {
6097 err = PTR_ERR(hdr);
6098 goto free_msg;
6099 }
6100
Johannes Berg683b6d32012-11-08 21:25:48 +01006101 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6102 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006103
6104 if (err)
6105 goto free_msg;
6106
David S. Miller9360ffd2012-03-29 04:41:26 -04006107 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6108 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006109
6110 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006111
6112 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006113
6114 nla_put_failure:
6115 err = -ENOBUFS;
6116 free_msg:
6117 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006118 return err;
6119}
6120
6121static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6122 struct genl_info *info)
6123{
Johannes Berg4c476992010-10-04 21:36:35 +02006124 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006125 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006126 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006127
6128 if (!info->attrs[NL80211_ATTR_COOKIE])
6129 return -EINVAL;
6130
Johannes Berg4c476992010-10-04 21:36:35 +02006131 if (!rdev->ops->cancel_remain_on_channel)
6132 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006133
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006134 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6135
Hila Gonene35e4d22012-06-27 17:19:42 +03006136 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006137}
6138
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006139static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6140 u8 *rates, u8 rates_len)
6141{
6142 u8 i;
6143 u32 mask = 0;
6144
6145 for (i = 0; i < rates_len; i++) {
6146 int rate = (rates[i] & 0x7f) * 5;
6147 int ridx;
6148 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6149 struct ieee80211_rate *srate =
6150 &sband->bitrates[ridx];
6151 if (rate == srate->bitrate) {
6152 mask |= 1 << ridx;
6153 break;
6154 }
6155 }
6156 if (ridx == sband->n_bitrates)
6157 return 0; /* rate not found */
6158 }
6159
6160 return mask;
6161}
6162
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006163static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6164 u8 *rates, u8 rates_len,
6165 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6166{
6167 u8 i;
6168
6169 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6170
6171 for (i = 0; i < rates_len; i++) {
6172 int ridx, rbit;
6173
6174 ridx = rates[i] / 8;
6175 rbit = BIT(rates[i] % 8);
6176
6177 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006178 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006179 return false;
6180
6181 /* check availability */
6182 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6183 mcs[ridx] |= rbit;
6184 else
6185 return false;
6186 }
6187
6188 return true;
6189}
6190
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006191static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006192 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6193 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006194 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6195 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006196};
6197
6198static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6199 struct genl_info *info)
6200{
6201 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006202 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006203 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006204 int rem, i;
6205 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006206 struct nlattr *tx_rates;
6207 struct ieee80211_supported_band *sband;
6208
6209 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6210 return -EINVAL;
6211
Johannes Berg4c476992010-10-04 21:36:35 +02006212 if (!rdev->ops->set_bitrate_mask)
6213 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006214
6215 memset(&mask, 0, sizeof(mask));
6216 /* Default to all rates enabled */
6217 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6218 sband = rdev->wiphy.bands[i];
6219 mask.control[i].legacy =
6220 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006221 if (sband)
6222 memcpy(mask.control[i].mcs,
6223 sband->ht_cap.mcs.rx_mask,
6224 sizeof(mask.control[i].mcs));
6225 else
6226 memset(mask.control[i].mcs, 0,
6227 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006228 }
6229
6230 /*
6231 * The nested attribute uses enum nl80211_band as the index. This maps
6232 * directly to the enum ieee80211_band values used in cfg80211.
6233 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006234 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006235 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6236 {
6237 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006238 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6239 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006240 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006241 if (sband == NULL)
6242 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006243 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6244 nla_len(tx_rates), nl80211_txattr_policy);
6245 if (tb[NL80211_TXRATE_LEGACY]) {
6246 mask.control[band].legacy = rateset_to_mask(
6247 sband,
6248 nla_data(tb[NL80211_TXRATE_LEGACY]),
6249 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306250 if ((mask.control[band].legacy == 0) &&
6251 nla_len(tb[NL80211_TXRATE_LEGACY]))
6252 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006253 }
6254 if (tb[NL80211_TXRATE_MCS]) {
6255 if (!ht_rateset_to_mask(
6256 sband,
6257 nla_data(tb[NL80211_TXRATE_MCS]),
6258 nla_len(tb[NL80211_TXRATE_MCS]),
6259 mask.control[band].mcs))
6260 return -EINVAL;
6261 }
6262
6263 if (mask.control[band].legacy == 0) {
6264 /* don't allow empty legacy rates if HT
6265 * is not even supported. */
6266 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6267 return -EINVAL;
6268
6269 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6270 if (mask.control[band].mcs[i])
6271 break;
6272
6273 /* legacy and mcs rates may not be both empty */
6274 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006275 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006276 }
6277 }
6278
Hila Gonene35e4d22012-06-27 17:19:42 +03006279 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006280}
6281
Johannes Berg2e161f72010-08-12 15:38:38 +02006282static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006283{
Johannes Berg4c476992010-10-04 21:36:35 +02006284 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006285 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006286 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006287
6288 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6289 return -EINVAL;
6290
Johannes Berg2e161f72010-08-12 15:38:38 +02006291 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6292 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006293
Johannes Berg71bbc992012-06-15 15:30:18 +02006294 switch (wdev->iftype) {
6295 case NL80211_IFTYPE_STATION:
6296 case NL80211_IFTYPE_ADHOC:
6297 case NL80211_IFTYPE_P2P_CLIENT:
6298 case NL80211_IFTYPE_AP:
6299 case NL80211_IFTYPE_AP_VLAN:
6300 case NL80211_IFTYPE_MESH_POINT:
6301 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006302 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006303 break;
6304 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006305 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006306 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006307
6308 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006309 if (!rdev->ops->mgmt_tx)
6310 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006311
Eric W. Biederman15e47302012-09-07 20:12:54 +00006312 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006313 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6314 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006315}
6316
Johannes Berg2e161f72010-08-12 15:38:38 +02006317static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006318{
Johannes Berg4c476992010-10-04 21:36:35 +02006319 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006320 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006321 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006322 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006323 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006324 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006325 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006326 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006327 bool offchan, no_cck, dont_wait_for_ack;
6328
6329 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006330
Johannes Berg683b6d32012-11-08 21:25:48 +01006331 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006332 return -EINVAL;
6333
Johannes Berg4c476992010-10-04 21:36:35 +02006334 if (!rdev->ops->mgmt_tx)
6335 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006336
Johannes Berg71bbc992012-06-15 15:30:18 +02006337 switch (wdev->iftype) {
6338 case NL80211_IFTYPE_STATION:
6339 case NL80211_IFTYPE_ADHOC:
6340 case NL80211_IFTYPE_P2P_CLIENT:
6341 case NL80211_IFTYPE_AP:
6342 case NL80211_IFTYPE_AP_VLAN:
6343 case NL80211_IFTYPE_MESH_POINT:
6344 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006345 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006346 break;
6347 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006348 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006349 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006350
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006351 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006352 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006353 return -EINVAL;
6354 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006355
6356 /*
6357 * We should wait on the channel for at least a minimum amount
6358 * of time (10ms) but no longer than the driver supports.
6359 */
6360 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6361 wait > rdev->wiphy.max_remain_on_channel_duration)
6362 return -EINVAL;
6363
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006364 }
6365
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006366 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6367
Johannes Berg7c4ef712011-11-18 15:33:48 +01006368 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6369 return -EINVAL;
6370
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306371 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6372
Johannes Berg683b6d32012-11-08 21:25:48 +01006373 err = nl80211_parse_chandef(rdev, info, &chandef);
6374 if (err)
6375 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006376
Johannes Berge247bd902011-11-04 11:18:21 +01006377 if (!dont_wait_for_ack) {
6378 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6379 if (!msg)
6380 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006381
Eric W. Biederman15e47302012-09-07 20:12:54 +00006382 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006383 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006384
Johannes Berge247bd902011-11-04 11:18:21 +01006385 if (IS_ERR(hdr)) {
6386 err = PTR_ERR(hdr);
6387 goto free_msg;
6388 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006389 }
Johannes Berge247bd902011-11-04 11:18:21 +01006390
Johannes Berg683b6d32012-11-08 21:25:48 +01006391 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006392 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6393 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006394 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006395 if (err)
6396 goto free_msg;
6397
Johannes Berge247bd902011-11-04 11:18:21 +01006398 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006399 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6400 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006401
Johannes Berge247bd902011-11-04 11:18:21 +01006402 genlmsg_end(msg, hdr);
6403 return genlmsg_reply(msg, info);
6404 }
6405
6406 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006407
6408 nla_put_failure:
6409 err = -ENOBUFS;
6410 free_msg:
6411 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006412 return err;
6413}
6414
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006415static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6416{
6417 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006418 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006419 u64 cookie;
6420
6421 if (!info->attrs[NL80211_ATTR_COOKIE])
6422 return -EINVAL;
6423
6424 if (!rdev->ops->mgmt_tx_cancel_wait)
6425 return -EOPNOTSUPP;
6426
Johannes Berg71bbc992012-06-15 15:30:18 +02006427 switch (wdev->iftype) {
6428 case NL80211_IFTYPE_STATION:
6429 case NL80211_IFTYPE_ADHOC:
6430 case NL80211_IFTYPE_P2P_CLIENT:
6431 case NL80211_IFTYPE_AP:
6432 case NL80211_IFTYPE_AP_VLAN:
6433 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006434 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006435 break;
6436 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006437 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006438 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006439
6440 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6441
Hila Gonene35e4d22012-06-27 17:19:42 +03006442 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006443}
6444
Kalle Valoffb9eb32010-02-17 17:58:10 +02006445static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6446{
Johannes Berg4c476992010-10-04 21:36:35 +02006447 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006448 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006449 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006450 u8 ps_state;
6451 bool state;
6452 int err;
6453
Johannes Berg4c476992010-10-04 21:36:35 +02006454 if (!info->attrs[NL80211_ATTR_PS_STATE])
6455 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006456
6457 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6458
Johannes Berg4c476992010-10-04 21:36:35 +02006459 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6460 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006461
6462 wdev = dev->ieee80211_ptr;
6463
Johannes Berg4c476992010-10-04 21:36:35 +02006464 if (!rdev->ops->set_power_mgmt)
6465 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006466
6467 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6468
6469 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006470 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006471
Hila Gonene35e4d22012-06-27 17:19:42 +03006472 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006473 if (!err)
6474 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006475 return err;
6476}
6477
6478static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6479{
Johannes Berg4c476992010-10-04 21:36:35 +02006480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006481 enum nl80211_ps_state ps_state;
6482 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006483 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006484 struct sk_buff *msg;
6485 void *hdr;
6486 int err;
6487
Kalle Valoffb9eb32010-02-17 17:58:10 +02006488 wdev = dev->ieee80211_ptr;
6489
Johannes Berg4c476992010-10-04 21:36:35 +02006490 if (!rdev->ops->set_power_mgmt)
6491 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006492
6493 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006494 if (!msg)
6495 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006496
Eric W. Biederman15e47302012-09-07 20:12:54 +00006497 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006498 NL80211_CMD_GET_POWER_SAVE);
6499 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006500 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006501 goto free_msg;
6502 }
6503
6504 if (wdev->ps)
6505 ps_state = NL80211_PS_ENABLED;
6506 else
6507 ps_state = NL80211_PS_DISABLED;
6508
David S. Miller9360ffd2012-03-29 04:41:26 -04006509 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6510 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006511
6512 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006513 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006514
Johannes Berg4c476992010-10-04 21:36:35 +02006515 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006516 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006517 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006518 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006519 return err;
6520}
6521
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006522static struct nla_policy
6523nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6524 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6525 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6526 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006527 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6528 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6529 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006530};
6531
Thomas Pedersen84f10702012-07-12 16:17:33 -07006532static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006533 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006534{
6535 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6536 struct wireless_dev *wdev;
6537 struct net_device *dev = info->user_ptr[1];
6538
Johannes Bergd9d8b012012-11-26 12:51:52 +01006539 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006540 return -EINVAL;
6541
6542 wdev = dev->ieee80211_ptr;
6543
6544 if (!rdev->ops->set_cqm_txe_config)
6545 return -EOPNOTSUPP;
6546
6547 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6548 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6549 return -EOPNOTSUPP;
6550
Hila Gonene35e4d22012-06-27 17:19:42 +03006551 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006552}
6553
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006554static int nl80211_set_cqm_rssi(struct genl_info *info,
6555 s32 threshold, u32 hysteresis)
6556{
Johannes Berg4c476992010-10-04 21:36:35 +02006557 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006558 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006559 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006560
6561 if (threshold > 0)
6562 return -EINVAL;
6563
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006564 wdev = dev->ieee80211_ptr;
6565
Johannes Berg4c476992010-10-04 21:36:35 +02006566 if (!rdev->ops->set_cqm_rssi_config)
6567 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006568
Johannes Berg074ac8d2010-09-16 14:58:22 +02006569 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006570 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6571 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006572
Hila Gonene35e4d22012-06-27 17:19:42 +03006573 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006574}
6575
6576static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6577{
6578 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6579 struct nlattr *cqm;
6580 int err;
6581
6582 cqm = info->attrs[NL80211_ATTR_CQM];
6583 if (!cqm) {
6584 err = -EINVAL;
6585 goto out;
6586 }
6587
6588 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6589 nl80211_attr_cqm_policy);
6590 if (err)
6591 goto out;
6592
6593 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6594 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6595 s32 threshold;
6596 u32 hysteresis;
6597 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6598 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6599 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006600 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6601 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6602 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6603 u32 rate, pkts, intvl;
6604 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6605 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6606 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6607 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006608 } else
6609 err = -EINVAL;
6610
6611out:
6612 return err;
6613}
6614
Johannes Berg29cbe682010-12-03 09:20:44 +01006615static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6616{
6617 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6618 struct net_device *dev = info->user_ptr[1];
6619 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006620 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006621 int err;
6622
6623 /* start with default */
6624 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006625 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006626
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006627 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006628 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006629 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006630 if (err)
6631 return err;
6632 }
6633
6634 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6635 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6636 return -EINVAL;
6637
Javier Cardonac80d5452010-12-16 17:37:49 -08006638 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6639 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6640
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006641 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6642 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6643 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6644 return -EINVAL;
6645
Javier Cardonac80d5452010-12-16 17:37:49 -08006646 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6647 /* parse additional setup parameters if given */
6648 err = nl80211_parse_mesh_setup(info, &setup);
6649 if (err)
6650 return err;
6651 }
6652
Johannes Bergcc1d2802012-05-16 23:50:20 +02006653 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01006654 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
6655 if (err)
6656 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006657 } else {
6658 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01006659 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006660 }
6661
Javier Cardonac80d5452010-12-16 17:37:49 -08006662 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006663}
6664
6665static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6666{
6667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6668 struct net_device *dev = info->user_ptr[1];
6669
6670 return cfg80211_leave_mesh(rdev, dev);
6671}
6672
Johannes Bergdfb89c52012-06-27 09:23:48 +02006673#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006674static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6675{
6676 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6677 struct sk_buff *msg;
6678 void *hdr;
6679
6680 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6681 return -EOPNOTSUPP;
6682
6683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6684 if (!msg)
6685 return -ENOMEM;
6686
Eric W. Biederman15e47302012-09-07 20:12:54 +00006687 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006688 NL80211_CMD_GET_WOWLAN);
6689 if (!hdr)
6690 goto nla_put_failure;
6691
6692 if (rdev->wowlan) {
6693 struct nlattr *nl_wowlan;
6694
6695 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6696 if (!nl_wowlan)
6697 goto nla_put_failure;
6698
David S. Miller9360ffd2012-03-29 04:41:26 -04006699 if ((rdev->wowlan->any &&
6700 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6701 (rdev->wowlan->disconnect &&
6702 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6703 (rdev->wowlan->magic_pkt &&
6704 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6705 (rdev->wowlan->gtk_rekey_failure &&
6706 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6707 (rdev->wowlan->eap_identity_req &&
6708 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6709 (rdev->wowlan->four_way_handshake &&
6710 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6711 (rdev->wowlan->rfkill_release &&
6712 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6713 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006714 if (rdev->wowlan->n_patterns) {
6715 struct nlattr *nl_pats, *nl_pat;
6716 int i, pat_len;
6717
6718 nl_pats = nla_nest_start(msg,
6719 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6720 if (!nl_pats)
6721 goto nla_put_failure;
6722
6723 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6724 nl_pat = nla_nest_start(msg, i + 1);
6725 if (!nl_pat)
6726 goto nla_put_failure;
6727 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006728 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6729 DIV_ROUND_UP(pat_len, 8),
6730 rdev->wowlan->patterns[i].mask) ||
6731 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6732 pat_len,
6733 rdev->wowlan->patterns[i].pattern))
6734 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006735 nla_nest_end(msg, nl_pat);
6736 }
6737 nla_nest_end(msg, nl_pats);
6738 }
6739
6740 nla_nest_end(msg, nl_wowlan);
6741 }
6742
6743 genlmsg_end(msg, hdr);
6744 return genlmsg_reply(msg, info);
6745
6746nla_put_failure:
6747 nlmsg_free(msg);
6748 return -ENOBUFS;
6749}
6750
6751static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6752{
6753 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6754 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006755 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006756 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006757 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6758 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006759 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006760
6761 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6762 return -EOPNOTSUPP;
6763
Johannes Bergae33bd82012-07-12 16:25:02 +02006764 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6765 cfg80211_rdev_free_wowlan(rdev);
6766 rdev->wowlan = NULL;
6767 goto set_wakeup;
6768 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006769
6770 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6771 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6772 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6773 nl80211_wowlan_policy);
6774 if (err)
6775 return err;
6776
6777 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6778 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6779 return -EINVAL;
6780 new_triggers.any = true;
6781 }
6782
6783 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6784 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6785 return -EINVAL;
6786 new_triggers.disconnect = true;
6787 }
6788
6789 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6790 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6791 return -EINVAL;
6792 new_triggers.magic_pkt = true;
6793 }
6794
Johannes Berg77dbbb12011-07-13 10:48:55 +02006795 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6796 return -EINVAL;
6797
6798 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6799 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6800 return -EINVAL;
6801 new_triggers.gtk_rekey_failure = true;
6802 }
6803
6804 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6805 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6806 return -EINVAL;
6807 new_triggers.eap_identity_req = true;
6808 }
6809
6810 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6811 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6812 return -EINVAL;
6813 new_triggers.four_way_handshake = true;
6814 }
6815
6816 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6817 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6818 return -EINVAL;
6819 new_triggers.rfkill_release = true;
6820 }
6821
Johannes Bergff1b6e62011-05-04 15:37:28 +02006822 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6823 struct nlattr *pat;
6824 int n_patterns = 0;
6825 int rem, pat_len, mask_len;
6826 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6827
6828 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6829 rem)
6830 n_patterns++;
6831 if (n_patterns > wowlan->n_patterns)
6832 return -EINVAL;
6833
6834 new_triggers.patterns = kcalloc(n_patterns,
6835 sizeof(new_triggers.patterns[0]),
6836 GFP_KERNEL);
6837 if (!new_triggers.patterns)
6838 return -ENOMEM;
6839
6840 new_triggers.n_patterns = n_patterns;
6841 i = 0;
6842
6843 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6844 rem) {
6845 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6846 nla_data(pat), nla_len(pat), NULL);
6847 err = -EINVAL;
6848 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6849 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6850 goto error;
6851 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6852 mask_len = DIV_ROUND_UP(pat_len, 8);
6853 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6854 mask_len)
6855 goto error;
6856 if (pat_len > wowlan->pattern_max_len ||
6857 pat_len < wowlan->pattern_min_len)
6858 goto error;
6859
6860 new_triggers.patterns[i].mask =
6861 kmalloc(mask_len + pat_len, GFP_KERNEL);
6862 if (!new_triggers.patterns[i].mask) {
6863 err = -ENOMEM;
6864 goto error;
6865 }
6866 new_triggers.patterns[i].pattern =
6867 new_triggers.patterns[i].mask + mask_len;
6868 memcpy(new_triggers.patterns[i].mask,
6869 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6870 mask_len);
6871 new_triggers.patterns[i].pattern_len = pat_len;
6872 memcpy(new_triggers.patterns[i].pattern,
6873 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6874 pat_len);
6875 i++;
6876 }
6877 }
6878
Johannes Bergae33bd82012-07-12 16:25:02 +02006879 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6880 if (!ntrig) {
6881 err = -ENOMEM;
6882 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006883 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006884 cfg80211_rdev_free_wowlan(rdev);
6885 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006886
Johannes Bergae33bd82012-07-12 16:25:02 +02006887 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006888 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03006889 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02006890
Johannes Bergff1b6e62011-05-04 15:37:28 +02006891 return 0;
6892 error:
6893 for (i = 0; i < new_triggers.n_patterns; i++)
6894 kfree(new_triggers.patterns[i].mask);
6895 kfree(new_triggers.patterns);
6896 return err;
6897}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006898#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006899
Johannes Berge5497d72011-07-05 16:35:40 +02006900static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6901{
6902 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6903 struct net_device *dev = info->user_ptr[1];
6904 struct wireless_dev *wdev = dev->ieee80211_ptr;
6905 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6906 struct cfg80211_gtk_rekey_data rekey_data;
6907 int err;
6908
6909 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6910 return -EINVAL;
6911
6912 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6913 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6914 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6915 nl80211_rekey_policy);
6916 if (err)
6917 return err;
6918
6919 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6920 return -ERANGE;
6921 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6922 return -ERANGE;
6923 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6924 return -ERANGE;
6925
6926 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6927 NL80211_KEK_LEN);
6928 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6929 NL80211_KCK_LEN);
6930 memcpy(rekey_data.replay_ctr,
6931 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6932 NL80211_REPLAY_CTR_LEN);
6933
6934 wdev_lock(wdev);
6935 if (!wdev->current_bss) {
6936 err = -ENOTCONN;
6937 goto out;
6938 }
6939
6940 if (!rdev->ops->set_rekey_data) {
6941 err = -EOPNOTSUPP;
6942 goto out;
6943 }
6944
Hila Gonene35e4d22012-06-27 17:19:42 +03006945 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02006946 out:
6947 wdev_unlock(wdev);
6948 return err;
6949}
6950
Johannes Berg28946da2011-11-04 11:18:12 +01006951static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6952 struct genl_info *info)
6953{
6954 struct net_device *dev = info->user_ptr[1];
6955 struct wireless_dev *wdev = dev->ieee80211_ptr;
6956
6957 if (wdev->iftype != NL80211_IFTYPE_AP &&
6958 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6959 return -EINVAL;
6960
Eric W. Biederman15e47302012-09-07 20:12:54 +00006961 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006962 return -EBUSY;
6963
Eric W. Biederman15e47302012-09-07 20:12:54 +00006964 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006965 return 0;
6966}
6967
Johannes Berg7f6cf312011-11-04 11:18:15 +01006968static int nl80211_probe_client(struct sk_buff *skb,
6969 struct genl_info *info)
6970{
6971 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6972 struct net_device *dev = info->user_ptr[1];
6973 struct wireless_dev *wdev = dev->ieee80211_ptr;
6974 struct sk_buff *msg;
6975 void *hdr;
6976 const u8 *addr;
6977 u64 cookie;
6978 int err;
6979
6980 if (wdev->iftype != NL80211_IFTYPE_AP &&
6981 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6982 return -EOPNOTSUPP;
6983
6984 if (!info->attrs[NL80211_ATTR_MAC])
6985 return -EINVAL;
6986
6987 if (!rdev->ops->probe_client)
6988 return -EOPNOTSUPP;
6989
6990 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6991 if (!msg)
6992 return -ENOMEM;
6993
Eric W. Biederman15e47302012-09-07 20:12:54 +00006994 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01006995 NL80211_CMD_PROBE_CLIENT);
6996
6997 if (IS_ERR(hdr)) {
6998 err = PTR_ERR(hdr);
6999 goto free_msg;
7000 }
7001
7002 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7003
Hila Gonene35e4d22012-06-27 17:19:42 +03007004 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007005 if (err)
7006 goto free_msg;
7007
David S. Miller9360ffd2012-03-29 04:41:26 -04007008 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7009 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007010
7011 genlmsg_end(msg, hdr);
7012
7013 return genlmsg_reply(msg, info);
7014
7015 nla_put_failure:
7016 err = -ENOBUFS;
7017 free_msg:
7018 nlmsg_free(msg);
7019 return err;
7020}
7021
Johannes Berg5e760232011-11-04 11:18:17 +01007022static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7023{
7024 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007025 struct cfg80211_beacon_registration *reg, *nreg;
7026 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007027
7028 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7029 return -EOPNOTSUPP;
7030
Ben Greear37c73b52012-10-26 14:49:25 -07007031 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7032 if (!nreg)
7033 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007034
Ben Greear37c73b52012-10-26 14:49:25 -07007035 /* First, check if already registered. */
7036 spin_lock_bh(&rdev->beacon_registrations_lock);
7037 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7038 if (reg->nlportid == info->snd_portid) {
7039 rv = -EALREADY;
7040 goto out_err;
7041 }
7042 }
7043 /* Add it to the list */
7044 nreg->nlportid = info->snd_portid;
7045 list_add(&nreg->list, &rdev->beacon_registrations);
7046
7047 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007048
7049 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007050out_err:
7051 spin_unlock_bh(&rdev->beacon_registrations_lock);
7052 kfree(nreg);
7053 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007054}
7055
Johannes Berg98104fde2012-06-16 00:19:54 +02007056static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7057{
7058 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7059 struct wireless_dev *wdev = info->user_ptr[1];
7060 int err;
7061
7062 if (!rdev->ops->start_p2p_device)
7063 return -EOPNOTSUPP;
7064
7065 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7066 return -EOPNOTSUPP;
7067
7068 if (wdev->p2p_started)
7069 return 0;
7070
7071 mutex_lock(&rdev->devlist_mtx);
7072 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7073 mutex_unlock(&rdev->devlist_mtx);
7074 if (err)
7075 return err;
7076
Johannes Bergeeb126e2012-10-23 15:16:50 +02007077 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007078 if (err)
7079 return err;
7080
7081 wdev->p2p_started = true;
7082 mutex_lock(&rdev->devlist_mtx);
7083 rdev->opencount++;
7084 mutex_unlock(&rdev->devlist_mtx);
7085
7086 return 0;
7087}
7088
7089static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7090{
7091 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7092 struct wireless_dev *wdev = info->user_ptr[1];
7093
7094 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7095 return -EOPNOTSUPP;
7096
7097 if (!rdev->ops->stop_p2p_device)
7098 return -EOPNOTSUPP;
7099
7100 if (!wdev->p2p_started)
7101 return 0;
7102
Johannes Bergeeb126e2012-10-23 15:16:50 +02007103 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007104 wdev->p2p_started = false;
7105
7106 mutex_lock(&rdev->devlist_mtx);
7107 rdev->opencount--;
7108 mutex_unlock(&rdev->devlist_mtx);
7109
7110 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7111 rdev->scan_req->aborted = true;
7112 ___cfg80211_scan_done(rdev, true);
7113 }
7114
7115 return 0;
7116}
7117
Johannes Berg4c476992010-10-04 21:36:35 +02007118#define NL80211_FLAG_NEED_WIPHY 0x01
7119#define NL80211_FLAG_NEED_NETDEV 0x02
7120#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007121#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7122#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7123 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007124#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007125/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007126#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7127 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007128
7129static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7130 struct genl_info *info)
7131{
7132 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007133 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007134 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007135 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7136
7137 if (rtnl)
7138 rtnl_lock();
7139
7140 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007141 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007142 if (IS_ERR(rdev)) {
7143 if (rtnl)
7144 rtnl_unlock();
7145 return PTR_ERR(rdev);
7146 }
7147 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007148 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7149 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007150 mutex_lock(&cfg80211_mutex);
7151 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7152 info->attrs);
7153 if (IS_ERR(wdev)) {
7154 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007155 if (rtnl)
7156 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007157 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007158 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007159
Johannes Berg89a54e42012-06-15 14:33:17 +02007160 dev = wdev->netdev;
7161 rdev = wiphy_to_dev(wdev->wiphy);
7162
Johannes Berg1bf614e2012-06-15 15:23:36 +02007163 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7164 if (!dev) {
7165 mutex_unlock(&cfg80211_mutex);
7166 if (rtnl)
7167 rtnl_unlock();
7168 return -EINVAL;
7169 }
7170
7171 info->user_ptr[1] = dev;
7172 } else {
7173 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007174 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007175
Johannes Berg1bf614e2012-06-15 15:23:36 +02007176 if (dev) {
7177 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7178 !netif_running(dev)) {
7179 mutex_unlock(&cfg80211_mutex);
7180 if (rtnl)
7181 rtnl_unlock();
7182 return -ENETDOWN;
7183 }
7184
7185 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007186 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7187 if (!wdev->p2p_started) {
7188 mutex_unlock(&cfg80211_mutex);
7189 if (rtnl)
7190 rtnl_unlock();
7191 return -ENETDOWN;
7192 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007193 }
7194
Johannes Berg89a54e42012-06-15 14:33:17 +02007195 cfg80211_lock_rdev(rdev);
7196
7197 mutex_unlock(&cfg80211_mutex);
7198
Johannes Berg4c476992010-10-04 21:36:35 +02007199 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007200 }
7201
7202 return 0;
7203}
7204
7205static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7206 struct genl_info *info)
7207{
7208 if (info->user_ptr[0])
7209 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007210 if (info->user_ptr[1]) {
7211 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7212 struct wireless_dev *wdev = info->user_ptr[1];
7213
7214 if (wdev->netdev)
7215 dev_put(wdev->netdev);
7216 } else {
7217 dev_put(info->user_ptr[1]);
7218 }
7219 }
Johannes Berg4c476992010-10-04 21:36:35 +02007220 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7221 rtnl_unlock();
7222}
7223
Johannes Berg55682962007-09-20 13:09:35 -04007224static struct genl_ops nl80211_ops[] = {
7225 {
7226 .cmd = NL80211_CMD_GET_WIPHY,
7227 .doit = nl80211_get_wiphy,
7228 .dumpit = nl80211_dump_wiphy,
7229 .policy = nl80211_policy,
7230 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007231 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007232 },
7233 {
7234 .cmd = NL80211_CMD_SET_WIPHY,
7235 .doit = nl80211_set_wiphy,
7236 .policy = nl80211_policy,
7237 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007238 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007239 },
7240 {
7241 .cmd = NL80211_CMD_GET_INTERFACE,
7242 .doit = nl80211_get_interface,
7243 .dumpit = nl80211_dump_interface,
7244 .policy = nl80211_policy,
7245 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007246 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007247 },
7248 {
7249 .cmd = NL80211_CMD_SET_INTERFACE,
7250 .doit = nl80211_set_interface,
7251 .policy = nl80211_policy,
7252 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007253 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7254 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007255 },
7256 {
7257 .cmd = NL80211_CMD_NEW_INTERFACE,
7258 .doit = nl80211_new_interface,
7259 .policy = nl80211_policy,
7260 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007261 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7262 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007263 },
7264 {
7265 .cmd = NL80211_CMD_DEL_INTERFACE,
7266 .doit = nl80211_del_interface,
7267 .policy = nl80211_policy,
7268 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007269 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007270 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007271 },
Johannes Berg41ade002007-12-19 02:03:29 +01007272 {
7273 .cmd = NL80211_CMD_GET_KEY,
7274 .doit = nl80211_get_key,
7275 .policy = nl80211_policy,
7276 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007277 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007278 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007279 },
7280 {
7281 .cmd = NL80211_CMD_SET_KEY,
7282 .doit = nl80211_set_key,
7283 .policy = nl80211_policy,
7284 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007285 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007286 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007287 },
7288 {
7289 .cmd = NL80211_CMD_NEW_KEY,
7290 .doit = nl80211_new_key,
7291 .policy = nl80211_policy,
7292 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007293 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007294 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007295 },
7296 {
7297 .cmd = NL80211_CMD_DEL_KEY,
7298 .doit = nl80211_del_key,
7299 .policy = nl80211_policy,
7300 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007301 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007302 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007303 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007304 {
7305 .cmd = NL80211_CMD_SET_BEACON,
7306 .policy = nl80211_policy,
7307 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007308 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007309 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007310 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007311 },
7312 {
Johannes Berg88600202012-02-13 15:17:18 +01007313 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007314 .policy = nl80211_policy,
7315 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007316 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007317 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007318 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007319 },
7320 {
Johannes Berg88600202012-02-13 15:17:18 +01007321 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007322 .policy = nl80211_policy,
7323 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007324 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007325 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007326 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007327 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007328 {
7329 .cmd = NL80211_CMD_GET_STATION,
7330 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007331 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007332 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007333 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7334 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007335 },
7336 {
7337 .cmd = NL80211_CMD_SET_STATION,
7338 .doit = nl80211_set_station,
7339 .policy = nl80211_policy,
7340 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007341 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007342 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007343 },
7344 {
7345 .cmd = NL80211_CMD_NEW_STATION,
7346 .doit = nl80211_new_station,
7347 .policy = nl80211_policy,
7348 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007349 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007350 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007351 },
7352 {
7353 .cmd = NL80211_CMD_DEL_STATION,
7354 .doit = nl80211_del_station,
7355 .policy = nl80211_policy,
7356 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007357 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007358 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007359 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007360 {
7361 .cmd = NL80211_CMD_GET_MPATH,
7362 .doit = nl80211_get_mpath,
7363 .dumpit = nl80211_dump_mpath,
7364 .policy = nl80211_policy,
7365 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007366 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007367 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007368 },
7369 {
7370 .cmd = NL80211_CMD_SET_MPATH,
7371 .doit = nl80211_set_mpath,
7372 .policy = nl80211_policy,
7373 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007374 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007375 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007376 },
7377 {
7378 .cmd = NL80211_CMD_NEW_MPATH,
7379 .doit = nl80211_new_mpath,
7380 .policy = nl80211_policy,
7381 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007382 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007383 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007384 },
7385 {
7386 .cmd = NL80211_CMD_DEL_MPATH,
7387 .doit = nl80211_del_mpath,
7388 .policy = nl80211_policy,
7389 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007390 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007391 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007392 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007393 {
7394 .cmd = NL80211_CMD_SET_BSS,
7395 .doit = nl80211_set_bss,
7396 .policy = nl80211_policy,
7397 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007398 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007399 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007400 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007401 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007402 .cmd = NL80211_CMD_GET_REG,
7403 .doit = nl80211_get_reg,
7404 .policy = nl80211_policy,
7405 /* can be retrieved by unprivileged users */
7406 },
7407 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007408 .cmd = NL80211_CMD_SET_REG,
7409 .doit = nl80211_set_reg,
7410 .policy = nl80211_policy,
7411 .flags = GENL_ADMIN_PERM,
7412 },
7413 {
7414 .cmd = NL80211_CMD_REQ_SET_REG,
7415 .doit = nl80211_req_set_reg,
7416 .policy = nl80211_policy,
7417 .flags = GENL_ADMIN_PERM,
7418 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007419 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007420 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7421 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007422 .policy = nl80211_policy,
7423 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007424 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007425 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007426 },
7427 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007428 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7429 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007430 .policy = nl80211_policy,
7431 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007432 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007433 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007434 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007435 {
Johannes Berg2a519312009-02-10 21:25:55 +01007436 .cmd = NL80211_CMD_TRIGGER_SCAN,
7437 .doit = nl80211_trigger_scan,
7438 .policy = nl80211_policy,
7439 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007440 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007441 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007442 },
7443 {
7444 .cmd = NL80211_CMD_GET_SCAN,
7445 .policy = nl80211_policy,
7446 .dumpit = nl80211_dump_scan,
7447 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007448 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007449 .cmd = NL80211_CMD_START_SCHED_SCAN,
7450 .doit = nl80211_start_sched_scan,
7451 .policy = nl80211_policy,
7452 .flags = GENL_ADMIN_PERM,
7453 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7454 NL80211_FLAG_NEED_RTNL,
7455 },
7456 {
7457 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7458 .doit = nl80211_stop_sched_scan,
7459 .policy = nl80211_policy,
7460 .flags = GENL_ADMIN_PERM,
7461 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7462 NL80211_FLAG_NEED_RTNL,
7463 },
7464 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007465 .cmd = NL80211_CMD_AUTHENTICATE,
7466 .doit = nl80211_authenticate,
7467 .policy = nl80211_policy,
7468 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007469 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007470 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007471 },
7472 {
7473 .cmd = NL80211_CMD_ASSOCIATE,
7474 .doit = nl80211_associate,
7475 .policy = nl80211_policy,
7476 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007477 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007478 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007479 },
7480 {
7481 .cmd = NL80211_CMD_DEAUTHENTICATE,
7482 .doit = nl80211_deauthenticate,
7483 .policy = nl80211_policy,
7484 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007485 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007486 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007487 },
7488 {
7489 .cmd = NL80211_CMD_DISASSOCIATE,
7490 .doit = nl80211_disassociate,
7491 .policy = nl80211_policy,
7492 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007493 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007494 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007495 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007496 {
7497 .cmd = NL80211_CMD_JOIN_IBSS,
7498 .doit = nl80211_join_ibss,
7499 .policy = nl80211_policy,
7500 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007501 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007502 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007503 },
7504 {
7505 .cmd = NL80211_CMD_LEAVE_IBSS,
7506 .doit = nl80211_leave_ibss,
7507 .policy = nl80211_policy,
7508 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007509 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007510 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007511 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007512#ifdef CONFIG_NL80211_TESTMODE
7513 {
7514 .cmd = NL80211_CMD_TESTMODE,
7515 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007516 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007517 .policy = nl80211_policy,
7518 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007519 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7520 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007521 },
7522#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007523 {
7524 .cmd = NL80211_CMD_CONNECT,
7525 .doit = nl80211_connect,
7526 .policy = nl80211_policy,
7527 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007528 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007529 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007530 },
7531 {
7532 .cmd = NL80211_CMD_DISCONNECT,
7533 .doit = nl80211_disconnect,
7534 .policy = nl80211_policy,
7535 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007536 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007537 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007538 },
Johannes Berg463d0182009-07-14 00:33:35 +02007539 {
7540 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7541 .doit = nl80211_wiphy_netns,
7542 .policy = nl80211_policy,
7543 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007544 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7545 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007546 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007547 {
7548 .cmd = NL80211_CMD_GET_SURVEY,
7549 .policy = nl80211_policy,
7550 .dumpit = nl80211_dump_survey,
7551 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007552 {
7553 .cmd = NL80211_CMD_SET_PMKSA,
7554 .doit = nl80211_setdel_pmksa,
7555 .policy = nl80211_policy,
7556 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007557 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007558 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007559 },
7560 {
7561 .cmd = NL80211_CMD_DEL_PMKSA,
7562 .doit = nl80211_setdel_pmksa,
7563 .policy = nl80211_policy,
7564 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007565 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007566 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007567 },
7568 {
7569 .cmd = NL80211_CMD_FLUSH_PMKSA,
7570 .doit = nl80211_flush_pmksa,
7571 .policy = nl80211_policy,
7572 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007573 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007574 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007575 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007576 {
7577 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7578 .doit = nl80211_remain_on_channel,
7579 .policy = nl80211_policy,
7580 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007581 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007582 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007583 },
7584 {
7585 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7586 .doit = nl80211_cancel_remain_on_channel,
7587 .policy = nl80211_policy,
7588 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007589 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007590 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007591 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007592 {
7593 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7594 .doit = nl80211_set_tx_bitrate_mask,
7595 .policy = nl80211_policy,
7596 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007597 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7598 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007599 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007600 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007601 .cmd = NL80211_CMD_REGISTER_FRAME,
7602 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007603 .policy = nl80211_policy,
7604 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007605 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007606 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007607 },
7608 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007609 .cmd = NL80211_CMD_FRAME,
7610 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007611 .policy = nl80211_policy,
7612 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007613 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007614 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007615 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007616 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007617 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7618 .doit = nl80211_tx_mgmt_cancel_wait,
7619 .policy = nl80211_policy,
7620 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007621 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007622 NL80211_FLAG_NEED_RTNL,
7623 },
7624 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007625 .cmd = NL80211_CMD_SET_POWER_SAVE,
7626 .doit = nl80211_set_power_save,
7627 .policy = nl80211_policy,
7628 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007629 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7630 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007631 },
7632 {
7633 .cmd = NL80211_CMD_GET_POWER_SAVE,
7634 .doit = nl80211_get_power_save,
7635 .policy = nl80211_policy,
7636 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007637 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7638 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007639 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007640 {
7641 .cmd = NL80211_CMD_SET_CQM,
7642 .doit = nl80211_set_cqm,
7643 .policy = nl80211_policy,
7644 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007645 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7646 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007647 },
Johannes Bergf444de02010-05-05 15:25:02 +02007648 {
7649 .cmd = NL80211_CMD_SET_CHANNEL,
7650 .doit = nl80211_set_channel,
7651 .policy = nl80211_policy,
7652 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007653 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7654 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007655 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007656 {
7657 .cmd = NL80211_CMD_SET_WDS_PEER,
7658 .doit = nl80211_set_wds_peer,
7659 .policy = nl80211_policy,
7660 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007661 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7662 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007663 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007664 {
7665 .cmd = NL80211_CMD_JOIN_MESH,
7666 .doit = nl80211_join_mesh,
7667 .policy = nl80211_policy,
7668 .flags = GENL_ADMIN_PERM,
7669 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7670 NL80211_FLAG_NEED_RTNL,
7671 },
7672 {
7673 .cmd = NL80211_CMD_LEAVE_MESH,
7674 .doit = nl80211_leave_mesh,
7675 .policy = nl80211_policy,
7676 .flags = GENL_ADMIN_PERM,
7677 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7678 NL80211_FLAG_NEED_RTNL,
7679 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007680#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007681 {
7682 .cmd = NL80211_CMD_GET_WOWLAN,
7683 .doit = nl80211_get_wowlan,
7684 .policy = nl80211_policy,
7685 /* can be retrieved by unprivileged users */
7686 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7687 NL80211_FLAG_NEED_RTNL,
7688 },
7689 {
7690 .cmd = NL80211_CMD_SET_WOWLAN,
7691 .doit = nl80211_set_wowlan,
7692 .policy = nl80211_policy,
7693 .flags = GENL_ADMIN_PERM,
7694 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7695 NL80211_FLAG_NEED_RTNL,
7696 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007697#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007698 {
7699 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7700 .doit = nl80211_set_rekey_data,
7701 .policy = nl80211_policy,
7702 .flags = GENL_ADMIN_PERM,
7703 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7704 NL80211_FLAG_NEED_RTNL,
7705 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007706 {
7707 .cmd = NL80211_CMD_TDLS_MGMT,
7708 .doit = nl80211_tdls_mgmt,
7709 .policy = nl80211_policy,
7710 .flags = GENL_ADMIN_PERM,
7711 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7712 NL80211_FLAG_NEED_RTNL,
7713 },
7714 {
7715 .cmd = NL80211_CMD_TDLS_OPER,
7716 .doit = nl80211_tdls_oper,
7717 .policy = nl80211_policy,
7718 .flags = GENL_ADMIN_PERM,
7719 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7720 NL80211_FLAG_NEED_RTNL,
7721 },
Johannes Berg28946da2011-11-04 11:18:12 +01007722 {
7723 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7724 .doit = nl80211_register_unexpected_frame,
7725 .policy = nl80211_policy,
7726 .flags = GENL_ADMIN_PERM,
7727 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7728 NL80211_FLAG_NEED_RTNL,
7729 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007730 {
7731 .cmd = NL80211_CMD_PROBE_CLIENT,
7732 .doit = nl80211_probe_client,
7733 .policy = nl80211_policy,
7734 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007735 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007736 NL80211_FLAG_NEED_RTNL,
7737 },
Johannes Berg5e760232011-11-04 11:18:17 +01007738 {
7739 .cmd = NL80211_CMD_REGISTER_BEACONS,
7740 .doit = nl80211_register_beacons,
7741 .policy = nl80211_policy,
7742 .flags = GENL_ADMIN_PERM,
7743 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7744 NL80211_FLAG_NEED_RTNL,
7745 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007746 {
7747 .cmd = NL80211_CMD_SET_NOACK_MAP,
7748 .doit = nl80211_set_noack_map,
7749 .policy = nl80211_policy,
7750 .flags = GENL_ADMIN_PERM,
7751 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7752 NL80211_FLAG_NEED_RTNL,
7753 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007754 {
7755 .cmd = NL80211_CMD_START_P2P_DEVICE,
7756 .doit = nl80211_start_p2p_device,
7757 .policy = nl80211_policy,
7758 .flags = GENL_ADMIN_PERM,
7759 .internal_flags = NL80211_FLAG_NEED_WDEV |
7760 NL80211_FLAG_NEED_RTNL,
7761 },
7762 {
7763 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7764 .doit = nl80211_stop_p2p_device,
7765 .policy = nl80211_policy,
7766 .flags = GENL_ADMIN_PERM,
7767 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7768 NL80211_FLAG_NEED_RTNL,
7769 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007770 {
7771 .cmd = NL80211_CMD_SET_MCAST_RATE,
7772 .doit = nl80211_set_mcast_rate,
7773 .policy = nl80211_policy,
7774 .flags = GENL_ADMIN_PERM,
7775 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7776 NL80211_FLAG_NEED_RTNL,
7777 },
Johannes Berg55682962007-09-20 13:09:35 -04007778};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007779
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007780static struct genl_multicast_group nl80211_mlme_mcgrp = {
7781 .name = "mlme",
7782};
Johannes Berg55682962007-09-20 13:09:35 -04007783
7784/* multicast groups */
7785static struct genl_multicast_group nl80211_config_mcgrp = {
7786 .name = "config",
7787};
Johannes Berg2a519312009-02-10 21:25:55 +01007788static struct genl_multicast_group nl80211_scan_mcgrp = {
7789 .name = "scan",
7790};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007791static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7792 .name = "regulatory",
7793};
Johannes Berg55682962007-09-20 13:09:35 -04007794
7795/* notification functions */
7796
7797void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7798{
7799 struct sk_buff *msg;
7800
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007801 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007802 if (!msg)
7803 return;
7804
7805 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7806 nlmsg_free(msg);
7807 return;
7808 }
7809
Johannes Berg463d0182009-07-14 00:33:35 +02007810 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7811 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007812}
7813
Johannes Berg362a4152009-05-24 16:43:15 +02007814static int nl80211_add_scan_req(struct sk_buff *msg,
7815 struct cfg80211_registered_device *rdev)
7816{
7817 struct cfg80211_scan_request *req = rdev->scan_req;
7818 struct nlattr *nest;
7819 int i;
7820
Johannes Berg667503dd2009-07-07 03:56:11 +02007821 ASSERT_RDEV_LOCK(rdev);
7822
Johannes Berg362a4152009-05-24 16:43:15 +02007823 if (WARN_ON(!req))
7824 return 0;
7825
7826 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7827 if (!nest)
7828 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007829 for (i = 0; i < req->n_ssids; i++) {
7830 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7831 goto nla_put_failure;
7832 }
Johannes Berg362a4152009-05-24 16:43:15 +02007833 nla_nest_end(msg, nest);
7834
7835 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7836 if (!nest)
7837 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007838 for (i = 0; i < req->n_channels; i++) {
7839 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7840 goto nla_put_failure;
7841 }
Johannes Berg362a4152009-05-24 16:43:15 +02007842 nla_nest_end(msg, nest);
7843
David S. Miller9360ffd2012-03-29 04:41:26 -04007844 if (req->ie &&
7845 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7846 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007847
Sam Lefflered4737712012-10-11 21:03:31 -07007848 if (req->flags)
7849 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7850
Johannes Berg362a4152009-05-24 16:43:15 +02007851 return 0;
7852 nla_put_failure:
7853 return -ENOBUFS;
7854}
7855
Johannes Berga538e2d2009-06-16 19:56:42 +02007856static int nl80211_send_scan_msg(struct sk_buff *msg,
7857 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007858 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007859 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007860 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007861{
7862 void *hdr;
7863
Eric W. Biederman15e47302012-09-07 20:12:54 +00007864 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007865 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) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007869 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7870 wdev->netdev->ifindex)) ||
7871 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007872 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007873
Johannes Berg362a4152009-05-24 16:43:15 +02007874 /* ignore errors and send incomplete event anyway */
7875 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007876
7877 return genlmsg_end(msg, hdr);
7878
7879 nla_put_failure:
7880 genlmsg_cancel(msg, hdr);
7881 return -EMSGSIZE;
7882}
7883
Luciano Coelho807f8a82011-05-11 17:09:35 +03007884static int
7885nl80211_send_sched_scan_msg(struct sk_buff *msg,
7886 struct cfg80211_registered_device *rdev,
7887 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007888 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007889{
7890 void *hdr;
7891
Eric W. Biederman15e47302012-09-07 20:12:54 +00007892 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007893 if (!hdr)
7894 return -1;
7895
David S. Miller9360ffd2012-03-29 04:41:26 -04007896 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7897 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7898 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007899
7900 return genlmsg_end(msg, hdr);
7901
7902 nla_put_failure:
7903 genlmsg_cancel(msg, hdr);
7904 return -EMSGSIZE;
7905}
7906
Johannes Berga538e2d2009-06-16 19:56:42 +02007907void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007908 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007909{
7910 struct sk_buff *msg;
7911
Thomas Graf58050fc2012-06-28 03:57:45 +00007912 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007913 if (!msg)
7914 return;
7915
Johannes Bergfd014282012-06-18 19:17:03 +02007916 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007917 NL80211_CMD_TRIGGER_SCAN) < 0) {
7918 nlmsg_free(msg);
7919 return;
7920 }
7921
Johannes Berg463d0182009-07-14 00:33:35 +02007922 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7923 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007924}
7925
Johannes Berg2a519312009-02-10 21:25:55 +01007926void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007927 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007928{
7929 struct sk_buff *msg;
7930
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007931 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007932 if (!msg)
7933 return;
7934
Johannes Bergfd014282012-06-18 19:17:03 +02007935 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007936 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007937 nlmsg_free(msg);
7938 return;
7939 }
7940
Johannes Berg463d0182009-07-14 00:33:35 +02007941 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7942 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007943}
7944
7945void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007946 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007947{
7948 struct sk_buff *msg;
7949
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007950 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007951 if (!msg)
7952 return;
7953
Johannes Bergfd014282012-06-18 19:17:03 +02007954 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007955 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007956 nlmsg_free(msg);
7957 return;
7958 }
7959
Johannes Berg463d0182009-07-14 00:33:35 +02007960 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7961 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007962}
7963
Luciano Coelho807f8a82011-05-11 17:09:35 +03007964void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7965 struct net_device *netdev)
7966{
7967 struct sk_buff *msg;
7968
7969 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7970 if (!msg)
7971 return;
7972
7973 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7974 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7975 nlmsg_free(msg);
7976 return;
7977 }
7978
7979 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7980 nl80211_scan_mcgrp.id, GFP_KERNEL);
7981}
7982
7983void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7984 struct net_device *netdev, u32 cmd)
7985{
7986 struct sk_buff *msg;
7987
Thomas Graf58050fc2012-06-28 03:57:45 +00007988 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007989 if (!msg)
7990 return;
7991
7992 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7993 nlmsg_free(msg);
7994 return;
7995 }
7996
7997 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7998 nl80211_scan_mcgrp.id, GFP_KERNEL);
7999}
8000
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008001/*
8002 * This can happen on global regulatory changes or device specific settings
8003 * based on custom world regulatory domains.
8004 */
8005void nl80211_send_reg_change_event(struct regulatory_request *request)
8006{
8007 struct sk_buff *msg;
8008 void *hdr;
8009
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008010 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008011 if (!msg)
8012 return;
8013
8014 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8015 if (!hdr) {
8016 nlmsg_free(msg);
8017 return;
8018 }
8019
8020 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008021 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8022 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008023
David S. Miller9360ffd2012-03-29 04:41:26 -04008024 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8025 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8026 NL80211_REGDOM_TYPE_WORLD))
8027 goto nla_put_failure;
8028 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8029 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8030 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8031 goto nla_put_failure;
8032 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8033 request->intersect) {
8034 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8035 NL80211_REGDOM_TYPE_INTERSECTION))
8036 goto nla_put_failure;
8037 } else {
8038 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8039 NL80211_REGDOM_TYPE_COUNTRY) ||
8040 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8041 request->alpha2))
8042 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008043 }
8044
David S. Miller9360ffd2012-03-29 04:41:26 -04008045 if (wiphy_idx_valid(request->wiphy_idx) &&
8046 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8047 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008048
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008049 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008050
Johannes Bergbc43b282009-07-25 10:54:13 +02008051 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008052 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008053 GFP_ATOMIC);
8054 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008055
8056 return;
8057
8058nla_put_failure:
8059 genlmsg_cancel(msg, hdr);
8060 nlmsg_free(msg);
8061}
8062
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008063static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8064 struct net_device *netdev,
8065 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008066 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008067{
8068 struct sk_buff *msg;
8069 void *hdr;
8070
Johannes Berge6d6e342009-07-01 21:26:47 +02008071 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008072 if (!msg)
8073 return;
8074
8075 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8076 if (!hdr) {
8077 nlmsg_free(msg);
8078 return;
8079 }
8080
David S. Miller9360ffd2012-03-29 04:41:26 -04008081 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8082 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8083 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8084 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008085
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008086 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008087
Johannes Berg463d0182009-07-14 00:33:35 +02008088 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8089 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008090 return;
8091
8092 nla_put_failure:
8093 genlmsg_cancel(msg, hdr);
8094 nlmsg_free(msg);
8095}
8096
8097void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008098 struct net_device *netdev, const u8 *buf,
8099 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008100{
8101 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008102 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008103}
8104
8105void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8106 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008107 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008108{
Johannes Berge6d6e342009-07-01 21:26:47 +02008109 nl80211_send_mlme_event(rdev, netdev, buf, len,
8110 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008111}
8112
Jouni Malinen53b46b82009-03-27 20:53:56 +02008113void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008114 struct net_device *netdev, const u8 *buf,
8115 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008116{
8117 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008118 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008119}
8120
Jouni Malinen53b46b82009-03-27 20:53:56 +02008121void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8122 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008123 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008124{
8125 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008126 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008127}
8128
Jouni Malinencf4e5942010-12-16 00:52:40 +02008129void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8130 struct net_device *netdev, const u8 *buf,
8131 size_t len, gfp_t gfp)
8132{
8133 nl80211_send_mlme_event(rdev, netdev, buf, len,
8134 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8135}
8136
8137void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8138 struct net_device *netdev, const u8 *buf,
8139 size_t len, gfp_t gfp)
8140{
8141 nl80211_send_mlme_event(rdev, netdev, buf, len,
8142 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8143}
8144
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008145static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8146 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008147 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008148{
8149 struct sk_buff *msg;
8150 void *hdr;
8151
Johannes Berge6d6e342009-07-01 21:26:47 +02008152 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008153 if (!msg)
8154 return;
8155
8156 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8157 if (!hdr) {
8158 nlmsg_free(msg);
8159 return;
8160 }
8161
David S. Miller9360ffd2012-03-29 04:41:26 -04008162 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8163 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8164 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8165 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8166 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008167
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008168 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008169
Johannes Berg463d0182009-07-14 00:33:35 +02008170 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8171 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008172 return;
8173
8174 nla_put_failure:
8175 genlmsg_cancel(msg, hdr);
8176 nlmsg_free(msg);
8177}
8178
8179void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008180 struct net_device *netdev, const u8 *addr,
8181 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008182{
8183 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008184 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008185}
8186
8187void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008188 struct net_device *netdev, const u8 *addr,
8189 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008190{
Johannes Berge6d6e342009-07-01 21:26:47 +02008191 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8192 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008193}
8194
Samuel Ortizb23aa672009-07-01 21:26:54 +02008195void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8196 struct net_device *netdev, const u8 *bssid,
8197 const u8 *req_ie, size_t req_ie_len,
8198 const u8 *resp_ie, size_t resp_ie_len,
8199 u16 status, gfp_t gfp)
8200{
8201 struct sk_buff *msg;
8202 void *hdr;
8203
Thomas Graf58050fc2012-06-28 03:57:45 +00008204 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008205 if (!msg)
8206 return;
8207
8208 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8209 if (!hdr) {
8210 nlmsg_free(msg);
8211 return;
8212 }
8213
David S. Miller9360ffd2012-03-29 04:41:26 -04008214 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8215 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8216 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8217 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8218 (req_ie &&
8219 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8220 (resp_ie &&
8221 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8222 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008223
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008224 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008225
Johannes Berg463d0182009-07-14 00:33:35 +02008226 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8227 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008228 return;
8229
8230 nla_put_failure:
8231 genlmsg_cancel(msg, hdr);
8232 nlmsg_free(msg);
8233
8234}
8235
8236void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8237 struct net_device *netdev, const u8 *bssid,
8238 const u8 *req_ie, size_t req_ie_len,
8239 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8240{
8241 struct sk_buff *msg;
8242 void *hdr;
8243
Thomas Graf58050fc2012-06-28 03:57:45 +00008244 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008245 if (!msg)
8246 return;
8247
8248 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8249 if (!hdr) {
8250 nlmsg_free(msg);
8251 return;
8252 }
8253
David S. Miller9360ffd2012-03-29 04:41:26 -04008254 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8255 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8256 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8257 (req_ie &&
8258 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8259 (resp_ie &&
8260 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8261 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008262
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008263 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008264
Johannes Berg463d0182009-07-14 00:33:35 +02008265 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8266 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008267 return;
8268
8269 nla_put_failure:
8270 genlmsg_cancel(msg, hdr);
8271 nlmsg_free(msg);
8272
8273}
8274
8275void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8276 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008277 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008278{
8279 struct sk_buff *msg;
8280 void *hdr;
8281
Thomas Graf58050fc2012-06-28 03:57:45 +00008282 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008283 if (!msg)
8284 return;
8285
8286 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8287 if (!hdr) {
8288 nlmsg_free(msg);
8289 return;
8290 }
8291
David S. Miller9360ffd2012-03-29 04:41:26 -04008292 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8293 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8294 (from_ap && reason &&
8295 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8296 (from_ap &&
8297 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8298 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8299 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008300
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008301 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008302
Johannes Berg463d0182009-07-14 00:33:35 +02008303 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8304 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008305 return;
8306
8307 nla_put_failure:
8308 genlmsg_cancel(msg, hdr);
8309 nlmsg_free(msg);
8310
8311}
8312
Johannes Berg04a773a2009-04-19 21:24:32 +02008313void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8314 struct net_device *netdev, const u8 *bssid,
8315 gfp_t gfp)
8316{
8317 struct sk_buff *msg;
8318 void *hdr;
8319
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008320 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008321 if (!msg)
8322 return;
8323
8324 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8325 if (!hdr) {
8326 nlmsg_free(msg);
8327 return;
8328 }
8329
David S. Miller9360ffd2012-03-29 04:41:26 -04008330 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8331 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8332 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8333 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008334
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008335 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008336
Johannes Berg463d0182009-07-14 00:33:35 +02008337 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8338 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008339 return;
8340
8341 nla_put_failure:
8342 genlmsg_cancel(msg, hdr);
8343 nlmsg_free(msg);
8344}
8345
Javier Cardonac93b5e72011-04-07 15:08:34 -07008346void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8347 struct net_device *netdev,
8348 const u8 *macaddr, const u8* ie, u8 ie_len,
8349 gfp_t gfp)
8350{
8351 struct sk_buff *msg;
8352 void *hdr;
8353
8354 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8355 if (!msg)
8356 return;
8357
8358 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8359 if (!hdr) {
8360 nlmsg_free(msg);
8361 return;
8362 }
8363
David S. Miller9360ffd2012-03-29 04:41:26 -04008364 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8365 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8366 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8367 (ie_len && ie &&
8368 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8369 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008370
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008371 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008372
8373 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8374 nl80211_mlme_mcgrp.id, gfp);
8375 return;
8376
8377 nla_put_failure:
8378 genlmsg_cancel(msg, hdr);
8379 nlmsg_free(msg);
8380}
8381
Jouni Malinena3b8b052009-03-27 21:59:49 +02008382void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8383 struct net_device *netdev, const u8 *addr,
8384 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008385 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008386{
8387 struct sk_buff *msg;
8388 void *hdr;
8389
Johannes Berge6d6e342009-07-01 21:26:47 +02008390 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008391 if (!msg)
8392 return;
8393
8394 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8395 if (!hdr) {
8396 nlmsg_free(msg);
8397 return;
8398 }
8399
David S. Miller9360ffd2012-03-29 04:41:26 -04008400 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8401 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8402 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8403 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8404 (key_id != -1 &&
8405 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8406 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8407 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008408
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008409 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008410
Johannes Berg463d0182009-07-14 00:33:35 +02008411 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8412 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008413 return;
8414
8415 nla_put_failure:
8416 genlmsg_cancel(msg, hdr);
8417 nlmsg_free(msg);
8418}
8419
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008420void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8421 struct ieee80211_channel *channel_before,
8422 struct ieee80211_channel *channel_after)
8423{
8424 struct sk_buff *msg;
8425 void *hdr;
8426 struct nlattr *nl_freq;
8427
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008428 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008429 if (!msg)
8430 return;
8431
8432 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8433 if (!hdr) {
8434 nlmsg_free(msg);
8435 return;
8436 }
8437
8438 /*
8439 * Since we are applying the beacon hint to a wiphy we know its
8440 * wiphy_idx is valid
8441 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008442 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8443 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008444
8445 /* Before */
8446 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8447 if (!nl_freq)
8448 goto nla_put_failure;
8449 if (nl80211_msg_put_channel(msg, channel_before))
8450 goto nla_put_failure;
8451 nla_nest_end(msg, nl_freq);
8452
8453 /* After */
8454 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8455 if (!nl_freq)
8456 goto nla_put_failure;
8457 if (nl80211_msg_put_channel(msg, channel_after))
8458 goto nla_put_failure;
8459 nla_nest_end(msg, nl_freq);
8460
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008461 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008462
Johannes Berg463d0182009-07-14 00:33:35 +02008463 rcu_read_lock();
8464 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8465 GFP_ATOMIC);
8466 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008467
8468 return;
8469
8470nla_put_failure:
8471 genlmsg_cancel(msg, hdr);
8472 nlmsg_free(msg);
8473}
8474
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008475static void nl80211_send_remain_on_chan_event(
8476 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008477 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008478 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008479 unsigned int duration, gfp_t gfp)
8480{
8481 struct sk_buff *msg;
8482 void *hdr;
8483
8484 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8485 if (!msg)
8486 return;
8487
8488 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8489 if (!hdr) {
8490 nlmsg_free(msg);
8491 return;
8492 }
8493
David S. Miller9360ffd2012-03-29 04:41:26 -04008494 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008495 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8496 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008497 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008498 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01008499 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
8500 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008501 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8502 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008503
David S. Miller9360ffd2012-03-29 04:41:26 -04008504 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8505 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8506 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008507
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008508 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008509
8510 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8511 nl80211_mlme_mcgrp.id, gfp);
8512 return;
8513
8514 nla_put_failure:
8515 genlmsg_cancel(msg, hdr);
8516 nlmsg_free(msg);
8517}
8518
8519void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008520 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008521 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008522 unsigned int duration, gfp_t gfp)
8523{
8524 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008525 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01008526 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008527}
8528
8529void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008530 struct cfg80211_registered_device *rdev,
8531 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01008532 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008533{
8534 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01008535 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008536}
8537
Johannes Berg98b62182009-12-23 13:15:44 +01008538void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8539 struct net_device *dev, const u8 *mac_addr,
8540 struct station_info *sinfo, gfp_t gfp)
8541{
8542 struct sk_buff *msg;
8543
Thomas Graf58050fc2012-06-28 03:57:45 +00008544 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008545 if (!msg)
8546 return;
8547
John W. Linville66266b32012-03-15 13:25:41 -04008548 if (nl80211_send_station(msg, 0, 0, 0,
8549 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008550 nlmsg_free(msg);
8551 return;
8552 }
8553
8554 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8555 nl80211_mlme_mcgrp.id, gfp);
8556}
8557
Jouni Malinenec15e682011-03-23 15:29:52 +02008558void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8559 struct net_device *dev, const u8 *mac_addr,
8560 gfp_t gfp)
8561{
8562 struct sk_buff *msg;
8563 void *hdr;
8564
Thomas Graf58050fc2012-06-28 03:57:45 +00008565 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008566 if (!msg)
8567 return;
8568
8569 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8570 if (!hdr) {
8571 nlmsg_free(msg);
8572 return;
8573 }
8574
David S. Miller9360ffd2012-03-29 04:41:26 -04008575 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8576 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8577 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008578
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008579 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008580
8581 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8582 nl80211_mlme_mcgrp.id, gfp);
8583 return;
8584
8585 nla_put_failure:
8586 genlmsg_cancel(msg, hdr);
8587 nlmsg_free(msg);
8588}
8589
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308590void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8591 struct net_device *dev, const u8 *mac_addr,
8592 enum nl80211_connect_failed_reason reason,
8593 gfp_t gfp)
8594{
8595 struct sk_buff *msg;
8596 void *hdr;
8597
8598 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8599 if (!msg)
8600 return;
8601
8602 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8603 if (!hdr) {
8604 nlmsg_free(msg);
8605 return;
8606 }
8607
8608 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8609 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8610 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8611 goto nla_put_failure;
8612
8613 genlmsg_end(msg, hdr);
8614
8615 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8616 nl80211_mlme_mcgrp.id, gfp);
8617 return;
8618
8619 nla_put_failure:
8620 genlmsg_cancel(msg, hdr);
8621 nlmsg_free(msg);
8622}
8623
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008624static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8625 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008626{
8627 struct wireless_dev *wdev = dev->ieee80211_ptr;
8628 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8629 struct sk_buff *msg;
8630 void *hdr;
8631 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008632 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008633
Eric W. Biederman15e47302012-09-07 20:12:54 +00008634 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008635 return false;
8636
8637 msg = nlmsg_new(100, gfp);
8638 if (!msg)
8639 return true;
8640
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008641 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008642 if (!hdr) {
8643 nlmsg_free(msg);
8644 return true;
8645 }
8646
David S. Miller9360ffd2012-03-29 04:41:26 -04008647 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8648 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8649 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8650 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008651
8652 err = genlmsg_end(msg, hdr);
8653 if (err < 0) {
8654 nlmsg_free(msg);
8655 return true;
8656 }
8657
Eric W. Biederman15e47302012-09-07 20:12:54 +00008658 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008659 return true;
8660
8661 nla_put_failure:
8662 genlmsg_cancel(msg, hdr);
8663 nlmsg_free(msg);
8664 return true;
8665}
8666
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008667bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8668{
8669 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8670 addr, gfp);
8671}
8672
8673bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8674 const u8 *addr, gfp_t gfp)
8675{
8676 return __nl80211_unexpected_frame(dev,
8677 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8678 addr, gfp);
8679}
8680
Johannes Berg2e161f72010-08-12 15:38:38 +02008681int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008682 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008683 int freq, int sig_dbm,
8684 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008685{
Johannes Berg71bbc992012-06-15 15:30:18 +02008686 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008687 struct sk_buff *msg;
8688 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008689
8690 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8691 if (!msg)
8692 return -ENOMEM;
8693
Johannes Berg2e161f72010-08-12 15:38:38 +02008694 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008695 if (!hdr) {
8696 nlmsg_free(msg);
8697 return -ENOMEM;
8698 }
8699
David S. Miller9360ffd2012-03-29 04:41:26 -04008700 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008701 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8702 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008703 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8704 (sig_dbm &&
8705 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8706 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8707 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008708
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008709 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008710
Eric W. Biederman15e47302012-09-07 20:12:54 +00008711 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008712
8713 nla_put_failure:
8714 genlmsg_cancel(msg, hdr);
8715 nlmsg_free(msg);
8716 return -ENOBUFS;
8717}
8718
Johannes Berg2e161f72010-08-12 15:38:38 +02008719void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008720 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008721 const u8 *buf, size_t len, bool ack,
8722 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008723{
Johannes Berg71bbc992012-06-15 15:30:18 +02008724 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008725 struct sk_buff *msg;
8726 void *hdr;
8727
8728 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8729 if (!msg)
8730 return;
8731
Johannes Berg2e161f72010-08-12 15:38:38 +02008732 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008733 if (!hdr) {
8734 nlmsg_free(msg);
8735 return;
8736 }
8737
David S. Miller9360ffd2012-03-29 04:41:26 -04008738 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008739 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8740 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008741 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8742 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8743 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8744 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008745
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008746 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008747
8748 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8749 return;
8750
8751 nla_put_failure:
8752 genlmsg_cancel(msg, hdr);
8753 nlmsg_free(msg);
8754}
8755
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008756void
8757nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8758 struct net_device *netdev,
8759 enum nl80211_cqm_rssi_threshold_event rssi_event,
8760 gfp_t gfp)
8761{
8762 struct sk_buff *msg;
8763 struct nlattr *pinfoattr;
8764 void *hdr;
8765
Thomas Graf58050fc2012-06-28 03:57:45 +00008766 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008767 if (!msg)
8768 return;
8769
8770 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8771 if (!hdr) {
8772 nlmsg_free(msg);
8773 return;
8774 }
8775
David S. Miller9360ffd2012-03-29 04:41:26 -04008776 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8777 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8778 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008779
8780 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8781 if (!pinfoattr)
8782 goto nla_put_failure;
8783
David S. Miller9360ffd2012-03-29 04:41:26 -04008784 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8785 rssi_event))
8786 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008787
8788 nla_nest_end(msg, pinfoattr);
8789
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008790 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008791
8792 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8793 nl80211_mlme_mcgrp.id, gfp);
8794 return;
8795
8796 nla_put_failure:
8797 genlmsg_cancel(msg, hdr);
8798 nlmsg_free(msg);
8799}
8800
Johannes Berge5497d72011-07-05 16:35:40 +02008801void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8802 struct net_device *netdev, const u8 *bssid,
8803 const u8 *replay_ctr, gfp_t gfp)
8804{
8805 struct sk_buff *msg;
8806 struct nlattr *rekey_attr;
8807 void *hdr;
8808
Thomas Graf58050fc2012-06-28 03:57:45 +00008809 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008810 if (!msg)
8811 return;
8812
8813 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8814 if (!hdr) {
8815 nlmsg_free(msg);
8816 return;
8817 }
8818
David S. Miller9360ffd2012-03-29 04:41:26 -04008819 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8820 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8821 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8822 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008823
8824 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8825 if (!rekey_attr)
8826 goto nla_put_failure;
8827
David S. Miller9360ffd2012-03-29 04:41:26 -04008828 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8829 NL80211_REPLAY_CTR_LEN, replay_ctr))
8830 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008831
8832 nla_nest_end(msg, rekey_attr);
8833
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008834 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008835
8836 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8837 nl80211_mlme_mcgrp.id, gfp);
8838 return;
8839
8840 nla_put_failure:
8841 genlmsg_cancel(msg, hdr);
8842 nlmsg_free(msg);
8843}
8844
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008845void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8846 struct net_device *netdev, int index,
8847 const u8 *bssid, bool preauth, gfp_t gfp)
8848{
8849 struct sk_buff *msg;
8850 struct nlattr *attr;
8851 void *hdr;
8852
Thomas Graf58050fc2012-06-28 03:57:45 +00008853 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008854 if (!msg)
8855 return;
8856
8857 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8858 if (!hdr) {
8859 nlmsg_free(msg);
8860 return;
8861 }
8862
David S. Miller9360ffd2012-03-29 04:41:26 -04008863 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8864 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8865 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008866
8867 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8868 if (!attr)
8869 goto nla_put_failure;
8870
David S. Miller9360ffd2012-03-29 04:41:26 -04008871 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8872 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8873 (preauth &&
8874 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8875 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008876
8877 nla_nest_end(msg, attr);
8878
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008879 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008880
8881 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8882 nl80211_mlme_mcgrp.id, gfp);
8883 return;
8884
8885 nla_put_failure:
8886 genlmsg_cancel(msg, hdr);
8887 nlmsg_free(msg);
8888}
8889
Thomas Pedersen53145262012-04-06 13:35:47 -07008890void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01008891 struct net_device *netdev,
8892 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07008893{
8894 struct sk_buff *msg;
8895 void *hdr;
8896
Thomas Graf58050fc2012-06-28 03:57:45 +00008897 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008898 if (!msg)
8899 return;
8900
8901 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8902 if (!hdr) {
8903 nlmsg_free(msg);
8904 return;
8905 }
8906
Johannes Berg683b6d32012-11-08 21:25:48 +01008907 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8908 goto nla_put_failure;
8909
8910 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04008911 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008912
8913 genlmsg_end(msg, hdr);
8914
8915 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8916 nl80211_mlme_mcgrp.id, gfp);
8917 return;
8918
8919 nla_put_failure:
8920 genlmsg_cancel(msg, hdr);
8921 nlmsg_free(msg);
8922}
8923
Johannes Bergc063dbf2010-11-24 08:10:05 +01008924void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008925nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8926 struct net_device *netdev, const u8 *peer,
8927 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8928{
8929 struct sk_buff *msg;
8930 struct nlattr *pinfoattr;
8931 void *hdr;
8932
8933 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8934 if (!msg)
8935 return;
8936
8937 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8938 if (!hdr) {
8939 nlmsg_free(msg);
8940 return;
8941 }
8942
8943 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8944 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8945 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8946 goto nla_put_failure;
8947
8948 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8949 if (!pinfoattr)
8950 goto nla_put_failure;
8951
8952 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8953 goto nla_put_failure;
8954
8955 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8956 goto nla_put_failure;
8957
8958 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8959 goto nla_put_failure;
8960
8961 nla_nest_end(msg, pinfoattr);
8962
8963 genlmsg_end(msg, hdr);
8964
8965 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8966 nl80211_mlme_mcgrp.id, gfp);
8967 return;
8968
8969 nla_put_failure:
8970 genlmsg_cancel(msg, hdr);
8971 nlmsg_free(msg);
8972}
8973
8974void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008975nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8976 struct net_device *netdev, const u8 *peer,
8977 u32 num_packets, gfp_t gfp)
8978{
8979 struct sk_buff *msg;
8980 struct nlattr *pinfoattr;
8981 void *hdr;
8982
Thomas Graf58050fc2012-06-28 03:57:45 +00008983 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008984 if (!msg)
8985 return;
8986
8987 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8988 if (!hdr) {
8989 nlmsg_free(msg);
8990 return;
8991 }
8992
David S. Miller9360ffd2012-03-29 04:41:26 -04008993 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8994 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8995 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8996 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008997
8998 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8999 if (!pinfoattr)
9000 goto nla_put_failure;
9001
David S. Miller9360ffd2012-03-29 04:41:26 -04009002 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9003 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009004
9005 nla_nest_end(msg, pinfoattr);
9006
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009007 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009008
9009 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9010 nl80211_mlme_mcgrp.id, gfp);
9011 return;
9012
9013 nla_put_failure:
9014 genlmsg_cancel(msg, hdr);
9015 nlmsg_free(msg);
9016}
9017
Johannes Berg7f6cf312011-11-04 11:18:15 +01009018void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9019 u64 cookie, bool acked, gfp_t gfp)
9020{
9021 struct wireless_dev *wdev = dev->ieee80211_ptr;
9022 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9023 struct sk_buff *msg;
9024 void *hdr;
9025 int err;
9026
Beni Lev4ee3e062012-08-27 12:49:39 +03009027 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9028
Thomas Graf58050fc2012-06-28 03:57:45 +00009029 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009030
Johannes Berg7f6cf312011-11-04 11:18:15 +01009031 if (!msg)
9032 return;
9033
9034 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9035 if (!hdr) {
9036 nlmsg_free(msg);
9037 return;
9038 }
9039
David S. Miller9360ffd2012-03-29 04:41:26 -04009040 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9041 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9042 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9043 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9044 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9045 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009046
9047 err = genlmsg_end(msg, hdr);
9048 if (err < 0) {
9049 nlmsg_free(msg);
9050 return;
9051 }
9052
9053 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9054 nl80211_mlme_mcgrp.id, gfp);
9055 return;
9056
9057 nla_put_failure:
9058 genlmsg_cancel(msg, hdr);
9059 nlmsg_free(msg);
9060}
9061EXPORT_SYMBOL(cfg80211_probe_status);
9062
Johannes Berg5e760232011-11-04 11:18:17 +01009063void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9064 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009065 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009066{
9067 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9068 struct sk_buff *msg;
9069 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009070 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009071
Beni Lev4ee3e062012-08-27 12:49:39 +03009072 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9073
Ben Greear37c73b52012-10-26 14:49:25 -07009074 spin_lock_bh(&rdev->beacon_registrations_lock);
9075 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9076 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9077 if (!msg) {
9078 spin_unlock_bh(&rdev->beacon_registrations_lock);
9079 return;
9080 }
Johannes Berg5e760232011-11-04 11:18:17 +01009081
Ben Greear37c73b52012-10-26 14:49:25 -07009082 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9083 if (!hdr)
9084 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009085
Ben Greear37c73b52012-10-26 14:49:25 -07009086 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9087 (freq &&
9088 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9089 (sig_dbm &&
9090 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9091 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9092 goto nla_put_failure;
9093
9094 genlmsg_end(msg, hdr);
9095
9096 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009097 }
Ben Greear37c73b52012-10-26 14:49:25 -07009098 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009099 return;
9100
9101 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009102 spin_unlock_bh(&rdev->beacon_registrations_lock);
9103 if (hdr)
9104 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009105 nlmsg_free(msg);
9106}
9107EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9108
Jouni Malinen3475b092012-11-16 22:49:57 +02009109void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9110 enum nl80211_tdls_operation oper,
9111 u16 reason_code, gfp_t gfp)
9112{
9113 struct wireless_dev *wdev = dev->ieee80211_ptr;
9114 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9115 struct sk_buff *msg;
9116 void *hdr;
9117 int err;
9118
9119 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9120 reason_code);
9121
9122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9123 if (!msg)
9124 return;
9125
9126 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9127 if (!hdr) {
9128 nlmsg_free(msg);
9129 return;
9130 }
9131
9132 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9133 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9134 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9135 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9136 (reason_code > 0 &&
9137 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9138 goto nla_put_failure;
9139
9140 err = genlmsg_end(msg, hdr);
9141 if (err < 0) {
9142 nlmsg_free(msg);
9143 return;
9144 }
9145
9146 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9147 nl80211_mlme_mcgrp.id, gfp);
9148 return;
9149
9150 nla_put_failure:
9151 genlmsg_cancel(msg, hdr);
9152 nlmsg_free(msg);
9153}
9154EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9155
Jouni Malinen026331c2010-02-15 12:53:10 +02009156static int nl80211_netlink_notify(struct notifier_block * nb,
9157 unsigned long state,
9158 void *_notify)
9159{
9160 struct netlink_notify *notify = _notify;
9161 struct cfg80211_registered_device *rdev;
9162 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009163 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009164
9165 if (state != NETLINK_URELEASE)
9166 return NOTIFY_DONE;
9167
9168 rcu_read_lock();
9169
Johannes Berg5e760232011-11-04 11:18:17 +01009170 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009171 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009172 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009173
9174 spin_lock_bh(&rdev->beacon_registrations_lock);
9175 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9176 list) {
9177 if (reg->nlportid == notify->portid) {
9178 list_del(&reg->list);
9179 kfree(reg);
9180 break;
9181 }
9182 }
9183 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009184 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009185
9186 rcu_read_unlock();
9187
9188 return NOTIFY_DONE;
9189}
9190
9191static struct notifier_block nl80211_netlink_notifier = {
9192 .notifier_call = nl80211_netlink_notify,
9193};
9194
Johannes Berg55682962007-09-20 13:09:35 -04009195/* initialisation/exit functions */
9196
9197int nl80211_init(void)
9198{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009199 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009200
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009201 err = genl_register_family_with_ops(&nl80211_fam,
9202 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009203 if (err)
9204 return err;
9205
Johannes Berg55682962007-09-20 13:09:35 -04009206 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9207 if (err)
9208 goto err_out;
9209
Johannes Berg2a519312009-02-10 21:25:55 +01009210 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9211 if (err)
9212 goto err_out;
9213
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009214 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9215 if (err)
9216 goto err_out;
9217
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009218 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9219 if (err)
9220 goto err_out;
9221
Johannes Bergaff89a92009-07-01 21:26:51 +02009222#ifdef CONFIG_NL80211_TESTMODE
9223 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9224 if (err)
9225 goto err_out;
9226#endif
9227
Jouni Malinen026331c2010-02-15 12:53:10 +02009228 err = netlink_register_notifier(&nl80211_netlink_notifier);
9229 if (err)
9230 goto err_out;
9231
Johannes Berg55682962007-09-20 13:09:35 -04009232 return 0;
9233 err_out:
9234 genl_unregister_family(&nl80211_fam);
9235 return err;
9236}
9237
9238void nl80211_exit(void)
9239{
Jouni Malinen026331c2010-02-15 12:53:10 +02009240 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009241 genl_unregister_family(&nl80211_fam);
9242}