blob: 9e7c10420da8a827bc5112f6902caec87418fca0 [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 Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg55682962007-09-20 13:09:35 -0400373};
374
Johannes Berge31b8212010-10-05 19:39:30 +0200375/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000376static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200377 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200378 [NL80211_KEY_IDX] = { .type = NLA_U8 },
379 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200380 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200381 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
382 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200383 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100384 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
385};
386
387/* policy for the key default flags */
388static const struct nla_policy
389nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
390 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
391 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200392};
393
Johannes Bergff1b6e62011-05-04 15:37:28 +0200394/* policy for WoWLAN attributes */
395static const struct nla_policy
396nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
397 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
398 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
400 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200401 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
402 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
403 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
404 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100405 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
406};
407
408static const struct nla_policy
409nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
410 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
411 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
412 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
413 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
414 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
415 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
416 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
417 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
418 },
419 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
420 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
421 },
422 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
423 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200425};
426
Johannes Berge5497d72011-07-05 16:35:40 +0200427/* policy for GTK rekey offload attributes */
428static const struct nla_policy
429nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
430 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
431 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
432 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
433};
434
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300435static const struct nla_policy
436nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200437 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300438 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700439 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300440};
441
Holger Schuriga0438972009-11-11 11:30:02 +0100442/* ifidx get helper */
443static int nl80211_get_ifidx(struct netlink_callback *cb)
444{
445 int res;
446
447 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
448 nl80211_fam.attrbuf, nl80211_fam.maxattr,
449 nl80211_policy);
450 if (res)
451 return res;
452
453 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
454 return -EINVAL;
455
456 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
457 if (!res)
458 return -EINVAL;
459 return res;
460}
461
Johannes Berg67748892010-10-04 21:14:06 +0200462static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
463 struct netlink_callback *cb,
464 struct cfg80211_registered_device **rdev,
465 struct net_device **dev)
466{
467 int ifidx = cb->args[0];
468 int err;
469
470 if (!ifidx)
471 ifidx = nl80211_get_ifidx(cb);
472 if (ifidx < 0)
473 return ifidx;
474
475 cb->args[0] = ifidx;
476
477 rtnl_lock();
478
479 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
480 if (!*dev) {
481 err = -ENODEV;
482 goto out_rtnl;
483 }
484
485 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100486 if (IS_ERR(*rdev)) {
487 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200488 goto out_rtnl;
489 }
490
491 return 0;
492 out_rtnl:
493 rtnl_unlock();
494 return err;
495}
496
497static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
498{
499 cfg80211_unlock_rdev(rdev);
500 rtnl_unlock();
501}
502
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100503/* IE validation */
504static bool is_valid_ie_attr(const struct nlattr *attr)
505{
506 const u8 *pos;
507 int len;
508
509 if (!attr)
510 return true;
511
512 pos = nla_data(attr);
513 len = nla_len(attr);
514
515 while (len) {
516 u8 elemlen;
517
518 if (len < 2)
519 return false;
520 len -= 2;
521
522 elemlen = pos[1];
523 if (elemlen > len)
524 return false;
525
526 len -= elemlen;
527 pos += 2 + elemlen;
528 }
529
530 return true;
531}
532
Johannes Berg55682962007-09-20 13:09:35 -0400533/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000534static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400535 int flags, u8 cmd)
536{
537 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000538 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400539}
540
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400541static int nl80211_msg_put_channel(struct sk_buff *msg,
542 struct ieee80211_channel *chan)
543{
David S. Miller9360ffd2012-03-29 04:41:26 -0400544 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
545 chan->center_freq))
546 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400547
David S. Miller9360ffd2012-03-29 04:41:26 -0400548 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
549 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
550 goto nla_put_failure;
551 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
552 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
553 goto nla_put_failure;
554 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
555 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
556 goto nla_put_failure;
Johannes Berg1c33a052013-02-18 23:44:38 +0100557 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
558 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
559 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400560
David S. Miller9360ffd2012-03-29 04:41:26 -0400561 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
562 DBM_TO_MBM(chan->max_power)))
563 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400564
565 return 0;
566
567 nla_put_failure:
568 return -ENOBUFS;
569}
570
Johannes Berg55682962007-09-20 13:09:35 -0400571/* netlink command implementations */
572
Johannes Bergb9454e82009-07-08 13:29:08 +0200573struct key_parse {
574 struct key_params p;
575 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200576 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200577 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100578 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200579};
580
581static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
582{
583 struct nlattr *tb[NL80211_KEY_MAX + 1];
584 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
585 nl80211_key_policy);
586 if (err)
587 return err;
588
589 k->def = !!tb[NL80211_KEY_DEFAULT];
590 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
591
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100592 if (k->def) {
593 k->def_uni = true;
594 k->def_multi = true;
595 }
596 if (k->defmgmt)
597 k->def_multi = true;
598
Johannes Bergb9454e82009-07-08 13:29:08 +0200599 if (tb[NL80211_KEY_IDX])
600 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
601
602 if (tb[NL80211_KEY_DATA]) {
603 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
604 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
605 }
606
607 if (tb[NL80211_KEY_SEQ]) {
608 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
609 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
610 }
611
612 if (tb[NL80211_KEY_CIPHER])
613 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
614
Johannes Berge31b8212010-10-05 19:39:30 +0200615 if (tb[NL80211_KEY_TYPE]) {
616 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
617 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
618 return -EINVAL;
619 }
620
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100621 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
622 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100623 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
624 tb[NL80211_KEY_DEFAULT_TYPES],
625 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100626 if (err)
627 return err;
628
629 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
630 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
631 }
632
Johannes Bergb9454e82009-07-08 13:29:08 +0200633 return 0;
634}
635
636static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
637{
638 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
639 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
640 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
641 }
642
643 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
644 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
645 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
646 }
647
648 if (info->attrs[NL80211_ATTR_KEY_IDX])
649 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
650
651 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
652 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
653
654 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
655 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
656
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100657 if (k->def) {
658 k->def_uni = true;
659 k->def_multi = true;
660 }
661 if (k->defmgmt)
662 k->def_multi = true;
663
Johannes Berge31b8212010-10-05 19:39:30 +0200664 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
665 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
666 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
667 return -EINVAL;
668 }
669
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100670 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
671 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
672 int err = nla_parse_nested(
673 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
674 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
675 nl80211_key_default_policy);
676 if (err)
677 return err;
678
679 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
680 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
681 }
682
Johannes Bergb9454e82009-07-08 13:29:08 +0200683 return 0;
684}
685
686static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
687{
688 int err;
689
690 memset(k, 0, sizeof(*k));
691 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200692 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200693
694 if (info->attrs[NL80211_ATTR_KEY])
695 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
696 else
697 err = nl80211_parse_key_old(info, k);
698
699 if (err)
700 return err;
701
702 if (k->def && k->defmgmt)
703 return -EINVAL;
704
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100705 if (k->defmgmt) {
706 if (k->def_uni || !k->def_multi)
707 return -EINVAL;
708 }
709
Johannes Bergb9454e82009-07-08 13:29:08 +0200710 if (k->idx != -1) {
711 if (k->defmgmt) {
712 if (k->idx < 4 || k->idx > 5)
713 return -EINVAL;
714 } else if (k->def) {
715 if (k->idx < 0 || k->idx > 3)
716 return -EINVAL;
717 } else {
718 if (k->idx < 0 || k->idx > 5)
719 return -EINVAL;
720 }
721 }
722
723 return 0;
724}
725
Johannes Bergfffd0932009-07-08 14:22:54 +0200726static struct cfg80211_cached_keys *
727nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530728 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200729{
730 struct key_parse parse;
731 struct nlattr *key;
732 struct cfg80211_cached_keys *result;
733 int rem, err, def = 0;
734
735 result = kzalloc(sizeof(*result), GFP_KERNEL);
736 if (!result)
737 return ERR_PTR(-ENOMEM);
738
739 result->def = -1;
740 result->defmgmt = -1;
741
742 nla_for_each_nested(key, keys, rem) {
743 memset(&parse, 0, sizeof(parse));
744 parse.idx = -1;
745
746 err = nl80211_parse_key_new(key, &parse);
747 if (err)
748 goto error;
749 err = -EINVAL;
750 if (!parse.p.key)
751 goto error;
752 if (parse.idx < 0 || parse.idx > 4)
753 goto error;
754 if (parse.def) {
755 if (def)
756 goto error;
757 def = 1;
758 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100759 if (!parse.def_uni || !parse.def_multi)
760 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200761 } else if (parse.defmgmt)
762 goto error;
763 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200764 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200765 if (err)
766 goto error;
767 result->params[parse.idx].cipher = parse.p.cipher;
768 result->params[parse.idx].key_len = parse.p.key_len;
769 result->params[parse.idx].key = result->data[parse.idx];
770 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530771
772 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
773 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
774 if (no_ht)
775 *no_ht = true;
776 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200777 }
778
779 return result;
780 error:
781 kfree(result);
782 return ERR_PTR(err);
783}
784
785static int nl80211_key_allowed(struct wireless_dev *wdev)
786{
787 ASSERT_WDEV_LOCK(wdev);
788
Johannes Bergfffd0932009-07-08 14:22:54 +0200789 switch (wdev->iftype) {
790 case NL80211_IFTYPE_AP:
791 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200792 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700793 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200794 break;
795 case NL80211_IFTYPE_ADHOC:
796 if (!wdev->current_bss)
797 return -ENOLINK;
798 break;
799 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200800 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200801 if (wdev->sme_state != CFG80211_SME_CONNECTED)
802 return -ENOLINK;
803 break;
804 default:
805 return -EINVAL;
806 }
807
808 return 0;
809}
810
Johannes Berg7527a782011-05-13 10:58:57 +0200811static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
812{
813 struct nlattr *nl_modes = nla_nest_start(msg, attr);
814 int i;
815
816 if (!nl_modes)
817 goto nla_put_failure;
818
819 i = 0;
820 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400821 if ((ifmodes & 1) && nla_put_flag(msg, i))
822 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200823 ifmodes >>= 1;
824 i++;
825 }
826
827 nla_nest_end(msg, nl_modes);
828 return 0;
829
830nla_put_failure:
831 return -ENOBUFS;
832}
833
834static int nl80211_put_iface_combinations(struct wiphy *wiphy,
835 struct sk_buff *msg)
836{
837 struct nlattr *nl_combis;
838 int i, j;
839
840 nl_combis = nla_nest_start(msg,
841 NL80211_ATTR_INTERFACE_COMBINATIONS);
842 if (!nl_combis)
843 goto nla_put_failure;
844
845 for (i = 0; i < wiphy->n_iface_combinations; i++) {
846 const struct ieee80211_iface_combination *c;
847 struct nlattr *nl_combi, *nl_limits;
848
849 c = &wiphy->iface_combinations[i];
850
851 nl_combi = nla_nest_start(msg, i + 1);
852 if (!nl_combi)
853 goto nla_put_failure;
854
855 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
856 if (!nl_limits)
857 goto nla_put_failure;
858
859 for (j = 0; j < c->n_limits; j++) {
860 struct nlattr *nl_limit;
861
862 nl_limit = nla_nest_start(msg, j + 1);
863 if (!nl_limit)
864 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400865 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
866 c->limits[j].max))
867 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200868 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
869 c->limits[j].types))
870 goto nla_put_failure;
871 nla_nest_end(msg, nl_limit);
872 }
873
874 nla_nest_end(msg, nl_limits);
875
David S. Miller9360ffd2012-03-29 04:41:26 -0400876 if (c->beacon_int_infra_match &&
877 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
878 goto nla_put_failure;
879 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
880 c->num_different_channels) ||
881 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
882 c->max_interfaces))
883 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200884
885 nla_nest_end(msg, nl_combi);
886 }
887
888 nla_nest_end(msg, nl_combis);
889
890 return 0;
891nla_put_failure:
892 return -ENOBUFS;
893}
894
Eric W. Biederman15e47302012-09-07 20:12:54 +0000895static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400896 struct cfg80211_registered_device *dev)
897{
898 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100899 struct nlattr *nl_bands, *nl_band;
900 struct nlattr *nl_freqs, *nl_freq;
901 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100902 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100903 enum ieee80211_band band;
904 struct ieee80211_channel *chan;
905 struct ieee80211_rate *rate;
906 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200907 const struct ieee80211_txrx_stypes *mgmt_stypes =
908 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400909
Eric W. Biederman15e47302012-09-07 20:12:54 +0000910 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400911 if (!hdr)
912 return -1;
913
David S. Miller9360ffd2012-03-29 04:41:26 -0400914 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
915 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
916 nla_put_u32(msg, NL80211_ATTR_GENERATION,
917 cfg80211_rdev_list_generation) ||
918 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
919 dev->wiphy.retry_short) ||
920 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
921 dev->wiphy.retry_long) ||
922 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
923 dev->wiphy.frag_threshold) ||
924 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
925 dev->wiphy.rts_threshold) ||
926 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
927 dev->wiphy.coverage_class) ||
928 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
929 dev->wiphy.max_scan_ssids) ||
930 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
931 dev->wiphy.max_sched_scan_ssids) ||
932 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
933 dev->wiphy.max_scan_ie_len) ||
934 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
935 dev->wiphy.max_sched_scan_ie_len) ||
936 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
937 dev->wiphy.max_match_sets))
938 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200939
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
941 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
942 goto nla_put_failure;
943 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
944 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
945 goto nla_put_failure;
946 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
947 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
948 goto nla_put_failure;
949 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
950 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
951 goto nla_put_failure;
952 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
953 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
954 goto nla_put_failure;
955 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
956 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
957 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200958
David S. Miller9360ffd2012-03-29 04:41:26 -0400959 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
960 sizeof(u32) * dev->wiphy.n_cipher_suites,
961 dev->wiphy.cipher_suites))
962 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100963
David S. Miller9360ffd2012-03-29 04:41:26 -0400964 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
965 dev->wiphy.max_num_pmkids))
966 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530967
David S. Miller9360ffd2012-03-29 04:41:26 -0400968 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
969 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
970 goto nla_put_failure;
Johannes Berg25e47c182009-04-02 20:14:06 +0200971
David S. Miller9360ffd2012-03-29 04:41:26 -0400972 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
973 dev->wiphy.available_antennas_tx) ||
974 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
975 dev->wiphy.available_antennas_rx))
976 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100977
David S. Miller9360ffd2012-03-29 04:41:26 -0400978 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
979 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
980 dev->wiphy.probe_resp_offload))
981 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200982
Bruno Randolf7f531e02010-12-16 11:30:22 +0900983 if ((dev->wiphy.available_antennas_tx ||
984 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900985 u32 tx_ant = 0, rx_ant = 0;
986 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300987 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900988 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400989 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
990 tx_ant) ||
991 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
992 rx_ant))
993 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900994 }
995 }
996
Johannes Berg7527a782011-05-13 10:58:57 +0200997 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
998 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700999 goto nla_put_failure;
1000
Johannes Bergee688b002008-01-24 19:38:39 +01001001 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1002 if (!nl_bands)
1003 goto nla_put_failure;
1004
1005 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1006 if (!dev->wiphy.bands[band])
1007 continue;
1008
1009 nl_band = nla_nest_start(msg, band);
1010 if (!nl_band)
1011 goto nla_put_failure;
1012
Johannes Bergd51626d2008-10-09 12:20:13 +02001013 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -04001014 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
1015 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1016 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
1017 &dev->wiphy.bands[band]->ht_cap.mcs) ||
1018 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1019 dev->wiphy.bands[band]->ht_cap.cap) ||
1020 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1021 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
1022 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1023 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
1024 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001025
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001026 /* add VHT info */
1027 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1028 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1029 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1030 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1031 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1032 dev->wiphy.bands[band]->vht_cap.cap)))
1033 goto nla_put_failure;
1034
Johannes Bergee688b002008-01-24 19:38:39 +01001035 /* add frequencies */
1036 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1037 if (!nl_freqs)
1038 goto nla_put_failure;
1039
1040 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1041 nl_freq = nla_nest_start(msg, i);
1042 if (!nl_freq)
1043 goto nla_put_failure;
1044
1045 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001046
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001047 if (nl80211_msg_put_channel(msg, chan))
1048 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001049
Johannes Bergee688b002008-01-24 19:38:39 +01001050 nla_nest_end(msg, nl_freq);
1051 }
1052
1053 nla_nest_end(msg, nl_freqs);
1054
1055 /* add bitrates */
1056 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1057 if (!nl_rates)
1058 goto nla_put_failure;
1059
1060 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1061 nl_rate = nla_nest_start(msg, i);
1062 if (!nl_rate)
1063 goto nla_put_failure;
1064
1065 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001066 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1067 rate->bitrate))
1068 goto nla_put_failure;
1069 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1070 nla_put_flag(msg,
1071 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1072 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001073
1074 nla_nest_end(msg, nl_rate);
1075 }
1076
1077 nla_nest_end(msg, nl_rates);
1078
1079 nla_nest_end(msg, nl_band);
1080 }
1081 nla_nest_end(msg, nl_bands);
1082
Johannes Berg8fdc6212009-03-14 09:34:01 +01001083 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1084 if (!nl_cmds)
1085 goto nla_put_failure;
1086
1087 i = 0;
1088#define CMD(op, n) \
1089 do { \
1090 if (dev->ops->op) { \
1091 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001092 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1093 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001094 } \
1095 } while (0)
1096
1097 CMD(add_virtual_intf, NEW_INTERFACE);
1098 CMD(change_virtual_intf, SET_INTERFACE);
1099 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001100 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001101 CMD(add_station, NEW_STATION);
1102 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001103 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001104 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001105 CMD(auth, AUTHENTICATE);
1106 CMD(assoc, ASSOCIATE);
1107 CMD(deauth, DEAUTHENTICATE);
1108 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001109 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001110 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001111 CMD(set_pmksa, SET_PMKSA);
1112 CMD(del_pmksa, DEL_PMKSA);
1113 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001114 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1115 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001116 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001117 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001118 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001119 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001120 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001121 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1122 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001123 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001124 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001125 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001126 i++;
1127 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1128 goto nla_put_failure;
1129 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001130 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001131 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1132 CMD(tdls_mgmt, TDLS_MGMT);
1133 CMD(tdls_oper, TDLS_OPER);
1134 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001135 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1136 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001137 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001138 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001139 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1140 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001141 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1142 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001143 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001144 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001145 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001146
Kalle Valo4745fc02011-11-17 19:06:10 +02001147#ifdef CONFIG_NL80211_TESTMODE
1148 CMD(testmode_cmd, TESTMODE);
1149#endif
1150
Johannes Berg8fdc6212009-03-14 09:34:01 +01001151#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001152
Johannes Berg6829c8782009-07-02 09:13:27 +02001153 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001154 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001155 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1156 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001157 }
1158
Johannes Berg6829c8782009-07-02 09:13:27 +02001159 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001160 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001161 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1162 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001163 }
1164
Johannes Berg8fdc6212009-03-14 09:34:01 +01001165 nla_nest_end(msg, nl_cmds);
1166
Johannes Berg7c4ef712011-11-18 15:33:48 +01001167 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001168 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1169 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1170 dev->wiphy.max_remain_on_channel_duration))
1171 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001172
David S. Miller9360ffd2012-03-29 04:41:26 -04001173 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1174 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1175 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001176
Johannes Berg2e161f72010-08-12 15:38:38 +02001177 if (mgmt_stypes) {
1178 u16 stypes;
1179 struct nlattr *nl_ftypes, *nl_ifs;
1180 enum nl80211_iftype ift;
1181
1182 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1183 if (!nl_ifs)
1184 goto nla_put_failure;
1185
1186 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1187 nl_ftypes = nla_nest_start(msg, ift);
1188 if (!nl_ftypes)
1189 goto nla_put_failure;
1190 i = 0;
1191 stypes = mgmt_stypes[ift].tx;
1192 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001193 if ((stypes & 1) &&
1194 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1195 (i << 4) | IEEE80211_FTYPE_MGMT))
1196 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001197 stypes >>= 1;
1198 i++;
1199 }
1200 nla_nest_end(msg, nl_ftypes);
1201 }
1202
Johannes Berg74b70a42010-08-24 12:15:53 +02001203 nla_nest_end(msg, nl_ifs);
1204
Johannes Berg2e161f72010-08-12 15:38:38 +02001205 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1206 if (!nl_ifs)
1207 goto nla_put_failure;
1208
1209 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1210 nl_ftypes = nla_nest_start(msg, ift);
1211 if (!nl_ftypes)
1212 goto nla_put_failure;
1213 i = 0;
1214 stypes = mgmt_stypes[ift].rx;
1215 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001216 if ((stypes & 1) &&
1217 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1218 (i << 4) | IEEE80211_FTYPE_MGMT))
1219 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001220 stypes >>= 1;
1221 i++;
1222 }
1223 nla_nest_end(msg, nl_ftypes);
1224 }
1225 nla_nest_end(msg, nl_ifs);
1226 }
1227
Johannes Bergdfb89c52012-06-27 09:23:48 +02001228#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001229 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1230 struct nlattr *nl_wowlan;
1231
1232 nl_wowlan = nla_nest_start(msg,
1233 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1234 if (!nl_wowlan)
1235 goto nla_put_failure;
1236
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1238 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1239 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1240 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1241 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1242 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1243 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1244 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1245 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1246 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1247 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1248 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1249 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1250 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1251 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1252 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1253 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001254 if (dev->wiphy.wowlan.n_patterns) {
1255 struct nl80211_wowlan_pattern_support pat = {
1256 .max_patterns = dev->wiphy.wowlan.n_patterns,
1257 .min_pattern_len =
1258 dev->wiphy.wowlan.pattern_min_len,
1259 .max_pattern_len =
1260 dev->wiphy.wowlan.pattern_max_len,
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08001261 .max_pkt_offset =
1262 dev->wiphy.wowlan.max_pkt_offset,
Johannes Bergff1b6e62011-05-04 15:37:28 +02001263 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001264 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1265 sizeof(pat), &pat))
1266 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001267 }
1268
1269 nla_nest_end(msg, nl_wowlan);
1270 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001271#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001272
Johannes Berg7527a782011-05-13 10:58:57 +02001273 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1274 dev->wiphy.software_iftypes))
1275 goto nla_put_failure;
1276
1277 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1278 goto nla_put_failure;
1279
David S. Miller9360ffd2012-03-29 04:41:26 -04001280 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1281 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1282 dev->wiphy.ap_sme_capa))
1283 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001284
David S. Miller9360ffd2012-03-29 04:41:26 -04001285 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1286 dev->wiphy.features))
1287 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001288
David S. Miller9360ffd2012-03-29 04:41:26 -04001289 if (dev->wiphy.ht_capa_mod_mask &&
1290 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1291 sizeof(*dev->wiphy.ht_capa_mod_mask),
1292 dev->wiphy.ht_capa_mod_mask))
1293 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001294
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05301295 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1296 dev->wiphy.max_acl_mac_addrs &&
1297 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1298 dev->wiphy.max_acl_mac_addrs))
1299 goto nla_put_failure;
1300
Johannes Berg55682962007-09-20 13:09:35 -04001301 return genlmsg_end(msg, hdr);
1302
1303 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001304 genlmsg_cancel(msg, hdr);
1305 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001306}
1307
1308static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1309{
Johannes Berg645e77d2013-03-01 14:03:49 +01001310 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001311 int start = cb->args[0];
1312 struct cfg80211_registered_device *dev;
1313
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001314 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001315 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001316 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1317 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001318 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001319 continue;
Johannes Berg645e77d2013-03-01 14:03:49 +01001320 ret = nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
1321 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1322 dev);
1323 if (ret < 0) {
1324 /*
1325 * If sending the wiphy data didn't fit (ENOBUFS or
1326 * EMSGSIZE returned), this SKB is still empty (so
1327 * it's not too big because another wiphy dataset is
1328 * already in the skb) and we've not tried to adjust
1329 * the dump allocation yet ... then adjust the alloc
1330 * size to be bigger, and return 1 but with the empty
1331 * skb. This results in an empty message being RX'ed
1332 * in userspace, but that is ignored.
1333 *
1334 * We can then retry with the larger buffer.
1335 */
1336 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1337 !skb->len &&
1338 cb->min_dump_alloc < 4096) {
1339 cb->min_dump_alloc = 4096;
1340 mutex_unlock(&cfg80211_mutex);
1341 return 1;
1342 }
Julius Volzb4637272008-07-08 14:02:19 +02001343 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001344 break;
Julius Volzb4637272008-07-08 14:02:19 +02001345 }
Johannes Berg55682962007-09-20 13:09:35 -04001346 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001347 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001348
1349 cb->args[0] = idx;
1350
1351 return skb->len;
1352}
1353
1354static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1355{
1356 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001357 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001358
Johannes Berg645e77d2013-03-01 14:03:49 +01001359 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001360 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001361 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001362
Eric W. Biederman15e47302012-09-07 20:12:54 +00001363 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001364 nlmsg_free(msg);
1365 return -ENOBUFS;
1366 }
Johannes Berg55682962007-09-20 13:09:35 -04001367
Johannes Berg134e6372009-07-10 09:51:34 +00001368 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001369}
1370
Jouni Malinen31888482008-10-30 16:59:24 +02001371static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1372 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1373 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1374 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1375 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1376 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1377};
1378
1379static int parse_txq_params(struct nlattr *tb[],
1380 struct ieee80211_txq_params *txq_params)
1381{
Johannes Berga3304b02012-03-28 11:04:24 +02001382 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001383 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1384 !tb[NL80211_TXQ_ATTR_AIFS])
1385 return -EINVAL;
1386
Johannes Berga3304b02012-03-28 11:04:24 +02001387 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001388 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1389 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1390 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1391 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1392
Johannes Berga3304b02012-03-28 11:04:24 +02001393 if (txq_params->ac >= NL80211_NUM_ACS)
1394 return -EINVAL;
1395
Jouni Malinen31888482008-10-30 16:59:24 +02001396 return 0;
1397}
1398
Johannes Bergf444de02010-05-05 15:25:02 +02001399static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1400{
1401 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001402 * You can only set the channel explicitly for WDS interfaces,
1403 * all others have their channel managed via their respective
1404 * "establish a connection" command (connect, join, ...)
1405 *
1406 * For AP/GO and mesh mode, the channel can be set with the
1407 * channel userspace API, but is only stored and passed to the
1408 * low-level driver when the AP starts or the mesh is joined.
1409 * This is for backward compatibility, userspace can also give
1410 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001411 *
1412 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001413 * whatever else is going on, so they have their own special
1414 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001415 */
1416 return !wdev ||
1417 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001418 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001419 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1420 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001421}
1422
Johannes Berg683b6d32012-11-08 21:25:48 +01001423static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1424 struct genl_info *info,
1425 struct cfg80211_chan_def *chandef)
1426{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301427 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001428
1429 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1430 return -EINVAL;
1431
1432 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1433
1434 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001435 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1436 chandef->center_freq1 = control_freq;
1437 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001438
1439 /* Primary channel not allowed */
1440 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1441 return -EINVAL;
1442
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001443 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1444 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001445
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001446 chantype = nla_get_u32(
1447 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1448
1449 switch (chantype) {
1450 case NL80211_CHAN_NO_HT:
1451 case NL80211_CHAN_HT20:
1452 case NL80211_CHAN_HT40PLUS:
1453 case NL80211_CHAN_HT40MINUS:
1454 cfg80211_chandef_create(chandef, chandef->chan,
1455 chantype);
1456 break;
1457 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001458 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001459 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001460 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1461 chandef->width =
1462 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1463 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1464 chandef->center_freq1 =
1465 nla_get_u32(
1466 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1467 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1468 chandef->center_freq2 =
1469 nla_get_u32(
1470 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1471 }
1472
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001473 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001474 return -EINVAL;
1475
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001476 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1477 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001478 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001479
Johannes Berg683b6d32012-11-08 21:25:48 +01001480 return 0;
1481}
1482
Johannes Bergf444de02010-05-05 15:25:02 +02001483static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1484 struct wireless_dev *wdev,
1485 struct genl_info *info)
1486{
Johannes Berg683b6d32012-11-08 21:25:48 +01001487 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001488 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001489 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1490
1491 if (wdev)
1492 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001493
Johannes Bergf444de02010-05-05 15:25:02 +02001494 if (!nl80211_can_set_dev_channel(wdev))
1495 return -EOPNOTSUPP;
1496
Johannes Berg683b6d32012-11-08 21:25:48 +01001497 result = nl80211_parse_chandef(rdev, info, &chandef);
1498 if (result)
1499 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001500
1501 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001502 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001503 case NL80211_IFTYPE_AP:
1504 case NL80211_IFTYPE_P2P_GO:
1505 if (wdev->beacon_interval) {
1506 result = -EBUSY;
1507 break;
1508 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001509 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001510 result = -EINVAL;
1511 break;
1512 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001513 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001514 result = 0;
1515 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001516 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001517 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001518 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001519 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001520 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001521 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001522 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001523 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001524 }
1525 mutex_unlock(&rdev->devlist_mtx);
1526
1527 return result;
1528}
1529
1530static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1531{
Johannes Berg4c476992010-10-04 21:36:35 +02001532 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1533 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001534
Johannes Berg4c476992010-10-04 21:36:35 +02001535 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001536}
1537
Bill Jordane8347eb2010-10-01 13:54:28 -04001538static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1539{
Johannes Berg43b19952010-10-07 13:10:30 +02001540 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1541 struct net_device *dev = info->user_ptr[1];
1542 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001543 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001544
1545 if (!info->attrs[NL80211_ATTR_MAC])
1546 return -EINVAL;
1547
Johannes Berg43b19952010-10-07 13:10:30 +02001548 if (netif_running(dev))
1549 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001550
Johannes Berg43b19952010-10-07 13:10:30 +02001551 if (!rdev->ops->set_wds_peer)
1552 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001553
Johannes Berg43b19952010-10-07 13:10:30 +02001554 if (wdev->iftype != NL80211_IFTYPE_WDS)
1555 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001556
1557 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001558 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001559}
1560
1561
Johannes Berg55682962007-09-20 13:09:35 -04001562static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1563{
1564 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001565 struct net_device *netdev = NULL;
1566 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001567 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001568 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001569 u32 changed;
1570 u8 retry_short = 0, retry_long = 0;
1571 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001572 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001573
Johannes Bergf444de02010-05-05 15:25:02 +02001574 /*
1575 * Try to find the wiphy and netdev. Normally this
1576 * function shouldn't need the netdev, but this is
1577 * done for backward compatibility -- previously
1578 * setting the channel was done per wiphy, but now
1579 * it is per netdev. Previous userland like hostapd
1580 * also passed a netdev to set_wiphy, so that it is
1581 * possible to let that go to the right netdev!
1582 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001583 mutex_lock(&cfg80211_mutex);
1584
Johannes Bergf444de02010-05-05 15:25:02 +02001585 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1586 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1587
1588 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1589 if (netdev && netdev->ieee80211_ptr) {
1590 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1591 mutex_lock(&rdev->mtx);
1592 } else
1593 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001594 }
1595
Johannes Bergf444de02010-05-05 15:25:02 +02001596 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001597 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1598 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001599 if (IS_ERR(rdev)) {
1600 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001601 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001602 }
1603 wdev = NULL;
1604 netdev = NULL;
1605 result = 0;
1606
1607 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001608 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001609 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001610
1611 /*
1612 * end workaround code, by now the rdev is available
1613 * and locked, and wdev may or may not be NULL.
1614 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001615
1616 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001617 result = cfg80211_dev_rename(
1618 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001619
1620 mutex_unlock(&cfg80211_mutex);
1621
1622 if (result)
1623 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001624
Jouni Malinen31888482008-10-30 16:59:24 +02001625 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1626 struct ieee80211_txq_params txq_params;
1627 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1628
1629 if (!rdev->ops->set_txq_params) {
1630 result = -EOPNOTSUPP;
1631 goto bad_res;
1632 }
1633
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001634 if (!netdev) {
1635 result = -EINVAL;
1636 goto bad_res;
1637 }
1638
Johannes Berg133a3ff2011-11-03 14:50:13 +01001639 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1640 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1641 result = -EINVAL;
1642 goto bad_res;
1643 }
1644
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001645 if (!netif_running(netdev)) {
1646 result = -ENETDOWN;
1647 goto bad_res;
1648 }
1649
Jouni Malinen31888482008-10-30 16:59:24 +02001650 nla_for_each_nested(nl_txq_params,
1651 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1652 rem_txq_params) {
1653 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1654 nla_data(nl_txq_params),
1655 nla_len(nl_txq_params),
1656 txq_params_policy);
1657 result = parse_txq_params(tb, &txq_params);
1658 if (result)
1659 goto bad_res;
1660
Hila Gonene35e4d22012-06-27 17:19:42 +03001661 result = rdev_set_txq_params(rdev, netdev,
1662 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001663 if (result)
1664 goto bad_res;
1665 }
1666 }
1667
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001668 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001669 result = __nl80211_set_channel(rdev,
1670 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1671 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001672 if (result)
1673 goto bad_res;
1674 }
1675
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001676 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001677 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001678 enum nl80211_tx_power_setting type;
1679 int idx, mbm = 0;
1680
Johannes Bergc8442112012-10-24 10:17:18 +02001681 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1682 txp_wdev = NULL;
1683
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001684 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001685 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001686 goto bad_res;
1687 }
1688
1689 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1690 type = nla_get_u32(info->attrs[idx]);
1691
1692 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1693 (type != NL80211_TX_POWER_AUTOMATIC)) {
1694 result = -EINVAL;
1695 goto bad_res;
1696 }
1697
1698 if (type != NL80211_TX_POWER_AUTOMATIC) {
1699 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1700 mbm = nla_get_u32(info->attrs[idx]);
1701 }
1702
Johannes Bergc8442112012-10-24 10:17:18 +02001703 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001704 if (result)
1705 goto bad_res;
1706 }
1707
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001708 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1709 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1710 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001711 if ((!rdev->wiphy.available_antennas_tx &&
1712 !rdev->wiphy.available_antennas_rx) ||
1713 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001714 result = -EOPNOTSUPP;
1715 goto bad_res;
1716 }
1717
1718 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1719 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1720
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001721 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001722 * available antenna masks, except for the "all" mask */
1723 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1724 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001725 result = -EINVAL;
1726 goto bad_res;
1727 }
1728
Bruno Randolf7f531e02010-12-16 11:30:22 +09001729 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1730 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001731
Hila Gonene35e4d22012-06-27 17:19:42 +03001732 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001733 if (result)
1734 goto bad_res;
1735 }
1736
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001737 changed = 0;
1738
1739 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1740 retry_short = nla_get_u8(
1741 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1742 if (retry_short == 0) {
1743 result = -EINVAL;
1744 goto bad_res;
1745 }
1746 changed |= WIPHY_PARAM_RETRY_SHORT;
1747 }
1748
1749 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1750 retry_long = nla_get_u8(
1751 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1752 if (retry_long == 0) {
1753 result = -EINVAL;
1754 goto bad_res;
1755 }
1756 changed |= WIPHY_PARAM_RETRY_LONG;
1757 }
1758
1759 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1760 frag_threshold = nla_get_u32(
1761 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1762 if (frag_threshold < 256) {
1763 result = -EINVAL;
1764 goto bad_res;
1765 }
1766 if (frag_threshold != (u32) -1) {
1767 /*
1768 * Fragments (apart from the last one) are required to
1769 * have even length. Make the fragmentation code
1770 * simpler by stripping LSB should someone try to use
1771 * odd threshold value.
1772 */
1773 frag_threshold &= ~0x1;
1774 }
1775 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1776 }
1777
1778 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1779 rts_threshold = nla_get_u32(
1780 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1781 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1782 }
1783
Lukáš Turek81077e82009-12-21 22:50:47 +01001784 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1785 coverage_class = nla_get_u8(
1786 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1787 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1788 }
1789
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001790 if (changed) {
1791 u8 old_retry_short, old_retry_long;
1792 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001793 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001794
1795 if (!rdev->ops->set_wiphy_params) {
1796 result = -EOPNOTSUPP;
1797 goto bad_res;
1798 }
1799
1800 old_retry_short = rdev->wiphy.retry_short;
1801 old_retry_long = rdev->wiphy.retry_long;
1802 old_frag_threshold = rdev->wiphy.frag_threshold;
1803 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001804 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001805
1806 if (changed & WIPHY_PARAM_RETRY_SHORT)
1807 rdev->wiphy.retry_short = retry_short;
1808 if (changed & WIPHY_PARAM_RETRY_LONG)
1809 rdev->wiphy.retry_long = retry_long;
1810 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1811 rdev->wiphy.frag_threshold = frag_threshold;
1812 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1813 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001814 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1815 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001816
Hila Gonene35e4d22012-06-27 17:19:42 +03001817 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001818 if (result) {
1819 rdev->wiphy.retry_short = old_retry_short;
1820 rdev->wiphy.retry_long = old_retry_long;
1821 rdev->wiphy.frag_threshold = old_frag_threshold;
1822 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001823 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001824 }
1825 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001826
Johannes Berg306d6112008-12-08 12:39:04 +01001827 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001828 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001829 if (netdev)
1830 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001831 return result;
1832}
1833
Johannes Berg71bbc992012-06-15 15:30:18 +02001834static inline u64 wdev_id(struct wireless_dev *wdev)
1835{
1836 return (u64)wdev->identifier |
1837 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1838}
Johannes Berg55682962007-09-20 13:09:35 -04001839
Johannes Berg683b6d32012-11-08 21:25:48 +01001840static int nl80211_send_chandef(struct sk_buff *msg,
1841 struct cfg80211_chan_def *chandef)
1842{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001843 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001844
Johannes Berg683b6d32012-11-08 21:25:48 +01001845 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1846 chandef->chan->center_freq))
1847 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001848 switch (chandef->width) {
1849 case NL80211_CHAN_WIDTH_20_NOHT:
1850 case NL80211_CHAN_WIDTH_20:
1851 case NL80211_CHAN_WIDTH_40:
1852 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1853 cfg80211_get_chandef_type(chandef)))
1854 return -ENOBUFS;
1855 break;
1856 default:
1857 break;
1858 }
1859 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1860 return -ENOBUFS;
1861 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1862 return -ENOBUFS;
1863 if (chandef->center_freq2 &&
1864 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001865 return -ENOBUFS;
1866 return 0;
1867}
1868
Eric W. Biederman15e47302012-09-07 20:12:54 +00001869static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001870 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001871 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001872{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001873 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001874 void *hdr;
1875
Eric W. Biederman15e47302012-09-07 20:12:54 +00001876 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001877 if (!hdr)
1878 return -1;
1879
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001880 if (dev &&
1881 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001882 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001883 goto nla_put_failure;
1884
1885 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1886 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001887 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001888 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001889 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1890 rdev->devlist_generation ^
1891 (cfg80211_rdev_list_generation << 2)))
1892 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001893
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001894 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001895 int ret;
1896 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001897
Johannes Berg683b6d32012-11-08 21:25:48 +01001898 ret = rdev_get_channel(rdev, wdev, &chandef);
1899 if (ret == 0) {
1900 if (nl80211_send_chandef(msg, &chandef))
1901 goto nla_put_failure;
1902 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001903 }
1904
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001905 if (wdev->ssid_len) {
1906 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1907 goto nla_put_failure;
1908 }
1909
Johannes Berg55682962007-09-20 13:09:35 -04001910 return genlmsg_end(msg, hdr);
1911
1912 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001913 genlmsg_cancel(msg, hdr);
1914 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001915}
1916
1917static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1918{
1919 int wp_idx = 0;
1920 int if_idx = 0;
1921 int wp_start = cb->args[0];
1922 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001923 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001924 struct wireless_dev *wdev;
1925
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001926 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001927 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1928 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001929 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001930 if (wp_idx < wp_start) {
1931 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001932 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001933 }
Johannes Berg55682962007-09-20 13:09:35 -04001934 if_idx = 0;
1935
Johannes Bergf5ea9122009-08-07 16:17:38 +02001936 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001937 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001938 if (if_idx < if_start) {
1939 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001940 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001941 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001942 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001943 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001944 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001945 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001946 goto out;
1947 }
1948 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001949 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001950 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001951
1952 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001953 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001954 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001955 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001956
1957 cb->args[0] = wp_idx;
1958 cb->args[1] = if_idx;
1959
1960 return skb->len;
1961}
1962
1963static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1964{
1965 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001966 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001967 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001968
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001969 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001970 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001971 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001972
Eric W. Biederman15e47302012-09-07 20:12:54 +00001973 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001974 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001975 nlmsg_free(msg);
1976 return -ENOBUFS;
1977 }
Johannes Berg55682962007-09-20 13:09:35 -04001978
Johannes Berg134e6372009-07-10 09:51:34 +00001979 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001980}
1981
Michael Wu66f7ac52008-01-31 19:48:22 +01001982static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1983 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1984 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1985 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1986 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1987 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1988};
1989
1990static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1991{
1992 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1993 int flag;
1994
1995 *mntrflags = 0;
1996
1997 if (!nla)
1998 return -EINVAL;
1999
2000 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2001 nla, mntr_flags_policy))
2002 return -EINVAL;
2003
2004 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2005 if (flags[flag])
2006 *mntrflags |= (1<<flag);
2007
2008 return 0;
2009}
2010
Johannes Berg9bc383d2009-11-19 11:55:19 +01002011static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002012 struct net_device *netdev, u8 use_4addr,
2013 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002014{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002015 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002016 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002017 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002018 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002019 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002020
2021 switch (iftype) {
2022 case NL80211_IFTYPE_AP_VLAN:
2023 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2024 return 0;
2025 break;
2026 case NL80211_IFTYPE_STATION:
2027 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2028 return 0;
2029 break;
2030 default:
2031 break;
2032 }
2033
2034 return -EOPNOTSUPP;
2035}
2036
Johannes Berg55682962007-09-20 13:09:35 -04002037static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2038{
Johannes Berg4c476992010-10-04 21:36:35 +02002039 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002040 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002041 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002042 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002043 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002044 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002045 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002046
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002047 memset(&params, 0, sizeof(params));
2048
Johannes Berg04a773a2009-04-19 21:24:32 +02002049 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002050
Johannes Berg723b0382008-09-16 20:22:09 +02002051 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002052 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002053 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002054 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002055 if (ntype > NL80211_IFTYPE_MAX)
2056 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002057 }
2058
Johannes Berg92ffe052008-09-16 20:39:36 +02002059 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002060 struct wireless_dev *wdev = dev->ieee80211_ptr;
2061
Johannes Berg4c476992010-10-04 21:36:35 +02002062 if (ntype != NL80211_IFTYPE_MESH_POINT)
2063 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002064 if (netif_running(dev))
2065 return -EBUSY;
2066
2067 wdev_lock(wdev);
2068 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2069 IEEE80211_MAX_MESH_ID_LEN);
2070 wdev->mesh_id_up_len =
2071 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2072 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2073 wdev->mesh_id_up_len);
2074 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002075 }
2076
Felix Fietkau8b787642009-11-10 18:53:10 +01002077 if (info->attrs[NL80211_ATTR_4ADDR]) {
2078 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2079 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002080 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002081 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002082 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002083 } else {
2084 params.use_4addr = -1;
2085 }
2086
Johannes Berg92ffe052008-09-16 20:39:36 +02002087 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002088 if (ntype != NL80211_IFTYPE_MONITOR)
2089 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002090 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2091 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002092 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002093 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002094
2095 flags = &_flags;
2096 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002097 }
Johannes Berg3b858752009-03-12 09:55:09 +01002098
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002099 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002100 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002101 else
2102 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002103
Johannes Berg9bc383d2009-11-19 11:55:19 +01002104 if (!err && params.use_4addr != -1)
2105 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2106
Johannes Berg55682962007-09-20 13:09:35 -04002107 return err;
2108}
2109
2110static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2111{
Johannes Berg4c476992010-10-04 21:36:35 +02002112 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002113 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002114 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002115 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002116 int err;
2117 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002118 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002119
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002120 memset(&params, 0, sizeof(params));
2121
Johannes Berg55682962007-09-20 13:09:35 -04002122 if (!info->attrs[NL80211_ATTR_IFNAME])
2123 return -EINVAL;
2124
2125 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2126 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2127 if (type > NL80211_IFTYPE_MAX)
2128 return -EINVAL;
2129 }
2130
Johannes Berg79c97e92009-07-07 03:56:12 +02002131 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002132 !(rdev->wiphy.interface_modes & (1 << type)))
2133 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002134
Arend van Spriel1c18f142013-01-08 10:17:27 +01002135 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2136 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2137 ETH_ALEN);
2138 if (!is_valid_ether_addr(params.macaddr))
2139 return -EADDRNOTAVAIL;
2140 }
2141
Johannes Berg9bc383d2009-11-19 11:55:19 +01002142 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002143 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002144 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002145 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002146 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002147 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002148
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002149 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2150 if (!msg)
2151 return -ENOMEM;
2152
Michael Wu66f7ac52008-01-31 19:48:22 +01002153 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2154 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2155 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002156 wdev = rdev_add_virtual_intf(rdev,
2157 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2158 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002159 if (IS_ERR(wdev)) {
2160 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002161 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002162 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002163
Johannes Berg98104fde2012-06-16 00:19:54 +02002164 switch (type) {
2165 case NL80211_IFTYPE_MESH_POINT:
2166 if (!info->attrs[NL80211_ATTR_MESH_ID])
2167 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002168 wdev_lock(wdev);
2169 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2170 IEEE80211_MAX_MESH_ID_LEN);
2171 wdev->mesh_id_up_len =
2172 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2173 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2174 wdev->mesh_id_up_len);
2175 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002176 break;
2177 case NL80211_IFTYPE_P2P_DEVICE:
2178 /*
2179 * P2P Device doesn't have a netdev, so doesn't go
2180 * through the netdev notifier and must be added here
2181 */
2182 mutex_init(&wdev->mtx);
2183 INIT_LIST_HEAD(&wdev->event_list);
2184 spin_lock_init(&wdev->event_lock);
2185 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2186 spin_lock_init(&wdev->mgmt_registrations_lock);
2187
2188 mutex_lock(&rdev->devlist_mtx);
2189 wdev->identifier = ++rdev->wdev_id;
2190 list_add_rcu(&wdev->list, &rdev->wdev_list);
2191 rdev->devlist_generation++;
2192 mutex_unlock(&rdev->devlist_mtx);
2193 break;
2194 default:
2195 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002196 }
2197
Eric W. Biederman15e47302012-09-07 20:12:54 +00002198 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002199 rdev, wdev) < 0) {
2200 nlmsg_free(msg);
2201 return -ENOBUFS;
2202 }
2203
2204 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002205}
2206
2207static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2208{
Johannes Berg4c476992010-10-04 21:36:35 +02002209 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002210 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002211
Johannes Berg4c476992010-10-04 21:36:35 +02002212 if (!rdev->ops->del_virtual_intf)
2213 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002214
Johannes Berg84efbb82012-06-16 00:00:26 +02002215 /*
2216 * If we remove a wireless device without a netdev then clear
2217 * user_ptr[1] so that nl80211_post_doit won't dereference it
2218 * to check if it needs to do dev_put(). Otherwise it crashes
2219 * since the wdev has been freed, unlike with a netdev where
2220 * we need the dev_put() for the netdev to really be freed.
2221 */
2222 if (!wdev->netdev)
2223 info->user_ptr[1] = NULL;
2224
Hila Gonene35e4d22012-06-27 17:19:42 +03002225 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002226}
2227
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002228static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2229{
2230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2231 struct net_device *dev = info->user_ptr[1];
2232 u16 noack_map;
2233
2234 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2235 return -EINVAL;
2236
2237 if (!rdev->ops->set_noack_map)
2238 return -EOPNOTSUPP;
2239
2240 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2241
Hila Gonene35e4d22012-06-27 17:19:42 +03002242 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002243}
2244
Johannes Berg41ade002007-12-19 02:03:29 +01002245struct get_key_cookie {
2246 struct sk_buff *msg;
2247 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002248 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002249};
2250
2251static void get_key_callback(void *c, struct key_params *params)
2252{
Johannes Bergb9454e82009-07-08 13:29:08 +02002253 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002254 struct get_key_cookie *cookie = c;
2255
David S. Miller9360ffd2012-03-29 04:41:26 -04002256 if ((params->key &&
2257 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2258 params->key_len, params->key)) ||
2259 (params->seq &&
2260 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2261 params->seq_len, params->seq)) ||
2262 (params->cipher &&
2263 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2264 params->cipher)))
2265 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002266
Johannes Bergb9454e82009-07-08 13:29:08 +02002267 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2268 if (!key)
2269 goto nla_put_failure;
2270
David S. Miller9360ffd2012-03-29 04:41:26 -04002271 if ((params->key &&
2272 nla_put(cookie->msg, NL80211_KEY_DATA,
2273 params->key_len, params->key)) ||
2274 (params->seq &&
2275 nla_put(cookie->msg, NL80211_KEY_SEQ,
2276 params->seq_len, params->seq)) ||
2277 (params->cipher &&
2278 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2279 params->cipher)))
2280 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002281
David S. Miller9360ffd2012-03-29 04:41:26 -04002282 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2283 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002284
2285 nla_nest_end(cookie->msg, key);
2286
Johannes Berg41ade002007-12-19 02:03:29 +01002287 return;
2288 nla_put_failure:
2289 cookie->error = 1;
2290}
2291
2292static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2293{
Johannes Berg4c476992010-10-04 21:36:35 +02002294 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002295 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002296 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002297 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002298 const u8 *mac_addr = NULL;
2299 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002300 struct get_key_cookie cookie = {
2301 .error = 0,
2302 };
2303 void *hdr;
2304 struct sk_buff *msg;
2305
2306 if (info->attrs[NL80211_ATTR_KEY_IDX])
2307 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2308
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002309 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002310 return -EINVAL;
2311
2312 if (info->attrs[NL80211_ATTR_MAC])
2313 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2314
Johannes Berge31b8212010-10-05 19:39:30 +02002315 pairwise = !!mac_addr;
2316 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2317 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2318 if (kt >= NUM_NL80211_KEYTYPES)
2319 return -EINVAL;
2320 if (kt != NL80211_KEYTYPE_GROUP &&
2321 kt != NL80211_KEYTYPE_PAIRWISE)
2322 return -EINVAL;
2323 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2324 }
2325
Johannes Berg4c476992010-10-04 21:36:35 +02002326 if (!rdev->ops->get_key)
2327 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002328
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002329 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002330 if (!msg)
2331 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002332
Eric W. Biederman15e47302012-09-07 20:12:54 +00002333 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002334 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002335 if (IS_ERR(hdr))
2336 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002337
2338 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002339 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002340
David S. Miller9360ffd2012-03-29 04:41:26 -04002341 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2342 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2343 goto nla_put_failure;
2344 if (mac_addr &&
2345 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2346 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002347
Johannes Berge31b8212010-10-05 19:39:30 +02002348 if (pairwise && mac_addr &&
2349 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2350 return -ENOENT;
2351
Hila Gonene35e4d22012-06-27 17:19:42 +03002352 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2353 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002354
2355 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002356 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002357
2358 if (cookie.error)
2359 goto nla_put_failure;
2360
2361 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002362 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002363
2364 nla_put_failure:
2365 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002366 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002367 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002368 return err;
2369}
2370
2371static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2372{
Johannes Berg4c476992010-10-04 21:36:35 +02002373 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002374 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002375 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002376 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002377
Johannes Bergb9454e82009-07-08 13:29:08 +02002378 err = nl80211_parse_key(info, &key);
2379 if (err)
2380 return err;
2381
2382 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002383 return -EINVAL;
2384
Johannes Bergb9454e82009-07-08 13:29:08 +02002385 /* only support setting default key */
2386 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002387 return -EINVAL;
2388
Johannes Bergfffd0932009-07-08 14:22:54 +02002389 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002390
2391 if (key.def) {
2392 if (!rdev->ops->set_default_key) {
2393 err = -EOPNOTSUPP;
2394 goto out;
2395 }
2396
2397 err = nl80211_key_allowed(dev->ieee80211_ptr);
2398 if (err)
2399 goto out;
2400
Hila Gonene35e4d22012-06-27 17:19:42 +03002401 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002402 key.def_uni, key.def_multi);
2403
2404 if (err)
2405 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002406
Johannes Berg3d23e342009-09-29 23:27:28 +02002407#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002408 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002409#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002410 } else {
2411 if (key.def_uni || !key.def_multi) {
2412 err = -EINVAL;
2413 goto out;
2414 }
2415
2416 if (!rdev->ops->set_default_mgmt_key) {
2417 err = -EOPNOTSUPP;
2418 goto out;
2419 }
2420
2421 err = nl80211_key_allowed(dev->ieee80211_ptr);
2422 if (err)
2423 goto out;
2424
Hila Gonene35e4d22012-06-27 17:19:42 +03002425 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002426 if (err)
2427 goto out;
2428
2429#ifdef CONFIG_CFG80211_WEXT
2430 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2431#endif
2432 }
2433
2434 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002435 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002436
Johannes Berg41ade002007-12-19 02:03:29 +01002437 return err;
2438}
2439
2440static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2441{
Johannes Berg4c476992010-10-04 21:36:35 +02002442 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002443 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002444 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002445 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002446 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002447
Johannes Bergb9454e82009-07-08 13:29:08 +02002448 err = nl80211_parse_key(info, &key);
2449 if (err)
2450 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002451
Johannes Bergb9454e82009-07-08 13:29:08 +02002452 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002453 return -EINVAL;
2454
Johannes Berg41ade002007-12-19 02:03:29 +01002455 if (info->attrs[NL80211_ATTR_MAC])
2456 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2457
Johannes Berge31b8212010-10-05 19:39:30 +02002458 if (key.type == -1) {
2459 if (mac_addr)
2460 key.type = NL80211_KEYTYPE_PAIRWISE;
2461 else
2462 key.type = NL80211_KEYTYPE_GROUP;
2463 }
2464
2465 /* for now */
2466 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2467 key.type != NL80211_KEYTYPE_GROUP)
2468 return -EINVAL;
2469
Johannes Berg4c476992010-10-04 21:36:35 +02002470 if (!rdev->ops->add_key)
2471 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002472
Johannes Berge31b8212010-10-05 19:39:30 +02002473 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2474 key.type == NL80211_KEYTYPE_PAIRWISE,
2475 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002476 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002477
2478 wdev_lock(dev->ieee80211_ptr);
2479 err = nl80211_key_allowed(dev->ieee80211_ptr);
2480 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002481 err = rdev_add_key(rdev, dev, key.idx,
2482 key.type == NL80211_KEYTYPE_PAIRWISE,
2483 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002484 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002485
Johannes Berg41ade002007-12-19 02:03:29 +01002486 return err;
2487}
2488
2489static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2490{
Johannes Berg4c476992010-10-04 21:36:35 +02002491 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002492 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002493 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002494 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002495 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002496
Johannes Bergb9454e82009-07-08 13:29:08 +02002497 err = nl80211_parse_key(info, &key);
2498 if (err)
2499 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002500
2501 if (info->attrs[NL80211_ATTR_MAC])
2502 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2503
Johannes Berge31b8212010-10-05 19:39:30 +02002504 if (key.type == -1) {
2505 if (mac_addr)
2506 key.type = NL80211_KEYTYPE_PAIRWISE;
2507 else
2508 key.type = NL80211_KEYTYPE_GROUP;
2509 }
2510
2511 /* for now */
2512 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2513 key.type != NL80211_KEYTYPE_GROUP)
2514 return -EINVAL;
2515
Johannes Berg4c476992010-10-04 21:36:35 +02002516 if (!rdev->ops->del_key)
2517 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002518
Johannes Bergfffd0932009-07-08 14:22:54 +02002519 wdev_lock(dev->ieee80211_ptr);
2520 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002521
2522 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2523 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2524 err = -ENOENT;
2525
Johannes Bergfffd0932009-07-08 14:22:54 +02002526 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002527 err = rdev_del_key(rdev, dev, key.idx,
2528 key.type == NL80211_KEYTYPE_PAIRWISE,
2529 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002530
Johannes Berg3d23e342009-09-29 23:27:28 +02002531#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002532 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002533 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002534 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002535 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002536 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2537 }
2538#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002539 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002540
Johannes Berg41ade002007-12-19 02:03:29 +01002541 return err;
2542}
2543
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302544/* This function returns an error or the number of nested attributes */
2545static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2546{
2547 struct nlattr *attr;
2548 int n_entries = 0, tmp;
2549
2550 nla_for_each_nested(attr, nl_attr, tmp) {
2551 if (nla_len(attr) != ETH_ALEN)
2552 return -EINVAL;
2553
2554 n_entries++;
2555 }
2556
2557 return n_entries;
2558}
2559
2560/*
2561 * This function parses ACL information and allocates memory for ACL data.
2562 * On successful return, the calling function is responsible to free the
2563 * ACL buffer returned by this function.
2564 */
2565static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2566 struct genl_info *info)
2567{
2568 enum nl80211_acl_policy acl_policy;
2569 struct nlattr *attr;
2570 struct cfg80211_acl_data *acl;
2571 int i = 0, n_entries, tmp;
2572
2573 if (!wiphy->max_acl_mac_addrs)
2574 return ERR_PTR(-EOPNOTSUPP);
2575
2576 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2577 return ERR_PTR(-EINVAL);
2578
2579 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2580 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2581 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2582 return ERR_PTR(-EINVAL);
2583
2584 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2585 return ERR_PTR(-EINVAL);
2586
2587 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2588 if (n_entries < 0)
2589 return ERR_PTR(n_entries);
2590
2591 if (n_entries > wiphy->max_acl_mac_addrs)
2592 return ERR_PTR(-ENOTSUPP);
2593
2594 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2595 GFP_KERNEL);
2596 if (!acl)
2597 return ERR_PTR(-ENOMEM);
2598
2599 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2600 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2601 i++;
2602 }
2603
2604 acl->n_acl_entries = n_entries;
2605 acl->acl_policy = acl_policy;
2606
2607 return acl;
2608}
2609
2610static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2611{
2612 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2613 struct net_device *dev = info->user_ptr[1];
2614 struct cfg80211_acl_data *acl;
2615 int err;
2616
2617 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2618 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2619 return -EOPNOTSUPP;
2620
2621 if (!dev->ieee80211_ptr->beacon_interval)
2622 return -EINVAL;
2623
2624 acl = parse_acl_data(&rdev->wiphy, info);
2625 if (IS_ERR(acl))
2626 return PTR_ERR(acl);
2627
2628 err = rdev_set_mac_acl(rdev, dev, acl);
2629
2630 kfree(acl);
2631
2632 return err;
2633}
2634
Johannes Berg88600202012-02-13 15:17:18 +01002635static int nl80211_parse_beacon(struct genl_info *info,
2636 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002637{
Johannes Berg88600202012-02-13 15:17:18 +01002638 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002639
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002640 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2641 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2642 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2643 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002644 return -EINVAL;
2645
Johannes Berg88600202012-02-13 15:17:18 +01002646 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002647
Johannes Berged1b6cc2007-12-19 02:03:32 +01002648 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002649 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2650 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2651 if (!bcn->head_len)
2652 return -EINVAL;
2653 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002654 }
2655
2656 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002657 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2658 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002659 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002660 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002661 }
2662
Johannes Berg4c476992010-10-04 21:36:35 +02002663 if (!haveinfo)
2664 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002665
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002666 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002667 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2668 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002669 }
2670
2671 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002672 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002673 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002674 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002675 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2676 }
2677
2678 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002679 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002680 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002681 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002682 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2683 }
2684
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002685 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002686 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002687 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002688 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002689 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2690 }
2691
Johannes Berg88600202012-02-13 15:17:18 +01002692 return 0;
2693}
2694
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002695static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2696 struct cfg80211_ap_settings *params)
2697{
2698 struct wireless_dev *wdev;
2699 bool ret = false;
2700
2701 mutex_lock(&rdev->devlist_mtx);
2702
Johannes Berg89a54e42012-06-15 14:33:17 +02002703 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002704 if (wdev->iftype != NL80211_IFTYPE_AP &&
2705 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2706 continue;
2707
Johannes Berg683b6d32012-11-08 21:25:48 +01002708 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002709 continue;
2710
Johannes Berg683b6d32012-11-08 21:25:48 +01002711 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002712 ret = true;
2713 break;
2714 }
2715
2716 mutex_unlock(&rdev->devlist_mtx);
2717
2718 return ret;
2719}
2720
Jouni Malinene39e5b52012-09-30 19:29:39 +03002721static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2722 enum nl80211_auth_type auth_type,
2723 enum nl80211_commands cmd)
2724{
2725 if (auth_type > NL80211_AUTHTYPE_MAX)
2726 return false;
2727
2728 switch (cmd) {
2729 case NL80211_CMD_AUTHENTICATE:
2730 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2731 auth_type == NL80211_AUTHTYPE_SAE)
2732 return false;
2733 return true;
2734 case NL80211_CMD_CONNECT:
2735 case NL80211_CMD_START_AP:
2736 /* SAE not supported yet */
2737 if (auth_type == NL80211_AUTHTYPE_SAE)
2738 return false;
2739 return true;
2740 default:
2741 return false;
2742 }
2743}
2744
Johannes Berg88600202012-02-13 15:17:18 +01002745static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2746{
2747 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2748 struct net_device *dev = info->user_ptr[1];
2749 struct wireless_dev *wdev = dev->ieee80211_ptr;
2750 struct cfg80211_ap_settings params;
2751 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002752 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002753
2754 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2755 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2756 return -EOPNOTSUPP;
2757
2758 if (!rdev->ops->start_ap)
2759 return -EOPNOTSUPP;
2760
2761 if (wdev->beacon_interval)
2762 return -EALREADY;
2763
2764 memset(&params, 0, sizeof(params));
2765
2766 /* these are required for START_AP */
2767 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2768 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2769 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2770 return -EINVAL;
2771
2772 err = nl80211_parse_beacon(info, &params.beacon);
2773 if (err)
2774 return err;
2775
2776 params.beacon_interval =
2777 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2778 params.dtim_period =
2779 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2780
2781 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2782 if (err)
2783 return err;
2784
2785 /*
2786 * In theory, some of these attributes should be required here
2787 * but since they were not used when the command was originally
2788 * added, keep them optional for old user space programs to let
2789 * them continue to work with drivers that do not need the
2790 * additional information -- drivers must check!
2791 */
2792 if (info->attrs[NL80211_ATTR_SSID]) {
2793 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2794 params.ssid_len =
2795 nla_len(info->attrs[NL80211_ATTR_SSID]);
2796 if (params.ssid_len == 0 ||
2797 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2798 return -EINVAL;
2799 }
2800
2801 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2802 params.hidden_ssid = nla_get_u32(
2803 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2804 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2805 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2806 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2807 return -EINVAL;
2808 }
2809
2810 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2811
2812 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2813 params.auth_type = nla_get_u32(
2814 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002815 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2816 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002817 return -EINVAL;
2818 } else
2819 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2820
2821 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2822 NL80211_MAX_NR_CIPHER_SUITES);
2823 if (err)
2824 return err;
2825
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302826 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2827 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2828 return -EOPNOTSUPP;
2829 params.inactivity_timeout = nla_get_u16(
2830 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2831 }
2832
Johannes Berg53cabad2012-11-14 15:17:28 +01002833 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2834 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2835 return -EINVAL;
2836 params.p2p_ctwindow =
2837 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2838 if (params.p2p_ctwindow > 127)
2839 return -EINVAL;
2840 if (params.p2p_ctwindow != 0 &&
2841 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2842 return -EINVAL;
2843 }
2844
2845 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2846 u8 tmp;
2847
2848 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2849 return -EINVAL;
2850 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2851 if (tmp > 1)
2852 return -EINVAL;
2853 params.p2p_opp_ps = tmp;
2854 if (params.p2p_opp_ps != 0 &&
2855 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2856 return -EINVAL;
2857 }
2858
Johannes Bergaa430da2012-05-16 23:50:18 +02002859 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002860 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2861 if (err)
2862 return err;
2863 } else if (wdev->preset_chandef.chan) {
2864 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002865 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002866 return -EINVAL;
2867
Johannes Berg683b6d32012-11-08 21:25:48 +01002868 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002869 return -EINVAL;
2870
Simon Wunderlich04f39042013-02-08 18:16:19 +01002871 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
2872 if (err < 0)
2873 return err;
2874 if (err) {
2875 radar_detect_width = BIT(params.chandef.width);
2876 params.radar_required = true;
2877 }
2878
Michal Kaziore4e32452012-06-29 12:47:08 +02002879 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01002880 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
2881 params.chandef.chan,
2882 CHAN_MODE_SHARED,
2883 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02002884 mutex_unlock(&rdev->devlist_mtx);
2885
2886 if (err)
2887 return err;
2888
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302889 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
2890 params.acl = parse_acl_data(&rdev->wiphy, info);
2891 if (IS_ERR(params.acl))
2892 return PTR_ERR(params.acl);
2893 }
2894
Hila Gonene35e4d22012-06-27 17:19:42 +03002895 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002896 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002897 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002898 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002899 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002900 wdev->ssid_len = params.ssid_len;
2901 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002902 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302903
2904 kfree(params.acl);
2905
Johannes Berg56d18932011-05-09 18:41:15 +02002906 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002907}
2908
Johannes Berg88600202012-02-13 15:17:18 +01002909static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2910{
2911 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2912 struct net_device *dev = info->user_ptr[1];
2913 struct wireless_dev *wdev = dev->ieee80211_ptr;
2914 struct cfg80211_beacon_data params;
2915 int err;
2916
2917 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2918 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2919 return -EOPNOTSUPP;
2920
2921 if (!rdev->ops->change_beacon)
2922 return -EOPNOTSUPP;
2923
2924 if (!wdev->beacon_interval)
2925 return -EINVAL;
2926
2927 err = nl80211_parse_beacon(info, &params);
2928 if (err)
2929 return err;
2930
Hila Gonene35e4d22012-06-27 17:19:42 +03002931 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002932}
2933
2934static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002935{
Johannes Berg4c476992010-10-04 21:36:35 +02002936 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2937 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002938
Michal Kazior60771782012-06-29 12:46:56 +02002939 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002940}
2941
Johannes Berg5727ef12007-12-19 02:03:34 +01002942static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2943 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2944 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2945 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002946 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002947 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002948 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002949};
2950
Johannes Bergeccb8e82009-05-11 21:57:56 +03002951static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002952 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002953 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002954{
2955 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002956 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002957 int flag;
2958
Johannes Bergeccb8e82009-05-11 21:57:56 +03002959 /*
2960 * Try parsing the new attribute first so userspace
2961 * can specify both for older kernels.
2962 */
2963 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2964 if (nla) {
2965 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002966
Johannes Bergeccb8e82009-05-11 21:57:56 +03002967 sta_flags = nla_data(nla);
2968 params->sta_flags_mask = sta_flags->mask;
2969 params->sta_flags_set = sta_flags->set;
2970 if ((params->sta_flags_mask |
2971 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2972 return -EINVAL;
2973 return 0;
2974 }
2975
2976 /* if present, parse the old attribute */
2977
2978 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002979 if (!nla)
2980 return 0;
2981
2982 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2983 nla, sta_flags_policy))
2984 return -EINVAL;
2985
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002986 /*
2987 * Only allow certain flags for interface types so that
2988 * other attributes are silently ignored. Remember that
2989 * this is backward compatibility code with old userspace
2990 * and shouldn't be hit in other cases anyway.
2991 */
2992 switch (iftype) {
2993 case NL80211_IFTYPE_AP:
2994 case NL80211_IFTYPE_AP_VLAN:
2995 case NL80211_IFTYPE_P2P_GO:
2996 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2997 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2998 BIT(NL80211_STA_FLAG_WME) |
2999 BIT(NL80211_STA_FLAG_MFP);
3000 break;
3001 case NL80211_IFTYPE_P2P_CLIENT:
3002 case NL80211_IFTYPE_STATION:
3003 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3004 BIT(NL80211_STA_FLAG_TDLS_PEER);
3005 break;
3006 case NL80211_IFTYPE_MESH_POINT:
3007 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3008 BIT(NL80211_STA_FLAG_MFP) |
3009 BIT(NL80211_STA_FLAG_AUTHORIZED);
3010 default:
3011 return -EINVAL;
3012 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003013
Johannes Berg3383b5a2012-05-10 20:14:43 +02003014 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3015 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003016 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003017
Johannes Berg3383b5a2012-05-10 20:14:43 +02003018 /* no longer support new API additions in old API */
3019 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3020 return -EINVAL;
3021 }
3022 }
3023
Johannes Berg5727ef12007-12-19 02:03:34 +01003024 return 0;
3025}
3026
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003027static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3028 int attr)
3029{
3030 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003031 u32 bitrate;
3032 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003033
3034 rate = nla_nest_start(msg, attr);
3035 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003036 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003037
3038 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3039 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003040 /* report 16-bit bitrate only if we can */
3041 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003042 if (bitrate > 0 &&
3043 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3044 return false;
3045 if (bitrate_compat > 0 &&
3046 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3047 return false;
3048
3049 if (info->flags & RATE_INFO_FLAGS_MCS) {
3050 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3051 return false;
3052 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3053 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3054 return false;
3055 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3056 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3057 return false;
3058 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3059 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3060 return false;
3061 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3062 return false;
3063 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3064 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3065 return false;
3066 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3067 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3068 return false;
3069 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3070 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3071 return false;
3072 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3073 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3074 return false;
3075 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3076 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3077 return false;
3078 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003079
3080 nla_nest_end(msg, rate);
3081 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003082}
3083
Eric W. Biederman15e47302012-09-07 20:12:54 +00003084static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003085 int flags,
3086 struct cfg80211_registered_device *rdev,
3087 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003088 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003089{
3090 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003091 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003092
Eric W. Biederman15e47302012-09-07 20:12:54 +00003093 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003094 if (!hdr)
3095 return -1;
3096
David S. Miller9360ffd2012-03-29 04:41:26 -04003097 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3098 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3099 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3100 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003101
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003102 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3103 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003104 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003105 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3106 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3107 sinfo->connected_time))
3108 goto nla_put_failure;
3109 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3110 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3111 sinfo->inactive_time))
3112 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003113 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3114 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003115 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003116 (u32)sinfo->rx_bytes))
3117 goto nla_put_failure;
3118 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3119 NL80211_STA_INFO_TX_BYTES64)) &&
3120 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3121 (u32)sinfo->tx_bytes))
3122 goto nla_put_failure;
3123 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3124 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003125 sinfo->rx_bytes))
3126 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003127 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3128 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003129 sinfo->tx_bytes))
3130 goto nla_put_failure;
3131 if ((sinfo->filled & STATION_INFO_LLID) &&
3132 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3133 goto nla_put_failure;
3134 if ((sinfo->filled & STATION_INFO_PLID) &&
3135 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3136 goto nla_put_failure;
3137 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3138 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3139 sinfo->plink_state))
3140 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003141 switch (rdev->wiphy.signal_type) {
3142 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003143 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3144 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3145 sinfo->signal))
3146 goto nla_put_failure;
3147 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3148 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3149 sinfo->signal_avg))
3150 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003151 break;
3152 default:
3153 break;
3154 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003155 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003156 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3157 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003158 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003159 }
3160 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3161 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3162 NL80211_STA_INFO_RX_BITRATE))
3163 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003164 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003165 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3166 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3167 sinfo->rx_packets))
3168 goto nla_put_failure;
3169 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3170 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3171 sinfo->tx_packets))
3172 goto nla_put_failure;
3173 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3174 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3175 sinfo->tx_retries))
3176 goto nla_put_failure;
3177 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3178 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3179 sinfo->tx_failed))
3180 goto nla_put_failure;
3181 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3182 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3183 sinfo->beacon_loss_count))
3184 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003185 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3186 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3187 sinfo->local_pm))
3188 goto nla_put_failure;
3189 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3190 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3191 sinfo->peer_pm))
3192 goto nla_put_failure;
3193 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3194 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3195 sinfo->nonpeer_pm))
3196 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003197 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3198 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3199 if (!bss_param)
3200 goto nla_put_failure;
3201
David S. Miller9360ffd2012-03-29 04:41:26 -04003202 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3203 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3204 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3205 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3206 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3207 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3208 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3209 sinfo->bss_param.dtim_period) ||
3210 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3211 sinfo->bss_param.beacon_interval))
3212 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003213
3214 nla_nest_end(msg, bss_param);
3215 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003216 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3217 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3218 sizeof(struct nl80211_sta_flag_update),
3219 &sinfo->sta_flags))
3220 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003221 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3222 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3223 sinfo->t_offset))
3224 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003225 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003226
David S. Miller9360ffd2012-03-29 04:41:26 -04003227 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3228 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3229 sinfo->assoc_req_ies))
3230 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003231
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003232 return genlmsg_end(msg, hdr);
3233
3234 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003235 genlmsg_cancel(msg, hdr);
3236 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003237}
3238
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003239static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003240 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003241{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003242 struct station_info sinfo;
3243 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003244 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003245 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003246 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003247 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003248
Johannes Berg67748892010-10-04 21:14:06 +02003249 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3250 if (err)
3251 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003252
3253 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003254 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003255 goto out_err;
3256 }
3257
Johannes Bergbba95fe2008-07-29 13:22:51 +02003258 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003259 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003260 err = rdev_dump_station(dev, netdev, sta_idx,
3261 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003262 if (err == -ENOENT)
3263 break;
3264 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003265 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003266
3267 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003268 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003269 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003270 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003271 &sinfo) < 0)
3272 goto out;
3273
3274 sta_idx++;
3275 }
3276
3277
3278 out:
3279 cb->args[1] = sta_idx;
3280 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003281 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003282 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003283
3284 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003285}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003286
Johannes Berg5727ef12007-12-19 02:03:34 +01003287static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3288{
Johannes Berg4c476992010-10-04 21:36:35 +02003289 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3290 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003291 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003292 struct sk_buff *msg;
3293 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003294 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003295
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003296 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003297
3298 if (!info->attrs[NL80211_ATTR_MAC])
3299 return -EINVAL;
3300
3301 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3302
Johannes Berg4c476992010-10-04 21:36:35 +02003303 if (!rdev->ops->get_station)
3304 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003305
Hila Gonene35e4d22012-06-27 17:19:42 +03003306 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003307 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003308 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003309
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003311 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003312 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003313
Eric W. Biederman15e47302012-09-07 20:12:54 +00003314 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003315 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003316 nlmsg_free(msg);
3317 return -ENOBUFS;
3318 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003319
Johannes Berg4c476992010-10-04 21:36:35 +02003320 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003321}
3322
3323/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003324 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003325 */
Johannes Berg80b99892011-11-18 16:23:01 +01003326static struct net_device *get_vlan(struct genl_info *info,
3327 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003328{
Johannes Berg463d0182009-07-14 00:33:35 +02003329 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003330 struct net_device *v;
3331 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003332
Johannes Berg80b99892011-11-18 16:23:01 +01003333 if (!vlanattr)
3334 return NULL;
3335
3336 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3337 if (!v)
3338 return ERR_PTR(-ENODEV);
3339
3340 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3341 ret = -EINVAL;
3342 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003343 }
Johannes Berg80b99892011-11-18 16:23:01 +01003344
3345 if (!netif_running(v)) {
3346 ret = -ENETDOWN;
3347 goto error;
3348 }
3349
3350 return v;
3351 error:
3352 dev_put(v);
3353 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003354}
3355
Jouni Malinendf881292013-02-14 21:10:54 +02003356static struct nla_policy
3357nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3358 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3359 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3360};
3361
Johannes Bergff276692013-02-15 00:09:01 +01003362static int nl80211_parse_sta_wme(struct genl_info *info,
3363 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003364{
Jouni Malinendf881292013-02-14 21:10:54 +02003365 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3366 struct nlattr *nla;
3367 int err;
3368
Jouni Malinendf881292013-02-14 21:10:54 +02003369 /* parse WME attributes if present */
3370 if (!info->attrs[NL80211_ATTR_STA_WME])
3371 return 0;
3372
3373 nla = info->attrs[NL80211_ATTR_STA_WME];
3374 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3375 nl80211_sta_wme_policy);
3376 if (err)
3377 return err;
3378
3379 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3380 params->uapsd_queues = nla_get_u8(
3381 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3382 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3383 return -EINVAL;
3384
3385 if (tb[NL80211_STA_WME_MAX_SP])
3386 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3387
3388 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3389 return -EINVAL;
3390
3391 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3392
3393 return 0;
3394}
3395
Johannes Bergff276692013-02-15 00:09:01 +01003396static int nl80211_set_station_tdls(struct genl_info *info,
3397 struct station_parameters *params)
3398{
3399 /* Dummy STA entry gets updated once the peer capabilities are known */
3400 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3401 params->ht_capa =
3402 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3403 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3404 params->vht_capa =
3405 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3406
3407 return nl80211_parse_sta_wme(info, params);
3408}
3409
Johannes Berg5727ef12007-12-19 02:03:34 +01003410static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3411{
Johannes Berg4c476992010-10-04 21:36:35 +02003412 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003413 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003414 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003415 struct station_parameters params;
3416 u8 *mac_addr = NULL;
3417
3418 memset(&params, 0, sizeof(params));
3419
3420 params.listen_interval = -1;
3421
3422 if (info->attrs[NL80211_ATTR_STA_AID])
3423 return -EINVAL;
3424
3425 if (!info->attrs[NL80211_ATTR_MAC])
3426 return -EINVAL;
3427
3428 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3429
3430 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3431 params.supported_rates =
3432 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3433 params.supported_rates_len =
3434 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3435 }
3436
Jouni Malinen9d62a982013-02-14 21:10:13 +02003437 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3438 params.capability =
3439 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3440 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3441 }
3442
3443 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3444 params.ext_capab =
3445 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3446 params.ext_capab_len =
3447 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3448 }
3449
Jouni Malinendf881292013-02-14 21:10:54 +02003450 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003451 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003452
Johannes Bergbdd90d52011-12-14 12:20:27 +01003453 if (!rdev->ops->change_station)
3454 return -EOPNOTSUPP;
3455
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003456 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003457 return -EINVAL;
3458
Johannes Bergf8bacc22013-02-14 23:27:01 +01003459 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003460 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003461 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3462 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3463 return -EINVAL;
3464 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003465
Johannes Bergf8bacc22013-02-14 23:27:01 +01003466 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003467 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003468 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3469 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3470 return -EINVAL;
3471 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3472 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003473
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003474 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3475 enum nl80211_mesh_power_mode pm = nla_get_u32(
3476 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3477
3478 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3479 pm > NL80211_MESH_POWER_MAX)
3480 return -EINVAL;
3481
3482 params.local_pm = pm;
3483 }
3484
Johannes Berga97f4422009-06-18 17:23:43 +02003485 switch (dev->ieee80211_ptr->iftype) {
3486 case NL80211_IFTYPE_AP:
3487 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003488 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003489 /* disallow mesh-specific things */
3490 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003491 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003492 if (params.local_pm)
3493 return -EINVAL;
Johannes Bergf8bacc22013-02-14 23:27:01 +01003494 if (params.sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3495 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003496
3497 /* TDLS can't be set, ... */
3498 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3499 return -EINVAL;
3500 /*
3501 * ... but don't bother the driver with it. This works around
3502 * a hostapd/wpa_supplicant issue -- it always includes the
3503 * TLDS_PEER flag in the mask even for AP mode.
3504 */
3505 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3506
3507 /* accept only the listed bits */
3508 if (params.sta_flags_mask &
3509 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003510 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3511 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003512 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3513 BIT(NL80211_STA_FLAG_WME) |
3514 BIT(NL80211_STA_FLAG_MFP)))
3515 return -EINVAL;
3516
Johannes Bergd582cff2012-10-26 17:53:44 +02003517 /* but authenticated/associated only if driver handles it */
3518 if (!(rdev->wiphy.features &
3519 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3520 params.sta_flags_mask &
3521 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3522 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3523 return -EINVAL;
3524
Johannes Bergba23d202012-12-27 17:32:09 +01003525 /* reject other things that can't change */
3526 if (params.supported_rates)
3527 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003528 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3529 return -EINVAL;
3530 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3531 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003532 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3533 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3534 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003535
Johannes Bergbdd90d52011-12-14 12:20:27 +01003536 /* must be last in here for error handling */
3537 params.vlan = get_vlan(info, rdev);
3538 if (IS_ERR(params.vlan))
3539 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003540 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003541 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003542 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003543 /*
3544 * Don't allow userspace to change the TDLS_PEER flag,
3545 * but silently ignore attempts to change it since we
3546 * don't have state here to verify that it doesn't try
3547 * to change the flag.
3548 */
3549 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Jouni Malinendf881292013-02-14 21:10:54 +02003550 /* Include parameters for TDLS peer (driver will check) */
3551 err = nl80211_set_station_tdls(info, &params);
3552 if (err)
3553 return err;
3554 /* disallow things sta doesn't support */
3555 if (params.plink_action)
3556 return -EINVAL;
3557 if (params.local_pm)
3558 return -EINVAL;
Johannes Bergf8bacc22013-02-14 23:27:01 +01003559 if (params.sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3560 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003561 /* reject any changes other than AUTHORIZED or WME (for TDLS) */
3562 if (params.sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3563 BIT(NL80211_STA_FLAG_WME)))
3564 return -EINVAL;
3565 break;
Antonio Quartulli267335d2012-01-31 20:25:47 +01003566 case NL80211_IFTYPE_ADHOC:
3567 /* disallow things sta doesn't support */
3568 if (params.plink_action)
3569 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003570 if (params.local_pm)
3571 return -EINVAL;
Johannes Bergf8bacc22013-02-14 23:27:01 +01003572 if (params.sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3573 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003574 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3575 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3576 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003577 /* reject any changes other than AUTHORIZED */
3578 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3579 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003580 break;
3581 case NL80211_IFTYPE_MESH_POINT:
3582 /* disallow things mesh doesn't support */
3583 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003584 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003585 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003586 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003587 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3588 return -EINVAL;
3589 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3590 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003591 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3592 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3593 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003594 /*
3595 * No special handling for TDLS here -- the userspace
3596 * mesh code doesn't have this bug.
3597 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003598 if (params.sta_flags_mask &
3599 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003600 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003601 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003602 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003603 break;
3604 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003605 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003606 }
3607
Johannes Bergbdd90d52011-12-14 12:20:27 +01003608 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003609
Hila Gonene35e4d22012-06-27 17:19:42 +03003610 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003611
Johannes Berg5727ef12007-12-19 02:03:34 +01003612 if (params.vlan)
3613 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003614
Johannes Berg5727ef12007-12-19 02:03:34 +01003615 return err;
3616}
3617
3618static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3619{
Johannes Berg4c476992010-10-04 21:36:35 +02003620 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003621 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003622 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003623 struct station_parameters params;
3624 u8 *mac_addr = NULL;
3625
3626 memset(&params, 0, sizeof(params));
3627
Johannes Berg984c3112013-02-14 23:43:25 +01003628 if (!rdev->ops->add_station)
3629 return -EOPNOTSUPP;
3630
Johannes Berg5727ef12007-12-19 02:03:34 +01003631 if (!info->attrs[NL80211_ATTR_MAC])
3632 return -EINVAL;
3633
Johannes Berg5727ef12007-12-19 02:03:34 +01003634 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3635 return -EINVAL;
3636
3637 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3638 return -EINVAL;
3639
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003640 if (!info->attrs[NL80211_ATTR_STA_AID])
3641 return -EINVAL;
3642
Johannes Berg5727ef12007-12-19 02:03:34 +01003643 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3644 params.supported_rates =
3645 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3646 params.supported_rates_len =
3647 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3648 params.listen_interval =
3649 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003650
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003651 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3652 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3653 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003654
Jouni Malinen9d62a982013-02-14 21:10:13 +02003655 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3656 params.capability =
3657 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3658 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3659 }
3660
3661 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3662 params.ext_capab =
3663 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3664 params.ext_capab_len =
3665 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3666 }
3667
Jouni Malinen36aedc92008-08-25 11:58:58 +03003668 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3669 params.ht_capa =
3670 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003671
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003672 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3673 params.vht_capa =
3674 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3675
Johannes Bergf8bacc22013-02-14 23:27:01 +01003676 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07003677 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003678 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3679 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3680 return -EINVAL;
3681 }
Javier Cardona96b78df2011-04-07 15:08:33 -07003682
Johannes Bergff276692013-02-15 00:09:01 +01003683 err = nl80211_parse_sta_wme(info, &params);
3684 if (err)
3685 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003686
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003687 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003688 return -EINVAL;
3689
Johannes Bergbdd90d52011-12-14 12:20:27 +01003690 switch (dev->ieee80211_ptr->iftype) {
3691 case NL80211_IFTYPE_AP:
3692 case NL80211_IFTYPE_AP_VLAN:
3693 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01003694 /* ignore WME attributes if iface/sta is not capable */
3695 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
3696 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
3697 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003698
Johannes Bergbdd90d52011-12-14 12:20:27 +01003699 /* TDLS peers cannot be added */
3700 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003701 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003702 /* but don't bother the driver with it */
3703 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003704
Johannes Bergd582cff2012-10-26 17:53:44 +02003705 /* allow authenticated/associated only if driver handles it */
3706 if (!(rdev->wiphy.features &
3707 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3708 params.sta_flags_mask &
3709 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3710 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3711 return -EINVAL;
3712
Johannes Bergbdd90d52011-12-14 12:20:27 +01003713 /* must be last in here for error handling */
3714 params.vlan = get_vlan(info, rdev);
3715 if (IS_ERR(params.vlan))
3716 return PTR_ERR(params.vlan);
3717 break;
3718 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01003719 /* ignore uAPSD data */
3720 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
3721
Johannes Bergd582cff2012-10-26 17:53:44 +02003722 /* associated is disallowed */
3723 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3724 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003725 /* TDLS peers cannot be added */
3726 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003727 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003728 break;
3729 case NL80211_IFTYPE_STATION:
Johannes Berg984c3112013-02-14 23:43:25 +01003730 /* ignore uAPSD data */
3731 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
3732
Johannes Bergd582cff2012-10-26 17:53:44 +02003733 /* associated is disallowed */
3734 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3735 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003736 /* Only TDLS peers can be added */
3737 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3738 return -EINVAL;
3739 /* Can only add if TDLS ... */
3740 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3741 return -EOPNOTSUPP;
3742 /* ... with external setup is supported */
3743 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3744 return -EOPNOTSUPP;
3745 break;
3746 default:
3747 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003748 }
3749
Johannes Bergbdd90d52011-12-14 12:20:27 +01003750 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003751
Hila Gonene35e4d22012-06-27 17:19:42 +03003752 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003753
Johannes Berg5727ef12007-12-19 02:03:34 +01003754 if (params.vlan)
3755 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003756 return err;
3757}
3758
3759static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3760{
Johannes Berg4c476992010-10-04 21:36:35 +02003761 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3762 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003763 u8 *mac_addr = NULL;
3764
3765 if (info->attrs[NL80211_ATTR_MAC])
3766 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3767
Johannes Berge80cf852009-05-11 14:43:13 +02003768 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003769 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003770 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003771 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3772 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003773
Johannes Berg4c476992010-10-04 21:36:35 +02003774 if (!rdev->ops->del_station)
3775 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003776
Hila Gonene35e4d22012-06-27 17:19:42 +03003777 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003778}
3779
Eric W. Biederman15e47302012-09-07 20:12:54 +00003780static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003781 int flags, struct net_device *dev,
3782 u8 *dst, u8 *next_hop,
3783 struct mpath_info *pinfo)
3784{
3785 void *hdr;
3786 struct nlattr *pinfoattr;
3787
Eric W. Biederman15e47302012-09-07 20:12:54 +00003788 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003789 if (!hdr)
3790 return -1;
3791
David S. Miller9360ffd2012-03-29 04:41:26 -04003792 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3793 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3794 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3795 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3796 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003797
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003798 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3799 if (!pinfoattr)
3800 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003801 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3802 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3803 pinfo->frame_qlen))
3804 goto nla_put_failure;
3805 if (((pinfo->filled & MPATH_INFO_SN) &&
3806 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3807 ((pinfo->filled & MPATH_INFO_METRIC) &&
3808 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3809 pinfo->metric)) ||
3810 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3811 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3812 pinfo->exptime)) ||
3813 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3814 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3815 pinfo->flags)) ||
3816 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3817 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3818 pinfo->discovery_timeout)) ||
3819 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3820 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3821 pinfo->discovery_retries)))
3822 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003823
3824 nla_nest_end(msg, pinfoattr);
3825
3826 return genlmsg_end(msg, hdr);
3827
3828 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003829 genlmsg_cancel(msg, hdr);
3830 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003831}
3832
3833static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003834 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003835{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003836 struct mpath_info pinfo;
3837 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003838 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003839 u8 dst[ETH_ALEN];
3840 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003841 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003842 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003843
Johannes Berg67748892010-10-04 21:14:06 +02003844 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3845 if (err)
3846 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003847
3848 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003849 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003850 goto out_err;
3851 }
3852
Jouni Malineneec60b02009-03-20 21:21:19 +02003853 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3854 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003855 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003856 }
3857
Johannes Bergbba95fe2008-07-29 13:22:51 +02003858 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003859 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3860 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003861 if (err == -ENOENT)
3862 break;
3863 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003864 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003865
Eric W. Biederman15e47302012-09-07 20:12:54 +00003866 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003867 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3868 netdev, dst, next_hop,
3869 &pinfo) < 0)
3870 goto out;
3871
3872 path_idx++;
3873 }
3874
3875
3876 out:
3877 cb->args[1] = path_idx;
3878 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003879 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003880 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003881 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003882}
3883
3884static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3885{
Johannes Berg4c476992010-10-04 21:36:35 +02003886 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003887 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003888 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003889 struct mpath_info pinfo;
3890 struct sk_buff *msg;
3891 u8 *dst = NULL;
3892 u8 next_hop[ETH_ALEN];
3893
3894 memset(&pinfo, 0, sizeof(pinfo));
3895
3896 if (!info->attrs[NL80211_ATTR_MAC])
3897 return -EINVAL;
3898
3899 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3900
Johannes Berg4c476992010-10-04 21:36:35 +02003901 if (!rdev->ops->get_mpath)
3902 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003903
Johannes Berg4c476992010-10-04 21:36:35 +02003904 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3905 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003906
Hila Gonene35e4d22012-06-27 17:19:42 +03003907 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003908 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003909 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003910
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003911 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003912 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003913 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003914
Eric W. Biederman15e47302012-09-07 20:12:54 +00003915 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003916 dev, dst, next_hop, &pinfo) < 0) {
3917 nlmsg_free(msg);
3918 return -ENOBUFS;
3919 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003920
Johannes Berg4c476992010-10-04 21:36:35 +02003921 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003922}
3923
3924static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3925{
Johannes Berg4c476992010-10-04 21:36:35 +02003926 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3927 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003928 u8 *dst = NULL;
3929 u8 *next_hop = NULL;
3930
3931 if (!info->attrs[NL80211_ATTR_MAC])
3932 return -EINVAL;
3933
3934 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3935 return -EINVAL;
3936
3937 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3938 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3939
Johannes Berg4c476992010-10-04 21:36:35 +02003940 if (!rdev->ops->change_mpath)
3941 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003942
Johannes Berg4c476992010-10-04 21:36:35 +02003943 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3944 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003945
Hila Gonene35e4d22012-06-27 17:19:42 +03003946 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003947}
Johannes Berg4c476992010-10-04 21:36:35 +02003948
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003949static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3950{
Johannes Berg4c476992010-10-04 21:36:35 +02003951 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3952 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003953 u8 *dst = NULL;
3954 u8 *next_hop = NULL;
3955
3956 if (!info->attrs[NL80211_ATTR_MAC])
3957 return -EINVAL;
3958
3959 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3960 return -EINVAL;
3961
3962 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3963 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3964
Johannes Berg4c476992010-10-04 21:36:35 +02003965 if (!rdev->ops->add_mpath)
3966 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003967
Johannes Berg4c476992010-10-04 21:36:35 +02003968 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3969 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003970
Hila Gonene35e4d22012-06-27 17:19:42 +03003971 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003972}
3973
3974static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3975{
Johannes Berg4c476992010-10-04 21:36:35 +02003976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3977 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003978 u8 *dst = NULL;
3979
3980 if (info->attrs[NL80211_ATTR_MAC])
3981 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3982
Johannes Berg4c476992010-10-04 21:36:35 +02003983 if (!rdev->ops->del_mpath)
3984 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003985
Hila Gonene35e4d22012-06-27 17:19:42 +03003986 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003987}
3988
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003989static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3990{
Johannes Berg4c476992010-10-04 21:36:35 +02003991 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3992 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003993 struct bss_parameters params;
3994
3995 memset(&params, 0, sizeof(params));
3996 /* default to not changing parameters */
3997 params.use_cts_prot = -1;
3998 params.use_short_preamble = -1;
3999 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004000 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004001 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004002 params.p2p_ctwindow = -1;
4003 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004004
4005 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4006 params.use_cts_prot =
4007 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4008 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4009 params.use_short_preamble =
4010 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4011 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4012 params.use_short_slot_time =
4013 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004014 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4015 params.basic_rates =
4016 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4017 params.basic_rates_len =
4018 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4019 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004020 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4021 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004022 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4023 params.ht_opmode =
4024 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004025
Johannes Berg53cabad2012-11-14 15:17:28 +01004026 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4027 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4028 return -EINVAL;
4029 params.p2p_ctwindow =
4030 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4031 if (params.p2p_ctwindow < 0)
4032 return -EINVAL;
4033 if (params.p2p_ctwindow != 0 &&
4034 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4035 return -EINVAL;
4036 }
4037
4038 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4039 u8 tmp;
4040
4041 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4042 return -EINVAL;
4043 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4044 if (tmp > 1)
4045 return -EINVAL;
4046 params.p2p_opp_ps = tmp;
4047 if (params.p2p_opp_ps &&
4048 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4049 return -EINVAL;
4050 }
4051
Johannes Berg4c476992010-10-04 21:36:35 +02004052 if (!rdev->ops->change_bss)
4053 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004054
Johannes Berg074ac8d2010-09-16 14:58:22 +02004055 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004056 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4057 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004058
Hila Gonene35e4d22012-06-27 17:19:42 +03004059 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004060}
4061
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004062static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004063 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4064 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4065 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4066 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4067 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4068 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4069};
4070
4071static int parse_reg_rule(struct nlattr *tb[],
4072 struct ieee80211_reg_rule *reg_rule)
4073{
4074 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4075 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4076
4077 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4078 return -EINVAL;
4079 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4080 return -EINVAL;
4081 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4082 return -EINVAL;
4083 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4084 return -EINVAL;
4085 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4086 return -EINVAL;
4087
4088 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4089
4090 freq_range->start_freq_khz =
4091 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4092 freq_range->end_freq_khz =
4093 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4094 freq_range->max_bandwidth_khz =
4095 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4096
4097 power_rule->max_eirp =
4098 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4099
4100 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4101 power_rule->max_antenna_gain =
4102 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4103
4104 return 0;
4105}
4106
4107static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4108{
4109 int r;
4110 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004111 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004112
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004113 /*
4114 * You should only get this when cfg80211 hasn't yet initialized
4115 * completely when built-in to the kernel right between the time
4116 * window between nl80211_init() and regulatory_init(), if that is
4117 * even possible.
4118 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004119 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004120 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004121
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004122 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4123 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004124
4125 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4126
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004127 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4128 user_reg_hint_type =
4129 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4130 else
4131 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4132
4133 switch (user_reg_hint_type) {
4134 case NL80211_USER_REG_HINT_USER:
4135 case NL80211_USER_REG_HINT_CELL_BASE:
4136 break;
4137 default:
4138 return -EINVAL;
4139 }
4140
4141 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004142
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004143 return r;
4144}
4145
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004146static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004147 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004148{
Johannes Berg4c476992010-10-04 21:36:35 +02004149 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004150 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004151 struct wireless_dev *wdev = dev->ieee80211_ptr;
4152 struct mesh_config cur_params;
4153 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004154 void *hdr;
4155 struct nlattr *pinfoattr;
4156 struct sk_buff *msg;
4157
Johannes Berg29cbe682010-12-03 09:20:44 +01004158 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4159 return -EOPNOTSUPP;
4160
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004161 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004162 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004163
Johannes Berg29cbe682010-12-03 09:20:44 +01004164 wdev_lock(wdev);
4165 /* If not connected, get default parameters */
4166 if (!wdev->mesh_id_len)
4167 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4168 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004169 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004170 wdev_unlock(wdev);
4171
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004172 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004173 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004174
4175 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004176 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004177 if (!msg)
4178 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004179 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004180 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004181 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004182 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004183 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004184 if (!pinfoattr)
4185 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004186 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4187 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4188 cur_params.dot11MeshRetryTimeout) ||
4189 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4190 cur_params.dot11MeshConfirmTimeout) ||
4191 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4192 cur_params.dot11MeshHoldingTimeout) ||
4193 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4194 cur_params.dot11MeshMaxPeerLinks) ||
4195 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4196 cur_params.dot11MeshMaxRetries) ||
4197 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4198 cur_params.dot11MeshTTL) ||
4199 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4200 cur_params.element_ttl) ||
4201 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4202 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004203 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4204 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004205 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4206 cur_params.dot11MeshHWMPmaxPREQretries) ||
4207 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4208 cur_params.path_refresh_time) ||
4209 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4210 cur_params.min_discovery_timeout) ||
4211 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4212 cur_params.dot11MeshHWMPactivePathTimeout) ||
4213 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4214 cur_params.dot11MeshHWMPpreqMinInterval) ||
4215 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4216 cur_params.dot11MeshHWMPperrMinInterval) ||
4217 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4218 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4219 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4220 cur_params.dot11MeshHWMPRootMode) ||
4221 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4222 cur_params.dot11MeshHWMPRannInterval) ||
4223 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4224 cur_params.dot11MeshGateAnnouncementProtocol) ||
4225 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4226 cur_params.dot11MeshForwarding) ||
4227 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004228 cur_params.rssi_threshold) ||
4229 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004230 cur_params.ht_opmode) ||
4231 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4232 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4233 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004234 cur_params.dot11MeshHWMProotInterval) ||
4235 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004236 cur_params.dot11MeshHWMPconfirmationInterval) ||
4237 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4238 cur_params.power_mode) ||
4239 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4240 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004241 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004242 nla_nest_end(msg, pinfoattr);
4243 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004244 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004245
Johannes Berg3b858752009-03-12 09:55:09 +01004246 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004247 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004248 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004249 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004250 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004251}
4252
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004253static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004254 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4255 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4256 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4257 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4258 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4259 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004260 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004261 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004262 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004263 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4264 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4265 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4266 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4267 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004268 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004269 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004270 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004271 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004272 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004273 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004274 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4275 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004276 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4277 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004278 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004279 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4280 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004281};
4282
Javier Cardonac80d5452010-12-16 17:37:49 -08004283static const struct nla_policy
4284 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004285 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004286 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4287 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004288 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004289 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004290 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004291 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004292};
4293
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004294static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004295 struct mesh_config *cfg,
4296 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004297{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004298 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004299 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004300
Marco Porschea54fba2013-01-07 16:04:48 +01004301#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4302do { \
4303 if (tb[attr]) { \
4304 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4305 return -EINVAL; \
4306 cfg->param = fn(tb[attr]); \
4307 mask |= (1 << (attr - 1)); \
4308 } \
4309} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004310
4311
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004312 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004313 return -EINVAL;
4314 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004315 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004316 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004317 return -EINVAL;
4318
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004319 /* This makes sure that there aren't more than 32 mesh config
4320 * parameters (otherwise our bitfield scheme would not work.) */
4321 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4322
4323 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004324 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004325 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4326 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004327 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004328 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4329 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004330 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004331 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4332 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004333 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004334 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4335 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004336 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004337 mask, NL80211_MESHCONF_MAX_RETRIES,
4338 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004339 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004340 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004341 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004342 mask, NL80211_MESHCONF_ELEMENT_TTL,
4343 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004344 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004345 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4346 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004347 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4348 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004349 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4350 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004351 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004352 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4353 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004354 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004355 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4356 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004357 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004358 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4359 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004360 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4361 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004362 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4363 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004364 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004365 1, 65535, mask,
4366 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004367 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004368 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004369 1, 65535, mask,
4370 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004371 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004372 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004373 dot11MeshHWMPnetDiameterTraversalTime,
4374 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004375 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4376 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004377 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4378 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4379 nla_get_u8);
4380 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4381 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004382 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004383 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004384 dot11MeshGateAnnouncementProtocol, 0, 1,
4385 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004386 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004387 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004388 mask, NL80211_MESHCONF_FORWARDING,
4389 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004390 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004391 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4392 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004393 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004394 mask, NL80211_MESHCONF_HT_OPMODE,
4395 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004396 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004397 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004398 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4399 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004400 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004401 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4402 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004403 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004404 dot11MeshHWMPconfirmationInterval,
4405 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004406 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4407 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004408 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4409 NL80211_MESH_POWER_ACTIVE,
4410 NL80211_MESH_POWER_MAX,
4411 mask, NL80211_MESHCONF_POWER_MODE,
4412 nla_get_u32);
4413 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4414 0, 65535, mask,
4415 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004416 if (mask_out)
4417 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004418
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004419 return 0;
4420
4421#undef FILL_IN_MESH_PARAM_IF_SET
4422}
4423
Javier Cardonac80d5452010-12-16 17:37:49 -08004424static int nl80211_parse_mesh_setup(struct genl_info *info,
4425 struct mesh_setup *setup)
4426{
4427 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4428
4429 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4430 return -EINVAL;
4431 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4432 info->attrs[NL80211_ATTR_MESH_SETUP],
4433 nl80211_mesh_setup_params_policy))
4434 return -EINVAL;
4435
Javier Cardonad299a1f2012-03-31 11:31:33 -07004436 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4437 setup->sync_method =
4438 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4439 IEEE80211_SYNC_METHOD_VENDOR :
4440 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4441
Javier Cardonac80d5452010-12-16 17:37:49 -08004442 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4443 setup->path_sel_proto =
4444 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4445 IEEE80211_PATH_PROTOCOL_VENDOR :
4446 IEEE80211_PATH_PROTOCOL_HWMP;
4447
4448 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4449 setup->path_metric =
4450 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4451 IEEE80211_PATH_METRIC_VENDOR :
4452 IEEE80211_PATH_METRIC_AIRTIME;
4453
Javier Cardona581a8b02011-04-07 15:08:27 -07004454
4455 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004456 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004457 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004458 if (!is_valid_ie_attr(ieattr))
4459 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004460 setup->ie = nla_data(ieattr);
4461 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004462 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004463 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4464 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004465
4466 return 0;
4467}
4468
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004469static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004470 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004471{
4472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4473 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004474 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004475 struct mesh_config cfg;
4476 u32 mask;
4477 int err;
4478
Johannes Berg29cbe682010-12-03 09:20:44 +01004479 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4480 return -EOPNOTSUPP;
4481
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004482 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004483 return -EOPNOTSUPP;
4484
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004485 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004486 if (err)
4487 return err;
4488
Johannes Berg29cbe682010-12-03 09:20:44 +01004489 wdev_lock(wdev);
4490 if (!wdev->mesh_id_len)
4491 err = -ENOLINK;
4492
4493 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004494 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004495
4496 wdev_unlock(wdev);
4497
4498 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004499}
4500
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004501static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4502{
Johannes Berg458f4f92012-12-06 15:47:38 +01004503 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004504 struct sk_buff *msg;
4505 void *hdr = NULL;
4506 struct nlattr *nl_reg_rules;
4507 unsigned int i;
4508 int err = -EINVAL;
4509
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004510 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004511
4512 if (!cfg80211_regdomain)
4513 goto out;
4514
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004515 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004516 if (!msg) {
4517 err = -ENOBUFS;
4518 goto out;
4519 }
4520
Eric W. Biederman15e47302012-09-07 20:12:54 +00004521 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004522 NL80211_CMD_GET_REG);
4523 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004524 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004525
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004526 if (reg_last_request_cell_base() &&
4527 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4528 NL80211_USER_REG_HINT_CELL_BASE))
4529 goto nla_put_failure;
4530
Johannes Berg458f4f92012-12-06 15:47:38 +01004531 rcu_read_lock();
4532 regdom = rcu_dereference(cfg80211_regdomain);
4533
4534 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4535 (regdom->dfs_region &&
4536 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4537 goto nla_put_failure_rcu;
4538
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004539 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4540 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004541 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004542
Johannes Berg458f4f92012-12-06 15:47:38 +01004543 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004544 struct nlattr *nl_reg_rule;
4545 const struct ieee80211_reg_rule *reg_rule;
4546 const struct ieee80211_freq_range *freq_range;
4547 const struct ieee80211_power_rule *power_rule;
4548
Johannes Berg458f4f92012-12-06 15:47:38 +01004549 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004550 freq_range = &reg_rule->freq_range;
4551 power_rule = &reg_rule->power_rule;
4552
4553 nl_reg_rule = nla_nest_start(msg, i);
4554 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004555 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004556
David S. Miller9360ffd2012-03-29 04:41:26 -04004557 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4558 reg_rule->flags) ||
4559 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4560 freq_range->start_freq_khz) ||
4561 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4562 freq_range->end_freq_khz) ||
4563 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4564 freq_range->max_bandwidth_khz) ||
4565 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4566 power_rule->max_antenna_gain) ||
4567 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4568 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004569 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004570
4571 nla_nest_end(msg, nl_reg_rule);
4572 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004573 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004574
4575 nla_nest_end(msg, nl_reg_rules);
4576
4577 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004578 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004579 goto out;
4580
Johannes Berg458f4f92012-12-06 15:47:38 +01004581nla_put_failure_rcu:
4582 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004583nla_put_failure:
4584 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004585put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004586 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004587 err = -EMSGSIZE;
4588out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004589 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004590 return err;
4591}
4592
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004593static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4594{
4595 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4596 struct nlattr *nl_reg_rule;
4597 char *alpha2 = NULL;
4598 int rem_reg_rules = 0, r = 0;
4599 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004600 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004601 struct ieee80211_regdomain *rd = NULL;
4602
4603 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4604 return -EINVAL;
4605
4606 if (!info->attrs[NL80211_ATTR_REG_RULES])
4607 return -EINVAL;
4608
4609 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4610
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004611 if (info->attrs[NL80211_ATTR_DFS_REGION])
4612 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4613
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004614 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004615 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004616 num_rules++;
4617 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004618 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004619 }
4620
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004621 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004622 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004623
4624 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004625 if (!rd)
4626 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004627
4628 rd->n_reg_rules = num_rules;
4629 rd->alpha2[0] = alpha2[0];
4630 rd->alpha2[1] = alpha2[1];
4631
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004632 /*
4633 * Disable DFS master mode if the DFS region was
4634 * not supported or known on this kernel.
4635 */
4636 if (reg_supported_dfs_region(dfs_region))
4637 rd->dfs_region = dfs_region;
4638
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004639 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004640 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004641 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004642 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4643 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004644 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4645 if (r)
4646 goto bad_reg;
4647
4648 rule_idx++;
4649
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004650 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4651 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004652 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004653 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004654 }
4655
Johannes Berg6913b492012-12-04 00:48:59 +01004656 mutex_lock(&cfg80211_mutex);
4657
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004658 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004659 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004660 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004661 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004662
Johannes Bergd2372b32008-10-24 20:32:20 +02004663 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004664 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004665 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004666}
4667
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004668static int validate_scan_freqs(struct nlattr *freqs)
4669{
4670 struct nlattr *attr1, *attr2;
4671 int n_channels = 0, tmp1, tmp2;
4672
4673 nla_for_each_nested(attr1, freqs, tmp1) {
4674 n_channels++;
4675 /*
4676 * Some hardware has a limited channel list for
4677 * scanning, and it is pretty much nonsensical
4678 * to scan for a channel twice, so disallow that
4679 * and don't require drivers to check that the
4680 * channel list they get isn't longer than what
4681 * they can scan, as long as they can scan all
4682 * the channels they registered at once.
4683 */
4684 nla_for_each_nested(attr2, freqs, tmp2)
4685 if (attr1 != attr2 &&
4686 nla_get_u32(attr1) == nla_get_u32(attr2))
4687 return 0;
4688 }
4689
4690 return n_channels;
4691}
4692
Johannes Berg2a519312009-02-10 21:25:55 +01004693static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4694{
Johannes Berg4c476992010-10-04 21:36:35 +02004695 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004696 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004697 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004698 struct nlattr *attr;
4699 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004700 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004701 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004702
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004703 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4704 return -EINVAL;
4705
Johannes Berg79c97e92009-07-07 03:56:12 +02004706 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004707
Johannes Berg4c476992010-10-04 21:36:35 +02004708 if (!rdev->ops->scan)
4709 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004710
Johannes Berg4c476992010-10-04 21:36:35 +02004711 if (rdev->scan_req)
4712 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004713
4714 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004715 n_channels = validate_scan_freqs(
4716 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004717 if (!n_channels)
4718 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004719 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004720 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004721 n_channels = 0;
4722
Johannes Berg2a519312009-02-10 21:25:55 +01004723 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4724 if (wiphy->bands[band])
4725 n_channels += wiphy->bands[band]->n_channels;
4726 }
4727
4728 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4729 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4730 n_ssids++;
4731
Johannes Berg4c476992010-10-04 21:36:35 +02004732 if (n_ssids > wiphy->max_scan_ssids)
4733 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004734
Jouni Malinen70692ad2009-02-16 19:39:13 +02004735 if (info->attrs[NL80211_ATTR_IE])
4736 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4737 else
4738 ie_len = 0;
4739
Johannes Berg4c476992010-10-04 21:36:35 +02004740 if (ie_len > wiphy->max_scan_ie_len)
4741 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004742
Johannes Berg2a519312009-02-10 21:25:55 +01004743 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004744 + sizeof(*request->ssids) * n_ssids
4745 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004746 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004747 if (!request)
4748 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004749
Johannes Berg2a519312009-02-10 21:25:55 +01004750 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004751 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004752 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004753 if (ie_len) {
4754 if (request->ssids)
4755 request->ie = (void *)(request->ssids + n_ssids);
4756 else
4757 request->ie = (void *)(request->channels + n_channels);
4758 }
Johannes Berg2a519312009-02-10 21:25:55 +01004759
Johannes Berg584991d2009-11-02 13:32:03 +01004760 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004761 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4762 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004763 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004764 struct ieee80211_channel *chan;
4765
4766 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4767
4768 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004769 err = -EINVAL;
4770 goto out_free;
4771 }
Johannes Berg584991d2009-11-02 13:32:03 +01004772
4773 /* ignore disabled channels */
4774 if (chan->flags & IEEE80211_CHAN_DISABLED)
4775 continue;
4776
4777 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004778 i++;
4779 }
4780 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004781 enum ieee80211_band band;
4782
Johannes Berg2a519312009-02-10 21:25:55 +01004783 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004784 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4785 int j;
4786 if (!wiphy->bands[band])
4787 continue;
4788 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004789 struct ieee80211_channel *chan;
4790
4791 chan = &wiphy->bands[band]->channels[j];
4792
4793 if (chan->flags & IEEE80211_CHAN_DISABLED)
4794 continue;
4795
4796 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004797 i++;
4798 }
4799 }
4800 }
4801
Johannes Berg584991d2009-11-02 13:32:03 +01004802 if (!i) {
4803 err = -EINVAL;
4804 goto out_free;
4805 }
4806
4807 request->n_channels = i;
4808
Johannes Berg2a519312009-02-10 21:25:55 +01004809 i = 0;
4810 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4811 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004812 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004813 err = -EINVAL;
4814 goto out_free;
4815 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004816 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004817 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004818 i++;
4819 }
4820 }
4821
Jouni Malinen70692ad2009-02-16 19:39:13 +02004822 if (info->attrs[NL80211_ATTR_IE]) {
4823 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004824 memcpy((void *)request->ie,
4825 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004826 request->ie_len);
4827 }
4828
Johannes Berg34850ab2011-07-18 18:08:35 +02004829 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004830 if (wiphy->bands[i])
4831 request->rates[i] =
4832 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004833
4834 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4835 nla_for_each_nested(attr,
4836 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4837 tmp) {
4838 enum ieee80211_band band = nla_type(attr);
4839
Dan Carpenter84404622011-07-29 11:52:18 +03004840 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004841 err = -EINVAL;
4842 goto out_free;
4843 }
4844 err = ieee80211_get_ratemask(wiphy->bands[band],
4845 nla_data(attr),
4846 nla_len(attr),
4847 &request->rates[band]);
4848 if (err)
4849 goto out_free;
4850 }
4851 }
4852
Sam Leffler46856bb2012-10-11 21:03:32 -07004853 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004854 request->flags = nla_get_u32(
4855 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004856 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4857 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4858 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4859 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004860 err = -EOPNOTSUPP;
4861 goto out_free;
4862 }
4863 }
Sam Lefflered4737712012-10-11 21:03:31 -07004864
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304865 request->no_cck =
4866 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4867
Johannes Bergfd014282012-06-18 19:17:03 +02004868 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004869 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004870 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004871
Johannes Berg79c97e92009-07-07 03:56:12 +02004872 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004873 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004874
Johannes Berg463d0182009-07-14 00:33:35 +02004875 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004876 nl80211_send_scan_start(rdev, wdev);
4877 if (wdev->netdev)
4878 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004879 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004880 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004881 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004882 kfree(request);
4883 }
Johannes Berg3b858752009-03-12 09:55:09 +01004884
Johannes Berg2a519312009-02-10 21:25:55 +01004885 return err;
4886}
4887
Luciano Coelho807f8a82011-05-11 17:09:35 +03004888static int nl80211_start_sched_scan(struct sk_buff *skb,
4889 struct genl_info *info)
4890{
4891 struct cfg80211_sched_scan_request *request;
4892 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4893 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004894 struct nlattr *attr;
4895 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004896 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004897 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004898 enum ieee80211_band band;
4899 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004900 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004901
4902 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4903 !rdev->ops->sched_scan_start)
4904 return -EOPNOTSUPP;
4905
4906 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4907 return -EINVAL;
4908
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004909 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4910 return -EINVAL;
4911
4912 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4913 if (interval == 0)
4914 return -EINVAL;
4915
Luciano Coelho807f8a82011-05-11 17:09:35 +03004916 wiphy = &rdev->wiphy;
4917
4918 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4919 n_channels = validate_scan_freqs(
4920 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4921 if (!n_channels)
4922 return -EINVAL;
4923 } else {
4924 n_channels = 0;
4925
4926 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4927 if (wiphy->bands[band])
4928 n_channels += wiphy->bands[band]->n_channels;
4929 }
4930
4931 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4932 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4933 tmp)
4934 n_ssids++;
4935
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004936 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004937 return -EINVAL;
4938
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004939 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4940 nla_for_each_nested(attr,
4941 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4942 tmp)
4943 n_match_sets++;
4944
4945 if (n_match_sets > wiphy->max_match_sets)
4946 return -EINVAL;
4947
Luciano Coelho807f8a82011-05-11 17:09:35 +03004948 if (info->attrs[NL80211_ATTR_IE])
4949 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4950 else
4951 ie_len = 0;
4952
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004953 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004954 return -EINVAL;
4955
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004956 mutex_lock(&rdev->sched_scan_mtx);
4957
4958 if (rdev->sched_scan_req) {
4959 err = -EINPROGRESS;
4960 goto out;
4961 }
4962
Luciano Coelho807f8a82011-05-11 17:09:35 +03004963 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004964 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004965 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004966 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004967 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004968 if (!request) {
4969 err = -ENOMEM;
4970 goto out;
4971 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004972
4973 if (n_ssids)
4974 request->ssids = (void *)&request->channels[n_channels];
4975 request->n_ssids = n_ssids;
4976 if (ie_len) {
4977 if (request->ssids)
4978 request->ie = (void *)(request->ssids + n_ssids);
4979 else
4980 request->ie = (void *)(request->channels + n_channels);
4981 }
4982
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004983 if (n_match_sets) {
4984 if (request->ie)
4985 request->match_sets = (void *)(request->ie + ie_len);
4986 else if (request->ssids)
4987 request->match_sets =
4988 (void *)(request->ssids + n_ssids);
4989 else
4990 request->match_sets =
4991 (void *)(request->channels + n_channels);
4992 }
4993 request->n_match_sets = n_match_sets;
4994
Luciano Coelho807f8a82011-05-11 17:09:35 +03004995 i = 0;
4996 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4997 /* user specified, bail out if channel not found */
4998 nla_for_each_nested(attr,
4999 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5000 tmp) {
5001 struct ieee80211_channel *chan;
5002
5003 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5004
5005 if (!chan) {
5006 err = -EINVAL;
5007 goto out_free;
5008 }
5009
5010 /* ignore disabled channels */
5011 if (chan->flags & IEEE80211_CHAN_DISABLED)
5012 continue;
5013
5014 request->channels[i] = chan;
5015 i++;
5016 }
5017 } else {
5018 /* all channels */
5019 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5020 int j;
5021 if (!wiphy->bands[band])
5022 continue;
5023 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5024 struct ieee80211_channel *chan;
5025
5026 chan = &wiphy->bands[band]->channels[j];
5027
5028 if (chan->flags & IEEE80211_CHAN_DISABLED)
5029 continue;
5030
5031 request->channels[i] = chan;
5032 i++;
5033 }
5034 }
5035 }
5036
5037 if (!i) {
5038 err = -EINVAL;
5039 goto out_free;
5040 }
5041
5042 request->n_channels = i;
5043
5044 i = 0;
5045 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5046 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5047 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005048 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005049 err = -EINVAL;
5050 goto out_free;
5051 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005052 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005053 memcpy(request->ssids[i].ssid, nla_data(attr),
5054 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005055 i++;
5056 }
5057 }
5058
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005059 i = 0;
5060 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5061 nla_for_each_nested(attr,
5062 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5063 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005064 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005065
5066 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5067 nla_data(attr), nla_len(attr),
5068 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005069 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005070 if (ssid) {
5071 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5072 err = -EINVAL;
5073 goto out_free;
5074 }
5075 memcpy(request->match_sets[i].ssid.ssid,
5076 nla_data(ssid), nla_len(ssid));
5077 request->match_sets[i].ssid.ssid_len =
5078 nla_len(ssid);
5079 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005080 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5081 if (rssi)
5082 request->rssi_thold = nla_get_u32(rssi);
5083 else
5084 request->rssi_thold =
5085 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005086 i++;
5087 }
5088 }
5089
Luciano Coelho807f8a82011-05-11 17:09:35 +03005090 if (info->attrs[NL80211_ATTR_IE]) {
5091 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5092 memcpy((void *)request->ie,
5093 nla_data(info->attrs[NL80211_ATTR_IE]),
5094 request->ie_len);
5095 }
5096
Sam Leffler46856bb2012-10-11 21:03:32 -07005097 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005098 request->flags = nla_get_u32(
5099 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005100 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5101 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5102 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5103 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005104 err = -EOPNOTSUPP;
5105 goto out_free;
5106 }
5107 }
Sam Lefflered4737712012-10-11 21:03:31 -07005108
Luciano Coelho807f8a82011-05-11 17:09:35 +03005109 request->dev = dev;
5110 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005111 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005112 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005113
Hila Gonene35e4d22012-06-27 17:19:42 +03005114 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005115 if (!err) {
5116 rdev->sched_scan_req = request;
5117 nl80211_send_sched_scan(rdev, dev,
5118 NL80211_CMD_START_SCHED_SCAN);
5119 goto out;
5120 }
5121
5122out_free:
5123 kfree(request);
5124out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005125 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005126 return err;
5127}
5128
5129static int nl80211_stop_sched_scan(struct sk_buff *skb,
5130 struct genl_info *info)
5131{
5132 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005133 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005134
5135 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5136 !rdev->ops->sched_scan_stop)
5137 return -EOPNOTSUPP;
5138
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005139 mutex_lock(&rdev->sched_scan_mtx);
5140 err = __cfg80211_stop_sched_scan(rdev, false);
5141 mutex_unlock(&rdev->sched_scan_mtx);
5142
5143 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005144}
5145
Simon Wunderlich04f39042013-02-08 18:16:19 +01005146static int nl80211_start_radar_detection(struct sk_buff *skb,
5147 struct genl_info *info)
5148{
5149 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5150 struct net_device *dev = info->user_ptr[1];
5151 struct wireless_dev *wdev = dev->ieee80211_ptr;
5152 struct cfg80211_chan_def chandef;
5153 int err;
5154
5155 err = nl80211_parse_chandef(rdev, info, &chandef);
5156 if (err)
5157 return err;
5158
5159 if (wdev->cac_started)
5160 return -EBUSY;
5161
5162 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5163 if (err < 0)
5164 return err;
5165
5166 if (err == 0)
5167 return -EINVAL;
5168
5169 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5170 return -EINVAL;
5171
5172 if (!rdev->ops->start_radar_detection)
5173 return -EOPNOTSUPP;
5174
5175 mutex_lock(&rdev->devlist_mtx);
5176 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5177 chandef.chan, CHAN_MODE_SHARED,
5178 BIT(chandef.width));
5179 if (err)
5180 goto err_locked;
5181
5182 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5183 if (!err) {
5184 wdev->channel = chandef.chan;
5185 wdev->cac_started = true;
5186 wdev->cac_start_time = jiffies;
5187 }
5188err_locked:
5189 mutex_unlock(&rdev->devlist_mtx);
5190
5191 return err;
5192}
5193
Johannes Berg9720bb32011-06-21 09:45:33 +02005194static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5195 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005196 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005197 struct wireless_dev *wdev,
5198 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005199{
Johannes Berg48ab9052009-07-10 18:42:31 +02005200 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005201 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005202 void *hdr;
5203 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005204 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005205
5206 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005207
Eric W. Biederman15e47302012-09-07 20:12:54 +00005208 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005209 NL80211_CMD_NEW_SCAN_RESULTS);
5210 if (!hdr)
5211 return -1;
5212
Johannes Berg9720bb32011-06-21 09:45:33 +02005213 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5214
David S. Miller9360ffd2012-03-29 04:41:26 -04005215 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5216 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5217 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005218
5219 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5220 if (!bss)
5221 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005222 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005223 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005224 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005225
5226 rcu_read_lock();
5227 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005228 if (ies) {
5229 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5230 goto fail_unlock_rcu;
5231 tsf = true;
5232 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5233 ies->len, ies->data))
5234 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005235 }
5236 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005237 if (ies) {
5238 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5239 goto fail_unlock_rcu;
5240 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5241 ies->len, ies->data))
5242 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005243 }
5244 rcu_read_unlock();
5245
David S. Miller9360ffd2012-03-29 04:41:26 -04005246 if (res->beacon_interval &&
5247 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5248 goto nla_put_failure;
5249 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5250 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5251 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5252 jiffies_to_msecs(jiffies - intbss->ts)))
5253 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005254
Johannes Berg77965c92009-02-18 18:45:06 +01005255 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005256 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005257 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5258 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005259 break;
5260 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005261 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5262 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005263 break;
5264 default:
5265 break;
5266 }
5267
Johannes Berg48ab9052009-07-10 18:42:31 +02005268 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005269 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005270 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005271 if (intbss == wdev->current_bss &&
5272 nla_put_u32(msg, NL80211_BSS_STATUS,
5273 NL80211_BSS_STATUS_ASSOCIATED))
5274 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005275 break;
5276 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005277 if (intbss == wdev->current_bss &&
5278 nla_put_u32(msg, NL80211_BSS_STATUS,
5279 NL80211_BSS_STATUS_IBSS_JOINED))
5280 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005281 break;
5282 default:
5283 break;
5284 }
5285
Johannes Berg2a519312009-02-10 21:25:55 +01005286 nla_nest_end(msg, bss);
5287
5288 return genlmsg_end(msg, hdr);
5289
Johannes Berg8cef2c92013-02-05 16:54:31 +01005290 fail_unlock_rcu:
5291 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005292 nla_put_failure:
5293 genlmsg_cancel(msg, hdr);
5294 return -EMSGSIZE;
5295}
5296
5297static int nl80211_dump_scan(struct sk_buff *skb,
5298 struct netlink_callback *cb)
5299{
Johannes Berg48ab9052009-07-10 18:42:31 +02005300 struct cfg80211_registered_device *rdev;
5301 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005302 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005303 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005304 int start = cb->args[1], idx = 0;
5305 int err;
5306
Johannes Berg67748892010-10-04 21:14:06 +02005307 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5308 if (err)
5309 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005310
Johannes Berg48ab9052009-07-10 18:42:31 +02005311 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005312
Johannes Berg48ab9052009-07-10 18:42:31 +02005313 wdev_lock(wdev);
5314 spin_lock_bh(&rdev->bss_lock);
5315 cfg80211_bss_expire(rdev);
5316
Johannes Berg9720bb32011-06-21 09:45:33 +02005317 cb->seq = rdev->bss_generation;
5318
Johannes Berg48ab9052009-07-10 18:42:31 +02005319 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005320 if (++idx <= start)
5321 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005322 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005323 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005324 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005325 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005326 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005327 }
5328 }
5329
Johannes Berg48ab9052009-07-10 18:42:31 +02005330 spin_unlock_bh(&rdev->bss_lock);
5331 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005332
5333 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005334 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005335
Johannes Berg67748892010-10-04 21:14:06 +02005336 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005337}
5338
Eric W. Biederman15e47302012-09-07 20:12:54 +00005339static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005340 int flags, struct net_device *dev,
5341 struct survey_info *survey)
5342{
5343 void *hdr;
5344 struct nlattr *infoattr;
5345
Eric W. Biederman15e47302012-09-07 20:12:54 +00005346 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005347 NL80211_CMD_NEW_SURVEY_RESULTS);
5348 if (!hdr)
5349 return -ENOMEM;
5350
David S. Miller9360ffd2012-03-29 04:41:26 -04005351 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5352 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005353
5354 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5355 if (!infoattr)
5356 goto nla_put_failure;
5357
David S. Miller9360ffd2012-03-29 04:41:26 -04005358 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5359 survey->channel->center_freq))
5360 goto nla_put_failure;
5361
5362 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5363 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5364 goto nla_put_failure;
5365 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5366 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5367 goto nla_put_failure;
5368 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5369 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5370 survey->channel_time))
5371 goto nla_put_failure;
5372 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5373 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5374 survey->channel_time_busy))
5375 goto nla_put_failure;
5376 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5377 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5378 survey->channel_time_ext_busy))
5379 goto nla_put_failure;
5380 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5381 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5382 survey->channel_time_rx))
5383 goto nla_put_failure;
5384 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5385 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5386 survey->channel_time_tx))
5387 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005388
5389 nla_nest_end(msg, infoattr);
5390
5391 return genlmsg_end(msg, hdr);
5392
5393 nla_put_failure:
5394 genlmsg_cancel(msg, hdr);
5395 return -EMSGSIZE;
5396}
5397
5398static int nl80211_dump_survey(struct sk_buff *skb,
5399 struct netlink_callback *cb)
5400{
5401 struct survey_info survey;
5402 struct cfg80211_registered_device *dev;
5403 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005404 int survey_idx = cb->args[1];
5405 int res;
5406
Johannes Berg67748892010-10-04 21:14:06 +02005407 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5408 if (res)
5409 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005410
5411 if (!dev->ops->dump_survey) {
5412 res = -EOPNOTSUPP;
5413 goto out_err;
5414 }
5415
5416 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005417 struct ieee80211_channel *chan;
5418
Hila Gonene35e4d22012-06-27 17:19:42 +03005419 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005420 if (res == -ENOENT)
5421 break;
5422 if (res)
5423 goto out_err;
5424
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005425 /* Survey without a channel doesn't make sense */
5426 if (!survey.channel) {
5427 res = -EINVAL;
5428 goto out;
5429 }
5430
5431 chan = ieee80211_get_channel(&dev->wiphy,
5432 survey.channel->center_freq);
5433 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5434 survey_idx++;
5435 continue;
5436 }
5437
Holger Schurig61fa7132009-11-11 12:25:40 +01005438 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005439 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005440 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5441 netdev,
5442 &survey) < 0)
5443 goto out;
5444 survey_idx++;
5445 }
5446
5447 out:
5448 cb->args[1] = survey_idx;
5449 res = skb->len;
5450 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005451 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005452 return res;
5453}
5454
Samuel Ortizb23aa672009-07-01 21:26:54 +02005455static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5456{
5457 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5458 NL80211_WPA_VERSION_2));
5459}
5460
Jouni Malinen636a5d32009-03-19 13:39:22 +02005461static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5462{
Johannes Berg4c476992010-10-04 21:36:35 +02005463 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5464 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005465 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005466 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5467 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005468 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005469 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005470 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005471
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005472 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5473 return -EINVAL;
5474
5475 if (!info->attrs[NL80211_ATTR_MAC])
5476 return -EINVAL;
5477
Jouni Malinen17780922009-03-27 20:52:47 +02005478 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5479 return -EINVAL;
5480
Johannes Berg19957bb2009-07-02 17:20:43 +02005481 if (!info->attrs[NL80211_ATTR_SSID])
5482 return -EINVAL;
5483
5484 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5485 return -EINVAL;
5486
Johannes Bergfffd0932009-07-08 14:22:54 +02005487 err = nl80211_parse_key(info, &key);
5488 if (err)
5489 return err;
5490
5491 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005492 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5493 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005494 if (!key.p.key || !key.p.key_len)
5495 return -EINVAL;
5496 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5497 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5498 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5499 key.p.key_len != WLAN_KEY_LEN_WEP104))
5500 return -EINVAL;
5501 if (key.idx > 4)
5502 return -EINVAL;
5503 } else {
5504 key.p.key_len = 0;
5505 key.p.key = NULL;
5506 }
5507
Johannes Bergafea0b72010-08-10 09:46:42 +02005508 if (key.idx >= 0) {
5509 int i;
5510 bool ok = false;
5511 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5512 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5513 ok = true;
5514 break;
5515 }
5516 }
Johannes Berg4c476992010-10-04 21:36:35 +02005517 if (!ok)
5518 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005519 }
5520
Johannes Berg4c476992010-10-04 21:36:35 +02005521 if (!rdev->ops->auth)
5522 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005523
Johannes Berg074ac8d2010-09-16 14:58:22 +02005524 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005525 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5526 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005527
Johannes Berg19957bb2009-07-02 17:20:43 +02005528 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005529 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005530 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005531 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5532 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005533
Johannes Berg19957bb2009-07-02 17:20:43 +02005534 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5535 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5536
5537 if (info->attrs[NL80211_ATTR_IE]) {
5538 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5539 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5540 }
5541
5542 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005543 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005544 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005545
Jouni Malinene39e5b52012-09-30 19:29:39 +03005546 if (auth_type == NL80211_AUTHTYPE_SAE &&
5547 !info->attrs[NL80211_ATTR_SAE_DATA])
5548 return -EINVAL;
5549
5550 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5551 if (auth_type != NL80211_AUTHTYPE_SAE)
5552 return -EINVAL;
5553 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5554 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5555 /* need to include at least Auth Transaction and Status Code */
5556 if (sae_data_len < 4)
5557 return -EINVAL;
5558 }
5559
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005560 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5561
Johannes Berg95de8172012-01-20 13:55:25 +01005562 /*
5563 * Since we no longer track auth state, ignore
5564 * requests to only change local state.
5565 */
5566 if (local_state_change)
5567 return 0;
5568
Johannes Berg4c476992010-10-04 21:36:35 +02005569 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5570 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005571 key.p.key, key.p.key_len, key.idx,
5572 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005573}
5574
Johannes Bergc0692b82010-08-27 14:26:53 +03005575static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5576 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005577 struct cfg80211_crypto_settings *settings,
5578 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005579{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005580 memset(settings, 0, sizeof(*settings));
5581
Samuel Ortizb23aa672009-07-01 21:26:54 +02005582 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5583
Johannes Bergc0692b82010-08-27 14:26:53 +03005584 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5585 u16 proto;
5586 proto = nla_get_u16(
5587 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5588 settings->control_port_ethertype = cpu_to_be16(proto);
5589 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5590 proto != ETH_P_PAE)
5591 return -EINVAL;
5592 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5593 settings->control_port_no_encrypt = true;
5594 } else
5595 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5596
Samuel Ortizb23aa672009-07-01 21:26:54 +02005597 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5598 void *data;
5599 int len, i;
5600
5601 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5602 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5603 settings->n_ciphers_pairwise = len / sizeof(u32);
5604
5605 if (len % sizeof(u32))
5606 return -EINVAL;
5607
Johannes Berg3dc27d22009-07-02 21:36:37 +02005608 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005609 return -EINVAL;
5610
5611 memcpy(settings->ciphers_pairwise, data, len);
5612
5613 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005614 if (!cfg80211_supported_cipher_suite(
5615 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005616 settings->ciphers_pairwise[i]))
5617 return -EINVAL;
5618 }
5619
5620 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5621 settings->cipher_group =
5622 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005623 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5624 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005625 return -EINVAL;
5626 }
5627
5628 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5629 settings->wpa_versions =
5630 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5631 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5632 return -EINVAL;
5633 }
5634
5635 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5636 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005637 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005638
5639 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5640 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5641 settings->n_akm_suites = len / sizeof(u32);
5642
5643 if (len % sizeof(u32))
5644 return -EINVAL;
5645
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005646 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5647 return -EINVAL;
5648
Samuel Ortizb23aa672009-07-01 21:26:54 +02005649 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005650 }
5651
5652 return 0;
5653}
5654
Jouni Malinen636a5d32009-03-19 13:39:22 +02005655static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5656{
Johannes Berg4c476992010-10-04 21:36:35 +02005657 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5658 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005659 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005660 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005661 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005662 int err, ssid_len, ie_len = 0;
5663 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005664 u32 flags = 0;
5665 struct ieee80211_ht_cap *ht_capa = NULL;
5666 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005667
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005668 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5669 return -EINVAL;
5670
5671 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005672 !info->attrs[NL80211_ATTR_SSID] ||
5673 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005674 return -EINVAL;
5675
Johannes Berg4c476992010-10-04 21:36:35 +02005676 if (!rdev->ops->assoc)
5677 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005678
Johannes Berg074ac8d2010-09-16 14:58:22 +02005679 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005680 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5681 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005682
Johannes Berg19957bb2009-07-02 17:20:43 +02005683 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005684
Johannes Berg19957bb2009-07-02 17:20:43 +02005685 chan = ieee80211_get_channel(&rdev->wiphy,
5686 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005687 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5688 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005689
Johannes Berg19957bb2009-07-02 17:20:43 +02005690 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5691 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005692
5693 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005694 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5695 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005696 }
5697
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005698 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005699 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005700 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005701 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005702 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005703 else if (mfp != NL80211_MFP_NO)
5704 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005705 }
5706
Johannes Berg3e5d7642009-07-07 14:37:26 +02005707 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5708 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5709
Ben Greear7e7c8922011-11-18 11:31:59 -08005710 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5711 flags |= ASSOC_REQ_DISABLE_HT;
5712
5713 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5714 ht_capa_mask =
5715 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5716
5717 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5718 if (!ht_capa_mask)
5719 return -EINVAL;
5720 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5721 }
5722
Johannes Bergc0692b82010-08-27 14:26:53 +03005723 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005724 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005725 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5726 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005727 &crypto, flags, ht_capa,
5728 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005729
Jouni Malinen636a5d32009-03-19 13:39:22 +02005730 return err;
5731}
5732
5733static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5734{
Johannes Berg4c476992010-10-04 21:36:35 +02005735 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5736 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005737 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005738 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005739 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005740 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005741
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005742 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5743 return -EINVAL;
5744
5745 if (!info->attrs[NL80211_ATTR_MAC])
5746 return -EINVAL;
5747
5748 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5749 return -EINVAL;
5750
Johannes Berg4c476992010-10-04 21:36:35 +02005751 if (!rdev->ops->deauth)
5752 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005753
Johannes Berg074ac8d2010-09-16 14:58:22 +02005754 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005755 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5756 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005757
Johannes Berg19957bb2009-07-02 17:20:43 +02005758 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005759
Johannes Berg19957bb2009-07-02 17:20:43 +02005760 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5761 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005762 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005763 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005764 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005765
5766 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005767 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5768 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005769 }
5770
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005771 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5772
Johannes Berg4c476992010-10-04 21:36:35 +02005773 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5774 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005775}
5776
5777static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5778{
Johannes Berg4c476992010-10-04 21:36:35 +02005779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5780 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005781 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005782 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005783 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005784 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005785
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005786 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5787 return -EINVAL;
5788
5789 if (!info->attrs[NL80211_ATTR_MAC])
5790 return -EINVAL;
5791
5792 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5793 return -EINVAL;
5794
Johannes Berg4c476992010-10-04 21:36:35 +02005795 if (!rdev->ops->disassoc)
5796 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005797
Johannes Berg074ac8d2010-09-16 14:58:22 +02005798 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005799 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5800 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005801
Johannes Berg19957bb2009-07-02 17:20:43 +02005802 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005803
Johannes Berg19957bb2009-07-02 17:20:43 +02005804 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5805 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005806 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005807 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005808 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005809
5810 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005811 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5812 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005813 }
5814
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005815 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5816
Johannes Berg4c476992010-10-04 21:36:35 +02005817 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5818 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005819}
5820
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005821static bool
5822nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5823 int mcast_rate[IEEE80211_NUM_BANDS],
5824 int rateval)
5825{
5826 struct wiphy *wiphy = &rdev->wiphy;
5827 bool found = false;
5828 int band, i;
5829
5830 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5831 struct ieee80211_supported_band *sband;
5832
5833 sband = wiphy->bands[band];
5834 if (!sband)
5835 continue;
5836
5837 for (i = 0; i < sband->n_bitrates; i++) {
5838 if (sband->bitrates[i].bitrate == rateval) {
5839 mcast_rate[band] = i + 1;
5840 found = true;
5841 break;
5842 }
5843 }
5844 }
5845
5846 return found;
5847}
5848
Johannes Berg04a773a2009-04-19 21:24:32 +02005849static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5850{
Johannes Berg4c476992010-10-04 21:36:35 +02005851 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5852 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005853 struct cfg80211_ibss_params ibss;
5854 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005855 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005856 int err;
5857
Johannes Berg8e30bc52009-04-22 17:45:38 +02005858 memset(&ibss, 0, sizeof(ibss));
5859
Johannes Berg04a773a2009-04-19 21:24:32 +02005860 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5861 return -EINVAL;
5862
Johannes Berg683b6d32012-11-08 21:25:48 +01005863 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005864 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5865 return -EINVAL;
5866
Johannes Berg8e30bc52009-04-22 17:45:38 +02005867 ibss.beacon_interval = 100;
5868
5869 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5870 ibss.beacon_interval =
5871 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5872 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5873 return -EINVAL;
5874 }
5875
Johannes Berg4c476992010-10-04 21:36:35 +02005876 if (!rdev->ops->join_ibss)
5877 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005878
Johannes Berg4c476992010-10-04 21:36:35 +02005879 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5880 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005881
Johannes Berg79c97e92009-07-07 03:56:12 +02005882 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005883
Johannes Berg39193492011-09-16 13:45:25 +02005884 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005885 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005886
5887 if (!is_valid_ether_addr(ibss.bssid))
5888 return -EINVAL;
5889 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005890 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5891 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5892
5893 if (info->attrs[NL80211_ATTR_IE]) {
5894 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5895 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5896 }
5897
Johannes Berg683b6d32012-11-08 21:25:48 +01005898 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5899 if (err)
5900 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005901
Johannes Berg683b6d32012-11-08 21:25:48 +01005902 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005903 return -EINVAL;
5904
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005905 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5906 return -EINVAL;
5907 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5908 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005909 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005910
Johannes Berg04a773a2009-04-19 21:24:32 +02005911 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005912 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005913
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005914 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5915 u8 *rates =
5916 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5917 int n_rates =
5918 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5919 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005920 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005921
Johannes Berg34850ab2011-07-18 18:08:35 +02005922 err = ieee80211_get_ratemask(sband, rates, n_rates,
5923 &ibss.basic_rates);
5924 if (err)
5925 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005926 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005927
5928 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5929 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5930 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5931 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005932
Johannes Berg4c476992010-10-04 21:36:35 +02005933 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305934 bool no_ht = false;
5935
Johannes Berg4c476992010-10-04 21:36:35 +02005936 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305937 info->attrs[NL80211_ATTR_KEYS],
5938 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005939 if (IS_ERR(connkeys))
5940 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305941
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005942 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5943 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305944 kfree(connkeys);
5945 return -EINVAL;
5946 }
Johannes Berg4c476992010-10-04 21:36:35 +02005947 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005948
Antonio Quartulli267335d2012-01-31 20:25:47 +01005949 ibss.control_port =
5950 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5951
Johannes Berg4c476992010-10-04 21:36:35 +02005952 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005953 if (err)
5954 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005955 return err;
5956}
5957
5958static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5959{
Johannes Berg4c476992010-10-04 21:36:35 +02005960 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5961 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005962
Johannes Berg4c476992010-10-04 21:36:35 +02005963 if (!rdev->ops->leave_ibss)
5964 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005965
Johannes Berg4c476992010-10-04 21:36:35 +02005966 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5967 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005968
Johannes Berg4c476992010-10-04 21:36:35 +02005969 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005970}
5971
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005972static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5973{
5974 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5975 struct net_device *dev = info->user_ptr[1];
5976 int mcast_rate[IEEE80211_NUM_BANDS];
5977 u32 nla_rate;
5978 int err;
5979
5980 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5981 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5982 return -EOPNOTSUPP;
5983
5984 if (!rdev->ops->set_mcast_rate)
5985 return -EOPNOTSUPP;
5986
5987 memset(mcast_rate, 0, sizeof(mcast_rate));
5988
5989 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5990 return -EINVAL;
5991
5992 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5993 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5994 return -EINVAL;
5995
5996 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5997
5998 return err;
5999}
6000
6001
Johannes Bergaff89a92009-07-01 21:26:51 +02006002#ifdef CONFIG_NL80211_TESTMODE
6003static struct genl_multicast_group nl80211_testmode_mcgrp = {
6004 .name = "testmode",
6005};
6006
6007static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6008{
Johannes Berg4c476992010-10-04 21:36:35 +02006009 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006010 int err;
6011
6012 if (!info->attrs[NL80211_ATTR_TESTDATA])
6013 return -EINVAL;
6014
Johannes Bergaff89a92009-07-01 21:26:51 +02006015 err = -EOPNOTSUPP;
6016 if (rdev->ops->testmode_cmd) {
6017 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006018 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006019 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6020 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6021 rdev->testmode_info = NULL;
6022 }
6023
Johannes Bergaff89a92009-07-01 21:26:51 +02006024 return err;
6025}
6026
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006027static int nl80211_testmode_dump(struct sk_buff *skb,
6028 struct netlink_callback *cb)
6029{
Johannes Berg00918d32011-12-13 17:22:05 +01006030 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006031 int err;
6032 long phy_idx;
6033 void *data = NULL;
6034 int data_len = 0;
6035
6036 if (cb->args[0]) {
6037 /*
6038 * 0 is a valid index, but not valid for args[0],
6039 * so we need to offset by 1.
6040 */
6041 phy_idx = cb->args[0] - 1;
6042 } else {
6043 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6044 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6045 nl80211_policy);
6046 if (err)
6047 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006048
Johannes Berg2bd7e352012-06-15 14:23:16 +02006049 mutex_lock(&cfg80211_mutex);
6050 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6051 nl80211_fam.attrbuf);
6052 if (IS_ERR(rdev)) {
6053 mutex_unlock(&cfg80211_mutex);
6054 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006055 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006056 phy_idx = rdev->wiphy_idx;
6057 rdev = NULL;
6058 mutex_unlock(&cfg80211_mutex);
6059
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006060 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6061 cb->args[1] =
6062 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6063 }
6064
6065 if (cb->args[1]) {
6066 data = nla_data((void *)cb->args[1]);
6067 data_len = nla_len((void *)cb->args[1]);
6068 }
6069
6070 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006071 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6072 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006073 mutex_unlock(&cfg80211_mutex);
6074 return -ENOENT;
6075 }
Johannes Berg00918d32011-12-13 17:22:05 +01006076 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006077 mutex_unlock(&cfg80211_mutex);
6078
Johannes Berg00918d32011-12-13 17:22:05 +01006079 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006080 err = -EOPNOTSUPP;
6081 goto out_err;
6082 }
6083
6084 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006085 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006086 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6087 NL80211_CMD_TESTMODE);
6088 struct nlattr *tmdata;
6089
David S. Miller9360ffd2012-03-29 04:41:26 -04006090 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006091 genlmsg_cancel(skb, hdr);
6092 break;
6093 }
6094
6095 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6096 if (!tmdata) {
6097 genlmsg_cancel(skb, hdr);
6098 break;
6099 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006100 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006101 nla_nest_end(skb, tmdata);
6102
6103 if (err == -ENOBUFS || err == -ENOENT) {
6104 genlmsg_cancel(skb, hdr);
6105 break;
6106 } else if (err) {
6107 genlmsg_cancel(skb, hdr);
6108 goto out_err;
6109 }
6110
6111 genlmsg_end(skb, hdr);
6112 }
6113
6114 err = skb->len;
6115 /* see above */
6116 cb->args[0] = phy_idx + 1;
6117 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006118 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006119 return err;
6120}
6121
Johannes Bergaff89a92009-07-01 21:26:51 +02006122static struct sk_buff *
6123__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006124 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006125{
6126 struct sk_buff *skb;
6127 void *hdr;
6128 struct nlattr *data;
6129
6130 skb = nlmsg_new(approxlen + 100, gfp);
6131 if (!skb)
6132 return NULL;
6133
Eric W. Biederman15e47302012-09-07 20:12:54 +00006134 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006135 if (!hdr) {
6136 kfree_skb(skb);
6137 return NULL;
6138 }
6139
David S. Miller9360ffd2012-03-29 04:41:26 -04006140 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6141 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006142 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6143
6144 ((void **)skb->cb)[0] = rdev;
6145 ((void **)skb->cb)[1] = hdr;
6146 ((void **)skb->cb)[2] = data;
6147
6148 return skb;
6149
6150 nla_put_failure:
6151 kfree_skb(skb);
6152 return NULL;
6153}
6154
6155struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6156 int approxlen)
6157{
6158 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6159
6160 if (WARN_ON(!rdev->testmode_info))
6161 return NULL;
6162
6163 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006164 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006165 rdev->testmode_info->snd_seq,
6166 GFP_KERNEL);
6167}
6168EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6169
6170int cfg80211_testmode_reply(struct sk_buff *skb)
6171{
6172 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6173 void *hdr = ((void **)skb->cb)[1];
6174 struct nlattr *data = ((void **)skb->cb)[2];
6175
6176 if (WARN_ON(!rdev->testmode_info)) {
6177 kfree_skb(skb);
6178 return -EINVAL;
6179 }
6180
6181 nla_nest_end(skb, data);
6182 genlmsg_end(skb, hdr);
6183 return genlmsg_reply(skb, rdev->testmode_info);
6184}
6185EXPORT_SYMBOL(cfg80211_testmode_reply);
6186
6187struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6188 int approxlen, gfp_t gfp)
6189{
6190 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6191
6192 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6193}
6194EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6195
6196void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6197{
6198 void *hdr = ((void **)skb->cb)[1];
6199 struct nlattr *data = ((void **)skb->cb)[2];
6200
6201 nla_nest_end(skb, data);
6202 genlmsg_end(skb, hdr);
6203 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6204}
6205EXPORT_SYMBOL(cfg80211_testmode_event);
6206#endif
6207
Samuel Ortizb23aa672009-07-01 21:26:54 +02006208static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6209{
Johannes Berg4c476992010-10-04 21:36:35 +02006210 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6211 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006212 struct cfg80211_connect_params connect;
6213 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006214 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006215 int err;
6216
6217 memset(&connect, 0, sizeof(connect));
6218
6219 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6220 return -EINVAL;
6221
6222 if (!info->attrs[NL80211_ATTR_SSID] ||
6223 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6224 return -EINVAL;
6225
6226 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6227 connect.auth_type =
6228 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006229 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6230 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006231 return -EINVAL;
6232 } else
6233 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6234
6235 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6236
Johannes Bergc0692b82010-08-27 14:26:53 +03006237 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006238 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006239 if (err)
6240 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006241
Johannes Berg074ac8d2010-09-16 14:58:22 +02006242 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006243 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6244 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006245
Johannes Berg79c97e92009-07-07 03:56:12 +02006246 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006247
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306248 connect.bg_scan_period = -1;
6249 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6250 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6251 connect.bg_scan_period =
6252 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6253 }
6254
Samuel Ortizb23aa672009-07-01 21:26:54 +02006255 if (info->attrs[NL80211_ATTR_MAC])
6256 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6257 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6258 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6259
6260 if (info->attrs[NL80211_ATTR_IE]) {
6261 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6262 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6263 }
6264
Jouni Malinencee00a92013-01-15 17:15:57 +02006265 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6266 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6267 if (connect.mfp != NL80211_MFP_REQUIRED &&
6268 connect.mfp != NL80211_MFP_NO)
6269 return -EINVAL;
6270 } else {
6271 connect.mfp = NL80211_MFP_NO;
6272 }
6273
Samuel Ortizb23aa672009-07-01 21:26:54 +02006274 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6275 connect.channel =
6276 ieee80211_get_channel(wiphy,
6277 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6278 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006279 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6280 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006281 }
6282
Johannes Bergfffd0932009-07-08 14:22:54 +02006283 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6284 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306285 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006286 if (IS_ERR(connkeys))
6287 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006288 }
6289
Ben Greear7e7c8922011-11-18 11:31:59 -08006290 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6291 connect.flags |= ASSOC_REQ_DISABLE_HT;
6292
6293 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6294 memcpy(&connect.ht_capa_mask,
6295 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6296 sizeof(connect.ht_capa_mask));
6297
6298 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006299 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6300 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006301 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006302 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006303 memcpy(&connect.ht_capa,
6304 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6305 sizeof(connect.ht_capa));
6306 }
6307
Johannes Bergfffd0932009-07-08 14:22:54 +02006308 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006309 if (err)
6310 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006311 return err;
6312}
6313
6314static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6315{
Johannes Berg4c476992010-10-04 21:36:35 +02006316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6317 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006318 u16 reason;
6319
6320 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6321 reason = WLAN_REASON_DEAUTH_LEAVING;
6322 else
6323 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6324
6325 if (reason == 0)
6326 return -EINVAL;
6327
Johannes Berg074ac8d2010-09-16 14:58:22 +02006328 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006329 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6330 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006331
Johannes Berg4c476992010-10-04 21:36:35 +02006332 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006333}
6334
Johannes Berg463d0182009-07-14 00:33:35 +02006335static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6336{
Johannes Berg4c476992010-10-04 21:36:35 +02006337 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006338 struct net *net;
6339 int err;
6340 u32 pid;
6341
6342 if (!info->attrs[NL80211_ATTR_PID])
6343 return -EINVAL;
6344
6345 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6346
Johannes Berg463d0182009-07-14 00:33:35 +02006347 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006348 if (IS_ERR(net))
6349 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006350
6351 err = 0;
6352
6353 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006354 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6355 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006356
Johannes Berg463d0182009-07-14 00:33:35 +02006357 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006358 return err;
6359}
6360
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006361static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6362{
Johannes Berg4c476992010-10-04 21:36:35 +02006363 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006364 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6365 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006366 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006367 struct cfg80211_pmksa pmksa;
6368
6369 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6370
6371 if (!info->attrs[NL80211_ATTR_MAC])
6372 return -EINVAL;
6373
6374 if (!info->attrs[NL80211_ATTR_PMKID])
6375 return -EINVAL;
6376
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006377 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6378 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6379
Johannes Berg074ac8d2010-09-16 14:58:22 +02006380 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006381 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6382 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006383
6384 switch (info->genlhdr->cmd) {
6385 case NL80211_CMD_SET_PMKSA:
6386 rdev_ops = rdev->ops->set_pmksa;
6387 break;
6388 case NL80211_CMD_DEL_PMKSA:
6389 rdev_ops = rdev->ops->del_pmksa;
6390 break;
6391 default:
6392 WARN_ON(1);
6393 break;
6394 }
6395
Johannes Berg4c476992010-10-04 21:36:35 +02006396 if (!rdev_ops)
6397 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006398
Johannes Berg4c476992010-10-04 21:36:35 +02006399 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006400}
6401
6402static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6403{
Johannes Berg4c476992010-10-04 21:36:35 +02006404 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6405 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006406
Johannes Berg074ac8d2010-09-16 14:58:22 +02006407 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006408 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6409 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006410
Johannes Berg4c476992010-10-04 21:36:35 +02006411 if (!rdev->ops->flush_pmksa)
6412 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006413
Hila Gonene35e4d22012-06-27 17:19:42 +03006414 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006415}
6416
Arik Nemtsov109086c2011-09-28 14:12:50 +03006417static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6418{
6419 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6420 struct net_device *dev = info->user_ptr[1];
6421 u8 action_code, dialog_token;
6422 u16 status_code;
6423 u8 *peer;
6424
6425 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6426 !rdev->ops->tdls_mgmt)
6427 return -EOPNOTSUPP;
6428
6429 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6430 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6431 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6432 !info->attrs[NL80211_ATTR_IE] ||
6433 !info->attrs[NL80211_ATTR_MAC])
6434 return -EINVAL;
6435
6436 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6437 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6438 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6439 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6440
Hila Gonene35e4d22012-06-27 17:19:42 +03006441 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6442 dialog_token, status_code,
6443 nla_data(info->attrs[NL80211_ATTR_IE]),
6444 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006445}
6446
6447static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6448{
6449 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6450 struct net_device *dev = info->user_ptr[1];
6451 enum nl80211_tdls_operation operation;
6452 u8 *peer;
6453
6454 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6455 !rdev->ops->tdls_oper)
6456 return -EOPNOTSUPP;
6457
6458 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6459 !info->attrs[NL80211_ATTR_MAC])
6460 return -EINVAL;
6461
6462 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6463 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6464
Hila Gonene35e4d22012-06-27 17:19:42 +03006465 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006466}
6467
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006468static int nl80211_remain_on_channel(struct sk_buff *skb,
6469 struct genl_info *info)
6470{
Johannes Berg4c476992010-10-04 21:36:35 +02006471 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006472 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006473 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006474 struct sk_buff *msg;
6475 void *hdr;
6476 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006477 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006478 int err;
6479
6480 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6481 !info->attrs[NL80211_ATTR_DURATION])
6482 return -EINVAL;
6483
6484 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6485
Johannes Berg7c4ef712011-11-18 15:33:48 +01006486 if (!rdev->ops->remain_on_channel ||
6487 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006488 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006489
Johannes Bergebf348f2012-06-01 12:50:54 +02006490 /*
6491 * We should be on that channel for at least a minimum amount of
6492 * time (10ms) but no longer than the driver supports.
6493 */
6494 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6495 duration > rdev->wiphy.max_remain_on_channel_duration)
6496 return -EINVAL;
6497
Johannes Berg683b6d32012-11-08 21:25:48 +01006498 err = nl80211_parse_chandef(rdev, info, &chandef);
6499 if (err)
6500 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006501
6502 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006503 if (!msg)
6504 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006505
Eric W. Biederman15e47302012-09-07 20:12:54 +00006506 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006507 NL80211_CMD_REMAIN_ON_CHANNEL);
6508
6509 if (IS_ERR(hdr)) {
6510 err = PTR_ERR(hdr);
6511 goto free_msg;
6512 }
6513
Johannes Berg683b6d32012-11-08 21:25:48 +01006514 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6515 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006516
6517 if (err)
6518 goto free_msg;
6519
David S. Miller9360ffd2012-03-29 04:41:26 -04006520 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6521 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006522
6523 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006524
6525 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006526
6527 nla_put_failure:
6528 err = -ENOBUFS;
6529 free_msg:
6530 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006531 return err;
6532}
6533
6534static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6535 struct genl_info *info)
6536{
Johannes Berg4c476992010-10-04 21:36:35 +02006537 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006538 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006539 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006540
6541 if (!info->attrs[NL80211_ATTR_COOKIE])
6542 return -EINVAL;
6543
Johannes Berg4c476992010-10-04 21:36:35 +02006544 if (!rdev->ops->cancel_remain_on_channel)
6545 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006546
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006547 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6548
Hila Gonene35e4d22012-06-27 17:19:42 +03006549 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006550}
6551
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006552static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6553 u8 *rates, u8 rates_len)
6554{
6555 u8 i;
6556 u32 mask = 0;
6557
6558 for (i = 0; i < rates_len; i++) {
6559 int rate = (rates[i] & 0x7f) * 5;
6560 int ridx;
6561 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6562 struct ieee80211_rate *srate =
6563 &sband->bitrates[ridx];
6564 if (rate == srate->bitrate) {
6565 mask |= 1 << ridx;
6566 break;
6567 }
6568 }
6569 if (ridx == sband->n_bitrates)
6570 return 0; /* rate not found */
6571 }
6572
6573 return mask;
6574}
6575
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006576static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6577 u8 *rates, u8 rates_len,
6578 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6579{
6580 u8 i;
6581
6582 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6583
6584 for (i = 0; i < rates_len; i++) {
6585 int ridx, rbit;
6586
6587 ridx = rates[i] / 8;
6588 rbit = BIT(rates[i] % 8);
6589
6590 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006591 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006592 return false;
6593
6594 /* check availability */
6595 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6596 mcs[ridx] |= rbit;
6597 else
6598 return false;
6599 }
6600
6601 return true;
6602}
6603
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006604static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006605 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6606 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006607 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6608 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006609};
6610
6611static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6612 struct genl_info *info)
6613{
6614 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006615 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006616 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006617 int rem, i;
6618 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006619 struct nlattr *tx_rates;
6620 struct ieee80211_supported_band *sband;
6621
6622 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6623 return -EINVAL;
6624
Johannes Berg4c476992010-10-04 21:36:35 +02006625 if (!rdev->ops->set_bitrate_mask)
6626 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006627
6628 memset(&mask, 0, sizeof(mask));
6629 /* Default to all rates enabled */
6630 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6631 sband = rdev->wiphy.bands[i];
6632 mask.control[i].legacy =
6633 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006634 if (sband)
6635 memcpy(mask.control[i].mcs,
6636 sband->ht_cap.mcs.rx_mask,
6637 sizeof(mask.control[i].mcs));
6638 else
6639 memset(mask.control[i].mcs, 0,
6640 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006641 }
6642
6643 /*
6644 * The nested attribute uses enum nl80211_band as the index. This maps
6645 * directly to the enum ieee80211_band values used in cfg80211.
6646 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006647 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006648 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6649 {
6650 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006651 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6652 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006653 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006654 if (sband == NULL)
6655 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006656 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6657 nla_len(tx_rates), nl80211_txattr_policy);
6658 if (tb[NL80211_TXRATE_LEGACY]) {
6659 mask.control[band].legacy = rateset_to_mask(
6660 sband,
6661 nla_data(tb[NL80211_TXRATE_LEGACY]),
6662 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306663 if ((mask.control[band].legacy == 0) &&
6664 nla_len(tb[NL80211_TXRATE_LEGACY]))
6665 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006666 }
6667 if (tb[NL80211_TXRATE_MCS]) {
6668 if (!ht_rateset_to_mask(
6669 sband,
6670 nla_data(tb[NL80211_TXRATE_MCS]),
6671 nla_len(tb[NL80211_TXRATE_MCS]),
6672 mask.control[band].mcs))
6673 return -EINVAL;
6674 }
6675
6676 if (mask.control[band].legacy == 0) {
6677 /* don't allow empty legacy rates if HT
6678 * is not even supported. */
6679 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6680 return -EINVAL;
6681
6682 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6683 if (mask.control[band].mcs[i])
6684 break;
6685
6686 /* legacy and mcs rates may not be both empty */
6687 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006688 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006689 }
6690 }
6691
Hila Gonene35e4d22012-06-27 17:19:42 +03006692 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006693}
6694
Johannes Berg2e161f72010-08-12 15:38:38 +02006695static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006696{
Johannes Berg4c476992010-10-04 21:36:35 +02006697 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006698 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006699 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006700
6701 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6702 return -EINVAL;
6703
Johannes Berg2e161f72010-08-12 15:38:38 +02006704 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6705 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006706
Johannes Berg71bbc992012-06-15 15:30:18 +02006707 switch (wdev->iftype) {
6708 case NL80211_IFTYPE_STATION:
6709 case NL80211_IFTYPE_ADHOC:
6710 case NL80211_IFTYPE_P2P_CLIENT:
6711 case NL80211_IFTYPE_AP:
6712 case NL80211_IFTYPE_AP_VLAN:
6713 case NL80211_IFTYPE_MESH_POINT:
6714 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006715 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006716 break;
6717 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006718 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006719 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006720
6721 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006722 if (!rdev->ops->mgmt_tx)
6723 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006724
Eric W. Biederman15e47302012-09-07 20:12:54 +00006725 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006726 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6727 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006728}
6729
Johannes Berg2e161f72010-08-12 15:38:38 +02006730static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006731{
Johannes Berg4c476992010-10-04 21:36:35 +02006732 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006733 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006734 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006735 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006736 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006737 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006738 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006739 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006740 bool offchan, no_cck, dont_wait_for_ack;
6741
6742 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006743
Johannes Berg683b6d32012-11-08 21:25:48 +01006744 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006745 return -EINVAL;
6746
Johannes Berg4c476992010-10-04 21:36:35 +02006747 if (!rdev->ops->mgmt_tx)
6748 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006749
Johannes Berg71bbc992012-06-15 15:30:18 +02006750 switch (wdev->iftype) {
6751 case NL80211_IFTYPE_STATION:
6752 case NL80211_IFTYPE_ADHOC:
6753 case NL80211_IFTYPE_P2P_CLIENT:
6754 case NL80211_IFTYPE_AP:
6755 case NL80211_IFTYPE_AP_VLAN:
6756 case NL80211_IFTYPE_MESH_POINT:
6757 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006758 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006759 break;
6760 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006761 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006762 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006763
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006764 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006765 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006766 return -EINVAL;
6767 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006768
6769 /*
6770 * We should wait on the channel for at least a minimum amount
6771 * of time (10ms) but no longer than the driver supports.
6772 */
6773 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6774 wait > rdev->wiphy.max_remain_on_channel_duration)
6775 return -EINVAL;
6776
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006777 }
6778
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006779 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6780
Johannes Berg7c4ef712011-11-18 15:33:48 +01006781 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6782 return -EINVAL;
6783
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306784 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6785
Johannes Berg683b6d32012-11-08 21:25:48 +01006786 err = nl80211_parse_chandef(rdev, info, &chandef);
6787 if (err)
6788 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006789
Johannes Berge247bd902011-11-04 11:18:21 +01006790 if (!dont_wait_for_ack) {
6791 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6792 if (!msg)
6793 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006794
Eric W. Biederman15e47302012-09-07 20:12:54 +00006795 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006796 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006797
Johannes Berge247bd902011-11-04 11:18:21 +01006798 if (IS_ERR(hdr)) {
6799 err = PTR_ERR(hdr);
6800 goto free_msg;
6801 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006802 }
Johannes Berge247bd902011-11-04 11:18:21 +01006803
Johannes Berg683b6d32012-11-08 21:25:48 +01006804 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006805 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6806 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006807 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006808 if (err)
6809 goto free_msg;
6810
Johannes Berge247bd902011-11-04 11:18:21 +01006811 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006812 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6813 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006814
Johannes Berge247bd902011-11-04 11:18:21 +01006815 genlmsg_end(msg, hdr);
6816 return genlmsg_reply(msg, info);
6817 }
6818
6819 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006820
6821 nla_put_failure:
6822 err = -ENOBUFS;
6823 free_msg:
6824 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006825 return err;
6826}
6827
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006828static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6829{
6830 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006831 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006832 u64 cookie;
6833
6834 if (!info->attrs[NL80211_ATTR_COOKIE])
6835 return -EINVAL;
6836
6837 if (!rdev->ops->mgmt_tx_cancel_wait)
6838 return -EOPNOTSUPP;
6839
Johannes Berg71bbc992012-06-15 15:30:18 +02006840 switch (wdev->iftype) {
6841 case NL80211_IFTYPE_STATION:
6842 case NL80211_IFTYPE_ADHOC:
6843 case NL80211_IFTYPE_P2P_CLIENT:
6844 case NL80211_IFTYPE_AP:
6845 case NL80211_IFTYPE_AP_VLAN:
6846 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006847 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006848 break;
6849 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006850 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006851 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006852
6853 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6854
Hila Gonene35e4d22012-06-27 17:19:42 +03006855 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006856}
6857
Kalle Valoffb9eb32010-02-17 17:58:10 +02006858static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6859{
Johannes Berg4c476992010-10-04 21:36:35 +02006860 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006861 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006862 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006863 u8 ps_state;
6864 bool state;
6865 int err;
6866
Johannes Berg4c476992010-10-04 21:36:35 +02006867 if (!info->attrs[NL80211_ATTR_PS_STATE])
6868 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006869
6870 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6871
Johannes Berg4c476992010-10-04 21:36:35 +02006872 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6873 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006874
6875 wdev = dev->ieee80211_ptr;
6876
Johannes Berg4c476992010-10-04 21:36:35 +02006877 if (!rdev->ops->set_power_mgmt)
6878 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006879
6880 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6881
6882 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006883 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006884
Hila Gonene35e4d22012-06-27 17:19:42 +03006885 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006886 if (!err)
6887 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006888 return err;
6889}
6890
6891static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6892{
Johannes Berg4c476992010-10-04 21:36:35 +02006893 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006894 enum nl80211_ps_state ps_state;
6895 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006896 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006897 struct sk_buff *msg;
6898 void *hdr;
6899 int err;
6900
Kalle Valoffb9eb32010-02-17 17:58:10 +02006901 wdev = dev->ieee80211_ptr;
6902
Johannes Berg4c476992010-10-04 21:36:35 +02006903 if (!rdev->ops->set_power_mgmt)
6904 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006905
6906 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006907 if (!msg)
6908 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006909
Eric W. Biederman15e47302012-09-07 20:12:54 +00006910 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006911 NL80211_CMD_GET_POWER_SAVE);
6912 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006913 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006914 goto free_msg;
6915 }
6916
6917 if (wdev->ps)
6918 ps_state = NL80211_PS_ENABLED;
6919 else
6920 ps_state = NL80211_PS_DISABLED;
6921
David S. Miller9360ffd2012-03-29 04:41:26 -04006922 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6923 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006924
6925 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006926 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006927
Johannes Berg4c476992010-10-04 21:36:35 +02006928 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006929 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006930 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006931 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006932 return err;
6933}
6934
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006935static struct nla_policy
6936nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6937 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6938 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6939 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006940 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6941 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6942 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006943};
6944
Thomas Pedersen84f10702012-07-12 16:17:33 -07006945static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006946 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006947{
6948 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6949 struct wireless_dev *wdev;
6950 struct net_device *dev = info->user_ptr[1];
6951
Johannes Bergd9d8b012012-11-26 12:51:52 +01006952 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006953 return -EINVAL;
6954
6955 wdev = dev->ieee80211_ptr;
6956
6957 if (!rdev->ops->set_cqm_txe_config)
6958 return -EOPNOTSUPP;
6959
6960 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6961 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6962 return -EOPNOTSUPP;
6963
Hila Gonene35e4d22012-06-27 17:19:42 +03006964 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006965}
6966
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006967static int nl80211_set_cqm_rssi(struct genl_info *info,
6968 s32 threshold, u32 hysteresis)
6969{
Johannes Berg4c476992010-10-04 21:36:35 +02006970 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006971 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006972 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006973
6974 if (threshold > 0)
6975 return -EINVAL;
6976
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006977 wdev = dev->ieee80211_ptr;
6978
Johannes Berg4c476992010-10-04 21:36:35 +02006979 if (!rdev->ops->set_cqm_rssi_config)
6980 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006981
Johannes Berg074ac8d2010-09-16 14:58:22 +02006982 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006983 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6984 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006985
Hila Gonene35e4d22012-06-27 17:19:42 +03006986 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006987}
6988
6989static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6990{
6991 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6992 struct nlattr *cqm;
6993 int err;
6994
6995 cqm = info->attrs[NL80211_ATTR_CQM];
6996 if (!cqm) {
6997 err = -EINVAL;
6998 goto out;
6999 }
7000
7001 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7002 nl80211_attr_cqm_policy);
7003 if (err)
7004 goto out;
7005
7006 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7007 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7008 s32 threshold;
7009 u32 hysteresis;
7010 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7011 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7012 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007013 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7014 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7015 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7016 u32 rate, pkts, intvl;
7017 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7018 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7019 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7020 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007021 } else
7022 err = -EINVAL;
7023
7024out:
7025 return err;
7026}
7027
Johannes Berg29cbe682010-12-03 09:20:44 +01007028static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7029{
7030 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7031 struct net_device *dev = info->user_ptr[1];
7032 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007033 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007034 int err;
7035
7036 /* start with default */
7037 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007038 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007039
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007040 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007041 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007042 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007043 if (err)
7044 return err;
7045 }
7046
7047 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7048 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7049 return -EINVAL;
7050
Javier Cardonac80d5452010-12-16 17:37:49 -08007051 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7052 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7053
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007054 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7055 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7056 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7057 return -EINVAL;
7058
Marco Porsch9bdbf042013-01-07 16:04:51 +01007059 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7060 setup.beacon_interval =
7061 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7062 if (setup.beacon_interval < 10 ||
7063 setup.beacon_interval > 10000)
7064 return -EINVAL;
7065 }
7066
7067 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7068 setup.dtim_period =
7069 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7070 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7071 return -EINVAL;
7072 }
7073
Javier Cardonac80d5452010-12-16 17:37:49 -08007074 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7075 /* parse additional setup parameters if given */
7076 err = nl80211_parse_mesh_setup(info, &setup);
7077 if (err)
7078 return err;
7079 }
7080
Johannes Bergcc1d2802012-05-16 23:50:20 +02007081 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007082 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7083 if (err)
7084 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007085 } else {
7086 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007087 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007088 }
7089
Javier Cardonac80d5452010-12-16 17:37:49 -08007090 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007091}
7092
7093static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7094{
7095 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7096 struct net_device *dev = info->user_ptr[1];
7097
7098 return cfg80211_leave_mesh(rdev, dev);
7099}
7100
Johannes Bergdfb89c52012-06-27 09:23:48 +02007101#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007102static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7103 struct cfg80211_registered_device *rdev)
7104{
7105 struct nlattr *nl_pats, *nl_pat;
7106 int i, pat_len;
7107
7108 if (!rdev->wowlan->n_patterns)
7109 return 0;
7110
7111 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7112 if (!nl_pats)
7113 return -ENOBUFS;
7114
7115 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7116 nl_pat = nla_nest_start(msg, i + 1);
7117 if (!nl_pat)
7118 return -ENOBUFS;
7119 pat_len = rdev->wowlan->patterns[i].pattern_len;
7120 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7121 DIV_ROUND_UP(pat_len, 8),
7122 rdev->wowlan->patterns[i].mask) ||
7123 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7124 pat_len, rdev->wowlan->patterns[i].pattern) ||
7125 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7126 rdev->wowlan->patterns[i].pkt_offset))
7127 return -ENOBUFS;
7128 nla_nest_end(msg, nl_pat);
7129 }
7130 nla_nest_end(msg, nl_pats);
7131
7132 return 0;
7133}
7134
Johannes Berg2a0e0472013-01-23 22:57:40 +01007135static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7136 struct cfg80211_wowlan_tcp *tcp)
7137{
7138 struct nlattr *nl_tcp;
7139
7140 if (!tcp)
7141 return 0;
7142
7143 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7144 if (!nl_tcp)
7145 return -ENOBUFS;
7146
7147 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7148 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7149 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7150 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7151 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7152 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7153 tcp->payload_len, tcp->payload) ||
7154 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7155 tcp->data_interval) ||
7156 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7157 tcp->wake_len, tcp->wake_data) ||
7158 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7159 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7160 return -ENOBUFS;
7161
7162 if (tcp->payload_seq.len &&
7163 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7164 sizeof(tcp->payload_seq), &tcp->payload_seq))
7165 return -ENOBUFS;
7166
7167 if (tcp->payload_tok.len &&
7168 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7169 sizeof(tcp->payload_tok) + tcp->tokens_size,
7170 &tcp->payload_tok))
7171 return -ENOBUFS;
7172
7173 return 0;
7174}
7175
Johannes Bergff1b6e62011-05-04 15:37:28 +02007176static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7177{
7178 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7179 struct sk_buff *msg;
7180 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007181 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007182
Johannes Berg2a0e0472013-01-23 22:57:40 +01007183 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7184 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007185 return -EOPNOTSUPP;
7186
Johannes Berg2a0e0472013-01-23 22:57:40 +01007187 if (rdev->wowlan && rdev->wowlan->tcp) {
7188 /* adjust size to have room for all the data */
7189 size += rdev->wowlan->tcp->tokens_size +
7190 rdev->wowlan->tcp->payload_len +
7191 rdev->wowlan->tcp->wake_len +
7192 rdev->wowlan->tcp->wake_len / 8;
7193 }
7194
7195 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007196 if (!msg)
7197 return -ENOMEM;
7198
Eric W. Biederman15e47302012-09-07 20:12:54 +00007199 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007200 NL80211_CMD_GET_WOWLAN);
7201 if (!hdr)
7202 goto nla_put_failure;
7203
7204 if (rdev->wowlan) {
7205 struct nlattr *nl_wowlan;
7206
7207 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7208 if (!nl_wowlan)
7209 goto nla_put_failure;
7210
David S. Miller9360ffd2012-03-29 04:41:26 -04007211 if ((rdev->wowlan->any &&
7212 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7213 (rdev->wowlan->disconnect &&
7214 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7215 (rdev->wowlan->magic_pkt &&
7216 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7217 (rdev->wowlan->gtk_rekey_failure &&
7218 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7219 (rdev->wowlan->eap_identity_req &&
7220 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7221 (rdev->wowlan->four_way_handshake &&
7222 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7223 (rdev->wowlan->rfkill_release &&
7224 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7225 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007226
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007227 if (nl80211_send_wowlan_patterns(msg, rdev))
7228 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007229
7230 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7231 goto nla_put_failure;
7232
Johannes Bergff1b6e62011-05-04 15:37:28 +02007233 nla_nest_end(msg, nl_wowlan);
7234 }
7235
7236 genlmsg_end(msg, hdr);
7237 return genlmsg_reply(msg, info);
7238
7239nla_put_failure:
7240 nlmsg_free(msg);
7241 return -ENOBUFS;
7242}
7243
Johannes Berg2a0e0472013-01-23 22:57:40 +01007244static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7245 struct nlattr *attr,
7246 struct cfg80211_wowlan *trig)
7247{
7248 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7249 struct cfg80211_wowlan_tcp *cfg;
7250 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7251 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7252 u32 size;
7253 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7254 int err, port;
7255
7256 if (!rdev->wiphy.wowlan.tcp)
7257 return -EINVAL;
7258
7259 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7260 nla_data(attr), nla_len(attr),
7261 nl80211_wowlan_tcp_policy);
7262 if (err)
7263 return err;
7264
7265 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7266 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7267 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7268 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7269 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7270 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7271 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7272 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7273 return -EINVAL;
7274
7275 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7276 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7277 return -EINVAL;
7278
7279 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7280 rdev->wiphy.wowlan.tcp->data_interval_max)
7281 return -EINVAL;
7282
7283 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7284 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7285 return -EINVAL;
7286
7287 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7288 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7289 return -EINVAL;
7290
7291 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7292 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7293
7294 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7295 tokens_size = tokln - sizeof(*tok);
7296
7297 if (!tok->len || tokens_size % tok->len)
7298 return -EINVAL;
7299 if (!rdev->wiphy.wowlan.tcp->tok)
7300 return -EINVAL;
7301 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7302 return -EINVAL;
7303 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7304 return -EINVAL;
7305 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7306 return -EINVAL;
7307 if (tok->offset + tok->len > data_size)
7308 return -EINVAL;
7309 }
7310
7311 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7312 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7313 if (!rdev->wiphy.wowlan.tcp->seq)
7314 return -EINVAL;
7315 if (seq->len == 0 || seq->len > 4)
7316 return -EINVAL;
7317 if (seq->len + seq->offset > data_size)
7318 return -EINVAL;
7319 }
7320
7321 size = sizeof(*cfg);
7322 size += data_size;
7323 size += wake_size + wake_mask_size;
7324 size += tokens_size;
7325
7326 cfg = kzalloc(size, GFP_KERNEL);
7327 if (!cfg)
7328 return -ENOMEM;
7329 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7330 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7331 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7332 ETH_ALEN);
7333 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7334 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7335 else
7336 port = 0;
7337#ifdef CONFIG_INET
7338 /* allocate a socket and port for it and use it */
7339 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7340 IPPROTO_TCP, &cfg->sock, 1);
7341 if (err) {
7342 kfree(cfg);
7343 return err;
7344 }
7345 if (inet_csk_get_port(cfg->sock->sk, port)) {
7346 sock_release(cfg->sock);
7347 kfree(cfg);
7348 return -EADDRINUSE;
7349 }
7350 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7351#else
7352 if (!port) {
7353 kfree(cfg);
7354 return -EINVAL;
7355 }
7356 cfg->src_port = port;
7357#endif
7358
7359 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7360 cfg->payload_len = data_size;
7361 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7362 memcpy((void *)cfg->payload,
7363 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7364 data_size);
7365 if (seq)
7366 cfg->payload_seq = *seq;
7367 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7368 cfg->wake_len = wake_size;
7369 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7370 memcpy((void *)cfg->wake_data,
7371 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7372 wake_size);
7373 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7374 data_size + wake_size;
7375 memcpy((void *)cfg->wake_mask,
7376 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7377 wake_mask_size);
7378 if (tok) {
7379 cfg->tokens_size = tokens_size;
7380 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7381 }
7382
7383 trig->tcp = cfg;
7384
7385 return 0;
7386}
7387
Johannes Bergff1b6e62011-05-04 15:37:28 +02007388static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7389{
7390 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7391 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007392 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007393 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007394 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7395 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007396 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007397
Johannes Berg2a0e0472013-01-23 22:57:40 +01007398 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7399 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007400 return -EOPNOTSUPP;
7401
Johannes Bergae33bd82012-07-12 16:25:02 +02007402 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7403 cfg80211_rdev_free_wowlan(rdev);
7404 rdev->wowlan = NULL;
7405 goto set_wakeup;
7406 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007407
7408 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7409 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7410 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7411 nl80211_wowlan_policy);
7412 if (err)
7413 return err;
7414
7415 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7416 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7417 return -EINVAL;
7418 new_triggers.any = true;
7419 }
7420
7421 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7422 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7423 return -EINVAL;
7424 new_triggers.disconnect = true;
7425 }
7426
7427 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7428 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7429 return -EINVAL;
7430 new_triggers.magic_pkt = true;
7431 }
7432
Johannes Berg77dbbb12011-07-13 10:48:55 +02007433 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7434 return -EINVAL;
7435
7436 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7437 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7438 return -EINVAL;
7439 new_triggers.gtk_rekey_failure = true;
7440 }
7441
7442 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7443 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7444 return -EINVAL;
7445 new_triggers.eap_identity_req = true;
7446 }
7447
7448 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7449 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7450 return -EINVAL;
7451 new_triggers.four_way_handshake = true;
7452 }
7453
7454 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7455 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7456 return -EINVAL;
7457 new_triggers.rfkill_release = true;
7458 }
7459
Johannes Bergff1b6e62011-05-04 15:37:28 +02007460 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7461 struct nlattr *pat;
7462 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007463 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007464 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7465
7466 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7467 rem)
7468 n_patterns++;
7469 if (n_patterns > wowlan->n_patterns)
7470 return -EINVAL;
7471
7472 new_triggers.patterns = kcalloc(n_patterns,
7473 sizeof(new_triggers.patterns[0]),
7474 GFP_KERNEL);
7475 if (!new_triggers.patterns)
7476 return -ENOMEM;
7477
7478 new_triggers.n_patterns = n_patterns;
7479 i = 0;
7480
7481 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7482 rem) {
7483 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7484 nla_data(pat), nla_len(pat), NULL);
7485 err = -EINVAL;
7486 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7487 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7488 goto error;
7489 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7490 mask_len = DIV_ROUND_UP(pat_len, 8);
7491 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7492 mask_len)
7493 goto error;
7494 if (pat_len > wowlan->pattern_max_len ||
7495 pat_len < wowlan->pattern_min_len)
7496 goto error;
7497
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007498 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7499 pkt_offset = 0;
7500 else
7501 pkt_offset = nla_get_u32(
7502 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7503 if (pkt_offset > wowlan->max_pkt_offset)
7504 goto error;
7505 new_triggers.patterns[i].pkt_offset = pkt_offset;
7506
Johannes Bergff1b6e62011-05-04 15:37:28 +02007507 new_triggers.patterns[i].mask =
7508 kmalloc(mask_len + pat_len, GFP_KERNEL);
7509 if (!new_triggers.patterns[i].mask) {
7510 err = -ENOMEM;
7511 goto error;
7512 }
7513 new_triggers.patterns[i].pattern =
7514 new_triggers.patterns[i].mask + mask_len;
7515 memcpy(new_triggers.patterns[i].mask,
7516 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7517 mask_len);
7518 new_triggers.patterns[i].pattern_len = pat_len;
7519 memcpy(new_triggers.patterns[i].pattern,
7520 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7521 pat_len);
7522 i++;
7523 }
7524 }
7525
Johannes Berg2a0e0472013-01-23 22:57:40 +01007526 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7527 err = nl80211_parse_wowlan_tcp(
7528 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7529 &new_triggers);
7530 if (err)
7531 goto error;
7532 }
7533
Johannes Bergae33bd82012-07-12 16:25:02 +02007534 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7535 if (!ntrig) {
7536 err = -ENOMEM;
7537 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007538 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007539 cfg80211_rdev_free_wowlan(rdev);
7540 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007541
Johannes Bergae33bd82012-07-12 16:25:02 +02007542 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007543 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007544 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007545
Johannes Bergff1b6e62011-05-04 15:37:28 +02007546 return 0;
7547 error:
7548 for (i = 0; i < new_triggers.n_patterns; i++)
7549 kfree(new_triggers.patterns[i].mask);
7550 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007551 if (new_triggers.tcp && new_triggers.tcp->sock)
7552 sock_release(new_triggers.tcp->sock);
7553 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007554 return err;
7555}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007556#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007557
Johannes Berge5497d72011-07-05 16:35:40 +02007558static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7559{
7560 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7561 struct net_device *dev = info->user_ptr[1];
7562 struct wireless_dev *wdev = dev->ieee80211_ptr;
7563 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7564 struct cfg80211_gtk_rekey_data rekey_data;
7565 int err;
7566
7567 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7568 return -EINVAL;
7569
7570 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7571 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7572 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7573 nl80211_rekey_policy);
7574 if (err)
7575 return err;
7576
7577 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7578 return -ERANGE;
7579 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7580 return -ERANGE;
7581 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7582 return -ERANGE;
7583
7584 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7585 NL80211_KEK_LEN);
7586 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7587 NL80211_KCK_LEN);
7588 memcpy(rekey_data.replay_ctr,
7589 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7590 NL80211_REPLAY_CTR_LEN);
7591
7592 wdev_lock(wdev);
7593 if (!wdev->current_bss) {
7594 err = -ENOTCONN;
7595 goto out;
7596 }
7597
7598 if (!rdev->ops->set_rekey_data) {
7599 err = -EOPNOTSUPP;
7600 goto out;
7601 }
7602
Hila Gonene35e4d22012-06-27 17:19:42 +03007603 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007604 out:
7605 wdev_unlock(wdev);
7606 return err;
7607}
7608
Johannes Berg28946da2011-11-04 11:18:12 +01007609static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7610 struct genl_info *info)
7611{
7612 struct net_device *dev = info->user_ptr[1];
7613 struct wireless_dev *wdev = dev->ieee80211_ptr;
7614
7615 if (wdev->iftype != NL80211_IFTYPE_AP &&
7616 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7617 return -EINVAL;
7618
Eric W. Biederman15e47302012-09-07 20:12:54 +00007619 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007620 return -EBUSY;
7621
Eric W. Biederman15e47302012-09-07 20:12:54 +00007622 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007623 return 0;
7624}
7625
Johannes Berg7f6cf312011-11-04 11:18:15 +01007626static int nl80211_probe_client(struct sk_buff *skb,
7627 struct genl_info *info)
7628{
7629 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7630 struct net_device *dev = info->user_ptr[1];
7631 struct wireless_dev *wdev = dev->ieee80211_ptr;
7632 struct sk_buff *msg;
7633 void *hdr;
7634 const u8 *addr;
7635 u64 cookie;
7636 int err;
7637
7638 if (wdev->iftype != NL80211_IFTYPE_AP &&
7639 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7640 return -EOPNOTSUPP;
7641
7642 if (!info->attrs[NL80211_ATTR_MAC])
7643 return -EINVAL;
7644
7645 if (!rdev->ops->probe_client)
7646 return -EOPNOTSUPP;
7647
7648 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7649 if (!msg)
7650 return -ENOMEM;
7651
Eric W. Biederman15e47302012-09-07 20:12:54 +00007652 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007653 NL80211_CMD_PROBE_CLIENT);
7654
7655 if (IS_ERR(hdr)) {
7656 err = PTR_ERR(hdr);
7657 goto free_msg;
7658 }
7659
7660 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7661
Hila Gonene35e4d22012-06-27 17:19:42 +03007662 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007663 if (err)
7664 goto free_msg;
7665
David S. Miller9360ffd2012-03-29 04:41:26 -04007666 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7667 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007668
7669 genlmsg_end(msg, hdr);
7670
7671 return genlmsg_reply(msg, info);
7672
7673 nla_put_failure:
7674 err = -ENOBUFS;
7675 free_msg:
7676 nlmsg_free(msg);
7677 return err;
7678}
7679
Johannes Berg5e760232011-11-04 11:18:17 +01007680static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7681{
7682 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007683 struct cfg80211_beacon_registration *reg, *nreg;
7684 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007685
7686 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7687 return -EOPNOTSUPP;
7688
Ben Greear37c73b52012-10-26 14:49:25 -07007689 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7690 if (!nreg)
7691 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007692
Ben Greear37c73b52012-10-26 14:49:25 -07007693 /* First, check if already registered. */
7694 spin_lock_bh(&rdev->beacon_registrations_lock);
7695 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7696 if (reg->nlportid == info->snd_portid) {
7697 rv = -EALREADY;
7698 goto out_err;
7699 }
7700 }
7701 /* Add it to the list */
7702 nreg->nlportid = info->snd_portid;
7703 list_add(&nreg->list, &rdev->beacon_registrations);
7704
7705 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007706
7707 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007708out_err:
7709 spin_unlock_bh(&rdev->beacon_registrations_lock);
7710 kfree(nreg);
7711 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007712}
7713
Johannes Berg98104fde2012-06-16 00:19:54 +02007714static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7715{
7716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7717 struct wireless_dev *wdev = info->user_ptr[1];
7718 int err;
7719
7720 if (!rdev->ops->start_p2p_device)
7721 return -EOPNOTSUPP;
7722
7723 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7724 return -EOPNOTSUPP;
7725
7726 if (wdev->p2p_started)
7727 return 0;
7728
7729 mutex_lock(&rdev->devlist_mtx);
7730 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7731 mutex_unlock(&rdev->devlist_mtx);
7732 if (err)
7733 return err;
7734
Johannes Bergeeb126e2012-10-23 15:16:50 +02007735 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007736 if (err)
7737 return err;
7738
7739 wdev->p2p_started = true;
7740 mutex_lock(&rdev->devlist_mtx);
7741 rdev->opencount++;
7742 mutex_unlock(&rdev->devlist_mtx);
7743
7744 return 0;
7745}
7746
7747static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7748{
7749 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7750 struct wireless_dev *wdev = info->user_ptr[1];
7751
7752 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7753 return -EOPNOTSUPP;
7754
7755 if (!rdev->ops->stop_p2p_device)
7756 return -EOPNOTSUPP;
7757
7758 if (!wdev->p2p_started)
7759 return 0;
7760
Johannes Bergeeb126e2012-10-23 15:16:50 +02007761 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007762 wdev->p2p_started = false;
7763
7764 mutex_lock(&rdev->devlist_mtx);
7765 rdev->opencount--;
7766 mutex_unlock(&rdev->devlist_mtx);
7767
7768 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7769 rdev->scan_req->aborted = true;
7770 ___cfg80211_scan_done(rdev, true);
7771 }
7772
7773 return 0;
7774}
7775
Johannes Berg4c476992010-10-04 21:36:35 +02007776#define NL80211_FLAG_NEED_WIPHY 0x01
7777#define NL80211_FLAG_NEED_NETDEV 0x02
7778#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007779#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7780#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7781 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007782#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007783/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007784#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7785 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007786
7787static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7788 struct genl_info *info)
7789{
7790 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007791 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007792 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007793 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7794
7795 if (rtnl)
7796 rtnl_lock();
7797
7798 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007799 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007800 if (IS_ERR(rdev)) {
7801 if (rtnl)
7802 rtnl_unlock();
7803 return PTR_ERR(rdev);
7804 }
7805 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007806 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7807 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007808 mutex_lock(&cfg80211_mutex);
7809 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7810 info->attrs);
7811 if (IS_ERR(wdev)) {
7812 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007813 if (rtnl)
7814 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007815 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007816 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007817
Johannes Berg89a54e42012-06-15 14:33:17 +02007818 dev = wdev->netdev;
7819 rdev = wiphy_to_dev(wdev->wiphy);
7820
Johannes Berg1bf614e2012-06-15 15:23:36 +02007821 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7822 if (!dev) {
7823 mutex_unlock(&cfg80211_mutex);
7824 if (rtnl)
7825 rtnl_unlock();
7826 return -EINVAL;
7827 }
7828
7829 info->user_ptr[1] = dev;
7830 } else {
7831 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007832 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007833
Johannes Berg1bf614e2012-06-15 15:23:36 +02007834 if (dev) {
7835 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7836 !netif_running(dev)) {
7837 mutex_unlock(&cfg80211_mutex);
7838 if (rtnl)
7839 rtnl_unlock();
7840 return -ENETDOWN;
7841 }
7842
7843 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007844 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7845 if (!wdev->p2p_started) {
7846 mutex_unlock(&cfg80211_mutex);
7847 if (rtnl)
7848 rtnl_unlock();
7849 return -ENETDOWN;
7850 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007851 }
7852
Johannes Berg89a54e42012-06-15 14:33:17 +02007853 cfg80211_lock_rdev(rdev);
7854
7855 mutex_unlock(&cfg80211_mutex);
7856
Johannes Berg4c476992010-10-04 21:36:35 +02007857 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007858 }
7859
7860 return 0;
7861}
7862
7863static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7864 struct genl_info *info)
7865{
7866 if (info->user_ptr[0])
7867 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007868 if (info->user_ptr[1]) {
7869 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7870 struct wireless_dev *wdev = info->user_ptr[1];
7871
7872 if (wdev->netdev)
7873 dev_put(wdev->netdev);
7874 } else {
7875 dev_put(info->user_ptr[1]);
7876 }
7877 }
Johannes Berg4c476992010-10-04 21:36:35 +02007878 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7879 rtnl_unlock();
7880}
7881
Johannes Berg55682962007-09-20 13:09:35 -04007882static struct genl_ops nl80211_ops[] = {
7883 {
7884 .cmd = NL80211_CMD_GET_WIPHY,
7885 .doit = nl80211_get_wiphy,
7886 .dumpit = nl80211_dump_wiphy,
7887 .policy = nl80211_policy,
7888 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007889 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007890 },
7891 {
7892 .cmd = NL80211_CMD_SET_WIPHY,
7893 .doit = nl80211_set_wiphy,
7894 .policy = nl80211_policy,
7895 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007896 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007897 },
7898 {
7899 .cmd = NL80211_CMD_GET_INTERFACE,
7900 .doit = nl80211_get_interface,
7901 .dumpit = nl80211_dump_interface,
7902 .policy = nl80211_policy,
7903 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007904 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007905 },
7906 {
7907 .cmd = NL80211_CMD_SET_INTERFACE,
7908 .doit = nl80211_set_interface,
7909 .policy = nl80211_policy,
7910 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007911 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7912 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007913 },
7914 {
7915 .cmd = NL80211_CMD_NEW_INTERFACE,
7916 .doit = nl80211_new_interface,
7917 .policy = nl80211_policy,
7918 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007919 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7920 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007921 },
7922 {
7923 .cmd = NL80211_CMD_DEL_INTERFACE,
7924 .doit = nl80211_del_interface,
7925 .policy = nl80211_policy,
7926 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007927 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007928 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007929 },
Johannes Berg41ade002007-12-19 02:03:29 +01007930 {
7931 .cmd = NL80211_CMD_GET_KEY,
7932 .doit = nl80211_get_key,
7933 .policy = nl80211_policy,
7934 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007935 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007936 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007937 },
7938 {
7939 .cmd = NL80211_CMD_SET_KEY,
7940 .doit = nl80211_set_key,
7941 .policy = nl80211_policy,
7942 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007943 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007944 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007945 },
7946 {
7947 .cmd = NL80211_CMD_NEW_KEY,
7948 .doit = nl80211_new_key,
7949 .policy = nl80211_policy,
7950 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007951 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007952 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007953 },
7954 {
7955 .cmd = NL80211_CMD_DEL_KEY,
7956 .doit = nl80211_del_key,
7957 .policy = nl80211_policy,
7958 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007959 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007960 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007961 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007962 {
7963 .cmd = NL80211_CMD_SET_BEACON,
7964 .policy = nl80211_policy,
7965 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007966 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007967 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007968 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007969 },
7970 {
Johannes Berg88600202012-02-13 15:17:18 +01007971 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007972 .policy = nl80211_policy,
7973 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007974 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007975 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007976 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007977 },
7978 {
Johannes Berg88600202012-02-13 15:17:18 +01007979 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007980 .policy = nl80211_policy,
7981 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007982 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007983 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007984 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007985 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007986 {
7987 .cmd = NL80211_CMD_GET_STATION,
7988 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007989 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007990 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007991 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7992 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007993 },
7994 {
7995 .cmd = NL80211_CMD_SET_STATION,
7996 .doit = nl80211_set_station,
7997 .policy = nl80211_policy,
7998 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007999 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008000 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008001 },
8002 {
8003 .cmd = NL80211_CMD_NEW_STATION,
8004 .doit = nl80211_new_station,
8005 .policy = nl80211_policy,
8006 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008007 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008008 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008009 },
8010 {
8011 .cmd = NL80211_CMD_DEL_STATION,
8012 .doit = nl80211_del_station,
8013 .policy = nl80211_policy,
8014 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008015 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008016 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008017 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008018 {
8019 .cmd = NL80211_CMD_GET_MPATH,
8020 .doit = nl80211_get_mpath,
8021 .dumpit = nl80211_dump_mpath,
8022 .policy = nl80211_policy,
8023 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008024 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008025 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008026 },
8027 {
8028 .cmd = NL80211_CMD_SET_MPATH,
8029 .doit = nl80211_set_mpath,
8030 .policy = nl80211_policy,
8031 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008032 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008033 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008034 },
8035 {
8036 .cmd = NL80211_CMD_NEW_MPATH,
8037 .doit = nl80211_new_mpath,
8038 .policy = nl80211_policy,
8039 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008040 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008041 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008042 },
8043 {
8044 .cmd = NL80211_CMD_DEL_MPATH,
8045 .doit = nl80211_del_mpath,
8046 .policy = nl80211_policy,
8047 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008048 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008049 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008050 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008051 {
8052 .cmd = NL80211_CMD_SET_BSS,
8053 .doit = nl80211_set_bss,
8054 .policy = nl80211_policy,
8055 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008056 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008057 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008058 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008059 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008060 .cmd = NL80211_CMD_GET_REG,
8061 .doit = nl80211_get_reg,
8062 .policy = nl80211_policy,
8063 /* can be retrieved by unprivileged users */
8064 },
8065 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008066 .cmd = NL80211_CMD_SET_REG,
8067 .doit = nl80211_set_reg,
8068 .policy = nl80211_policy,
8069 .flags = GENL_ADMIN_PERM,
8070 },
8071 {
8072 .cmd = NL80211_CMD_REQ_SET_REG,
8073 .doit = nl80211_req_set_reg,
8074 .policy = nl80211_policy,
8075 .flags = GENL_ADMIN_PERM,
8076 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008077 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008078 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8079 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008080 .policy = nl80211_policy,
8081 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008082 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008083 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008084 },
8085 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008086 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8087 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008088 .policy = nl80211_policy,
8089 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008090 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008091 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008092 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008093 {
Johannes Berg2a519312009-02-10 21:25:55 +01008094 .cmd = NL80211_CMD_TRIGGER_SCAN,
8095 .doit = nl80211_trigger_scan,
8096 .policy = nl80211_policy,
8097 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008098 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008099 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008100 },
8101 {
8102 .cmd = NL80211_CMD_GET_SCAN,
8103 .policy = nl80211_policy,
8104 .dumpit = nl80211_dump_scan,
8105 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008106 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008107 .cmd = NL80211_CMD_START_SCHED_SCAN,
8108 .doit = nl80211_start_sched_scan,
8109 .policy = nl80211_policy,
8110 .flags = GENL_ADMIN_PERM,
8111 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8112 NL80211_FLAG_NEED_RTNL,
8113 },
8114 {
8115 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8116 .doit = nl80211_stop_sched_scan,
8117 .policy = nl80211_policy,
8118 .flags = GENL_ADMIN_PERM,
8119 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8120 NL80211_FLAG_NEED_RTNL,
8121 },
8122 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008123 .cmd = NL80211_CMD_AUTHENTICATE,
8124 .doit = nl80211_authenticate,
8125 .policy = nl80211_policy,
8126 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008127 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008128 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008129 },
8130 {
8131 .cmd = NL80211_CMD_ASSOCIATE,
8132 .doit = nl80211_associate,
8133 .policy = nl80211_policy,
8134 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008135 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008136 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008137 },
8138 {
8139 .cmd = NL80211_CMD_DEAUTHENTICATE,
8140 .doit = nl80211_deauthenticate,
8141 .policy = nl80211_policy,
8142 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008143 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008144 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008145 },
8146 {
8147 .cmd = NL80211_CMD_DISASSOCIATE,
8148 .doit = nl80211_disassociate,
8149 .policy = nl80211_policy,
8150 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008151 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008152 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008153 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008154 {
8155 .cmd = NL80211_CMD_JOIN_IBSS,
8156 .doit = nl80211_join_ibss,
8157 .policy = nl80211_policy,
8158 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008159 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008160 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008161 },
8162 {
8163 .cmd = NL80211_CMD_LEAVE_IBSS,
8164 .doit = nl80211_leave_ibss,
8165 .policy = nl80211_policy,
8166 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008167 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008168 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008169 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008170#ifdef CONFIG_NL80211_TESTMODE
8171 {
8172 .cmd = NL80211_CMD_TESTMODE,
8173 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008174 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008175 .policy = nl80211_policy,
8176 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008177 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8178 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008179 },
8180#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008181 {
8182 .cmd = NL80211_CMD_CONNECT,
8183 .doit = nl80211_connect,
8184 .policy = nl80211_policy,
8185 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008186 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008187 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008188 },
8189 {
8190 .cmd = NL80211_CMD_DISCONNECT,
8191 .doit = nl80211_disconnect,
8192 .policy = nl80211_policy,
8193 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008194 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008195 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008196 },
Johannes Berg463d0182009-07-14 00:33:35 +02008197 {
8198 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8199 .doit = nl80211_wiphy_netns,
8200 .policy = nl80211_policy,
8201 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008202 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8203 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008204 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008205 {
8206 .cmd = NL80211_CMD_GET_SURVEY,
8207 .policy = nl80211_policy,
8208 .dumpit = nl80211_dump_survey,
8209 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008210 {
8211 .cmd = NL80211_CMD_SET_PMKSA,
8212 .doit = nl80211_setdel_pmksa,
8213 .policy = nl80211_policy,
8214 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008215 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008216 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008217 },
8218 {
8219 .cmd = NL80211_CMD_DEL_PMKSA,
8220 .doit = nl80211_setdel_pmksa,
8221 .policy = nl80211_policy,
8222 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008223 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008224 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008225 },
8226 {
8227 .cmd = NL80211_CMD_FLUSH_PMKSA,
8228 .doit = nl80211_flush_pmksa,
8229 .policy = nl80211_policy,
8230 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008231 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008232 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008233 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008234 {
8235 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8236 .doit = nl80211_remain_on_channel,
8237 .policy = nl80211_policy,
8238 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008239 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008240 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008241 },
8242 {
8243 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8244 .doit = nl80211_cancel_remain_on_channel,
8245 .policy = nl80211_policy,
8246 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008247 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008248 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008249 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008250 {
8251 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8252 .doit = nl80211_set_tx_bitrate_mask,
8253 .policy = nl80211_policy,
8254 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008255 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8256 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008257 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008258 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008259 .cmd = NL80211_CMD_REGISTER_FRAME,
8260 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008261 .policy = nl80211_policy,
8262 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008263 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008264 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008265 },
8266 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008267 .cmd = NL80211_CMD_FRAME,
8268 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008269 .policy = nl80211_policy,
8270 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008271 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008272 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008273 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008274 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008275 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8276 .doit = nl80211_tx_mgmt_cancel_wait,
8277 .policy = nl80211_policy,
8278 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008279 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008280 NL80211_FLAG_NEED_RTNL,
8281 },
8282 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008283 .cmd = NL80211_CMD_SET_POWER_SAVE,
8284 .doit = nl80211_set_power_save,
8285 .policy = nl80211_policy,
8286 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008287 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8288 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008289 },
8290 {
8291 .cmd = NL80211_CMD_GET_POWER_SAVE,
8292 .doit = nl80211_get_power_save,
8293 .policy = nl80211_policy,
8294 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008295 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8296 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008297 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008298 {
8299 .cmd = NL80211_CMD_SET_CQM,
8300 .doit = nl80211_set_cqm,
8301 .policy = nl80211_policy,
8302 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008303 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8304 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008305 },
Johannes Bergf444de02010-05-05 15:25:02 +02008306 {
8307 .cmd = NL80211_CMD_SET_CHANNEL,
8308 .doit = nl80211_set_channel,
8309 .policy = nl80211_policy,
8310 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008311 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8312 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008313 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008314 {
8315 .cmd = NL80211_CMD_SET_WDS_PEER,
8316 .doit = nl80211_set_wds_peer,
8317 .policy = nl80211_policy,
8318 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008319 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8320 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008321 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008322 {
8323 .cmd = NL80211_CMD_JOIN_MESH,
8324 .doit = nl80211_join_mesh,
8325 .policy = nl80211_policy,
8326 .flags = GENL_ADMIN_PERM,
8327 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8328 NL80211_FLAG_NEED_RTNL,
8329 },
8330 {
8331 .cmd = NL80211_CMD_LEAVE_MESH,
8332 .doit = nl80211_leave_mesh,
8333 .policy = nl80211_policy,
8334 .flags = GENL_ADMIN_PERM,
8335 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8336 NL80211_FLAG_NEED_RTNL,
8337 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008338#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008339 {
8340 .cmd = NL80211_CMD_GET_WOWLAN,
8341 .doit = nl80211_get_wowlan,
8342 .policy = nl80211_policy,
8343 /* can be retrieved by unprivileged users */
8344 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8345 NL80211_FLAG_NEED_RTNL,
8346 },
8347 {
8348 .cmd = NL80211_CMD_SET_WOWLAN,
8349 .doit = nl80211_set_wowlan,
8350 .policy = nl80211_policy,
8351 .flags = GENL_ADMIN_PERM,
8352 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8353 NL80211_FLAG_NEED_RTNL,
8354 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008355#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008356 {
8357 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8358 .doit = nl80211_set_rekey_data,
8359 .policy = nl80211_policy,
8360 .flags = GENL_ADMIN_PERM,
8361 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8362 NL80211_FLAG_NEED_RTNL,
8363 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008364 {
8365 .cmd = NL80211_CMD_TDLS_MGMT,
8366 .doit = nl80211_tdls_mgmt,
8367 .policy = nl80211_policy,
8368 .flags = GENL_ADMIN_PERM,
8369 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8370 NL80211_FLAG_NEED_RTNL,
8371 },
8372 {
8373 .cmd = NL80211_CMD_TDLS_OPER,
8374 .doit = nl80211_tdls_oper,
8375 .policy = nl80211_policy,
8376 .flags = GENL_ADMIN_PERM,
8377 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8378 NL80211_FLAG_NEED_RTNL,
8379 },
Johannes Berg28946da2011-11-04 11:18:12 +01008380 {
8381 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8382 .doit = nl80211_register_unexpected_frame,
8383 .policy = nl80211_policy,
8384 .flags = GENL_ADMIN_PERM,
8385 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8386 NL80211_FLAG_NEED_RTNL,
8387 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008388 {
8389 .cmd = NL80211_CMD_PROBE_CLIENT,
8390 .doit = nl80211_probe_client,
8391 .policy = nl80211_policy,
8392 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008393 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008394 NL80211_FLAG_NEED_RTNL,
8395 },
Johannes Berg5e760232011-11-04 11:18:17 +01008396 {
8397 .cmd = NL80211_CMD_REGISTER_BEACONS,
8398 .doit = nl80211_register_beacons,
8399 .policy = nl80211_policy,
8400 .flags = GENL_ADMIN_PERM,
8401 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8402 NL80211_FLAG_NEED_RTNL,
8403 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008404 {
8405 .cmd = NL80211_CMD_SET_NOACK_MAP,
8406 .doit = nl80211_set_noack_map,
8407 .policy = nl80211_policy,
8408 .flags = GENL_ADMIN_PERM,
8409 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8410 NL80211_FLAG_NEED_RTNL,
8411 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008412 {
8413 .cmd = NL80211_CMD_START_P2P_DEVICE,
8414 .doit = nl80211_start_p2p_device,
8415 .policy = nl80211_policy,
8416 .flags = GENL_ADMIN_PERM,
8417 .internal_flags = NL80211_FLAG_NEED_WDEV |
8418 NL80211_FLAG_NEED_RTNL,
8419 },
8420 {
8421 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8422 .doit = nl80211_stop_p2p_device,
8423 .policy = nl80211_policy,
8424 .flags = GENL_ADMIN_PERM,
8425 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8426 NL80211_FLAG_NEED_RTNL,
8427 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008428 {
8429 .cmd = NL80211_CMD_SET_MCAST_RATE,
8430 .doit = nl80211_set_mcast_rate,
8431 .policy = nl80211_policy,
8432 .flags = GENL_ADMIN_PERM,
8433 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8434 NL80211_FLAG_NEED_RTNL,
8435 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308436 {
8437 .cmd = NL80211_CMD_SET_MAC_ACL,
8438 .doit = nl80211_set_mac_acl,
8439 .policy = nl80211_policy,
8440 .flags = GENL_ADMIN_PERM,
8441 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8442 NL80211_FLAG_NEED_RTNL,
8443 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008444 {
8445 .cmd = NL80211_CMD_RADAR_DETECT,
8446 .doit = nl80211_start_radar_detection,
8447 .policy = nl80211_policy,
8448 .flags = GENL_ADMIN_PERM,
8449 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8450 NL80211_FLAG_NEED_RTNL,
8451 },
Johannes Berg55682962007-09-20 13:09:35 -04008452};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008453
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008454static struct genl_multicast_group nl80211_mlme_mcgrp = {
8455 .name = "mlme",
8456};
Johannes Berg55682962007-09-20 13:09:35 -04008457
8458/* multicast groups */
8459static struct genl_multicast_group nl80211_config_mcgrp = {
8460 .name = "config",
8461};
Johannes Berg2a519312009-02-10 21:25:55 +01008462static struct genl_multicast_group nl80211_scan_mcgrp = {
8463 .name = "scan",
8464};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008465static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8466 .name = "regulatory",
8467};
Johannes Berg55682962007-09-20 13:09:35 -04008468
8469/* notification functions */
8470
8471void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8472{
8473 struct sk_buff *msg;
8474
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008475 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008476 if (!msg)
8477 return;
8478
8479 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8480 nlmsg_free(msg);
8481 return;
8482 }
8483
Johannes Berg463d0182009-07-14 00:33:35 +02008484 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8485 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008486}
8487
Johannes Berg362a4152009-05-24 16:43:15 +02008488static int nl80211_add_scan_req(struct sk_buff *msg,
8489 struct cfg80211_registered_device *rdev)
8490{
8491 struct cfg80211_scan_request *req = rdev->scan_req;
8492 struct nlattr *nest;
8493 int i;
8494
Johannes Berg667503d2009-07-07 03:56:11 +02008495 ASSERT_RDEV_LOCK(rdev);
8496
Johannes Berg362a4152009-05-24 16:43:15 +02008497 if (WARN_ON(!req))
8498 return 0;
8499
8500 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8501 if (!nest)
8502 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008503 for (i = 0; i < req->n_ssids; i++) {
8504 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8505 goto nla_put_failure;
8506 }
Johannes Berg362a4152009-05-24 16:43:15 +02008507 nla_nest_end(msg, nest);
8508
8509 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8510 if (!nest)
8511 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008512 for (i = 0; i < req->n_channels; i++) {
8513 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8514 goto nla_put_failure;
8515 }
Johannes Berg362a4152009-05-24 16:43:15 +02008516 nla_nest_end(msg, nest);
8517
David S. Miller9360ffd2012-03-29 04:41:26 -04008518 if (req->ie &&
8519 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8520 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008521
Sam Lefflered4737712012-10-11 21:03:31 -07008522 if (req->flags)
8523 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8524
Johannes Berg362a4152009-05-24 16:43:15 +02008525 return 0;
8526 nla_put_failure:
8527 return -ENOBUFS;
8528}
8529
Johannes Berga538e2d2009-06-16 19:56:42 +02008530static int nl80211_send_scan_msg(struct sk_buff *msg,
8531 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008532 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008533 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008534 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008535{
8536 void *hdr;
8537
Eric W. Biederman15e47302012-09-07 20:12:54 +00008538 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008539 if (!hdr)
8540 return -1;
8541
David S. Miller9360ffd2012-03-29 04:41:26 -04008542 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008543 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8544 wdev->netdev->ifindex)) ||
8545 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008546 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008547
Johannes Berg362a4152009-05-24 16:43:15 +02008548 /* ignore errors and send incomplete event anyway */
8549 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008550
8551 return genlmsg_end(msg, hdr);
8552
8553 nla_put_failure:
8554 genlmsg_cancel(msg, hdr);
8555 return -EMSGSIZE;
8556}
8557
Luciano Coelho807f8a82011-05-11 17:09:35 +03008558static int
8559nl80211_send_sched_scan_msg(struct sk_buff *msg,
8560 struct cfg80211_registered_device *rdev,
8561 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008562 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008563{
8564 void *hdr;
8565
Eric W. Biederman15e47302012-09-07 20:12:54 +00008566 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008567 if (!hdr)
8568 return -1;
8569
David S. Miller9360ffd2012-03-29 04:41:26 -04008570 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8571 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8572 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008573
8574 return genlmsg_end(msg, hdr);
8575
8576 nla_put_failure:
8577 genlmsg_cancel(msg, hdr);
8578 return -EMSGSIZE;
8579}
8580
Johannes Berga538e2d2009-06-16 19:56:42 +02008581void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008582 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008583{
8584 struct sk_buff *msg;
8585
Thomas Graf58050fc2012-06-28 03:57:45 +00008586 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008587 if (!msg)
8588 return;
8589
Johannes Bergfd014282012-06-18 19:17:03 +02008590 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008591 NL80211_CMD_TRIGGER_SCAN) < 0) {
8592 nlmsg_free(msg);
8593 return;
8594 }
8595
Johannes Berg463d0182009-07-14 00:33:35 +02008596 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8597 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008598}
8599
Johannes Berg2a519312009-02-10 21:25:55 +01008600void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008601 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008602{
8603 struct sk_buff *msg;
8604
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008605 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008606 if (!msg)
8607 return;
8608
Johannes Bergfd014282012-06-18 19:17:03 +02008609 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008610 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008611 nlmsg_free(msg);
8612 return;
8613 }
8614
Johannes Berg463d0182009-07-14 00:33:35 +02008615 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8616 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008617}
8618
8619void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008620 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008621{
8622 struct sk_buff *msg;
8623
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008624 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008625 if (!msg)
8626 return;
8627
Johannes Bergfd014282012-06-18 19:17:03 +02008628 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008629 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008630 nlmsg_free(msg);
8631 return;
8632 }
8633
Johannes Berg463d0182009-07-14 00:33:35 +02008634 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8635 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008636}
8637
Luciano Coelho807f8a82011-05-11 17:09:35 +03008638void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8639 struct net_device *netdev)
8640{
8641 struct sk_buff *msg;
8642
8643 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8644 if (!msg)
8645 return;
8646
8647 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8648 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8649 nlmsg_free(msg);
8650 return;
8651 }
8652
8653 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8654 nl80211_scan_mcgrp.id, GFP_KERNEL);
8655}
8656
8657void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8658 struct net_device *netdev, u32 cmd)
8659{
8660 struct sk_buff *msg;
8661
Thomas Graf58050fc2012-06-28 03:57:45 +00008662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008663 if (!msg)
8664 return;
8665
8666 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8667 nlmsg_free(msg);
8668 return;
8669 }
8670
8671 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8672 nl80211_scan_mcgrp.id, GFP_KERNEL);
8673}
8674
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008675/*
8676 * This can happen on global regulatory changes or device specific settings
8677 * based on custom world regulatory domains.
8678 */
8679void nl80211_send_reg_change_event(struct regulatory_request *request)
8680{
8681 struct sk_buff *msg;
8682 void *hdr;
8683
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008684 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008685 if (!msg)
8686 return;
8687
8688 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8689 if (!hdr) {
8690 nlmsg_free(msg);
8691 return;
8692 }
8693
8694 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008695 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8696 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008697
David S. Miller9360ffd2012-03-29 04:41:26 -04008698 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8699 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8700 NL80211_REGDOM_TYPE_WORLD))
8701 goto nla_put_failure;
8702 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8703 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8704 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8705 goto nla_put_failure;
8706 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8707 request->intersect) {
8708 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8709 NL80211_REGDOM_TYPE_INTERSECTION))
8710 goto nla_put_failure;
8711 } else {
8712 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8713 NL80211_REGDOM_TYPE_COUNTRY) ||
8714 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8715 request->alpha2))
8716 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008717 }
8718
Johannes Bergf4173762012-12-03 18:23:37 +01008719 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008720 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8721 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008722
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008723 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008724
Johannes Bergbc43b282009-07-25 10:54:13 +02008725 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008726 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008727 GFP_ATOMIC);
8728 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008729
8730 return;
8731
8732nla_put_failure:
8733 genlmsg_cancel(msg, hdr);
8734 nlmsg_free(msg);
8735}
8736
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008737static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8738 struct net_device *netdev,
8739 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008740 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008741{
8742 struct sk_buff *msg;
8743 void *hdr;
8744
Johannes Berge6d6e342009-07-01 21:26:47 +02008745 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008746 if (!msg)
8747 return;
8748
8749 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8750 if (!hdr) {
8751 nlmsg_free(msg);
8752 return;
8753 }
8754
David S. Miller9360ffd2012-03-29 04:41:26 -04008755 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8756 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8757 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8758 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008759
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008760 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008761
Johannes Berg463d0182009-07-14 00:33:35 +02008762 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8763 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008764 return;
8765
8766 nla_put_failure:
8767 genlmsg_cancel(msg, hdr);
8768 nlmsg_free(msg);
8769}
8770
8771void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008772 struct net_device *netdev, const u8 *buf,
8773 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008774{
8775 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008776 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008777}
8778
8779void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8780 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008781 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008782{
Johannes Berge6d6e342009-07-01 21:26:47 +02008783 nl80211_send_mlme_event(rdev, netdev, buf, len,
8784 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008785}
8786
Jouni Malinen53b46b82009-03-27 20:53:56 +02008787void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008788 struct net_device *netdev, const u8 *buf,
8789 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008790{
8791 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008792 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008793}
8794
Jouni Malinen53b46b82009-03-27 20:53:56 +02008795void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8796 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008797 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008798{
8799 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008800 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008801}
8802
Jouni Malinencf4e5942010-12-16 00:52:40 +02008803void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8804 struct net_device *netdev, const u8 *buf,
8805 size_t len, gfp_t gfp)
8806{
8807 nl80211_send_mlme_event(rdev, netdev, buf, len,
8808 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8809}
8810
8811void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8812 struct net_device *netdev, const u8 *buf,
8813 size_t len, gfp_t gfp)
8814{
8815 nl80211_send_mlme_event(rdev, netdev, buf, len,
8816 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8817}
8818
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008819static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8820 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008821 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008822{
8823 struct sk_buff *msg;
8824 void *hdr;
8825
Johannes Berge6d6e342009-07-01 21:26:47 +02008826 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008827 if (!msg)
8828 return;
8829
8830 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8831 if (!hdr) {
8832 nlmsg_free(msg);
8833 return;
8834 }
8835
David S. Miller9360ffd2012-03-29 04:41:26 -04008836 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8837 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8838 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8839 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8840 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008841
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008842 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008843
Johannes Berg463d0182009-07-14 00:33:35 +02008844 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8845 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008846 return;
8847
8848 nla_put_failure:
8849 genlmsg_cancel(msg, hdr);
8850 nlmsg_free(msg);
8851}
8852
8853void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008854 struct net_device *netdev, const u8 *addr,
8855 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008856{
8857 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008858 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008859}
8860
8861void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008862 struct net_device *netdev, const u8 *addr,
8863 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008864{
Johannes Berge6d6e342009-07-01 21:26:47 +02008865 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8866 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008867}
8868
Samuel Ortizb23aa672009-07-01 21:26:54 +02008869void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8870 struct net_device *netdev, const u8 *bssid,
8871 const u8 *req_ie, size_t req_ie_len,
8872 const u8 *resp_ie, size_t resp_ie_len,
8873 u16 status, gfp_t gfp)
8874{
8875 struct sk_buff *msg;
8876 void *hdr;
8877
Thomas Graf58050fc2012-06-28 03:57:45 +00008878 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008879 if (!msg)
8880 return;
8881
8882 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8883 if (!hdr) {
8884 nlmsg_free(msg);
8885 return;
8886 }
8887
David S. Miller9360ffd2012-03-29 04:41:26 -04008888 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8889 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8890 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8891 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8892 (req_ie &&
8893 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8894 (resp_ie &&
8895 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8896 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008897
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008898 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008899
Johannes Berg463d0182009-07-14 00:33:35 +02008900 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8901 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008902 return;
8903
8904 nla_put_failure:
8905 genlmsg_cancel(msg, hdr);
8906 nlmsg_free(msg);
8907
8908}
8909
8910void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8911 struct net_device *netdev, const u8 *bssid,
8912 const u8 *req_ie, size_t req_ie_len,
8913 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8914{
8915 struct sk_buff *msg;
8916 void *hdr;
8917
Thomas Graf58050fc2012-06-28 03:57:45 +00008918 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008919 if (!msg)
8920 return;
8921
8922 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8923 if (!hdr) {
8924 nlmsg_free(msg);
8925 return;
8926 }
8927
David S. Miller9360ffd2012-03-29 04:41:26 -04008928 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8929 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8930 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8931 (req_ie &&
8932 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8933 (resp_ie &&
8934 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8935 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008936
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008937 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008938
Johannes Berg463d0182009-07-14 00:33:35 +02008939 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8940 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008941 return;
8942
8943 nla_put_failure:
8944 genlmsg_cancel(msg, hdr);
8945 nlmsg_free(msg);
8946
8947}
8948
8949void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8950 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02008951 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008952{
8953 struct sk_buff *msg;
8954 void *hdr;
8955
Thomas Graf58050fc2012-06-28 03:57:45 +00008956 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008957 if (!msg)
8958 return;
8959
8960 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8961 if (!hdr) {
8962 nlmsg_free(msg);
8963 return;
8964 }
8965
David S. Miller9360ffd2012-03-29 04:41:26 -04008966 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8967 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8968 (from_ap && reason &&
8969 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8970 (from_ap &&
8971 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8972 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8973 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008974
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008975 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008976
Johannes Berg463d0182009-07-14 00:33:35 +02008977 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8978 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008979 return;
8980
8981 nla_put_failure:
8982 genlmsg_cancel(msg, hdr);
8983 nlmsg_free(msg);
8984
8985}
8986
Johannes Berg04a773a2009-04-19 21:24:32 +02008987void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8988 struct net_device *netdev, const u8 *bssid,
8989 gfp_t gfp)
8990{
8991 struct sk_buff *msg;
8992 void *hdr;
8993
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008994 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008995 if (!msg)
8996 return;
8997
8998 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8999 if (!hdr) {
9000 nlmsg_free(msg);
9001 return;
9002 }
9003
David S. Miller9360ffd2012-03-29 04:41:26 -04009004 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9005 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9006 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9007 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009008
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009009 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009010
Johannes Berg463d0182009-07-14 00:33:35 +02009011 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9012 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009013 return;
9014
9015 nla_put_failure:
9016 genlmsg_cancel(msg, hdr);
9017 nlmsg_free(msg);
9018}
9019
Javier Cardonac93b5e72011-04-07 15:08:34 -07009020void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
9021 struct net_device *netdev,
9022 const u8 *macaddr, const u8* ie, u8 ie_len,
9023 gfp_t gfp)
9024{
9025 struct sk_buff *msg;
9026 void *hdr;
9027
9028 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9029 if (!msg)
9030 return;
9031
9032 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9033 if (!hdr) {
9034 nlmsg_free(msg);
9035 return;
9036 }
9037
David S. Miller9360ffd2012-03-29 04:41:26 -04009038 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9039 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9040 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
9041 (ie_len && ie &&
9042 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9043 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009044
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009045 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009046
9047 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9048 nl80211_mlme_mcgrp.id, gfp);
9049 return;
9050
9051 nla_put_failure:
9052 genlmsg_cancel(msg, hdr);
9053 nlmsg_free(msg);
9054}
9055
Jouni Malinena3b8b052009-03-27 21:59:49 +02009056void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9057 struct net_device *netdev, const u8 *addr,
9058 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009059 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009060{
9061 struct sk_buff *msg;
9062 void *hdr;
9063
Johannes Berge6d6e342009-07-01 21:26:47 +02009064 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009065 if (!msg)
9066 return;
9067
9068 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9069 if (!hdr) {
9070 nlmsg_free(msg);
9071 return;
9072 }
9073
David S. Miller9360ffd2012-03-29 04:41:26 -04009074 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9075 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9076 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9077 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9078 (key_id != -1 &&
9079 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9080 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9081 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009082
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009083 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009084
Johannes Berg463d0182009-07-14 00:33:35 +02009085 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9086 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009087 return;
9088
9089 nla_put_failure:
9090 genlmsg_cancel(msg, hdr);
9091 nlmsg_free(msg);
9092}
9093
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009094void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9095 struct ieee80211_channel *channel_before,
9096 struct ieee80211_channel *channel_after)
9097{
9098 struct sk_buff *msg;
9099 void *hdr;
9100 struct nlattr *nl_freq;
9101
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009102 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009103 if (!msg)
9104 return;
9105
9106 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9107 if (!hdr) {
9108 nlmsg_free(msg);
9109 return;
9110 }
9111
9112 /*
9113 * Since we are applying the beacon hint to a wiphy we know its
9114 * wiphy_idx is valid
9115 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009116 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9117 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009118
9119 /* Before */
9120 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9121 if (!nl_freq)
9122 goto nla_put_failure;
9123 if (nl80211_msg_put_channel(msg, channel_before))
9124 goto nla_put_failure;
9125 nla_nest_end(msg, nl_freq);
9126
9127 /* After */
9128 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9129 if (!nl_freq)
9130 goto nla_put_failure;
9131 if (nl80211_msg_put_channel(msg, channel_after))
9132 goto nla_put_failure;
9133 nla_nest_end(msg, nl_freq);
9134
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009135 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009136
Johannes Berg463d0182009-07-14 00:33:35 +02009137 rcu_read_lock();
9138 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9139 GFP_ATOMIC);
9140 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009141
9142 return;
9143
9144nla_put_failure:
9145 genlmsg_cancel(msg, hdr);
9146 nlmsg_free(msg);
9147}
9148
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009149static void nl80211_send_remain_on_chan_event(
9150 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009151 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009152 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009153 unsigned int duration, gfp_t gfp)
9154{
9155 struct sk_buff *msg;
9156 void *hdr;
9157
9158 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9159 if (!msg)
9160 return;
9161
9162 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9163 if (!hdr) {
9164 nlmsg_free(msg);
9165 return;
9166 }
9167
David S. Miller9360ffd2012-03-29 04:41:26 -04009168 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009169 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9170 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009171 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009172 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009173 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9174 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009175 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9176 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009177
David S. Miller9360ffd2012-03-29 04:41:26 -04009178 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9179 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9180 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009181
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009182 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009183
9184 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9185 nl80211_mlme_mcgrp.id, gfp);
9186 return;
9187
9188 nla_put_failure:
9189 genlmsg_cancel(msg, hdr);
9190 nlmsg_free(msg);
9191}
9192
9193void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009194 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009195 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009196 unsigned int duration, gfp_t gfp)
9197{
9198 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009199 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009200 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009201}
9202
9203void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009204 struct cfg80211_registered_device *rdev,
9205 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009206 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009207{
9208 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009209 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009210}
9211
Johannes Berg98b62182009-12-23 13:15:44 +01009212void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9213 struct net_device *dev, const u8 *mac_addr,
9214 struct station_info *sinfo, gfp_t gfp)
9215{
9216 struct sk_buff *msg;
9217
Thomas Graf58050fc2012-06-28 03:57:45 +00009218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009219 if (!msg)
9220 return;
9221
John W. Linville66266b32012-03-15 13:25:41 -04009222 if (nl80211_send_station(msg, 0, 0, 0,
9223 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009224 nlmsg_free(msg);
9225 return;
9226 }
9227
9228 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9229 nl80211_mlme_mcgrp.id, gfp);
9230}
9231
Jouni Malinenec15e682011-03-23 15:29:52 +02009232void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9233 struct net_device *dev, const u8 *mac_addr,
9234 gfp_t gfp)
9235{
9236 struct sk_buff *msg;
9237 void *hdr;
9238
Thomas Graf58050fc2012-06-28 03:57:45 +00009239 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009240 if (!msg)
9241 return;
9242
9243 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9244 if (!hdr) {
9245 nlmsg_free(msg);
9246 return;
9247 }
9248
David S. Miller9360ffd2012-03-29 04:41:26 -04009249 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9250 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9251 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009252
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009253 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009254
9255 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9256 nl80211_mlme_mcgrp.id, gfp);
9257 return;
9258
9259 nla_put_failure:
9260 genlmsg_cancel(msg, hdr);
9261 nlmsg_free(msg);
9262}
9263
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309264void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9265 struct net_device *dev, const u8 *mac_addr,
9266 enum nl80211_connect_failed_reason reason,
9267 gfp_t gfp)
9268{
9269 struct sk_buff *msg;
9270 void *hdr;
9271
9272 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9273 if (!msg)
9274 return;
9275
9276 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9277 if (!hdr) {
9278 nlmsg_free(msg);
9279 return;
9280 }
9281
9282 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9283 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9284 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9285 goto nla_put_failure;
9286
9287 genlmsg_end(msg, hdr);
9288
9289 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9290 nl80211_mlme_mcgrp.id, gfp);
9291 return;
9292
9293 nla_put_failure:
9294 genlmsg_cancel(msg, hdr);
9295 nlmsg_free(msg);
9296}
9297
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009298static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9299 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009300{
9301 struct wireless_dev *wdev = dev->ieee80211_ptr;
9302 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9303 struct sk_buff *msg;
9304 void *hdr;
9305 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009306 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009307
Eric W. Biederman15e47302012-09-07 20:12:54 +00009308 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009309 return false;
9310
9311 msg = nlmsg_new(100, gfp);
9312 if (!msg)
9313 return true;
9314
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009315 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009316 if (!hdr) {
9317 nlmsg_free(msg);
9318 return true;
9319 }
9320
David S. Miller9360ffd2012-03-29 04:41:26 -04009321 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9322 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9323 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9324 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009325
9326 err = genlmsg_end(msg, hdr);
9327 if (err < 0) {
9328 nlmsg_free(msg);
9329 return true;
9330 }
9331
Eric W. Biederman15e47302012-09-07 20:12:54 +00009332 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009333 return true;
9334
9335 nla_put_failure:
9336 genlmsg_cancel(msg, hdr);
9337 nlmsg_free(msg);
9338 return true;
9339}
9340
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009341bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9342{
9343 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9344 addr, gfp);
9345}
9346
9347bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9348 const u8 *addr, gfp_t gfp)
9349{
9350 return __nl80211_unexpected_frame(dev,
9351 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9352 addr, gfp);
9353}
9354
Johannes Berg2e161f72010-08-12 15:38:38 +02009355int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009356 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009357 int freq, int sig_dbm,
9358 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009359{
Johannes Berg71bbc992012-06-15 15:30:18 +02009360 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009361 struct sk_buff *msg;
9362 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009363
9364 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9365 if (!msg)
9366 return -ENOMEM;
9367
Johannes Berg2e161f72010-08-12 15:38:38 +02009368 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009369 if (!hdr) {
9370 nlmsg_free(msg);
9371 return -ENOMEM;
9372 }
9373
David S. Miller9360ffd2012-03-29 04:41:26 -04009374 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009375 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9376 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009377 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9378 (sig_dbm &&
9379 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9380 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9381 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009382
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009383 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009384
Eric W. Biederman15e47302012-09-07 20:12:54 +00009385 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009386
9387 nla_put_failure:
9388 genlmsg_cancel(msg, hdr);
9389 nlmsg_free(msg);
9390 return -ENOBUFS;
9391}
9392
Johannes Berg2e161f72010-08-12 15:38:38 +02009393void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009394 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009395 const u8 *buf, size_t len, bool ack,
9396 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009397{
Johannes Berg71bbc992012-06-15 15:30:18 +02009398 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009399 struct sk_buff *msg;
9400 void *hdr;
9401
9402 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9403 if (!msg)
9404 return;
9405
Johannes Berg2e161f72010-08-12 15:38:38 +02009406 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009407 if (!hdr) {
9408 nlmsg_free(msg);
9409 return;
9410 }
9411
David S. Miller9360ffd2012-03-29 04:41:26 -04009412 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009413 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9414 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009415 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9416 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9417 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9418 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009419
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009420 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009421
9422 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9423 return;
9424
9425 nla_put_failure:
9426 genlmsg_cancel(msg, hdr);
9427 nlmsg_free(msg);
9428}
9429
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009430void
9431nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9432 struct net_device *netdev,
9433 enum nl80211_cqm_rssi_threshold_event rssi_event,
9434 gfp_t gfp)
9435{
9436 struct sk_buff *msg;
9437 struct nlattr *pinfoattr;
9438 void *hdr;
9439
Thomas Graf58050fc2012-06-28 03:57:45 +00009440 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009441 if (!msg)
9442 return;
9443
9444 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9445 if (!hdr) {
9446 nlmsg_free(msg);
9447 return;
9448 }
9449
David S. Miller9360ffd2012-03-29 04:41:26 -04009450 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9451 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9452 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009453
9454 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9455 if (!pinfoattr)
9456 goto nla_put_failure;
9457
David S. Miller9360ffd2012-03-29 04:41:26 -04009458 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9459 rssi_event))
9460 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009461
9462 nla_nest_end(msg, pinfoattr);
9463
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009464 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009465
9466 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9467 nl80211_mlme_mcgrp.id, gfp);
9468 return;
9469
9470 nla_put_failure:
9471 genlmsg_cancel(msg, hdr);
9472 nlmsg_free(msg);
9473}
9474
Johannes Berge5497d72011-07-05 16:35:40 +02009475void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9476 struct net_device *netdev, const u8 *bssid,
9477 const u8 *replay_ctr, gfp_t gfp)
9478{
9479 struct sk_buff *msg;
9480 struct nlattr *rekey_attr;
9481 void *hdr;
9482
Thomas Graf58050fc2012-06-28 03:57:45 +00009483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009484 if (!msg)
9485 return;
9486
9487 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9488 if (!hdr) {
9489 nlmsg_free(msg);
9490 return;
9491 }
9492
David S. Miller9360ffd2012-03-29 04:41:26 -04009493 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9494 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9495 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9496 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009497
9498 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9499 if (!rekey_attr)
9500 goto nla_put_failure;
9501
David S. Miller9360ffd2012-03-29 04:41:26 -04009502 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9503 NL80211_REPLAY_CTR_LEN, replay_ctr))
9504 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009505
9506 nla_nest_end(msg, rekey_attr);
9507
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009508 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009509
9510 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9511 nl80211_mlme_mcgrp.id, gfp);
9512 return;
9513
9514 nla_put_failure:
9515 genlmsg_cancel(msg, hdr);
9516 nlmsg_free(msg);
9517}
9518
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009519void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9520 struct net_device *netdev, int index,
9521 const u8 *bssid, bool preauth, gfp_t gfp)
9522{
9523 struct sk_buff *msg;
9524 struct nlattr *attr;
9525 void *hdr;
9526
Thomas Graf58050fc2012-06-28 03:57:45 +00009527 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009528 if (!msg)
9529 return;
9530
9531 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9532 if (!hdr) {
9533 nlmsg_free(msg);
9534 return;
9535 }
9536
David S. Miller9360ffd2012-03-29 04:41:26 -04009537 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9538 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9539 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009540
9541 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9542 if (!attr)
9543 goto nla_put_failure;
9544
David S. Miller9360ffd2012-03-29 04:41:26 -04009545 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9546 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9547 (preauth &&
9548 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9549 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009550
9551 nla_nest_end(msg, attr);
9552
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009553 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009554
9555 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9556 nl80211_mlme_mcgrp.id, gfp);
9557 return;
9558
9559 nla_put_failure:
9560 genlmsg_cancel(msg, hdr);
9561 nlmsg_free(msg);
9562}
9563
Thomas Pedersen53145262012-04-06 13:35:47 -07009564void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009565 struct net_device *netdev,
9566 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009567{
9568 struct sk_buff *msg;
9569 void *hdr;
9570
Thomas Graf58050fc2012-06-28 03:57:45 +00009571 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009572 if (!msg)
9573 return;
9574
9575 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9576 if (!hdr) {
9577 nlmsg_free(msg);
9578 return;
9579 }
9580
Johannes Berg683b6d32012-11-08 21:25:48 +01009581 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9582 goto nla_put_failure;
9583
9584 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009585 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009586
9587 genlmsg_end(msg, hdr);
9588
9589 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9590 nl80211_mlme_mcgrp.id, gfp);
9591 return;
9592
9593 nla_put_failure:
9594 genlmsg_cancel(msg, hdr);
9595 nlmsg_free(msg);
9596}
9597
Johannes Bergc063dbf2010-11-24 08:10:05 +01009598void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009599nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9600 struct net_device *netdev, const u8 *peer,
9601 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9602{
9603 struct sk_buff *msg;
9604 struct nlattr *pinfoattr;
9605 void *hdr;
9606
9607 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9608 if (!msg)
9609 return;
9610
9611 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9612 if (!hdr) {
9613 nlmsg_free(msg);
9614 return;
9615 }
9616
9617 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9618 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9619 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9620 goto nla_put_failure;
9621
9622 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9623 if (!pinfoattr)
9624 goto nla_put_failure;
9625
9626 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9627 goto nla_put_failure;
9628
9629 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9630 goto nla_put_failure;
9631
9632 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9633 goto nla_put_failure;
9634
9635 nla_nest_end(msg, pinfoattr);
9636
9637 genlmsg_end(msg, hdr);
9638
9639 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9640 nl80211_mlme_mcgrp.id, gfp);
9641 return;
9642
9643 nla_put_failure:
9644 genlmsg_cancel(msg, hdr);
9645 nlmsg_free(msg);
9646}
9647
9648void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009649nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9650 struct cfg80211_chan_def *chandef,
9651 enum nl80211_radar_event event,
9652 struct net_device *netdev, gfp_t gfp)
9653{
9654 struct sk_buff *msg;
9655 void *hdr;
9656
9657 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9658 if (!msg)
9659 return;
9660
9661 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9662 if (!hdr) {
9663 nlmsg_free(msg);
9664 return;
9665 }
9666
9667 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9668 goto nla_put_failure;
9669
9670 /* NOP and radar events don't need a netdev parameter */
9671 if (netdev) {
9672 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9673
9674 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9675 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9676 goto nla_put_failure;
9677 }
9678
9679 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9680 goto nla_put_failure;
9681
9682 if (nl80211_send_chandef(msg, chandef))
9683 goto nla_put_failure;
9684
9685 if (genlmsg_end(msg, hdr) < 0) {
9686 nlmsg_free(msg);
9687 return;
9688 }
9689
9690 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9691 nl80211_mlme_mcgrp.id, gfp);
9692 return;
9693
9694 nla_put_failure:
9695 genlmsg_cancel(msg, hdr);
9696 nlmsg_free(msg);
9697}
9698
9699void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009700nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9701 struct net_device *netdev, const u8 *peer,
9702 u32 num_packets, gfp_t gfp)
9703{
9704 struct sk_buff *msg;
9705 struct nlattr *pinfoattr;
9706 void *hdr;
9707
Thomas Graf58050fc2012-06-28 03:57:45 +00009708 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009709 if (!msg)
9710 return;
9711
9712 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9713 if (!hdr) {
9714 nlmsg_free(msg);
9715 return;
9716 }
9717
David S. Miller9360ffd2012-03-29 04:41:26 -04009718 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9719 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9720 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9721 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009722
9723 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9724 if (!pinfoattr)
9725 goto nla_put_failure;
9726
David S. Miller9360ffd2012-03-29 04:41:26 -04009727 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9728 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009729
9730 nla_nest_end(msg, pinfoattr);
9731
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009732 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009733
9734 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9735 nl80211_mlme_mcgrp.id, gfp);
9736 return;
9737
9738 nla_put_failure:
9739 genlmsg_cancel(msg, hdr);
9740 nlmsg_free(msg);
9741}
9742
Johannes Berg7f6cf312011-11-04 11:18:15 +01009743void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9744 u64 cookie, bool acked, gfp_t gfp)
9745{
9746 struct wireless_dev *wdev = dev->ieee80211_ptr;
9747 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9748 struct sk_buff *msg;
9749 void *hdr;
9750 int err;
9751
Beni Lev4ee3e062012-08-27 12:49:39 +03009752 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9753
Thomas Graf58050fc2012-06-28 03:57:45 +00009754 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009755
Johannes Berg7f6cf312011-11-04 11:18:15 +01009756 if (!msg)
9757 return;
9758
9759 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9760 if (!hdr) {
9761 nlmsg_free(msg);
9762 return;
9763 }
9764
David S. Miller9360ffd2012-03-29 04:41:26 -04009765 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9766 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9767 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9768 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9769 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9770 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009771
9772 err = genlmsg_end(msg, hdr);
9773 if (err < 0) {
9774 nlmsg_free(msg);
9775 return;
9776 }
9777
9778 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9779 nl80211_mlme_mcgrp.id, gfp);
9780 return;
9781
9782 nla_put_failure:
9783 genlmsg_cancel(msg, hdr);
9784 nlmsg_free(msg);
9785}
9786EXPORT_SYMBOL(cfg80211_probe_status);
9787
Johannes Berg5e760232011-11-04 11:18:17 +01009788void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9789 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009790 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009791{
9792 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9793 struct sk_buff *msg;
9794 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009795 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009796
Beni Lev4ee3e062012-08-27 12:49:39 +03009797 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9798
Ben Greear37c73b52012-10-26 14:49:25 -07009799 spin_lock_bh(&rdev->beacon_registrations_lock);
9800 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9801 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9802 if (!msg) {
9803 spin_unlock_bh(&rdev->beacon_registrations_lock);
9804 return;
9805 }
Johannes Berg5e760232011-11-04 11:18:17 +01009806
Ben Greear37c73b52012-10-26 14:49:25 -07009807 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9808 if (!hdr)
9809 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009810
Ben Greear37c73b52012-10-26 14:49:25 -07009811 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9812 (freq &&
9813 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9814 (sig_dbm &&
9815 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9816 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9817 goto nla_put_failure;
9818
9819 genlmsg_end(msg, hdr);
9820
9821 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009822 }
Ben Greear37c73b52012-10-26 14:49:25 -07009823 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009824 return;
9825
9826 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009827 spin_unlock_bh(&rdev->beacon_registrations_lock);
9828 if (hdr)
9829 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009830 nlmsg_free(msg);
9831}
9832EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9833
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009834#ifdef CONFIG_PM
9835void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9836 struct cfg80211_wowlan_wakeup *wakeup,
9837 gfp_t gfp)
9838{
9839 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9840 struct sk_buff *msg;
9841 void *hdr;
9842 int err, size = 200;
9843
9844 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9845
9846 if (wakeup)
9847 size += wakeup->packet_present_len;
9848
9849 msg = nlmsg_new(size, gfp);
9850 if (!msg)
9851 return;
9852
9853 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9854 if (!hdr)
9855 goto free_msg;
9856
9857 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9858 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9859 goto free_msg;
9860
9861 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9862 wdev->netdev->ifindex))
9863 goto free_msg;
9864
9865 if (wakeup) {
9866 struct nlattr *reasons;
9867
9868 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9869
9870 if (wakeup->disconnect &&
9871 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9872 goto free_msg;
9873 if (wakeup->magic_pkt &&
9874 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9875 goto free_msg;
9876 if (wakeup->gtk_rekey_failure &&
9877 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9878 goto free_msg;
9879 if (wakeup->eap_identity_req &&
9880 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9881 goto free_msg;
9882 if (wakeup->four_way_handshake &&
9883 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9884 goto free_msg;
9885 if (wakeup->rfkill_release &&
9886 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9887 goto free_msg;
9888
9889 if (wakeup->pattern_idx >= 0 &&
9890 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9891 wakeup->pattern_idx))
9892 goto free_msg;
9893
Johannes Berg2a0e0472013-01-23 22:57:40 +01009894 if (wakeup->tcp_match)
9895 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
9896
9897 if (wakeup->tcp_connlost)
9898 nla_put_flag(msg,
9899 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
9900
9901 if (wakeup->tcp_nomoretokens)
9902 nla_put_flag(msg,
9903 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
9904
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009905 if (wakeup->packet) {
9906 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9907 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9908
9909 if (!wakeup->packet_80211) {
9910 pkt_attr =
9911 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9912 len_attr =
9913 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9914 }
9915
9916 if (wakeup->packet_len &&
9917 nla_put_u32(msg, len_attr, wakeup->packet_len))
9918 goto free_msg;
9919
9920 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9921 wakeup->packet))
9922 goto free_msg;
9923 }
9924
9925 nla_nest_end(msg, reasons);
9926 }
9927
9928 err = genlmsg_end(msg, hdr);
9929 if (err < 0)
9930 goto free_msg;
9931
9932 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9933 nl80211_mlme_mcgrp.id, gfp);
9934 return;
9935
9936 free_msg:
9937 nlmsg_free(msg);
9938}
9939EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9940#endif
9941
Jouni Malinen3475b092012-11-16 22:49:57 +02009942void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9943 enum nl80211_tdls_operation oper,
9944 u16 reason_code, gfp_t gfp)
9945{
9946 struct wireless_dev *wdev = dev->ieee80211_ptr;
9947 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9948 struct sk_buff *msg;
9949 void *hdr;
9950 int err;
9951
9952 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9953 reason_code);
9954
9955 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9956 if (!msg)
9957 return;
9958
9959 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9960 if (!hdr) {
9961 nlmsg_free(msg);
9962 return;
9963 }
9964
9965 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9966 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9967 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9968 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9969 (reason_code > 0 &&
9970 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9971 goto nla_put_failure;
9972
9973 err = genlmsg_end(msg, hdr);
9974 if (err < 0) {
9975 nlmsg_free(msg);
9976 return;
9977 }
9978
9979 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9980 nl80211_mlme_mcgrp.id, gfp);
9981 return;
9982
9983 nla_put_failure:
9984 genlmsg_cancel(msg, hdr);
9985 nlmsg_free(msg);
9986}
9987EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9988
Jouni Malinen026331c2010-02-15 12:53:10 +02009989static int nl80211_netlink_notify(struct notifier_block * nb,
9990 unsigned long state,
9991 void *_notify)
9992{
9993 struct netlink_notify *notify = _notify;
9994 struct cfg80211_registered_device *rdev;
9995 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009996 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009997
9998 if (state != NETLINK_URELEASE)
9999 return NOTIFY_DONE;
10000
10001 rcu_read_lock();
10002
Johannes Berg5e760232011-11-04 11:18:17 +010010003 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010004 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010005 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010006
10007 spin_lock_bh(&rdev->beacon_registrations_lock);
10008 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10009 list) {
10010 if (reg->nlportid == notify->portid) {
10011 list_del(&reg->list);
10012 kfree(reg);
10013 break;
10014 }
10015 }
10016 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010017 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010018
10019 rcu_read_unlock();
10020
10021 return NOTIFY_DONE;
10022}
10023
10024static struct notifier_block nl80211_netlink_notifier = {
10025 .notifier_call = nl80211_netlink_notify,
10026};
10027
Johannes Berg55682962007-09-20 13:09:35 -040010028/* initialisation/exit functions */
10029
10030int nl80211_init(void)
10031{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010032 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010033
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010034 err = genl_register_family_with_ops(&nl80211_fam,
10035 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010036 if (err)
10037 return err;
10038
Johannes Berg55682962007-09-20 13:09:35 -040010039 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10040 if (err)
10041 goto err_out;
10042
Johannes Berg2a519312009-02-10 21:25:55 +010010043 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10044 if (err)
10045 goto err_out;
10046
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010047 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10048 if (err)
10049 goto err_out;
10050
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010051 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10052 if (err)
10053 goto err_out;
10054
Johannes Bergaff89a92009-07-01 21:26:51 +020010055#ifdef CONFIG_NL80211_TESTMODE
10056 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10057 if (err)
10058 goto err_out;
10059#endif
10060
Jouni Malinen026331c2010-02-15 12:53:10 +020010061 err = netlink_register_notifier(&nl80211_netlink_notifier);
10062 if (err)
10063 goto err_out;
10064
Johannes Berg55682962007-09-20 13:09:35 -040010065 return 0;
10066 err_out:
10067 genl_unregister_family(&nl80211_fam);
10068 return err;
10069}
10070
10071void nl80211_exit(void)
10072{
Jouni Malinen026331c2010-02-15 12:53:10 +020010073 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010074 genl_unregister_family(&nl80211_fam);
10075}