blob: 58e13a8c95f90a7b880a9d65530799fc02451f1e [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 Malinendc6382c2009-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 Berg25e47c12009-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 Berg6829c872009-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 Berg6829c872009-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
3362static int nl80211_set_station_tdls(struct genl_info *info,
3363 struct station_parameters *params)
3364{
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 /* Dummy STA entry gets updated once the peer capabilities are known */
3370 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3371 params->ht_capa =
3372 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3373 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3374 params->vht_capa =
3375 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3376
3377 /* parse WME attributes if present */
3378 if (!info->attrs[NL80211_ATTR_STA_WME])
3379 return 0;
3380
3381 nla = info->attrs[NL80211_ATTR_STA_WME];
3382 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3383 nl80211_sta_wme_policy);
3384 if (err)
3385 return err;
3386
3387 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3388 params->uapsd_queues = nla_get_u8(
3389 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3390 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3391 return -EINVAL;
3392
3393 if (tb[NL80211_STA_WME_MAX_SP])
3394 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3395
3396 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3397 return -EINVAL;
3398
3399 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3400
3401 return 0;
3402}
3403
Johannes Berg5727ef12007-12-19 02:03:34 +01003404static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3405{
Johannes Berg4c476992010-10-04 21:36:35 +02003406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003407 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003408 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003409 struct station_parameters params;
3410 u8 *mac_addr = NULL;
3411
3412 memset(&params, 0, sizeof(params));
3413
3414 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003415 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003416
3417 if (info->attrs[NL80211_ATTR_STA_AID])
3418 return -EINVAL;
3419
3420 if (!info->attrs[NL80211_ATTR_MAC])
3421 return -EINVAL;
3422
3423 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3424
3425 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3426 params.supported_rates =
3427 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3428 params.supported_rates_len =
3429 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3430 }
3431
Jouni Malinen9d62a982013-02-14 21:10:13 +02003432 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3433 params.capability =
3434 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3435 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3436 }
3437
3438 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3439 params.ext_capab =
3440 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3441 params.ext_capab_len =
3442 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3443 }
3444
Jouni Malinendf881292013-02-14 21:10:54 +02003445 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003446 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003447
Johannes Bergbdd90d52011-12-14 12:20:27 +01003448 if (!rdev->ops->change_station)
3449 return -EOPNOTSUPP;
3450
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003451 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003452 return -EINVAL;
3453
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003454 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3455 params.plink_action =
3456 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3457
Javier Cardona9c3990a2011-05-03 16:57:11 -07003458 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3459 params.plink_state =
3460 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3461
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003462 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3463 enum nl80211_mesh_power_mode pm = nla_get_u32(
3464 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3465
3466 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3467 pm > NL80211_MESH_POWER_MAX)
3468 return -EINVAL;
3469
3470 params.local_pm = pm;
3471 }
3472
Johannes Berga97f4422009-06-18 17:23:43 +02003473 switch (dev->ieee80211_ptr->iftype) {
3474 case NL80211_IFTYPE_AP:
3475 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003476 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003477 /* disallow mesh-specific things */
3478 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003479 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003480 if (params.local_pm)
3481 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003482
3483 /* TDLS can't be set, ... */
3484 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3485 return -EINVAL;
3486 /*
3487 * ... but don't bother the driver with it. This works around
3488 * a hostapd/wpa_supplicant issue -- it always includes the
3489 * TLDS_PEER flag in the mask even for AP mode.
3490 */
3491 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3492
3493 /* accept only the listed bits */
3494 if (params.sta_flags_mask &
3495 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003496 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3497 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003498 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3499 BIT(NL80211_STA_FLAG_WME) |
3500 BIT(NL80211_STA_FLAG_MFP)))
3501 return -EINVAL;
3502
Johannes Bergd582cff2012-10-26 17:53:44 +02003503 /* but authenticated/associated only if driver handles it */
3504 if (!(rdev->wiphy.features &
3505 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3506 params.sta_flags_mask &
3507 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3508 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3509 return -EINVAL;
3510
Johannes Bergba23d202012-12-27 17:32:09 +01003511 /* reject other things that can't change */
3512 if (params.supported_rates)
3513 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003514 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3515 return -EINVAL;
3516 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3517 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003518 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3519 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3520 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003521
Johannes Bergbdd90d52011-12-14 12:20:27 +01003522 /* must be last in here for error handling */
3523 params.vlan = get_vlan(info, rdev);
3524 if (IS_ERR(params.vlan))
3525 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003526 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003527 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003528 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003529 /*
3530 * Don't allow userspace to change the TDLS_PEER flag,
3531 * but silently ignore attempts to change it since we
3532 * don't have state here to verify that it doesn't try
3533 * to change the flag.
3534 */
3535 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Jouni Malinendf881292013-02-14 21:10:54 +02003536 /* Include parameters for TDLS peer (driver will check) */
3537 err = nl80211_set_station_tdls(info, &params);
3538 if (err)
3539 return err;
3540 /* disallow things sta doesn't support */
3541 if (params.plink_action)
3542 return -EINVAL;
3543 if (params.local_pm)
3544 return -EINVAL;
3545 /* reject any changes other than AUTHORIZED or WME (for TDLS) */
3546 if (params.sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3547 BIT(NL80211_STA_FLAG_WME)))
3548 return -EINVAL;
3549 break;
Antonio Quartulli267335d2012-01-31 20:25:47 +01003550 case NL80211_IFTYPE_ADHOC:
3551 /* disallow things sta doesn't support */
3552 if (params.plink_action)
3553 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003554 if (params.local_pm)
3555 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003556 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3557 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3558 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003559 /* reject any changes other than AUTHORIZED */
3560 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3561 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003562 break;
3563 case NL80211_IFTYPE_MESH_POINT:
3564 /* disallow things mesh doesn't support */
3565 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003566 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003567 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003568 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003569 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3570 return -EINVAL;
3571 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3572 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003573 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3574 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3575 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003576 /*
3577 * No special handling for TDLS here -- the userspace
3578 * mesh code doesn't have this bug.
3579 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003580 if (params.sta_flags_mask &
3581 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003582 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003583 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003584 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003585 break;
3586 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003587 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003588 }
3589
Johannes Bergbdd90d52011-12-14 12:20:27 +01003590 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003591
Hila Gonene35e4d22012-06-27 17:19:42 +03003592 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003593
Johannes Berg5727ef12007-12-19 02:03:34 +01003594 if (params.vlan)
3595 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003596
Johannes Berg5727ef12007-12-19 02:03:34 +01003597 return err;
3598}
3599
3600static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3601{
Johannes Berg4c476992010-10-04 21:36:35 +02003602 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003603 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003604 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003605 struct station_parameters params;
3606 u8 *mac_addr = NULL;
3607
3608 memset(&params, 0, sizeof(params));
3609
3610 if (!info->attrs[NL80211_ATTR_MAC])
3611 return -EINVAL;
3612
Johannes Berg5727ef12007-12-19 02:03:34 +01003613 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3614 return -EINVAL;
3615
3616 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3617 return -EINVAL;
3618
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003619 if (!info->attrs[NL80211_ATTR_STA_AID])
3620 return -EINVAL;
3621
Johannes Berg5727ef12007-12-19 02:03:34 +01003622 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3623 params.supported_rates =
3624 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3625 params.supported_rates_len =
3626 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3627 params.listen_interval =
3628 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003629
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003630 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3631 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3632 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003633
Jouni Malinen9d62a982013-02-14 21:10:13 +02003634 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3635 params.capability =
3636 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3637 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3638 }
3639
3640 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3641 params.ext_capab =
3642 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3643 params.ext_capab_len =
3644 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3645 }
3646
Jouni Malinen36aedc92008-08-25 11:58:58 +03003647 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3648 params.ht_capa =
3649 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003650
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003651 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3652 params.vht_capa =
3653 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3654
Javier Cardona96b78df2011-04-07 15:08:33 -07003655 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3656 params.plink_action =
3657 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3658
Johannes Bergbdd90d52011-12-14 12:20:27 +01003659 if (!rdev->ops->add_station)
3660 return -EOPNOTSUPP;
3661
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003662 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003663 return -EINVAL;
3664
Johannes Bergbdd90d52011-12-14 12:20:27 +01003665 switch (dev->ieee80211_ptr->iftype) {
3666 case NL80211_IFTYPE_AP:
3667 case NL80211_IFTYPE_AP_VLAN:
3668 case NL80211_IFTYPE_P2P_GO:
3669 /* parse WME attributes if sta is WME capable */
3670 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3671 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3672 info->attrs[NL80211_ATTR_STA_WME]) {
3673 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3674 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003675
Johannes Bergbdd90d52011-12-14 12:20:27 +01003676 nla = info->attrs[NL80211_ATTR_STA_WME];
3677 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3678 nl80211_sta_wme_policy);
3679 if (err)
3680 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003681
Johannes Bergbdd90d52011-12-14 12:20:27 +01003682 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3683 params.uapsd_queues =
3684 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3685 if (params.uapsd_queues &
3686 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3687 return -EINVAL;
3688
3689 if (tb[NL80211_STA_WME_MAX_SP])
3690 params.max_sp =
3691 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3692
3693 if (params.max_sp &
3694 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3695 return -EINVAL;
3696
3697 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3698 }
3699 /* 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 Bergd582cff2012-10-26 17:53:44 +02003719 /* associated is disallowed */
3720 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3721 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003722 /* TDLS peers cannot be added */
3723 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003724 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003725 break;
3726 case NL80211_IFTYPE_STATION:
Johannes Bergd582cff2012-10-26 17:53:44 +02003727 /* associated is disallowed */
3728 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3729 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003730 /* Only TDLS peers can be added */
3731 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3732 return -EINVAL;
3733 /* Can only add if TDLS ... */
3734 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3735 return -EOPNOTSUPP;
3736 /* ... with external setup is supported */
3737 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3738 return -EOPNOTSUPP;
3739 break;
3740 default:
3741 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003742 }
3743
Johannes Bergbdd90d52011-12-14 12:20:27 +01003744 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003745
Hila Gonene35e4d22012-06-27 17:19:42 +03003746 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003747
Johannes Berg5727ef12007-12-19 02:03:34 +01003748 if (params.vlan)
3749 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003750 return err;
3751}
3752
3753static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3754{
Johannes Berg4c476992010-10-04 21:36:35 +02003755 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3756 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003757 u8 *mac_addr = NULL;
3758
3759 if (info->attrs[NL80211_ATTR_MAC])
3760 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3761
Johannes Berge80cf852009-05-11 14:43:13 +02003762 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003763 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003764 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003765 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3766 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003767
Johannes Berg4c476992010-10-04 21:36:35 +02003768 if (!rdev->ops->del_station)
3769 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003770
Hila Gonene35e4d22012-06-27 17:19:42 +03003771 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003772}
3773
Eric W. Biederman15e47302012-09-07 20:12:54 +00003774static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003775 int flags, struct net_device *dev,
3776 u8 *dst, u8 *next_hop,
3777 struct mpath_info *pinfo)
3778{
3779 void *hdr;
3780 struct nlattr *pinfoattr;
3781
Eric W. Biederman15e47302012-09-07 20:12:54 +00003782 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003783 if (!hdr)
3784 return -1;
3785
David S. Miller9360ffd2012-03-29 04:41:26 -04003786 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3787 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3788 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3789 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3790 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003791
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003792 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3793 if (!pinfoattr)
3794 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003795 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3796 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3797 pinfo->frame_qlen))
3798 goto nla_put_failure;
3799 if (((pinfo->filled & MPATH_INFO_SN) &&
3800 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3801 ((pinfo->filled & MPATH_INFO_METRIC) &&
3802 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3803 pinfo->metric)) ||
3804 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3805 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3806 pinfo->exptime)) ||
3807 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3808 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3809 pinfo->flags)) ||
3810 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3811 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3812 pinfo->discovery_timeout)) ||
3813 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3814 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3815 pinfo->discovery_retries)))
3816 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003817
3818 nla_nest_end(msg, pinfoattr);
3819
3820 return genlmsg_end(msg, hdr);
3821
3822 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003823 genlmsg_cancel(msg, hdr);
3824 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003825}
3826
3827static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003828 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003829{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003830 struct mpath_info pinfo;
3831 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003832 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003833 u8 dst[ETH_ALEN];
3834 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003835 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003836 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003837
Johannes Berg67748892010-10-04 21:14:06 +02003838 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3839 if (err)
3840 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003841
3842 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003843 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003844 goto out_err;
3845 }
3846
Jouni Malineneec60b02009-03-20 21:21:19 +02003847 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3848 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003849 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003850 }
3851
Johannes Bergbba95fe2008-07-29 13:22:51 +02003852 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003853 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3854 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003855 if (err == -ENOENT)
3856 break;
3857 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003858 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003859
Eric W. Biederman15e47302012-09-07 20:12:54 +00003860 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003861 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3862 netdev, dst, next_hop,
3863 &pinfo) < 0)
3864 goto out;
3865
3866 path_idx++;
3867 }
3868
3869
3870 out:
3871 cb->args[1] = path_idx;
3872 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003873 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003874 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003875 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003876}
3877
3878static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3879{
Johannes Berg4c476992010-10-04 21:36:35 +02003880 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003881 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003882 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003883 struct mpath_info pinfo;
3884 struct sk_buff *msg;
3885 u8 *dst = NULL;
3886 u8 next_hop[ETH_ALEN];
3887
3888 memset(&pinfo, 0, sizeof(pinfo));
3889
3890 if (!info->attrs[NL80211_ATTR_MAC])
3891 return -EINVAL;
3892
3893 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3894
Johannes Berg4c476992010-10-04 21:36:35 +02003895 if (!rdev->ops->get_mpath)
3896 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003897
Johannes Berg4c476992010-10-04 21:36:35 +02003898 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3899 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003900
Hila Gonene35e4d22012-06-27 17:19:42 +03003901 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003902 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003903 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003904
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003905 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003906 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003907 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003908
Eric W. Biederman15e47302012-09-07 20:12:54 +00003909 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003910 dev, dst, next_hop, &pinfo) < 0) {
3911 nlmsg_free(msg);
3912 return -ENOBUFS;
3913 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003914
Johannes Berg4c476992010-10-04 21:36:35 +02003915 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003916}
3917
3918static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3919{
Johannes Berg4c476992010-10-04 21:36:35 +02003920 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3921 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003922 u8 *dst = NULL;
3923 u8 *next_hop = NULL;
3924
3925 if (!info->attrs[NL80211_ATTR_MAC])
3926 return -EINVAL;
3927
3928 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3929 return -EINVAL;
3930
3931 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3932 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3933
Johannes Berg4c476992010-10-04 21:36:35 +02003934 if (!rdev->ops->change_mpath)
3935 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003936
Johannes Berg4c476992010-10-04 21:36:35 +02003937 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3938 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003939
Hila Gonene35e4d22012-06-27 17:19:42 +03003940 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003941}
Johannes Berg4c476992010-10-04 21:36:35 +02003942
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003943static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3944{
Johannes Berg4c476992010-10-04 21:36:35 +02003945 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3946 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003947 u8 *dst = NULL;
3948 u8 *next_hop = NULL;
3949
3950 if (!info->attrs[NL80211_ATTR_MAC])
3951 return -EINVAL;
3952
3953 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3954 return -EINVAL;
3955
3956 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3957 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3958
Johannes Berg4c476992010-10-04 21:36:35 +02003959 if (!rdev->ops->add_mpath)
3960 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003961
Johannes Berg4c476992010-10-04 21:36:35 +02003962 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3963 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003964
Hila Gonene35e4d22012-06-27 17:19:42 +03003965 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003966}
3967
3968static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3969{
Johannes Berg4c476992010-10-04 21:36:35 +02003970 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3971 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003972 u8 *dst = NULL;
3973
3974 if (info->attrs[NL80211_ATTR_MAC])
3975 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3976
Johannes Berg4c476992010-10-04 21:36:35 +02003977 if (!rdev->ops->del_mpath)
3978 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003979
Hila Gonene35e4d22012-06-27 17:19:42 +03003980 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003981}
3982
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003983static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3984{
Johannes Berg4c476992010-10-04 21:36:35 +02003985 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3986 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003987 struct bss_parameters params;
3988
3989 memset(&params, 0, sizeof(params));
3990 /* default to not changing parameters */
3991 params.use_cts_prot = -1;
3992 params.use_short_preamble = -1;
3993 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003994 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003995 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01003996 params.p2p_ctwindow = -1;
3997 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003998
3999 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4000 params.use_cts_prot =
4001 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4002 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4003 params.use_short_preamble =
4004 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4005 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4006 params.use_short_slot_time =
4007 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004008 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4009 params.basic_rates =
4010 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4011 params.basic_rates_len =
4012 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4013 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004014 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4015 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004016 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4017 params.ht_opmode =
4018 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004019
Johannes Berg53cabad2012-11-14 15:17:28 +01004020 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4021 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4022 return -EINVAL;
4023 params.p2p_ctwindow =
4024 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4025 if (params.p2p_ctwindow < 0)
4026 return -EINVAL;
4027 if (params.p2p_ctwindow != 0 &&
4028 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4029 return -EINVAL;
4030 }
4031
4032 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4033 u8 tmp;
4034
4035 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4036 return -EINVAL;
4037 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4038 if (tmp > 1)
4039 return -EINVAL;
4040 params.p2p_opp_ps = tmp;
4041 if (params.p2p_opp_ps &&
4042 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4043 return -EINVAL;
4044 }
4045
Johannes Berg4c476992010-10-04 21:36:35 +02004046 if (!rdev->ops->change_bss)
4047 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004048
Johannes Berg074ac8d2010-09-16 14:58:22 +02004049 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004050 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4051 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004052
Hila Gonene35e4d22012-06-27 17:19:42 +03004053 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004054}
4055
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004056static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004057 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4058 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4059 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4060 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4061 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4062 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4063};
4064
4065static int parse_reg_rule(struct nlattr *tb[],
4066 struct ieee80211_reg_rule *reg_rule)
4067{
4068 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4069 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4070
4071 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4072 return -EINVAL;
4073 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4074 return -EINVAL;
4075 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4076 return -EINVAL;
4077 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4078 return -EINVAL;
4079 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4080 return -EINVAL;
4081
4082 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4083
4084 freq_range->start_freq_khz =
4085 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4086 freq_range->end_freq_khz =
4087 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4088 freq_range->max_bandwidth_khz =
4089 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4090
4091 power_rule->max_eirp =
4092 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4093
4094 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4095 power_rule->max_antenna_gain =
4096 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4097
4098 return 0;
4099}
4100
4101static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4102{
4103 int r;
4104 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004105 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004106
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004107 /*
4108 * You should only get this when cfg80211 hasn't yet initialized
4109 * completely when built-in to the kernel right between the time
4110 * window between nl80211_init() and regulatory_init(), if that is
4111 * even possible.
4112 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004113 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004114 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004115
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004116 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4117 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004118
4119 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4120
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004121 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4122 user_reg_hint_type =
4123 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4124 else
4125 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4126
4127 switch (user_reg_hint_type) {
4128 case NL80211_USER_REG_HINT_USER:
4129 case NL80211_USER_REG_HINT_CELL_BASE:
4130 break;
4131 default:
4132 return -EINVAL;
4133 }
4134
4135 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004136
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004137 return r;
4138}
4139
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004140static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004141 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004142{
Johannes Berg4c476992010-10-04 21:36:35 +02004143 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004144 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004145 struct wireless_dev *wdev = dev->ieee80211_ptr;
4146 struct mesh_config cur_params;
4147 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004148 void *hdr;
4149 struct nlattr *pinfoattr;
4150 struct sk_buff *msg;
4151
Johannes Berg29cbe682010-12-03 09:20:44 +01004152 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4153 return -EOPNOTSUPP;
4154
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004155 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004156 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004157
Johannes Berg29cbe682010-12-03 09:20:44 +01004158 wdev_lock(wdev);
4159 /* If not connected, get default parameters */
4160 if (!wdev->mesh_id_len)
4161 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4162 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004163 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004164 wdev_unlock(wdev);
4165
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004166 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004167 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004168
4169 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004170 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004171 if (!msg)
4172 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004173 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004174 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004175 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004176 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004177 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004178 if (!pinfoattr)
4179 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004180 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4181 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4182 cur_params.dot11MeshRetryTimeout) ||
4183 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4184 cur_params.dot11MeshConfirmTimeout) ||
4185 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4186 cur_params.dot11MeshHoldingTimeout) ||
4187 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4188 cur_params.dot11MeshMaxPeerLinks) ||
4189 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4190 cur_params.dot11MeshMaxRetries) ||
4191 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4192 cur_params.dot11MeshTTL) ||
4193 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4194 cur_params.element_ttl) ||
4195 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4196 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004197 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4198 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004199 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4200 cur_params.dot11MeshHWMPmaxPREQretries) ||
4201 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4202 cur_params.path_refresh_time) ||
4203 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4204 cur_params.min_discovery_timeout) ||
4205 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4206 cur_params.dot11MeshHWMPactivePathTimeout) ||
4207 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4208 cur_params.dot11MeshHWMPpreqMinInterval) ||
4209 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4210 cur_params.dot11MeshHWMPperrMinInterval) ||
4211 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4212 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4213 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4214 cur_params.dot11MeshHWMPRootMode) ||
4215 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4216 cur_params.dot11MeshHWMPRannInterval) ||
4217 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4218 cur_params.dot11MeshGateAnnouncementProtocol) ||
4219 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4220 cur_params.dot11MeshForwarding) ||
4221 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004222 cur_params.rssi_threshold) ||
4223 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004224 cur_params.ht_opmode) ||
4225 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4226 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4227 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004228 cur_params.dot11MeshHWMProotInterval) ||
4229 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004230 cur_params.dot11MeshHWMPconfirmationInterval) ||
4231 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4232 cur_params.power_mode) ||
4233 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4234 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004235 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004236 nla_nest_end(msg, pinfoattr);
4237 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004238 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004239
Johannes Berg3b858752009-03-12 09:55:09 +01004240 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004241 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004242 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004243 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004244 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004245}
4246
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004247static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004248 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4249 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4250 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4251 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4252 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4253 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004254 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004255 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004256 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004257 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4258 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4259 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4260 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4261 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004262 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004263 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004264 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004265 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004266 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004267 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004268 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4269 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004270 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4271 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004272 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004273 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4274 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004275};
4276
Javier Cardonac80d5452010-12-16 17:37:49 -08004277static const struct nla_policy
4278 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004279 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004280 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4281 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004282 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004283 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004284 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004285 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004286};
4287
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004288static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004289 struct mesh_config *cfg,
4290 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004291{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004292 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004293 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004294
Marco Porschea54fba2013-01-07 16:04:48 +01004295#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4296do { \
4297 if (tb[attr]) { \
4298 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4299 return -EINVAL; \
4300 cfg->param = fn(tb[attr]); \
4301 mask |= (1 << (attr - 1)); \
4302 } \
4303} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004304
4305
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004306 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004307 return -EINVAL;
4308 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004309 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004310 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004311 return -EINVAL;
4312
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004313 /* This makes sure that there aren't more than 32 mesh config
4314 * parameters (otherwise our bitfield scheme would not work.) */
4315 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4316
4317 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004318 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004319 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4320 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004321 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004322 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4323 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004324 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004325 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4326 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004327 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004328 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4329 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004330 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004331 mask, NL80211_MESHCONF_MAX_RETRIES,
4332 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004333 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004334 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004335 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004336 mask, NL80211_MESHCONF_ELEMENT_TTL,
4337 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004338 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004339 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4340 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004341 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4342 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004343 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4344 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004345 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004346 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4347 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004348 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004349 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4350 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004351 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004352 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4353 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004354 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4355 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004356 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4357 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004358 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004359 1, 65535, mask,
4360 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004361 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004362 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004363 1, 65535, mask,
4364 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004365 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004366 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004367 dot11MeshHWMPnetDiameterTraversalTime,
4368 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004369 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4370 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004371 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4372 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4373 nla_get_u8);
4374 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4375 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004376 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004377 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004378 dot11MeshGateAnnouncementProtocol, 0, 1,
4379 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004380 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004381 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004382 mask, NL80211_MESHCONF_FORWARDING,
4383 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004384 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004385 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4386 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004387 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004388 mask, NL80211_MESHCONF_HT_OPMODE,
4389 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004390 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004391 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004392 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4393 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004394 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004395 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4396 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004397 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004398 dot11MeshHWMPconfirmationInterval,
4399 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004400 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4401 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004402 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4403 NL80211_MESH_POWER_ACTIVE,
4404 NL80211_MESH_POWER_MAX,
4405 mask, NL80211_MESHCONF_POWER_MODE,
4406 nla_get_u32);
4407 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4408 0, 65535, mask,
4409 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004410 if (mask_out)
4411 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004412
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004413 return 0;
4414
4415#undef FILL_IN_MESH_PARAM_IF_SET
4416}
4417
Javier Cardonac80d5452010-12-16 17:37:49 -08004418static int nl80211_parse_mesh_setup(struct genl_info *info,
4419 struct mesh_setup *setup)
4420{
4421 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4422
4423 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4424 return -EINVAL;
4425 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4426 info->attrs[NL80211_ATTR_MESH_SETUP],
4427 nl80211_mesh_setup_params_policy))
4428 return -EINVAL;
4429
Javier Cardonad299a1f2012-03-31 11:31:33 -07004430 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4431 setup->sync_method =
4432 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4433 IEEE80211_SYNC_METHOD_VENDOR :
4434 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4435
Javier Cardonac80d5452010-12-16 17:37:49 -08004436 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4437 setup->path_sel_proto =
4438 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4439 IEEE80211_PATH_PROTOCOL_VENDOR :
4440 IEEE80211_PATH_PROTOCOL_HWMP;
4441
4442 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4443 setup->path_metric =
4444 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4445 IEEE80211_PATH_METRIC_VENDOR :
4446 IEEE80211_PATH_METRIC_AIRTIME;
4447
Javier Cardona581a8b02011-04-07 15:08:27 -07004448
4449 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004450 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004451 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004452 if (!is_valid_ie_attr(ieattr))
4453 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004454 setup->ie = nla_data(ieattr);
4455 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004456 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004457 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4458 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004459
4460 return 0;
4461}
4462
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004463static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004464 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004465{
4466 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4467 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004468 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004469 struct mesh_config cfg;
4470 u32 mask;
4471 int err;
4472
Johannes Berg29cbe682010-12-03 09:20:44 +01004473 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4474 return -EOPNOTSUPP;
4475
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004476 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004477 return -EOPNOTSUPP;
4478
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004479 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004480 if (err)
4481 return err;
4482
Johannes Berg29cbe682010-12-03 09:20:44 +01004483 wdev_lock(wdev);
4484 if (!wdev->mesh_id_len)
4485 err = -ENOLINK;
4486
4487 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004488 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004489
4490 wdev_unlock(wdev);
4491
4492 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004493}
4494
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004495static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4496{
Johannes Berg458f4f92012-12-06 15:47:38 +01004497 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004498 struct sk_buff *msg;
4499 void *hdr = NULL;
4500 struct nlattr *nl_reg_rules;
4501 unsigned int i;
4502 int err = -EINVAL;
4503
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004504 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004505
4506 if (!cfg80211_regdomain)
4507 goto out;
4508
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004509 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004510 if (!msg) {
4511 err = -ENOBUFS;
4512 goto out;
4513 }
4514
Eric W. Biederman15e47302012-09-07 20:12:54 +00004515 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004516 NL80211_CMD_GET_REG);
4517 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004518 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004519
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004520 if (reg_last_request_cell_base() &&
4521 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4522 NL80211_USER_REG_HINT_CELL_BASE))
4523 goto nla_put_failure;
4524
Johannes Berg458f4f92012-12-06 15:47:38 +01004525 rcu_read_lock();
4526 regdom = rcu_dereference(cfg80211_regdomain);
4527
4528 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4529 (regdom->dfs_region &&
4530 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4531 goto nla_put_failure_rcu;
4532
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004533 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4534 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004535 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004536
Johannes Berg458f4f92012-12-06 15:47:38 +01004537 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004538 struct nlattr *nl_reg_rule;
4539 const struct ieee80211_reg_rule *reg_rule;
4540 const struct ieee80211_freq_range *freq_range;
4541 const struct ieee80211_power_rule *power_rule;
4542
Johannes Berg458f4f92012-12-06 15:47:38 +01004543 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004544 freq_range = &reg_rule->freq_range;
4545 power_rule = &reg_rule->power_rule;
4546
4547 nl_reg_rule = nla_nest_start(msg, i);
4548 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004549 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004550
David S. Miller9360ffd2012-03-29 04:41:26 -04004551 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4552 reg_rule->flags) ||
4553 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4554 freq_range->start_freq_khz) ||
4555 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4556 freq_range->end_freq_khz) ||
4557 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4558 freq_range->max_bandwidth_khz) ||
4559 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4560 power_rule->max_antenna_gain) ||
4561 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4562 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004563 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004564
4565 nla_nest_end(msg, nl_reg_rule);
4566 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004567 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004568
4569 nla_nest_end(msg, nl_reg_rules);
4570
4571 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004572 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004573 goto out;
4574
Johannes Berg458f4f92012-12-06 15:47:38 +01004575nla_put_failure_rcu:
4576 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004577nla_put_failure:
4578 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004579put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004580 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004581 err = -EMSGSIZE;
4582out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004583 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004584 return err;
4585}
4586
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004587static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4588{
4589 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4590 struct nlattr *nl_reg_rule;
4591 char *alpha2 = NULL;
4592 int rem_reg_rules = 0, r = 0;
4593 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004594 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004595 struct ieee80211_regdomain *rd = NULL;
4596
4597 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4598 return -EINVAL;
4599
4600 if (!info->attrs[NL80211_ATTR_REG_RULES])
4601 return -EINVAL;
4602
4603 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4604
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004605 if (info->attrs[NL80211_ATTR_DFS_REGION])
4606 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4607
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004608 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004609 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004610 num_rules++;
4611 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004612 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004613 }
4614
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004615 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004616 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004617
4618 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004619 if (!rd)
4620 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004621
4622 rd->n_reg_rules = num_rules;
4623 rd->alpha2[0] = alpha2[0];
4624 rd->alpha2[1] = alpha2[1];
4625
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004626 /*
4627 * Disable DFS master mode if the DFS region was
4628 * not supported or known on this kernel.
4629 */
4630 if (reg_supported_dfs_region(dfs_region))
4631 rd->dfs_region = dfs_region;
4632
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004633 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004634 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004635 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004636 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4637 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004638 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4639 if (r)
4640 goto bad_reg;
4641
4642 rule_idx++;
4643
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004644 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4645 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004646 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004647 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004648 }
4649
Johannes Berg6913b492012-12-04 00:48:59 +01004650 mutex_lock(&cfg80211_mutex);
4651
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004652 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004653 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004654 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004655 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004656
Johannes Bergd2372b32008-10-24 20:32:20 +02004657 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004658 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004659 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004660}
4661
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004662static int validate_scan_freqs(struct nlattr *freqs)
4663{
4664 struct nlattr *attr1, *attr2;
4665 int n_channels = 0, tmp1, tmp2;
4666
4667 nla_for_each_nested(attr1, freqs, tmp1) {
4668 n_channels++;
4669 /*
4670 * Some hardware has a limited channel list for
4671 * scanning, and it is pretty much nonsensical
4672 * to scan for a channel twice, so disallow that
4673 * and don't require drivers to check that the
4674 * channel list they get isn't longer than what
4675 * they can scan, as long as they can scan all
4676 * the channels they registered at once.
4677 */
4678 nla_for_each_nested(attr2, freqs, tmp2)
4679 if (attr1 != attr2 &&
4680 nla_get_u32(attr1) == nla_get_u32(attr2))
4681 return 0;
4682 }
4683
4684 return n_channels;
4685}
4686
Johannes Berg2a519312009-02-10 21:25:55 +01004687static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4688{
Johannes Berg4c476992010-10-04 21:36:35 +02004689 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004690 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004691 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004692 struct nlattr *attr;
4693 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004694 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004695 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004696
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004697 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4698 return -EINVAL;
4699
Johannes Berg79c97e92009-07-07 03:56:12 +02004700 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004701
Johannes Berg4c476992010-10-04 21:36:35 +02004702 if (!rdev->ops->scan)
4703 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004704
Johannes Bergf9f47522013-03-19 15:04:07 +01004705 mutex_lock(&rdev->sched_scan_mtx);
4706 if (rdev->scan_req) {
4707 err = -EBUSY;
4708 goto unlock;
4709 }
Johannes Berg2a519312009-02-10 21:25:55 +01004710
4711 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004712 n_channels = validate_scan_freqs(
4713 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01004714 if (!n_channels) {
4715 err = -EINVAL;
4716 goto unlock;
4717 }
Johannes Berg2a519312009-02-10 21:25:55 +01004718 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004719 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004720 n_channels = 0;
4721
Johannes Berg2a519312009-02-10 21:25:55 +01004722 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4723 if (wiphy->bands[band])
4724 n_channels += wiphy->bands[band]->n_channels;
4725 }
4726
4727 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4728 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4729 n_ssids++;
4730
Johannes Bergf9f47522013-03-19 15:04:07 +01004731 if (n_ssids > wiphy->max_scan_ssids) {
4732 err = -EINVAL;
4733 goto unlock;
4734 }
Johannes Berg2a519312009-02-10 21:25:55 +01004735
Jouni Malinen70692ad2009-02-16 19:39:13 +02004736 if (info->attrs[NL80211_ATTR_IE])
4737 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4738 else
4739 ie_len = 0;
4740
Johannes Bergf9f47522013-03-19 15:04:07 +01004741 if (ie_len > wiphy->max_scan_ie_len) {
4742 err = -EINVAL;
4743 goto unlock;
4744 }
Johannes Berg18a83652009-03-31 12:12:05 +02004745
Johannes Berg2a519312009-02-10 21:25:55 +01004746 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004747 + sizeof(*request->ssids) * n_ssids
4748 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004749 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01004750 if (!request) {
4751 err = -ENOMEM;
4752 goto unlock;
4753 }
Johannes Berg2a519312009-02-10 21:25:55 +01004754
Johannes Berg2a519312009-02-10 21:25:55 +01004755 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004756 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004757 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004758 if (ie_len) {
4759 if (request->ssids)
4760 request->ie = (void *)(request->ssids + n_ssids);
4761 else
4762 request->ie = (void *)(request->channels + n_channels);
4763 }
Johannes Berg2a519312009-02-10 21:25:55 +01004764
Johannes Berg584991d2009-11-02 13:32:03 +01004765 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004766 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4767 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004768 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004769 struct ieee80211_channel *chan;
4770
4771 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4772
4773 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004774 err = -EINVAL;
4775 goto out_free;
4776 }
Johannes Berg584991d2009-11-02 13:32:03 +01004777
4778 /* ignore disabled channels */
4779 if (chan->flags & IEEE80211_CHAN_DISABLED)
4780 continue;
4781
4782 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004783 i++;
4784 }
4785 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004786 enum ieee80211_band band;
4787
Johannes Berg2a519312009-02-10 21:25:55 +01004788 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004789 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4790 int j;
4791 if (!wiphy->bands[band])
4792 continue;
4793 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004794 struct ieee80211_channel *chan;
4795
4796 chan = &wiphy->bands[band]->channels[j];
4797
4798 if (chan->flags & IEEE80211_CHAN_DISABLED)
4799 continue;
4800
4801 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004802 i++;
4803 }
4804 }
4805 }
4806
Johannes Berg584991d2009-11-02 13:32:03 +01004807 if (!i) {
4808 err = -EINVAL;
4809 goto out_free;
4810 }
4811
4812 request->n_channels = i;
4813
Johannes Berg2a519312009-02-10 21:25:55 +01004814 i = 0;
4815 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4816 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004817 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004818 err = -EINVAL;
4819 goto out_free;
4820 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004821 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004822 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004823 i++;
4824 }
4825 }
4826
Jouni Malinen70692ad2009-02-16 19:39:13 +02004827 if (info->attrs[NL80211_ATTR_IE]) {
4828 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004829 memcpy((void *)request->ie,
4830 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004831 request->ie_len);
4832 }
4833
Johannes Berg34850ab2011-07-18 18:08:35 +02004834 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004835 if (wiphy->bands[i])
4836 request->rates[i] =
4837 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004838
4839 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4840 nla_for_each_nested(attr,
4841 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4842 tmp) {
4843 enum ieee80211_band band = nla_type(attr);
4844
Dan Carpenter84404622011-07-29 11:52:18 +03004845 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004846 err = -EINVAL;
4847 goto out_free;
4848 }
4849 err = ieee80211_get_ratemask(wiphy->bands[band],
4850 nla_data(attr),
4851 nla_len(attr),
4852 &request->rates[band]);
4853 if (err)
4854 goto out_free;
4855 }
4856 }
4857
Sam Leffler46856bb2012-10-11 21:03:32 -07004858 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004859 request->flags = nla_get_u32(
4860 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004861 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4862 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4863 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4864 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004865 err = -EOPNOTSUPP;
4866 goto out_free;
4867 }
4868 }
Sam Lefflered4737712012-10-11 21:03:31 -07004869
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304870 request->no_cck =
4871 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4872
Johannes Bergfd014282012-06-18 19:17:03 +02004873 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004874 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004875 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004876
Johannes Berg79c97e92009-07-07 03:56:12 +02004877 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004878 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004879
Johannes Berg463d0182009-07-14 00:33:35 +02004880 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004881 nl80211_send_scan_start(rdev, wdev);
4882 if (wdev->netdev)
4883 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004884 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004885 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004886 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004887 kfree(request);
4888 }
Johannes Berg3b858752009-03-12 09:55:09 +01004889
Johannes Bergf9f47522013-03-19 15:04:07 +01004890 unlock:
4891 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01004892 return err;
4893}
4894
Luciano Coelho807f8a82011-05-11 17:09:35 +03004895static int nl80211_start_sched_scan(struct sk_buff *skb,
4896 struct genl_info *info)
4897{
4898 struct cfg80211_sched_scan_request *request;
4899 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4900 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004901 struct nlattr *attr;
4902 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004903 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004904 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004905 enum ieee80211_band band;
4906 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004907 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004908
4909 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4910 !rdev->ops->sched_scan_start)
4911 return -EOPNOTSUPP;
4912
4913 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4914 return -EINVAL;
4915
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004916 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4917 return -EINVAL;
4918
4919 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4920 if (interval == 0)
4921 return -EINVAL;
4922
Luciano Coelho807f8a82011-05-11 17:09:35 +03004923 wiphy = &rdev->wiphy;
4924
4925 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4926 n_channels = validate_scan_freqs(
4927 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4928 if (!n_channels)
4929 return -EINVAL;
4930 } else {
4931 n_channels = 0;
4932
4933 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4934 if (wiphy->bands[band])
4935 n_channels += wiphy->bands[band]->n_channels;
4936 }
4937
4938 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4939 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4940 tmp)
4941 n_ssids++;
4942
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004943 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004944 return -EINVAL;
4945
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004946 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4947 nla_for_each_nested(attr,
4948 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4949 tmp)
4950 n_match_sets++;
4951
4952 if (n_match_sets > wiphy->max_match_sets)
4953 return -EINVAL;
4954
Luciano Coelho807f8a82011-05-11 17:09:35 +03004955 if (info->attrs[NL80211_ATTR_IE])
4956 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4957 else
4958 ie_len = 0;
4959
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004960 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004961 return -EINVAL;
4962
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004963 mutex_lock(&rdev->sched_scan_mtx);
4964
4965 if (rdev->sched_scan_req) {
4966 err = -EINPROGRESS;
4967 goto out;
4968 }
4969
Luciano Coelho807f8a82011-05-11 17:09:35 +03004970 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004971 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004972 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004973 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004974 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004975 if (!request) {
4976 err = -ENOMEM;
4977 goto out;
4978 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004979
4980 if (n_ssids)
4981 request->ssids = (void *)&request->channels[n_channels];
4982 request->n_ssids = n_ssids;
4983 if (ie_len) {
4984 if (request->ssids)
4985 request->ie = (void *)(request->ssids + n_ssids);
4986 else
4987 request->ie = (void *)(request->channels + n_channels);
4988 }
4989
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004990 if (n_match_sets) {
4991 if (request->ie)
4992 request->match_sets = (void *)(request->ie + ie_len);
4993 else if (request->ssids)
4994 request->match_sets =
4995 (void *)(request->ssids + n_ssids);
4996 else
4997 request->match_sets =
4998 (void *)(request->channels + n_channels);
4999 }
5000 request->n_match_sets = n_match_sets;
5001
Luciano Coelho807f8a82011-05-11 17:09:35 +03005002 i = 0;
5003 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5004 /* user specified, bail out if channel not found */
5005 nla_for_each_nested(attr,
5006 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5007 tmp) {
5008 struct ieee80211_channel *chan;
5009
5010 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5011
5012 if (!chan) {
5013 err = -EINVAL;
5014 goto out_free;
5015 }
5016
5017 /* ignore disabled channels */
5018 if (chan->flags & IEEE80211_CHAN_DISABLED)
5019 continue;
5020
5021 request->channels[i] = chan;
5022 i++;
5023 }
5024 } else {
5025 /* all channels */
5026 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5027 int j;
5028 if (!wiphy->bands[band])
5029 continue;
5030 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5031 struct ieee80211_channel *chan;
5032
5033 chan = &wiphy->bands[band]->channels[j];
5034
5035 if (chan->flags & IEEE80211_CHAN_DISABLED)
5036 continue;
5037
5038 request->channels[i] = chan;
5039 i++;
5040 }
5041 }
5042 }
5043
5044 if (!i) {
5045 err = -EINVAL;
5046 goto out_free;
5047 }
5048
5049 request->n_channels = i;
5050
5051 i = 0;
5052 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5053 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5054 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005055 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005056 err = -EINVAL;
5057 goto out_free;
5058 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005059 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005060 memcpy(request->ssids[i].ssid, nla_data(attr),
5061 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005062 i++;
5063 }
5064 }
5065
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005066 i = 0;
5067 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5068 nla_for_each_nested(attr,
5069 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5070 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005071 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005072
5073 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5074 nla_data(attr), nla_len(attr),
5075 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005076 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005077 if (ssid) {
5078 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5079 err = -EINVAL;
5080 goto out_free;
5081 }
5082 memcpy(request->match_sets[i].ssid.ssid,
5083 nla_data(ssid), nla_len(ssid));
5084 request->match_sets[i].ssid.ssid_len =
5085 nla_len(ssid);
5086 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005087 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5088 if (rssi)
5089 request->rssi_thold = nla_get_u32(rssi);
5090 else
5091 request->rssi_thold =
5092 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005093 i++;
5094 }
5095 }
5096
Luciano Coelho807f8a82011-05-11 17:09:35 +03005097 if (info->attrs[NL80211_ATTR_IE]) {
5098 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5099 memcpy((void *)request->ie,
5100 nla_data(info->attrs[NL80211_ATTR_IE]),
5101 request->ie_len);
5102 }
5103
Sam Leffler46856bb2012-10-11 21:03:32 -07005104 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005105 request->flags = nla_get_u32(
5106 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005107 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5108 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5109 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5110 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005111 err = -EOPNOTSUPP;
5112 goto out_free;
5113 }
5114 }
Sam Lefflered4737712012-10-11 21:03:31 -07005115
Luciano Coelho807f8a82011-05-11 17:09:35 +03005116 request->dev = dev;
5117 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005118 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005119 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005120
Hila Gonene35e4d22012-06-27 17:19:42 +03005121 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005122 if (!err) {
5123 rdev->sched_scan_req = request;
5124 nl80211_send_sched_scan(rdev, dev,
5125 NL80211_CMD_START_SCHED_SCAN);
5126 goto out;
5127 }
5128
5129out_free:
5130 kfree(request);
5131out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005132 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005133 return err;
5134}
5135
5136static int nl80211_stop_sched_scan(struct sk_buff *skb,
5137 struct genl_info *info)
5138{
5139 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005140 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005141
5142 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5143 !rdev->ops->sched_scan_stop)
5144 return -EOPNOTSUPP;
5145
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005146 mutex_lock(&rdev->sched_scan_mtx);
5147 err = __cfg80211_stop_sched_scan(rdev, false);
5148 mutex_unlock(&rdev->sched_scan_mtx);
5149
5150 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005151}
5152
Simon Wunderlich04f39042013-02-08 18:16:19 +01005153static int nl80211_start_radar_detection(struct sk_buff *skb,
5154 struct genl_info *info)
5155{
5156 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5157 struct net_device *dev = info->user_ptr[1];
5158 struct wireless_dev *wdev = dev->ieee80211_ptr;
5159 struct cfg80211_chan_def chandef;
5160 int err;
5161
5162 err = nl80211_parse_chandef(rdev, info, &chandef);
5163 if (err)
5164 return err;
5165
5166 if (wdev->cac_started)
5167 return -EBUSY;
5168
5169 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5170 if (err < 0)
5171 return err;
5172
5173 if (err == 0)
5174 return -EINVAL;
5175
5176 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5177 return -EINVAL;
5178
5179 if (!rdev->ops->start_radar_detection)
5180 return -EOPNOTSUPP;
5181
5182 mutex_lock(&rdev->devlist_mtx);
5183 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5184 chandef.chan, CHAN_MODE_SHARED,
5185 BIT(chandef.width));
5186 if (err)
5187 goto err_locked;
5188
5189 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5190 if (!err) {
5191 wdev->channel = chandef.chan;
5192 wdev->cac_started = true;
5193 wdev->cac_start_time = jiffies;
5194 }
5195err_locked:
5196 mutex_unlock(&rdev->devlist_mtx);
5197
5198 return err;
5199}
5200
Johannes Berg9720bb32011-06-21 09:45:33 +02005201static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5202 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005203 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005204 struct wireless_dev *wdev,
5205 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005206{
Johannes Berg48ab9052009-07-10 18:42:31 +02005207 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005208 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005209 void *hdr;
5210 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005211 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005212
5213 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005214
Eric W. Biederman15e47302012-09-07 20:12:54 +00005215 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005216 NL80211_CMD_NEW_SCAN_RESULTS);
5217 if (!hdr)
5218 return -1;
5219
Johannes Berg9720bb32011-06-21 09:45:33 +02005220 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5221
David S. Miller9360ffd2012-03-29 04:41:26 -04005222 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5223 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5224 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005225
5226 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5227 if (!bss)
5228 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005229 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005230 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005231 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005232
5233 rcu_read_lock();
5234 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005235 if (ies) {
5236 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5237 goto fail_unlock_rcu;
5238 tsf = true;
5239 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5240 ies->len, ies->data))
5241 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005242 }
5243 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005244 if (ies) {
5245 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5246 goto fail_unlock_rcu;
5247 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5248 ies->len, ies->data))
5249 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005250 }
5251 rcu_read_unlock();
5252
David S. Miller9360ffd2012-03-29 04:41:26 -04005253 if (res->beacon_interval &&
5254 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5255 goto nla_put_failure;
5256 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5257 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5258 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5259 jiffies_to_msecs(jiffies - intbss->ts)))
5260 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005261
Johannes Berg77965c92009-02-18 18:45:06 +01005262 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005263 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005264 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5265 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005266 break;
5267 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005268 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5269 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005270 break;
5271 default:
5272 break;
5273 }
5274
Johannes Berg48ab9052009-07-10 18:42:31 +02005275 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005276 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005277 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005278 if (intbss == wdev->current_bss &&
5279 nla_put_u32(msg, NL80211_BSS_STATUS,
5280 NL80211_BSS_STATUS_ASSOCIATED))
5281 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005282 break;
5283 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005284 if (intbss == wdev->current_bss &&
5285 nla_put_u32(msg, NL80211_BSS_STATUS,
5286 NL80211_BSS_STATUS_IBSS_JOINED))
5287 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005288 break;
5289 default:
5290 break;
5291 }
5292
Johannes Berg2a519312009-02-10 21:25:55 +01005293 nla_nest_end(msg, bss);
5294
5295 return genlmsg_end(msg, hdr);
5296
Johannes Berg8cef2c92013-02-05 16:54:31 +01005297 fail_unlock_rcu:
5298 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005299 nla_put_failure:
5300 genlmsg_cancel(msg, hdr);
5301 return -EMSGSIZE;
5302}
5303
5304static int nl80211_dump_scan(struct sk_buff *skb,
5305 struct netlink_callback *cb)
5306{
Johannes Berg48ab9052009-07-10 18:42:31 +02005307 struct cfg80211_registered_device *rdev;
5308 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005309 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005310 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005311 int start = cb->args[1], idx = 0;
5312 int err;
5313
Johannes Berg67748892010-10-04 21:14:06 +02005314 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5315 if (err)
5316 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005317
Johannes Berg48ab9052009-07-10 18:42:31 +02005318 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005319
Johannes Berg48ab9052009-07-10 18:42:31 +02005320 wdev_lock(wdev);
5321 spin_lock_bh(&rdev->bss_lock);
5322 cfg80211_bss_expire(rdev);
5323
Johannes Berg9720bb32011-06-21 09:45:33 +02005324 cb->seq = rdev->bss_generation;
5325
Johannes Berg48ab9052009-07-10 18:42:31 +02005326 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005327 if (++idx <= start)
5328 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005329 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005330 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005331 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005332 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005333 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005334 }
5335 }
5336
Johannes Berg48ab9052009-07-10 18:42:31 +02005337 spin_unlock_bh(&rdev->bss_lock);
5338 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005339
5340 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005341 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005342
Johannes Berg67748892010-10-04 21:14:06 +02005343 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005344}
5345
Eric W. Biederman15e47302012-09-07 20:12:54 +00005346static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005347 int flags, struct net_device *dev,
5348 struct survey_info *survey)
5349{
5350 void *hdr;
5351 struct nlattr *infoattr;
5352
Eric W. Biederman15e47302012-09-07 20:12:54 +00005353 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005354 NL80211_CMD_NEW_SURVEY_RESULTS);
5355 if (!hdr)
5356 return -ENOMEM;
5357
David S. Miller9360ffd2012-03-29 04:41:26 -04005358 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5359 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005360
5361 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5362 if (!infoattr)
5363 goto nla_put_failure;
5364
David S. Miller9360ffd2012-03-29 04:41:26 -04005365 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5366 survey->channel->center_freq))
5367 goto nla_put_failure;
5368
5369 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5370 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5371 goto nla_put_failure;
5372 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5373 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5374 goto nla_put_failure;
5375 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5376 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5377 survey->channel_time))
5378 goto nla_put_failure;
5379 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5380 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5381 survey->channel_time_busy))
5382 goto nla_put_failure;
5383 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5384 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5385 survey->channel_time_ext_busy))
5386 goto nla_put_failure;
5387 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5388 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5389 survey->channel_time_rx))
5390 goto nla_put_failure;
5391 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5392 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5393 survey->channel_time_tx))
5394 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005395
5396 nla_nest_end(msg, infoattr);
5397
5398 return genlmsg_end(msg, hdr);
5399
5400 nla_put_failure:
5401 genlmsg_cancel(msg, hdr);
5402 return -EMSGSIZE;
5403}
5404
5405static int nl80211_dump_survey(struct sk_buff *skb,
5406 struct netlink_callback *cb)
5407{
5408 struct survey_info survey;
5409 struct cfg80211_registered_device *dev;
5410 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005411 int survey_idx = cb->args[1];
5412 int res;
5413
Johannes Berg67748892010-10-04 21:14:06 +02005414 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5415 if (res)
5416 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005417
5418 if (!dev->ops->dump_survey) {
5419 res = -EOPNOTSUPP;
5420 goto out_err;
5421 }
5422
5423 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005424 struct ieee80211_channel *chan;
5425
Hila Gonene35e4d22012-06-27 17:19:42 +03005426 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005427 if (res == -ENOENT)
5428 break;
5429 if (res)
5430 goto out_err;
5431
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005432 /* Survey without a channel doesn't make sense */
5433 if (!survey.channel) {
5434 res = -EINVAL;
5435 goto out;
5436 }
5437
5438 chan = ieee80211_get_channel(&dev->wiphy,
5439 survey.channel->center_freq);
5440 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5441 survey_idx++;
5442 continue;
5443 }
5444
Holger Schurig61fa7132009-11-11 12:25:40 +01005445 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005446 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005447 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5448 netdev,
5449 &survey) < 0)
5450 goto out;
5451 survey_idx++;
5452 }
5453
5454 out:
5455 cb->args[1] = survey_idx;
5456 res = skb->len;
5457 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005458 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005459 return res;
5460}
5461
Samuel Ortizb23aa672009-07-01 21:26:54 +02005462static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5463{
5464 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5465 NL80211_WPA_VERSION_2));
5466}
5467
Jouni Malinen636a5d32009-03-19 13:39:22 +02005468static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5469{
Johannes Berg4c476992010-10-04 21:36:35 +02005470 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5471 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005472 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005473 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5474 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005475 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005476 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005477 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005478
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005479 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5480 return -EINVAL;
5481
5482 if (!info->attrs[NL80211_ATTR_MAC])
5483 return -EINVAL;
5484
Jouni Malinen17780922009-03-27 20:52:47 +02005485 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5486 return -EINVAL;
5487
Johannes Berg19957bb2009-07-02 17:20:43 +02005488 if (!info->attrs[NL80211_ATTR_SSID])
5489 return -EINVAL;
5490
5491 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5492 return -EINVAL;
5493
Johannes Bergfffd0932009-07-08 14:22:54 +02005494 err = nl80211_parse_key(info, &key);
5495 if (err)
5496 return err;
5497
5498 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005499 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5500 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005501 if (!key.p.key || !key.p.key_len)
5502 return -EINVAL;
5503 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5504 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5505 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5506 key.p.key_len != WLAN_KEY_LEN_WEP104))
5507 return -EINVAL;
5508 if (key.idx > 4)
5509 return -EINVAL;
5510 } else {
5511 key.p.key_len = 0;
5512 key.p.key = NULL;
5513 }
5514
Johannes Bergafea0b72010-08-10 09:46:42 +02005515 if (key.idx >= 0) {
5516 int i;
5517 bool ok = false;
5518 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5519 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5520 ok = true;
5521 break;
5522 }
5523 }
Johannes Berg4c476992010-10-04 21:36:35 +02005524 if (!ok)
5525 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005526 }
5527
Johannes Berg4c476992010-10-04 21:36:35 +02005528 if (!rdev->ops->auth)
5529 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005530
Johannes Berg074ac8d2010-09-16 14:58:22 +02005531 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005532 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5533 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005534
Johannes Berg19957bb2009-07-02 17:20:43 +02005535 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005536 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005537 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005538 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5539 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005540
Johannes Berg19957bb2009-07-02 17:20:43 +02005541 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5542 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5543
5544 if (info->attrs[NL80211_ATTR_IE]) {
5545 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5546 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5547 }
5548
5549 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005550 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005551 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005552
Jouni Malinene39e5b52012-09-30 19:29:39 +03005553 if (auth_type == NL80211_AUTHTYPE_SAE &&
5554 !info->attrs[NL80211_ATTR_SAE_DATA])
5555 return -EINVAL;
5556
5557 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5558 if (auth_type != NL80211_AUTHTYPE_SAE)
5559 return -EINVAL;
5560 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5561 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5562 /* need to include at least Auth Transaction and Status Code */
5563 if (sae_data_len < 4)
5564 return -EINVAL;
5565 }
5566
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005567 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5568
Johannes Berg95de8172012-01-20 13:55:25 +01005569 /*
5570 * Since we no longer track auth state, ignore
5571 * requests to only change local state.
5572 */
5573 if (local_state_change)
5574 return 0;
5575
Johannes Berg4c476992010-10-04 21:36:35 +02005576 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5577 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005578 key.p.key, key.p.key_len, key.idx,
5579 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005580}
5581
Johannes Bergc0692b82010-08-27 14:26:53 +03005582static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5583 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005584 struct cfg80211_crypto_settings *settings,
5585 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005586{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005587 memset(settings, 0, sizeof(*settings));
5588
Samuel Ortizb23aa672009-07-01 21:26:54 +02005589 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5590
Johannes Bergc0692b82010-08-27 14:26:53 +03005591 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5592 u16 proto;
5593 proto = nla_get_u16(
5594 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5595 settings->control_port_ethertype = cpu_to_be16(proto);
5596 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5597 proto != ETH_P_PAE)
5598 return -EINVAL;
5599 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5600 settings->control_port_no_encrypt = true;
5601 } else
5602 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5603
Samuel Ortizb23aa672009-07-01 21:26:54 +02005604 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5605 void *data;
5606 int len, i;
5607
5608 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5609 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5610 settings->n_ciphers_pairwise = len / sizeof(u32);
5611
5612 if (len % sizeof(u32))
5613 return -EINVAL;
5614
Johannes Berg3dc27d22009-07-02 21:36:37 +02005615 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005616 return -EINVAL;
5617
5618 memcpy(settings->ciphers_pairwise, data, len);
5619
5620 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005621 if (!cfg80211_supported_cipher_suite(
5622 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005623 settings->ciphers_pairwise[i]))
5624 return -EINVAL;
5625 }
5626
5627 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5628 settings->cipher_group =
5629 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005630 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5631 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005632 return -EINVAL;
5633 }
5634
5635 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5636 settings->wpa_versions =
5637 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5638 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5639 return -EINVAL;
5640 }
5641
5642 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5643 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005644 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005645
5646 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5647 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5648 settings->n_akm_suites = len / sizeof(u32);
5649
5650 if (len % sizeof(u32))
5651 return -EINVAL;
5652
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005653 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5654 return -EINVAL;
5655
Samuel Ortizb23aa672009-07-01 21:26:54 +02005656 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005657 }
5658
5659 return 0;
5660}
5661
Jouni Malinen636a5d32009-03-19 13:39:22 +02005662static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5663{
Johannes Berg4c476992010-10-04 21:36:35 +02005664 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5665 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005666 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005667 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005668 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005669 int err, ssid_len, ie_len = 0;
5670 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005671 u32 flags = 0;
5672 struct ieee80211_ht_cap *ht_capa = NULL;
5673 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005674
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005675 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5676 return -EINVAL;
5677
5678 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005679 !info->attrs[NL80211_ATTR_SSID] ||
5680 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005681 return -EINVAL;
5682
Johannes Berg4c476992010-10-04 21:36:35 +02005683 if (!rdev->ops->assoc)
5684 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005685
Johannes Berg074ac8d2010-09-16 14:58:22 +02005686 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005687 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5688 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005689
Johannes Berg19957bb2009-07-02 17:20:43 +02005690 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005691
Johannes Berg19957bb2009-07-02 17:20:43 +02005692 chan = ieee80211_get_channel(&rdev->wiphy,
5693 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005694 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5695 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005696
Johannes Berg19957bb2009-07-02 17:20:43 +02005697 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5698 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005699
5700 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005701 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5702 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005703 }
5704
Jouni Malinendc6382c2009-05-06 22:09:37 +03005705 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005706 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005707 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005708 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005709 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005710 else if (mfp != NL80211_MFP_NO)
5711 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005712 }
5713
Johannes Berg3e5d7642009-07-07 14:37:26 +02005714 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5715 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5716
Ben Greear7e7c8922011-11-18 11:31:59 -08005717 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5718 flags |= ASSOC_REQ_DISABLE_HT;
5719
5720 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5721 ht_capa_mask =
5722 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5723
5724 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5725 if (!ht_capa_mask)
5726 return -EINVAL;
5727 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5728 }
5729
Johannes Bergc0692b82010-08-27 14:26:53 +03005730 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005731 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005732 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5733 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005734 &crypto, flags, ht_capa,
5735 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005736
Jouni Malinen636a5d32009-03-19 13:39:22 +02005737 return err;
5738}
5739
5740static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5741{
Johannes Berg4c476992010-10-04 21:36:35 +02005742 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5743 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005744 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005745 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005746 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005747 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005748
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005749 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5750 return -EINVAL;
5751
5752 if (!info->attrs[NL80211_ATTR_MAC])
5753 return -EINVAL;
5754
5755 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5756 return -EINVAL;
5757
Johannes Berg4c476992010-10-04 21:36:35 +02005758 if (!rdev->ops->deauth)
5759 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005760
Johannes Berg074ac8d2010-09-16 14:58:22 +02005761 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005762 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5763 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005764
Johannes Berg19957bb2009-07-02 17:20:43 +02005765 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005766
Johannes Berg19957bb2009-07-02 17:20:43 +02005767 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5768 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005769 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005770 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005771 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005772
5773 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005774 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5775 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005776 }
5777
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005778 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5779
Johannes Berg4c476992010-10-04 21:36:35 +02005780 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5781 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005782}
5783
5784static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5785{
Johannes Berg4c476992010-10-04 21:36:35 +02005786 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5787 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005788 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005789 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005790 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005791 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005792
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005793 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5794 return -EINVAL;
5795
5796 if (!info->attrs[NL80211_ATTR_MAC])
5797 return -EINVAL;
5798
5799 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5800 return -EINVAL;
5801
Johannes Berg4c476992010-10-04 21:36:35 +02005802 if (!rdev->ops->disassoc)
5803 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005804
Johannes Berg074ac8d2010-09-16 14:58:22 +02005805 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005806 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5807 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005808
Johannes Berg19957bb2009-07-02 17:20:43 +02005809 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005810
Johannes Berg19957bb2009-07-02 17:20:43 +02005811 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5812 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005813 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005814 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005815 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005816
5817 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005818 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5819 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005820 }
5821
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005822 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5823
Johannes Berg4c476992010-10-04 21:36:35 +02005824 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5825 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005826}
5827
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005828static bool
5829nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5830 int mcast_rate[IEEE80211_NUM_BANDS],
5831 int rateval)
5832{
5833 struct wiphy *wiphy = &rdev->wiphy;
5834 bool found = false;
5835 int band, i;
5836
5837 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5838 struct ieee80211_supported_band *sband;
5839
5840 sband = wiphy->bands[band];
5841 if (!sband)
5842 continue;
5843
5844 for (i = 0; i < sband->n_bitrates; i++) {
5845 if (sband->bitrates[i].bitrate == rateval) {
5846 mcast_rate[band] = i + 1;
5847 found = true;
5848 break;
5849 }
5850 }
5851 }
5852
5853 return found;
5854}
5855
Johannes Berg04a773a2009-04-19 21:24:32 +02005856static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5857{
Johannes Berg4c476992010-10-04 21:36:35 +02005858 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5859 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005860 struct cfg80211_ibss_params ibss;
5861 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005862 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005863 int err;
5864
Johannes Berg8e30bc52009-04-22 17:45:38 +02005865 memset(&ibss, 0, sizeof(ibss));
5866
Johannes Berg04a773a2009-04-19 21:24:32 +02005867 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5868 return -EINVAL;
5869
Johannes Berg683b6d32012-11-08 21:25:48 +01005870 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005871 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5872 return -EINVAL;
5873
Johannes Berg8e30bc52009-04-22 17:45:38 +02005874 ibss.beacon_interval = 100;
5875
5876 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5877 ibss.beacon_interval =
5878 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5879 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5880 return -EINVAL;
5881 }
5882
Johannes Berg4c476992010-10-04 21:36:35 +02005883 if (!rdev->ops->join_ibss)
5884 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005885
Johannes Berg4c476992010-10-04 21:36:35 +02005886 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5887 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005888
Johannes Berg79c97e92009-07-07 03:56:12 +02005889 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005890
Johannes Berg39193492011-09-16 13:45:25 +02005891 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005892 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005893
5894 if (!is_valid_ether_addr(ibss.bssid))
5895 return -EINVAL;
5896 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005897 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5898 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5899
5900 if (info->attrs[NL80211_ATTR_IE]) {
5901 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5902 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5903 }
5904
Johannes Berg683b6d32012-11-08 21:25:48 +01005905 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5906 if (err)
5907 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005908
Johannes Berg683b6d32012-11-08 21:25:48 +01005909 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005910 return -EINVAL;
5911
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005912 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5913 return -EINVAL;
5914 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5915 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005916 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005917
Johannes Berg04a773a2009-04-19 21:24:32 +02005918 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005919 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005920
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005921 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5922 u8 *rates =
5923 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5924 int n_rates =
5925 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5926 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005927 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005928
Johannes Berg34850ab2011-07-18 18:08:35 +02005929 err = ieee80211_get_ratemask(sband, rates, n_rates,
5930 &ibss.basic_rates);
5931 if (err)
5932 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005933 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005934
5935 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5936 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5937 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5938 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005939
Johannes Berg4c476992010-10-04 21:36:35 +02005940 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305941 bool no_ht = false;
5942
Johannes Berg4c476992010-10-04 21:36:35 +02005943 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305944 info->attrs[NL80211_ATTR_KEYS],
5945 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005946 if (IS_ERR(connkeys))
5947 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305948
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005949 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5950 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305951 kfree(connkeys);
5952 return -EINVAL;
5953 }
Johannes Berg4c476992010-10-04 21:36:35 +02005954 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005955
Antonio Quartulli267335d2012-01-31 20:25:47 +01005956 ibss.control_port =
5957 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5958
Johannes Berg4c476992010-10-04 21:36:35 +02005959 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005960 if (err)
5961 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005962 return err;
5963}
5964
5965static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5966{
Johannes Berg4c476992010-10-04 21:36:35 +02005967 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5968 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005969
Johannes Berg4c476992010-10-04 21:36:35 +02005970 if (!rdev->ops->leave_ibss)
5971 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005972
Johannes Berg4c476992010-10-04 21:36:35 +02005973 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5974 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005975
Johannes Berg4c476992010-10-04 21:36:35 +02005976 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005977}
5978
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005979static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5980{
5981 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5982 struct net_device *dev = info->user_ptr[1];
5983 int mcast_rate[IEEE80211_NUM_BANDS];
5984 u32 nla_rate;
5985 int err;
5986
5987 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5988 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5989 return -EOPNOTSUPP;
5990
5991 if (!rdev->ops->set_mcast_rate)
5992 return -EOPNOTSUPP;
5993
5994 memset(mcast_rate, 0, sizeof(mcast_rate));
5995
5996 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5997 return -EINVAL;
5998
5999 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6000 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6001 return -EINVAL;
6002
6003 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6004
6005 return err;
6006}
6007
6008
Johannes Bergaff89a92009-07-01 21:26:51 +02006009#ifdef CONFIG_NL80211_TESTMODE
6010static struct genl_multicast_group nl80211_testmode_mcgrp = {
6011 .name = "testmode",
6012};
6013
6014static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6015{
Johannes Berg4c476992010-10-04 21:36:35 +02006016 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006017 int err;
6018
6019 if (!info->attrs[NL80211_ATTR_TESTDATA])
6020 return -EINVAL;
6021
Johannes Bergaff89a92009-07-01 21:26:51 +02006022 err = -EOPNOTSUPP;
6023 if (rdev->ops->testmode_cmd) {
6024 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006025 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006026 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6027 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6028 rdev->testmode_info = NULL;
6029 }
6030
Johannes Bergaff89a92009-07-01 21:26:51 +02006031 return err;
6032}
6033
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006034static int nl80211_testmode_dump(struct sk_buff *skb,
6035 struct netlink_callback *cb)
6036{
Johannes Berg00918d32011-12-13 17:22:05 +01006037 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006038 int err;
6039 long phy_idx;
6040 void *data = NULL;
6041 int data_len = 0;
6042
6043 if (cb->args[0]) {
6044 /*
6045 * 0 is a valid index, but not valid for args[0],
6046 * so we need to offset by 1.
6047 */
6048 phy_idx = cb->args[0] - 1;
6049 } else {
6050 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6051 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6052 nl80211_policy);
6053 if (err)
6054 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006055
Johannes Berg2bd7e352012-06-15 14:23:16 +02006056 mutex_lock(&cfg80211_mutex);
6057 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6058 nl80211_fam.attrbuf);
6059 if (IS_ERR(rdev)) {
6060 mutex_unlock(&cfg80211_mutex);
6061 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006062 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006063 phy_idx = rdev->wiphy_idx;
6064 rdev = NULL;
6065 mutex_unlock(&cfg80211_mutex);
6066
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006067 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6068 cb->args[1] =
6069 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6070 }
6071
6072 if (cb->args[1]) {
6073 data = nla_data((void *)cb->args[1]);
6074 data_len = nla_len((void *)cb->args[1]);
6075 }
6076
6077 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006078 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6079 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006080 mutex_unlock(&cfg80211_mutex);
6081 return -ENOENT;
6082 }
Johannes Berg00918d32011-12-13 17:22:05 +01006083 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006084 mutex_unlock(&cfg80211_mutex);
6085
Johannes Berg00918d32011-12-13 17:22:05 +01006086 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006087 err = -EOPNOTSUPP;
6088 goto out_err;
6089 }
6090
6091 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006092 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006093 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6094 NL80211_CMD_TESTMODE);
6095 struct nlattr *tmdata;
6096
David S. Miller9360ffd2012-03-29 04:41:26 -04006097 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006098 genlmsg_cancel(skb, hdr);
6099 break;
6100 }
6101
6102 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6103 if (!tmdata) {
6104 genlmsg_cancel(skb, hdr);
6105 break;
6106 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006107 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006108 nla_nest_end(skb, tmdata);
6109
6110 if (err == -ENOBUFS || err == -ENOENT) {
6111 genlmsg_cancel(skb, hdr);
6112 break;
6113 } else if (err) {
6114 genlmsg_cancel(skb, hdr);
6115 goto out_err;
6116 }
6117
6118 genlmsg_end(skb, hdr);
6119 }
6120
6121 err = skb->len;
6122 /* see above */
6123 cb->args[0] = phy_idx + 1;
6124 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006125 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006126 return err;
6127}
6128
Johannes Bergaff89a92009-07-01 21:26:51 +02006129static struct sk_buff *
6130__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006131 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006132{
6133 struct sk_buff *skb;
6134 void *hdr;
6135 struct nlattr *data;
6136
6137 skb = nlmsg_new(approxlen + 100, gfp);
6138 if (!skb)
6139 return NULL;
6140
Eric W. Biederman15e47302012-09-07 20:12:54 +00006141 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006142 if (!hdr) {
6143 kfree_skb(skb);
6144 return NULL;
6145 }
6146
David S. Miller9360ffd2012-03-29 04:41:26 -04006147 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6148 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006149 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6150
6151 ((void **)skb->cb)[0] = rdev;
6152 ((void **)skb->cb)[1] = hdr;
6153 ((void **)skb->cb)[2] = data;
6154
6155 return skb;
6156
6157 nla_put_failure:
6158 kfree_skb(skb);
6159 return NULL;
6160}
6161
6162struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6163 int approxlen)
6164{
6165 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6166
6167 if (WARN_ON(!rdev->testmode_info))
6168 return NULL;
6169
6170 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006171 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006172 rdev->testmode_info->snd_seq,
6173 GFP_KERNEL);
6174}
6175EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6176
6177int cfg80211_testmode_reply(struct sk_buff *skb)
6178{
6179 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6180 void *hdr = ((void **)skb->cb)[1];
6181 struct nlattr *data = ((void **)skb->cb)[2];
6182
6183 if (WARN_ON(!rdev->testmode_info)) {
6184 kfree_skb(skb);
6185 return -EINVAL;
6186 }
6187
6188 nla_nest_end(skb, data);
6189 genlmsg_end(skb, hdr);
6190 return genlmsg_reply(skb, rdev->testmode_info);
6191}
6192EXPORT_SYMBOL(cfg80211_testmode_reply);
6193
6194struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6195 int approxlen, gfp_t gfp)
6196{
6197 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6198
6199 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6200}
6201EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6202
6203void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6204{
6205 void *hdr = ((void **)skb->cb)[1];
6206 struct nlattr *data = ((void **)skb->cb)[2];
6207
6208 nla_nest_end(skb, data);
6209 genlmsg_end(skb, hdr);
6210 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6211}
6212EXPORT_SYMBOL(cfg80211_testmode_event);
6213#endif
6214
Samuel Ortizb23aa672009-07-01 21:26:54 +02006215static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6216{
Johannes Berg4c476992010-10-04 21:36:35 +02006217 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6218 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006219 struct cfg80211_connect_params connect;
6220 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006221 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006222 int err;
6223
6224 memset(&connect, 0, sizeof(connect));
6225
6226 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6227 return -EINVAL;
6228
6229 if (!info->attrs[NL80211_ATTR_SSID] ||
6230 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6231 return -EINVAL;
6232
6233 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6234 connect.auth_type =
6235 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006236 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6237 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006238 return -EINVAL;
6239 } else
6240 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6241
6242 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6243
Johannes Bergc0692b82010-08-27 14:26:53 +03006244 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006245 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006246 if (err)
6247 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006248
Johannes Berg074ac8d2010-09-16 14:58:22 +02006249 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006250 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6251 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006252
Johannes Berg79c97e92009-07-07 03:56:12 +02006253 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006254
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306255 connect.bg_scan_period = -1;
6256 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6257 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6258 connect.bg_scan_period =
6259 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6260 }
6261
Samuel Ortizb23aa672009-07-01 21:26:54 +02006262 if (info->attrs[NL80211_ATTR_MAC])
6263 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6264 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6265 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6266
6267 if (info->attrs[NL80211_ATTR_IE]) {
6268 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6269 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6270 }
6271
Jouni Malinencee00a92013-01-15 17:15:57 +02006272 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6273 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6274 if (connect.mfp != NL80211_MFP_REQUIRED &&
6275 connect.mfp != NL80211_MFP_NO)
6276 return -EINVAL;
6277 } else {
6278 connect.mfp = NL80211_MFP_NO;
6279 }
6280
Samuel Ortizb23aa672009-07-01 21:26:54 +02006281 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6282 connect.channel =
6283 ieee80211_get_channel(wiphy,
6284 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6285 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006286 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6287 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006288 }
6289
Johannes Bergfffd0932009-07-08 14:22:54 +02006290 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6291 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306292 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006293 if (IS_ERR(connkeys))
6294 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006295 }
6296
Ben Greear7e7c8922011-11-18 11:31:59 -08006297 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6298 connect.flags |= ASSOC_REQ_DISABLE_HT;
6299
6300 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6301 memcpy(&connect.ht_capa_mask,
6302 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6303 sizeof(connect.ht_capa_mask));
6304
6305 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006306 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6307 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006308 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006309 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006310 memcpy(&connect.ht_capa,
6311 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6312 sizeof(connect.ht_capa));
6313 }
6314
Johannes Bergfffd0932009-07-08 14:22:54 +02006315 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006316 if (err)
6317 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006318 return err;
6319}
6320
6321static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6322{
Johannes Berg4c476992010-10-04 21:36:35 +02006323 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6324 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006325 u16 reason;
6326
6327 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6328 reason = WLAN_REASON_DEAUTH_LEAVING;
6329 else
6330 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6331
6332 if (reason == 0)
6333 return -EINVAL;
6334
Johannes Berg074ac8d2010-09-16 14:58:22 +02006335 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006336 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6337 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006338
Johannes Berg4c476992010-10-04 21:36:35 +02006339 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006340}
6341
Johannes Berg463d0182009-07-14 00:33:35 +02006342static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6343{
Johannes Berg4c476992010-10-04 21:36:35 +02006344 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006345 struct net *net;
6346 int err;
6347 u32 pid;
6348
6349 if (!info->attrs[NL80211_ATTR_PID])
6350 return -EINVAL;
6351
6352 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6353
Johannes Berg463d0182009-07-14 00:33:35 +02006354 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006355 if (IS_ERR(net))
6356 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006357
6358 err = 0;
6359
6360 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006361 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6362 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006363
Johannes Berg463d0182009-07-14 00:33:35 +02006364 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006365 return err;
6366}
6367
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006368static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6369{
Johannes Berg4c476992010-10-04 21:36:35 +02006370 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006371 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6372 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006373 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006374 struct cfg80211_pmksa pmksa;
6375
6376 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6377
6378 if (!info->attrs[NL80211_ATTR_MAC])
6379 return -EINVAL;
6380
6381 if (!info->attrs[NL80211_ATTR_PMKID])
6382 return -EINVAL;
6383
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006384 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6385 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6386
Johannes Berg074ac8d2010-09-16 14:58:22 +02006387 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006388 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6389 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006390
6391 switch (info->genlhdr->cmd) {
6392 case NL80211_CMD_SET_PMKSA:
6393 rdev_ops = rdev->ops->set_pmksa;
6394 break;
6395 case NL80211_CMD_DEL_PMKSA:
6396 rdev_ops = rdev->ops->del_pmksa;
6397 break;
6398 default:
6399 WARN_ON(1);
6400 break;
6401 }
6402
Johannes Berg4c476992010-10-04 21:36:35 +02006403 if (!rdev_ops)
6404 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006405
Johannes Berg4c476992010-10-04 21:36:35 +02006406 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006407}
6408
6409static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6410{
Johannes Berg4c476992010-10-04 21:36:35 +02006411 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6412 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006413
Johannes Berg074ac8d2010-09-16 14:58:22 +02006414 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006415 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6416 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006417
Johannes Berg4c476992010-10-04 21:36:35 +02006418 if (!rdev->ops->flush_pmksa)
6419 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006420
Hila Gonene35e4d22012-06-27 17:19:42 +03006421 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006422}
6423
Arik Nemtsov109086c2011-09-28 14:12:50 +03006424static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6425{
6426 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6427 struct net_device *dev = info->user_ptr[1];
6428 u8 action_code, dialog_token;
6429 u16 status_code;
6430 u8 *peer;
6431
6432 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6433 !rdev->ops->tdls_mgmt)
6434 return -EOPNOTSUPP;
6435
6436 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6437 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6438 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6439 !info->attrs[NL80211_ATTR_IE] ||
6440 !info->attrs[NL80211_ATTR_MAC])
6441 return -EINVAL;
6442
6443 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6444 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6445 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6446 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6447
Hila Gonene35e4d22012-06-27 17:19:42 +03006448 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6449 dialog_token, status_code,
6450 nla_data(info->attrs[NL80211_ATTR_IE]),
6451 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006452}
6453
6454static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6455{
6456 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6457 struct net_device *dev = info->user_ptr[1];
6458 enum nl80211_tdls_operation operation;
6459 u8 *peer;
6460
6461 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6462 !rdev->ops->tdls_oper)
6463 return -EOPNOTSUPP;
6464
6465 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6466 !info->attrs[NL80211_ATTR_MAC])
6467 return -EINVAL;
6468
6469 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6470 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6471
Hila Gonene35e4d22012-06-27 17:19:42 +03006472 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006473}
6474
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006475static int nl80211_remain_on_channel(struct sk_buff *skb,
6476 struct genl_info *info)
6477{
Johannes Berg4c476992010-10-04 21:36:35 +02006478 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006479 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006480 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006481 struct sk_buff *msg;
6482 void *hdr;
6483 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006484 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006485 int err;
6486
6487 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6488 !info->attrs[NL80211_ATTR_DURATION])
6489 return -EINVAL;
6490
6491 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6492
Johannes Berg7c4ef712011-11-18 15:33:48 +01006493 if (!rdev->ops->remain_on_channel ||
6494 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006495 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006496
Johannes Bergebf348f2012-06-01 12:50:54 +02006497 /*
6498 * We should be on that channel for at least a minimum amount of
6499 * time (10ms) but no longer than the driver supports.
6500 */
6501 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6502 duration > rdev->wiphy.max_remain_on_channel_duration)
6503 return -EINVAL;
6504
Johannes Berg683b6d32012-11-08 21:25:48 +01006505 err = nl80211_parse_chandef(rdev, info, &chandef);
6506 if (err)
6507 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006508
6509 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006510 if (!msg)
6511 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006512
Eric W. Biederman15e47302012-09-07 20:12:54 +00006513 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006514 NL80211_CMD_REMAIN_ON_CHANNEL);
6515
6516 if (IS_ERR(hdr)) {
6517 err = PTR_ERR(hdr);
6518 goto free_msg;
6519 }
6520
Johannes Berg683b6d32012-11-08 21:25:48 +01006521 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6522 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006523
6524 if (err)
6525 goto free_msg;
6526
David S. Miller9360ffd2012-03-29 04:41:26 -04006527 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6528 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006529
6530 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006531
6532 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006533
6534 nla_put_failure:
6535 err = -ENOBUFS;
6536 free_msg:
6537 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006538 return err;
6539}
6540
6541static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6542 struct genl_info *info)
6543{
Johannes Berg4c476992010-10-04 21:36:35 +02006544 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006545 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006546 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006547
6548 if (!info->attrs[NL80211_ATTR_COOKIE])
6549 return -EINVAL;
6550
Johannes Berg4c476992010-10-04 21:36:35 +02006551 if (!rdev->ops->cancel_remain_on_channel)
6552 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006553
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006554 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6555
Hila Gonene35e4d22012-06-27 17:19:42 +03006556 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006557}
6558
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006559static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6560 u8 *rates, u8 rates_len)
6561{
6562 u8 i;
6563 u32 mask = 0;
6564
6565 for (i = 0; i < rates_len; i++) {
6566 int rate = (rates[i] & 0x7f) * 5;
6567 int ridx;
6568 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6569 struct ieee80211_rate *srate =
6570 &sband->bitrates[ridx];
6571 if (rate == srate->bitrate) {
6572 mask |= 1 << ridx;
6573 break;
6574 }
6575 }
6576 if (ridx == sband->n_bitrates)
6577 return 0; /* rate not found */
6578 }
6579
6580 return mask;
6581}
6582
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006583static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6584 u8 *rates, u8 rates_len,
6585 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6586{
6587 u8 i;
6588
6589 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6590
6591 for (i = 0; i < rates_len; i++) {
6592 int ridx, rbit;
6593
6594 ridx = rates[i] / 8;
6595 rbit = BIT(rates[i] % 8);
6596
6597 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006598 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006599 return false;
6600
6601 /* check availability */
6602 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6603 mcs[ridx] |= rbit;
6604 else
6605 return false;
6606 }
6607
6608 return true;
6609}
6610
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006611static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006612 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6613 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006614 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6615 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006616};
6617
6618static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6619 struct genl_info *info)
6620{
6621 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006623 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006624 int rem, i;
6625 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006626 struct nlattr *tx_rates;
6627 struct ieee80211_supported_band *sband;
6628
6629 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6630 return -EINVAL;
6631
Johannes Berg4c476992010-10-04 21:36:35 +02006632 if (!rdev->ops->set_bitrate_mask)
6633 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006634
6635 memset(&mask, 0, sizeof(mask));
6636 /* Default to all rates enabled */
6637 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6638 sband = rdev->wiphy.bands[i];
6639 mask.control[i].legacy =
6640 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006641 if (sband)
6642 memcpy(mask.control[i].mcs,
6643 sband->ht_cap.mcs.rx_mask,
6644 sizeof(mask.control[i].mcs));
6645 else
6646 memset(mask.control[i].mcs, 0,
6647 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006648 }
6649
6650 /*
6651 * The nested attribute uses enum nl80211_band as the index. This maps
6652 * directly to the enum ieee80211_band values used in cfg80211.
6653 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006654 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006655 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6656 {
6657 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006658 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6659 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006660 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006661 if (sband == NULL)
6662 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006663 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6664 nla_len(tx_rates), nl80211_txattr_policy);
6665 if (tb[NL80211_TXRATE_LEGACY]) {
6666 mask.control[band].legacy = rateset_to_mask(
6667 sband,
6668 nla_data(tb[NL80211_TXRATE_LEGACY]),
6669 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306670 if ((mask.control[band].legacy == 0) &&
6671 nla_len(tb[NL80211_TXRATE_LEGACY]))
6672 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006673 }
6674 if (tb[NL80211_TXRATE_MCS]) {
6675 if (!ht_rateset_to_mask(
6676 sband,
6677 nla_data(tb[NL80211_TXRATE_MCS]),
6678 nla_len(tb[NL80211_TXRATE_MCS]),
6679 mask.control[band].mcs))
6680 return -EINVAL;
6681 }
6682
6683 if (mask.control[band].legacy == 0) {
6684 /* don't allow empty legacy rates if HT
6685 * is not even supported. */
6686 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6687 return -EINVAL;
6688
6689 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6690 if (mask.control[band].mcs[i])
6691 break;
6692
6693 /* legacy and mcs rates may not be both empty */
6694 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006695 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006696 }
6697 }
6698
Hila Gonene35e4d22012-06-27 17:19:42 +03006699 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006700}
6701
Johannes Berg2e161f72010-08-12 15:38:38 +02006702static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006703{
Johannes Berg4c476992010-10-04 21:36:35 +02006704 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006705 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006706 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006707
6708 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6709 return -EINVAL;
6710
Johannes Berg2e161f72010-08-12 15:38:38 +02006711 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6712 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006713
Johannes Berg71bbc992012-06-15 15:30:18 +02006714 switch (wdev->iftype) {
6715 case NL80211_IFTYPE_STATION:
6716 case NL80211_IFTYPE_ADHOC:
6717 case NL80211_IFTYPE_P2P_CLIENT:
6718 case NL80211_IFTYPE_AP:
6719 case NL80211_IFTYPE_AP_VLAN:
6720 case NL80211_IFTYPE_MESH_POINT:
6721 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006722 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006723 break;
6724 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006725 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006726 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006727
6728 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006729 if (!rdev->ops->mgmt_tx)
6730 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006731
Eric W. Biederman15e47302012-09-07 20:12:54 +00006732 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006733 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6734 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006735}
6736
Johannes Berg2e161f72010-08-12 15:38:38 +02006737static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006738{
Johannes Berg4c476992010-10-04 21:36:35 +02006739 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006740 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006741 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006742 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006743 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006744 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006745 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006746 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006747 bool offchan, no_cck, dont_wait_for_ack;
6748
6749 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006750
Johannes Berg683b6d32012-11-08 21:25:48 +01006751 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006752 return -EINVAL;
6753
Johannes Berg4c476992010-10-04 21:36:35 +02006754 if (!rdev->ops->mgmt_tx)
6755 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006756
Johannes Berg71bbc992012-06-15 15:30:18 +02006757 switch (wdev->iftype) {
6758 case NL80211_IFTYPE_STATION:
6759 case NL80211_IFTYPE_ADHOC:
6760 case NL80211_IFTYPE_P2P_CLIENT:
6761 case NL80211_IFTYPE_AP:
6762 case NL80211_IFTYPE_AP_VLAN:
6763 case NL80211_IFTYPE_MESH_POINT:
6764 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006765 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006766 break;
6767 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006768 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006769 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006770
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006771 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006772 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006773 return -EINVAL;
6774 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006775
6776 /*
6777 * We should wait on the channel for at least a minimum amount
6778 * of time (10ms) but no longer than the driver supports.
6779 */
6780 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6781 wait > rdev->wiphy.max_remain_on_channel_duration)
6782 return -EINVAL;
6783
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006784 }
6785
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006786 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6787
Johannes Berg7c4ef712011-11-18 15:33:48 +01006788 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6789 return -EINVAL;
6790
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306791 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6792
Johannes Berg683b6d32012-11-08 21:25:48 +01006793 err = nl80211_parse_chandef(rdev, info, &chandef);
6794 if (err)
6795 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006796
Johannes Berge247bd902011-11-04 11:18:21 +01006797 if (!dont_wait_for_ack) {
6798 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6799 if (!msg)
6800 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006801
Eric W. Biederman15e47302012-09-07 20:12:54 +00006802 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006803 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006804
Johannes Berge247bd902011-11-04 11:18:21 +01006805 if (IS_ERR(hdr)) {
6806 err = PTR_ERR(hdr);
6807 goto free_msg;
6808 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006809 }
Johannes Berge247bd902011-11-04 11:18:21 +01006810
Johannes Berg683b6d32012-11-08 21:25:48 +01006811 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006812 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6813 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006814 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006815 if (err)
6816 goto free_msg;
6817
Johannes Berge247bd902011-11-04 11:18:21 +01006818 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006819 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6820 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006821
Johannes Berge247bd902011-11-04 11:18:21 +01006822 genlmsg_end(msg, hdr);
6823 return genlmsg_reply(msg, info);
6824 }
6825
6826 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006827
6828 nla_put_failure:
6829 err = -ENOBUFS;
6830 free_msg:
6831 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006832 return err;
6833}
6834
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006835static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6836{
6837 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006838 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006839 u64 cookie;
6840
6841 if (!info->attrs[NL80211_ATTR_COOKIE])
6842 return -EINVAL;
6843
6844 if (!rdev->ops->mgmt_tx_cancel_wait)
6845 return -EOPNOTSUPP;
6846
Johannes Berg71bbc992012-06-15 15:30:18 +02006847 switch (wdev->iftype) {
6848 case NL80211_IFTYPE_STATION:
6849 case NL80211_IFTYPE_ADHOC:
6850 case NL80211_IFTYPE_P2P_CLIENT:
6851 case NL80211_IFTYPE_AP:
6852 case NL80211_IFTYPE_AP_VLAN:
6853 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006854 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006855 break;
6856 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006857 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006858 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006859
6860 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6861
Hila Gonene35e4d22012-06-27 17:19:42 +03006862 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006863}
6864
Kalle Valoffb9eb32010-02-17 17:58:10 +02006865static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6866{
Johannes Berg4c476992010-10-04 21:36:35 +02006867 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006868 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006869 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006870 u8 ps_state;
6871 bool state;
6872 int err;
6873
Johannes Berg4c476992010-10-04 21:36:35 +02006874 if (!info->attrs[NL80211_ATTR_PS_STATE])
6875 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006876
6877 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6878
Johannes Berg4c476992010-10-04 21:36:35 +02006879 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6880 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006881
6882 wdev = dev->ieee80211_ptr;
6883
Johannes Berg4c476992010-10-04 21:36:35 +02006884 if (!rdev->ops->set_power_mgmt)
6885 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006886
6887 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6888
6889 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006890 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006891
Hila Gonene35e4d22012-06-27 17:19:42 +03006892 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006893 if (!err)
6894 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006895 return err;
6896}
6897
6898static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6899{
Johannes Berg4c476992010-10-04 21:36:35 +02006900 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006901 enum nl80211_ps_state ps_state;
6902 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006903 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006904 struct sk_buff *msg;
6905 void *hdr;
6906 int err;
6907
Kalle Valoffb9eb32010-02-17 17:58:10 +02006908 wdev = dev->ieee80211_ptr;
6909
Johannes Berg4c476992010-10-04 21:36:35 +02006910 if (!rdev->ops->set_power_mgmt)
6911 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006912
6913 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006914 if (!msg)
6915 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006916
Eric W. Biederman15e47302012-09-07 20:12:54 +00006917 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006918 NL80211_CMD_GET_POWER_SAVE);
6919 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006920 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006921 goto free_msg;
6922 }
6923
6924 if (wdev->ps)
6925 ps_state = NL80211_PS_ENABLED;
6926 else
6927 ps_state = NL80211_PS_DISABLED;
6928
David S. Miller9360ffd2012-03-29 04:41:26 -04006929 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6930 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006931
6932 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006933 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006934
Johannes Berg4c476992010-10-04 21:36:35 +02006935 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006936 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006937 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006938 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006939 return err;
6940}
6941
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006942static struct nla_policy
6943nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6944 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6945 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6946 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006947 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6948 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6949 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006950};
6951
Thomas Pedersen84f10702012-07-12 16:17:33 -07006952static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006953 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006954{
6955 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6956 struct wireless_dev *wdev;
6957 struct net_device *dev = info->user_ptr[1];
6958
Johannes Bergd9d8b012012-11-26 12:51:52 +01006959 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006960 return -EINVAL;
6961
6962 wdev = dev->ieee80211_ptr;
6963
6964 if (!rdev->ops->set_cqm_txe_config)
6965 return -EOPNOTSUPP;
6966
6967 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6968 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6969 return -EOPNOTSUPP;
6970
Hila Gonene35e4d22012-06-27 17:19:42 +03006971 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006972}
6973
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006974static int nl80211_set_cqm_rssi(struct genl_info *info,
6975 s32 threshold, u32 hysteresis)
6976{
Johannes Berg4c476992010-10-04 21:36:35 +02006977 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006978 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006979 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006980
6981 if (threshold > 0)
6982 return -EINVAL;
6983
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006984 wdev = dev->ieee80211_ptr;
6985
Johannes Berg4c476992010-10-04 21:36:35 +02006986 if (!rdev->ops->set_cqm_rssi_config)
6987 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006988
Johannes Berg074ac8d2010-09-16 14:58:22 +02006989 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006990 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6991 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006992
Hila Gonene35e4d22012-06-27 17:19:42 +03006993 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006994}
6995
6996static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6997{
6998 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6999 struct nlattr *cqm;
7000 int err;
7001
7002 cqm = info->attrs[NL80211_ATTR_CQM];
7003 if (!cqm) {
7004 err = -EINVAL;
7005 goto out;
7006 }
7007
7008 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7009 nl80211_attr_cqm_policy);
7010 if (err)
7011 goto out;
7012
7013 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7014 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7015 s32 threshold;
7016 u32 hysteresis;
7017 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7018 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7019 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007020 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7021 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7022 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7023 u32 rate, pkts, intvl;
7024 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7025 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7026 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7027 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007028 } else
7029 err = -EINVAL;
7030
7031out:
7032 return err;
7033}
7034
Johannes Berg29cbe682010-12-03 09:20:44 +01007035static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7036{
7037 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7038 struct net_device *dev = info->user_ptr[1];
7039 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007040 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007041 int err;
7042
7043 /* start with default */
7044 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007045 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007046
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007047 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007048 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007049 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007050 if (err)
7051 return err;
7052 }
7053
7054 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7055 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7056 return -EINVAL;
7057
Javier Cardonac80d5452010-12-16 17:37:49 -08007058 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7059 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7060
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007061 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7062 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7063 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7064 return -EINVAL;
7065
Marco Porsch9bdbf042013-01-07 16:04:51 +01007066 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7067 setup.beacon_interval =
7068 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7069 if (setup.beacon_interval < 10 ||
7070 setup.beacon_interval > 10000)
7071 return -EINVAL;
7072 }
7073
7074 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7075 setup.dtim_period =
7076 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7077 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7078 return -EINVAL;
7079 }
7080
Javier Cardonac80d5452010-12-16 17:37:49 -08007081 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7082 /* parse additional setup parameters if given */
7083 err = nl80211_parse_mesh_setup(info, &setup);
7084 if (err)
7085 return err;
7086 }
7087
Johannes Bergcc1d2802012-05-16 23:50:20 +02007088 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007089 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7090 if (err)
7091 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007092 } else {
7093 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007094 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007095 }
7096
Javier Cardonac80d5452010-12-16 17:37:49 -08007097 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007098}
7099
7100static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7101{
7102 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7103 struct net_device *dev = info->user_ptr[1];
7104
7105 return cfg80211_leave_mesh(rdev, dev);
7106}
7107
Johannes Bergdfb89c52012-06-27 09:23:48 +02007108#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007109static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7110 struct cfg80211_registered_device *rdev)
7111{
7112 struct nlattr *nl_pats, *nl_pat;
7113 int i, pat_len;
7114
7115 if (!rdev->wowlan->n_patterns)
7116 return 0;
7117
7118 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7119 if (!nl_pats)
7120 return -ENOBUFS;
7121
7122 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7123 nl_pat = nla_nest_start(msg, i + 1);
7124 if (!nl_pat)
7125 return -ENOBUFS;
7126 pat_len = rdev->wowlan->patterns[i].pattern_len;
7127 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7128 DIV_ROUND_UP(pat_len, 8),
7129 rdev->wowlan->patterns[i].mask) ||
7130 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7131 pat_len, rdev->wowlan->patterns[i].pattern) ||
7132 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7133 rdev->wowlan->patterns[i].pkt_offset))
7134 return -ENOBUFS;
7135 nla_nest_end(msg, nl_pat);
7136 }
7137 nla_nest_end(msg, nl_pats);
7138
7139 return 0;
7140}
7141
Johannes Berg2a0e0472013-01-23 22:57:40 +01007142static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7143 struct cfg80211_wowlan_tcp *tcp)
7144{
7145 struct nlattr *nl_tcp;
7146
7147 if (!tcp)
7148 return 0;
7149
7150 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7151 if (!nl_tcp)
7152 return -ENOBUFS;
7153
7154 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7155 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7156 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7157 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7158 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7159 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7160 tcp->payload_len, tcp->payload) ||
7161 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7162 tcp->data_interval) ||
7163 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7164 tcp->wake_len, tcp->wake_data) ||
7165 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7166 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7167 return -ENOBUFS;
7168
7169 if (tcp->payload_seq.len &&
7170 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7171 sizeof(tcp->payload_seq), &tcp->payload_seq))
7172 return -ENOBUFS;
7173
7174 if (tcp->payload_tok.len &&
7175 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7176 sizeof(tcp->payload_tok) + tcp->tokens_size,
7177 &tcp->payload_tok))
7178 return -ENOBUFS;
7179
7180 return 0;
7181}
7182
Johannes Bergff1b6e62011-05-04 15:37:28 +02007183static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7184{
7185 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7186 struct sk_buff *msg;
7187 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007188 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007189
Johannes Berg2a0e0472013-01-23 22:57:40 +01007190 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7191 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007192 return -EOPNOTSUPP;
7193
Johannes Berg2a0e0472013-01-23 22:57:40 +01007194 if (rdev->wowlan && rdev->wowlan->tcp) {
7195 /* adjust size to have room for all the data */
7196 size += rdev->wowlan->tcp->tokens_size +
7197 rdev->wowlan->tcp->payload_len +
7198 rdev->wowlan->tcp->wake_len +
7199 rdev->wowlan->tcp->wake_len / 8;
7200 }
7201
7202 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007203 if (!msg)
7204 return -ENOMEM;
7205
Eric W. Biederman15e47302012-09-07 20:12:54 +00007206 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007207 NL80211_CMD_GET_WOWLAN);
7208 if (!hdr)
7209 goto nla_put_failure;
7210
7211 if (rdev->wowlan) {
7212 struct nlattr *nl_wowlan;
7213
7214 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7215 if (!nl_wowlan)
7216 goto nla_put_failure;
7217
David S. Miller9360ffd2012-03-29 04:41:26 -04007218 if ((rdev->wowlan->any &&
7219 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7220 (rdev->wowlan->disconnect &&
7221 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7222 (rdev->wowlan->magic_pkt &&
7223 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7224 (rdev->wowlan->gtk_rekey_failure &&
7225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7226 (rdev->wowlan->eap_identity_req &&
7227 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7228 (rdev->wowlan->four_way_handshake &&
7229 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7230 (rdev->wowlan->rfkill_release &&
7231 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7232 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007233
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007234 if (nl80211_send_wowlan_patterns(msg, rdev))
7235 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007236
7237 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7238 goto nla_put_failure;
7239
Johannes Bergff1b6e62011-05-04 15:37:28 +02007240 nla_nest_end(msg, nl_wowlan);
7241 }
7242
7243 genlmsg_end(msg, hdr);
7244 return genlmsg_reply(msg, info);
7245
7246nla_put_failure:
7247 nlmsg_free(msg);
7248 return -ENOBUFS;
7249}
7250
Johannes Berg2a0e0472013-01-23 22:57:40 +01007251static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7252 struct nlattr *attr,
7253 struct cfg80211_wowlan *trig)
7254{
7255 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7256 struct cfg80211_wowlan_tcp *cfg;
7257 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7258 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7259 u32 size;
7260 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7261 int err, port;
7262
7263 if (!rdev->wiphy.wowlan.tcp)
7264 return -EINVAL;
7265
7266 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7267 nla_data(attr), nla_len(attr),
7268 nl80211_wowlan_tcp_policy);
7269 if (err)
7270 return err;
7271
7272 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7273 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7274 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7275 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7276 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7277 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7278 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7279 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7280 return -EINVAL;
7281
7282 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7283 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7284 return -EINVAL;
7285
7286 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7287 rdev->wiphy.wowlan.tcp->data_interval_max)
7288 return -EINVAL;
7289
7290 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7291 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7292 return -EINVAL;
7293
7294 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7295 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7296 return -EINVAL;
7297
7298 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7299 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7300
7301 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7302 tokens_size = tokln - sizeof(*tok);
7303
7304 if (!tok->len || tokens_size % tok->len)
7305 return -EINVAL;
7306 if (!rdev->wiphy.wowlan.tcp->tok)
7307 return -EINVAL;
7308 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7309 return -EINVAL;
7310 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7311 return -EINVAL;
7312 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7313 return -EINVAL;
7314 if (tok->offset + tok->len > data_size)
7315 return -EINVAL;
7316 }
7317
7318 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7319 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7320 if (!rdev->wiphy.wowlan.tcp->seq)
7321 return -EINVAL;
7322 if (seq->len == 0 || seq->len > 4)
7323 return -EINVAL;
7324 if (seq->len + seq->offset > data_size)
7325 return -EINVAL;
7326 }
7327
7328 size = sizeof(*cfg);
7329 size += data_size;
7330 size += wake_size + wake_mask_size;
7331 size += tokens_size;
7332
7333 cfg = kzalloc(size, GFP_KERNEL);
7334 if (!cfg)
7335 return -ENOMEM;
7336 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7337 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7338 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7339 ETH_ALEN);
7340 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7341 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7342 else
7343 port = 0;
7344#ifdef CONFIG_INET
7345 /* allocate a socket and port for it and use it */
7346 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7347 IPPROTO_TCP, &cfg->sock, 1);
7348 if (err) {
7349 kfree(cfg);
7350 return err;
7351 }
7352 if (inet_csk_get_port(cfg->sock->sk, port)) {
7353 sock_release(cfg->sock);
7354 kfree(cfg);
7355 return -EADDRINUSE;
7356 }
7357 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7358#else
7359 if (!port) {
7360 kfree(cfg);
7361 return -EINVAL;
7362 }
7363 cfg->src_port = port;
7364#endif
7365
7366 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7367 cfg->payload_len = data_size;
7368 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7369 memcpy((void *)cfg->payload,
7370 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7371 data_size);
7372 if (seq)
7373 cfg->payload_seq = *seq;
7374 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7375 cfg->wake_len = wake_size;
7376 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7377 memcpy((void *)cfg->wake_data,
7378 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7379 wake_size);
7380 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7381 data_size + wake_size;
7382 memcpy((void *)cfg->wake_mask,
7383 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7384 wake_mask_size);
7385 if (tok) {
7386 cfg->tokens_size = tokens_size;
7387 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7388 }
7389
7390 trig->tcp = cfg;
7391
7392 return 0;
7393}
7394
Johannes Bergff1b6e62011-05-04 15:37:28 +02007395static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7396{
7397 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7398 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007399 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007400 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007401 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7402 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007403 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007404
Johannes Berg2a0e0472013-01-23 22:57:40 +01007405 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7406 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007407 return -EOPNOTSUPP;
7408
Johannes Bergae33bd82012-07-12 16:25:02 +02007409 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7410 cfg80211_rdev_free_wowlan(rdev);
7411 rdev->wowlan = NULL;
7412 goto set_wakeup;
7413 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007414
7415 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7416 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7417 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7418 nl80211_wowlan_policy);
7419 if (err)
7420 return err;
7421
7422 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7423 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7424 return -EINVAL;
7425 new_triggers.any = true;
7426 }
7427
7428 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7429 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7430 return -EINVAL;
7431 new_triggers.disconnect = true;
7432 }
7433
7434 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7435 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7436 return -EINVAL;
7437 new_triggers.magic_pkt = true;
7438 }
7439
Johannes Berg77dbbb12011-07-13 10:48:55 +02007440 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7441 return -EINVAL;
7442
7443 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7444 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7445 return -EINVAL;
7446 new_triggers.gtk_rekey_failure = true;
7447 }
7448
7449 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7450 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7451 return -EINVAL;
7452 new_triggers.eap_identity_req = true;
7453 }
7454
7455 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7456 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7457 return -EINVAL;
7458 new_triggers.four_way_handshake = true;
7459 }
7460
7461 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7462 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7463 return -EINVAL;
7464 new_triggers.rfkill_release = true;
7465 }
7466
Johannes Bergff1b6e62011-05-04 15:37:28 +02007467 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7468 struct nlattr *pat;
7469 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007470 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007471 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7472
7473 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7474 rem)
7475 n_patterns++;
7476 if (n_patterns > wowlan->n_patterns)
7477 return -EINVAL;
7478
7479 new_triggers.patterns = kcalloc(n_patterns,
7480 sizeof(new_triggers.patterns[0]),
7481 GFP_KERNEL);
7482 if (!new_triggers.patterns)
7483 return -ENOMEM;
7484
7485 new_triggers.n_patterns = n_patterns;
7486 i = 0;
7487
7488 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7489 rem) {
7490 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7491 nla_data(pat), nla_len(pat), NULL);
7492 err = -EINVAL;
7493 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7494 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7495 goto error;
7496 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7497 mask_len = DIV_ROUND_UP(pat_len, 8);
7498 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7499 mask_len)
7500 goto error;
7501 if (pat_len > wowlan->pattern_max_len ||
7502 pat_len < wowlan->pattern_min_len)
7503 goto error;
7504
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007505 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7506 pkt_offset = 0;
7507 else
7508 pkt_offset = nla_get_u32(
7509 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7510 if (pkt_offset > wowlan->max_pkt_offset)
7511 goto error;
7512 new_triggers.patterns[i].pkt_offset = pkt_offset;
7513
Johannes Bergff1b6e62011-05-04 15:37:28 +02007514 new_triggers.patterns[i].mask =
7515 kmalloc(mask_len + pat_len, GFP_KERNEL);
7516 if (!new_triggers.patterns[i].mask) {
7517 err = -ENOMEM;
7518 goto error;
7519 }
7520 new_triggers.patterns[i].pattern =
7521 new_triggers.patterns[i].mask + mask_len;
7522 memcpy(new_triggers.patterns[i].mask,
7523 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7524 mask_len);
7525 new_triggers.patterns[i].pattern_len = pat_len;
7526 memcpy(new_triggers.patterns[i].pattern,
7527 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7528 pat_len);
7529 i++;
7530 }
7531 }
7532
Johannes Berg2a0e0472013-01-23 22:57:40 +01007533 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7534 err = nl80211_parse_wowlan_tcp(
7535 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7536 &new_triggers);
7537 if (err)
7538 goto error;
7539 }
7540
Johannes Bergae33bd82012-07-12 16:25:02 +02007541 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7542 if (!ntrig) {
7543 err = -ENOMEM;
7544 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007545 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007546 cfg80211_rdev_free_wowlan(rdev);
7547 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007548
Johannes Bergae33bd82012-07-12 16:25:02 +02007549 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007550 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007551 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007552
Johannes Bergff1b6e62011-05-04 15:37:28 +02007553 return 0;
7554 error:
7555 for (i = 0; i < new_triggers.n_patterns; i++)
7556 kfree(new_triggers.patterns[i].mask);
7557 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007558 if (new_triggers.tcp && new_triggers.tcp->sock)
7559 sock_release(new_triggers.tcp->sock);
7560 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007561 return err;
7562}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007563#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007564
Johannes Berge5497d72011-07-05 16:35:40 +02007565static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7566{
7567 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7568 struct net_device *dev = info->user_ptr[1];
7569 struct wireless_dev *wdev = dev->ieee80211_ptr;
7570 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7571 struct cfg80211_gtk_rekey_data rekey_data;
7572 int err;
7573
7574 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7575 return -EINVAL;
7576
7577 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7578 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7579 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7580 nl80211_rekey_policy);
7581 if (err)
7582 return err;
7583
7584 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7585 return -ERANGE;
7586 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7587 return -ERANGE;
7588 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7589 return -ERANGE;
7590
7591 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7592 NL80211_KEK_LEN);
7593 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7594 NL80211_KCK_LEN);
7595 memcpy(rekey_data.replay_ctr,
7596 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7597 NL80211_REPLAY_CTR_LEN);
7598
7599 wdev_lock(wdev);
7600 if (!wdev->current_bss) {
7601 err = -ENOTCONN;
7602 goto out;
7603 }
7604
7605 if (!rdev->ops->set_rekey_data) {
7606 err = -EOPNOTSUPP;
7607 goto out;
7608 }
7609
Hila Gonene35e4d22012-06-27 17:19:42 +03007610 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007611 out:
7612 wdev_unlock(wdev);
7613 return err;
7614}
7615
Johannes Berg28946da2011-11-04 11:18:12 +01007616static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7617 struct genl_info *info)
7618{
7619 struct net_device *dev = info->user_ptr[1];
7620 struct wireless_dev *wdev = dev->ieee80211_ptr;
7621
7622 if (wdev->iftype != NL80211_IFTYPE_AP &&
7623 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7624 return -EINVAL;
7625
Eric W. Biederman15e47302012-09-07 20:12:54 +00007626 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007627 return -EBUSY;
7628
Eric W. Biederman15e47302012-09-07 20:12:54 +00007629 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007630 return 0;
7631}
7632
Johannes Berg7f6cf312011-11-04 11:18:15 +01007633static int nl80211_probe_client(struct sk_buff *skb,
7634 struct genl_info *info)
7635{
7636 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7637 struct net_device *dev = info->user_ptr[1];
7638 struct wireless_dev *wdev = dev->ieee80211_ptr;
7639 struct sk_buff *msg;
7640 void *hdr;
7641 const u8 *addr;
7642 u64 cookie;
7643 int err;
7644
7645 if (wdev->iftype != NL80211_IFTYPE_AP &&
7646 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7647 return -EOPNOTSUPP;
7648
7649 if (!info->attrs[NL80211_ATTR_MAC])
7650 return -EINVAL;
7651
7652 if (!rdev->ops->probe_client)
7653 return -EOPNOTSUPP;
7654
7655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7656 if (!msg)
7657 return -ENOMEM;
7658
Eric W. Biederman15e47302012-09-07 20:12:54 +00007659 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007660 NL80211_CMD_PROBE_CLIENT);
7661
7662 if (IS_ERR(hdr)) {
7663 err = PTR_ERR(hdr);
7664 goto free_msg;
7665 }
7666
7667 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7668
Hila Gonene35e4d22012-06-27 17:19:42 +03007669 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007670 if (err)
7671 goto free_msg;
7672
David S. Miller9360ffd2012-03-29 04:41:26 -04007673 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7674 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007675
7676 genlmsg_end(msg, hdr);
7677
7678 return genlmsg_reply(msg, info);
7679
7680 nla_put_failure:
7681 err = -ENOBUFS;
7682 free_msg:
7683 nlmsg_free(msg);
7684 return err;
7685}
7686
Johannes Berg5e760232011-11-04 11:18:17 +01007687static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7688{
7689 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007690 struct cfg80211_beacon_registration *reg, *nreg;
7691 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007692
7693 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7694 return -EOPNOTSUPP;
7695
Ben Greear37c73b52012-10-26 14:49:25 -07007696 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7697 if (!nreg)
7698 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007699
Ben Greear37c73b52012-10-26 14:49:25 -07007700 /* First, check if already registered. */
7701 spin_lock_bh(&rdev->beacon_registrations_lock);
7702 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7703 if (reg->nlportid == info->snd_portid) {
7704 rv = -EALREADY;
7705 goto out_err;
7706 }
7707 }
7708 /* Add it to the list */
7709 nreg->nlportid = info->snd_portid;
7710 list_add(&nreg->list, &rdev->beacon_registrations);
7711
7712 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007713
7714 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007715out_err:
7716 spin_unlock_bh(&rdev->beacon_registrations_lock);
7717 kfree(nreg);
7718 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007719}
7720
Johannes Berg98104fde2012-06-16 00:19:54 +02007721static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7722{
7723 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7724 struct wireless_dev *wdev = info->user_ptr[1];
7725 int err;
7726
7727 if (!rdev->ops->start_p2p_device)
7728 return -EOPNOTSUPP;
7729
7730 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7731 return -EOPNOTSUPP;
7732
7733 if (wdev->p2p_started)
7734 return 0;
7735
7736 mutex_lock(&rdev->devlist_mtx);
7737 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7738 mutex_unlock(&rdev->devlist_mtx);
7739 if (err)
7740 return err;
7741
Johannes Bergeeb126e2012-10-23 15:16:50 +02007742 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007743 if (err)
7744 return err;
7745
7746 wdev->p2p_started = true;
7747 mutex_lock(&rdev->devlist_mtx);
7748 rdev->opencount++;
7749 mutex_unlock(&rdev->devlist_mtx);
7750
7751 return 0;
7752}
7753
7754static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7755{
7756 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7757 struct wireless_dev *wdev = info->user_ptr[1];
7758
7759 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7760 return -EOPNOTSUPP;
7761
7762 if (!rdev->ops->stop_p2p_device)
7763 return -EOPNOTSUPP;
7764
Johannes Bergf9f47522013-03-19 15:04:07 +01007765 mutex_lock(&rdev->sched_scan_mtx);
7766 cfg80211_stop_p2p_device(rdev, wdev);
7767 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +02007768
7769 return 0;
7770}
7771
Johannes Berg4c476992010-10-04 21:36:35 +02007772#define NL80211_FLAG_NEED_WIPHY 0x01
7773#define NL80211_FLAG_NEED_NETDEV 0x02
7774#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007775#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7776#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7777 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007778#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007779/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007780#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7781 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007782
7783static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7784 struct genl_info *info)
7785{
7786 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007787 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007788 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007789 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7790
7791 if (rtnl)
7792 rtnl_lock();
7793
7794 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007795 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007796 if (IS_ERR(rdev)) {
7797 if (rtnl)
7798 rtnl_unlock();
7799 return PTR_ERR(rdev);
7800 }
7801 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007802 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7803 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007804 mutex_lock(&cfg80211_mutex);
7805 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7806 info->attrs);
7807 if (IS_ERR(wdev)) {
7808 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007809 if (rtnl)
7810 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007811 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007812 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007813
Johannes Berg89a54e42012-06-15 14:33:17 +02007814 dev = wdev->netdev;
7815 rdev = wiphy_to_dev(wdev->wiphy);
7816
Johannes Berg1bf614e2012-06-15 15:23:36 +02007817 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7818 if (!dev) {
7819 mutex_unlock(&cfg80211_mutex);
7820 if (rtnl)
7821 rtnl_unlock();
7822 return -EINVAL;
7823 }
7824
7825 info->user_ptr[1] = dev;
7826 } else {
7827 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007828 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007829
Johannes Berg1bf614e2012-06-15 15:23:36 +02007830 if (dev) {
7831 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7832 !netif_running(dev)) {
7833 mutex_unlock(&cfg80211_mutex);
7834 if (rtnl)
7835 rtnl_unlock();
7836 return -ENETDOWN;
7837 }
7838
7839 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007840 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7841 if (!wdev->p2p_started) {
7842 mutex_unlock(&cfg80211_mutex);
7843 if (rtnl)
7844 rtnl_unlock();
7845 return -ENETDOWN;
7846 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007847 }
7848
Johannes Berg89a54e42012-06-15 14:33:17 +02007849 cfg80211_lock_rdev(rdev);
7850
7851 mutex_unlock(&cfg80211_mutex);
7852
Johannes Berg4c476992010-10-04 21:36:35 +02007853 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007854 }
7855
7856 return 0;
7857}
7858
7859static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7860 struct genl_info *info)
7861{
7862 if (info->user_ptr[0])
7863 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007864 if (info->user_ptr[1]) {
7865 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7866 struct wireless_dev *wdev = info->user_ptr[1];
7867
7868 if (wdev->netdev)
7869 dev_put(wdev->netdev);
7870 } else {
7871 dev_put(info->user_ptr[1]);
7872 }
7873 }
Johannes Berg4c476992010-10-04 21:36:35 +02007874 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7875 rtnl_unlock();
7876}
7877
Johannes Berg55682962007-09-20 13:09:35 -04007878static struct genl_ops nl80211_ops[] = {
7879 {
7880 .cmd = NL80211_CMD_GET_WIPHY,
7881 .doit = nl80211_get_wiphy,
7882 .dumpit = nl80211_dump_wiphy,
7883 .policy = nl80211_policy,
7884 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007885 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007886 },
7887 {
7888 .cmd = NL80211_CMD_SET_WIPHY,
7889 .doit = nl80211_set_wiphy,
7890 .policy = nl80211_policy,
7891 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007892 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007893 },
7894 {
7895 .cmd = NL80211_CMD_GET_INTERFACE,
7896 .doit = nl80211_get_interface,
7897 .dumpit = nl80211_dump_interface,
7898 .policy = nl80211_policy,
7899 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007900 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007901 },
7902 {
7903 .cmd = NL80211_CMD_SET_INTERFACE,
7904 .doit = nl80211_set_interface,
7905 .policy = nl80211_policy,
7906 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007907 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7908 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007909 },
7910 {
7911 .cmd = NL80211_CMD_NEW_INTERFACE,
7912 .doit = nl80211_new_interface,
7913 .policy = nl80211_policy,
7914 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007915 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7916 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007917 },
7918 {
7919 .cmd = NL80211_CMD_DEL_INTERFACE,
7920 .doit = nl80211_del_interface,
7921 .policy = nl80211_policy,
7922 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007923 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007924 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007925 },
Johannes Berg41ade002007-12-19 02:03:29 +01007926 {
7927 .cmd = NL80211_CMD_GET_KEY,
7928 .doit = nl80211_get_key,
7929 .policy = nl80211_policy,
7930 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007931 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007932 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007933 },
7934 {
7935 .cmd = NL80211_CMD_SET_KEY,
7936 .doit = nl80211_set_key,
7937 .policy = nl80211_policy,
7938 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007939 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007940 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007941 },
7942 {
7943 .cmd = NL80211_CMD_NEW_KEY,
7944 .doit = nl80211_new_key,
7945 .policy = nl80211_policy,
7946 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007947 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007948 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007949 },
7950 {
7951 .cmd = NL80211_CMD_DEL_KEY,
7952 .doit = nl80211_del_key,
7953 .policy = nl80211_policy,
7954 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007955 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007956 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007957 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007958 {
7959 .cmd = NL80211_CMD_SET_BEACON,
7960 .policy = nl80211_policy,
7961 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007962 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007963 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007964 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007965 },
7966 {
Johannes Berg88600202012-02-13 15:17:18 +01007967 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007968 .policy = nl80211_policy,
7969 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007970 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007971 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007972 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007973 },
7974 {
Johannes Berg88600202012-02-13 15:17:18 +01007975 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007976 .policy = nl80211_policy,
7977 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007978 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007979 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007980 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007981 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007982 {
7983 .cmd = NL80211_CMD_GET_STATION,
7984 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007985 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007986 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007987 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7988 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007989 },
7990 {
7991 .cmd = NL80211_CMD_SET_STATION,
7992 .doit = nl80211_set_station,
7993 .policy = nl80211_policy,
7994 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007995 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007996 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007997 },
7998 {
7999 .cmd = NL80211_CMD_NEW_STATION,
8000 .doit = nl80211_new_station,
8001 .policy = nl80211_policy,
8002 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008003 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008004 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008005 },
8006 {
8007 .cmd = NL80211_CMD_DEL_STATION,
8008 .doit = nl80211_del_station,
8009 .policy = nl80211_policy,
8010 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008011 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008012 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008013 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008014 {
8015 .cmd = NL80211_CMD_GET_MPATH,
8016 .doit = nl80211_get_mpath,
8017 .dumpit = nl80211_dump_mpath,
8018 .policy = nl80211_policy,
8019 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008020 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008021 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008022 },
8023 {
8024 .cmd = NL80211_CMD_SET_MPATH,
8025 .doit = nl80211_set_mpath,
8026 .policy = nl80211_policy,
8027 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008028 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008029 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008030 },
8031 {
8032 .cmd = NL80211_CMD_NEW_MPATH,
8033 .doit = nl80211_new_mpath,
8034 .policy = nl80211_policy,
8035 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008036 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008037 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008038 },
8039 {
8040 .cmd = NL80211_CMD_DEL_MPATH,
8041 .doit = nl80211_del_mpath,
8042 .policy = nl80211_policy,
8043 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008044 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008045 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008046 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008047 {
8048 .cmd = NL80211_CMD_SET_BSS,
8049 .doit = nl80211_set_bss,
8050 .policy = nl80211_policy,
8051 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008052 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008053 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008054 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008055 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008056 .cmd = NL80211_CMD_GET_REG,
8057 .doit = nl80211_get_reg,
8058 .policy = nl80211_policy,
8059 /* can be retrieved by unprivileged users */
8060 },
8061 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008062 .cmd = NL80211_CMD_SET_REG,
8063 .doit = nl80211_set_reg,
8064 .policy = nl80211_policy,
8065 .flags = GENL_ADMIN_PERM,
8066 },
8067 {
8068 .cmd = NL80211_CMD_REQ_SET_REG,
8069 .doit = nl80211_req_set_reg,
8070 .policy = nl80211_policy,
8071 .flags = GENL_ADMIN_PERM,
8072 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008073 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008074 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8075 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008076 .policy = nl80211_policy,
8077 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008078 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008079 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008080 },
8081 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008082 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8083 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008084 .policy = nl80211_policy,
8085 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008086 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008087 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008088 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008089 {
Johannes Berg2a519312009-02-10 21:25:55 +01008090 .cmd = NL80211_CMD_TRIGGER_SCAN,
8091 .doit = nl80211_trigger_scan,
8092 .policy = nl80211_policy,
8093 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008094 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008095 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008096 },
8097 {
8098 .cmd = NL80211_CMD_GET_SCAN,
8099 .policy = nl80211_policy,
8100 .dumpit = nl80211_dump_scan,
8101 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008102 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008103 .cmd = NL80211_CMD_START_SCHED_SCAN,
8104 .doit = nl80211_start_sched_scan,
8105 .policy = nl80211_policy,
8106 .flags = GENL_ADMIN_PERM,
8107 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8108 NL80211_FLAG_NEED_RTNL,
8109 },
8110 {
8111 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8112 .doit = nl80211_stop_sched_scan,
8113 .policy = nl80211_policy,
8114 .flags = GENL_ADMIN_PERM,
8115 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8116 NL80211_FLAG_NEED_RTNL,
8117 },
8118 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008119 .cmd = NL80211_CMD_AUTHENTICATE,
8120 .doit = nl80211_authenticate,
8121 .policy = nl80211_policy,
8122 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008123 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008124 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008125 },
8126 {
8127 .cmd = NL80211_CMD_ASSOCIATE,
8128 .doit = nl80211_associate,
8129 .policy = nl80211_policy,
8130 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008131 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008132 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008133 },
8134 {
8135 .cmd = NL80211_CMD_DEAUTHENTICATE,
8136 .doit = nl80211_deauthenticate,
8137 .policy = nl80211_policy,
8138 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008139 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008140 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008141 },
8142 {
8143 .cmd = NL80211_CMD_DISASSOCIATE,
8144 .doit = nl80211_disassociate,
8145 .policy = nl80211_policy,
8146 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008147 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008148 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008149 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008150 {
8151 .cmd = NL80211_CMD_JOIN_IBSS,
8152 .doit = nl80211_join_ibss,
8153 .policy = nl80211_policy,
8154 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008155 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008156 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008157 },
8158 {
8159 .cmd = NL80211_CMD_LEAVE_IBSS,
8160 .doit = nl80211_leave_ibss,
8161 .policy = nl80211_policy,
8162 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008163 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008164 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008165 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008166#ifdef CONFIG_NL80211_TESTMODE
8167 {
8168 .cmd = NL80211_CMD_TESTMODE,
8169 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008170 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008171 .policy = nl80211_policy,
8172 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008173 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8174 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008175 },
8176#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008177 {
8178 .cmd = NL80211_CMD_CONNECT,
8179 .doit = nl80211_connect,
8180 .policy = nl80211_policy,
8181 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008182 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008183 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008184 },
8185 {
8186 .cmd = NL80211_CMD_DISCONNECT,
8187 .doit = nl80211_disconnect,
8188 .policy = nl80211_policy,
8189 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008190 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008191 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008192 },
Johannes Berg463d0182009-07-14 00:33:35 +02008193 {
8194 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8195 .doit = nl80211_wiphy_netns,
8196 .policy = nl80211_policy,
8197 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008198 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8199 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008200 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008201 {
8202 .cmd = NL80211_CMD_GET_SURVEY,
8203 .policy = nl80211_policy,
8204 .dumpit = nl80211_dump_survey,
8205 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008206 {
8207 .cmd = NL80211_CMD_SET_PMKSA,
8208 .doit = nl80211_setdel_pmksa,
8209 .policy = nl80211_policy,
8210 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008211 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008212 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008213 },
8214 {
8215 .cmd = NL80211_CMD_DEL_PMKSA,
8216 .doit = nl80211_setdel_pmksa,
8217 .policy = nl80211_policy,
8218 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008219 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008220 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008221 },
8222 {
8223 .cmd = NL80211_CMD_FLUSH_PMKSA,
8224 .doit = nl80211_flush_pmksa,
8225 .policy = nl80211_policy,
8226 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008227 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008228 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008229 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008230 {
8231 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8232 .doit = nl80211_remain_on_channel,
8233 .policy = nl80211_policy,
8234 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008235 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008236 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008237 },
8238 {
8239 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8240 .doit = nl80211_cancel_remain_on_channel,
8241 .policy = nl80211_policy,
8242 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008243 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008244 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008245 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008246 {
8247 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8248 .doit = nl80211_set_tx_bitrate_mask,
8249 .policy = nl80211_policy,
8250 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008251 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8252 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008253 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008254 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008255 .cmd = NL80211_CMD_REGISTER_FRAME,
8256 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008257 .policy = nl80211_policy,
8258 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008259 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008260 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008261 },
8262 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008263 .cmd = NL80211_CMD_FRAME,
8264 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008265 .policy = nl80211_policy,
8266 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008267 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008268 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008269 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008270 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008271 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8272 .doit = nl80211_tx_mgmt_cancel_wait,
8273 .policy = nl80211_policy,
8274 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008275 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008276 NL80211_FLAG_NEED_RTNL,
8277 },
8278 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008279 .cmd = NL80211_CMD_SET_POWER_SAVE,
8280 .doit = nl80211_set_power_save,
8281 .policy = nl80211_policy,
8282 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008283 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8284 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008285 },
8286 {
8287 .cmd = NL80211_CMD_GET_POWER_SAVE,
8288 .doit = nl80211_get_power_save,
8289 .policy = nl80211_policy,
8290 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008291 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8292 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008293 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008294 {
8295 .cmd = NL80211_CMD_SET_CQM,
8296 .doit = nl80211_set_cqm,
8297 .policy = nl80211_policy,
8298 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008299 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8300 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008301 },
Johannes Bergf444de02010-05-05 15:25:02 +02008302 {
8303 .cmd = NL80211_CMD_SET_CHANNEL,
8304 .doit = nl80211_set_channel,
8305 .policy = nl80211_policy,
8306 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008307 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8308 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008309 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008310 {
8311 .cmd = NL80211_CMD_SET_WDS_PEER,
8312 .doit = nl80211_set_wds_peer,
8313 .policy = nl80211_policy,
8314 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008315 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8316 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008317 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008318 {
8319 .cmd = NL80211_CMD_JOIN_MESH,
8320 .doit = nl80211_join_mesh,
8321 .policy = nl80211_policy,
8322 .flags = GENL_ADMIN_PERM,
8323 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8324 NL80211_FLAG_NEED_RTNL,
8325 },
8326 {
8327 .cmd = NL80211_CMD_LEAVE_MESH,
8328 .doit = nl80211_leave_mesh,
8329 .policy = nl80211_policy,
8330 .flags = GENL_ADMIN_PERM,
8331 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8332 NL80211_FLAG_NEED_RTNL,
8333 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008334#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008335 {
8336 .cmd = NL80211_CMD_GET_WOWLAN,
8337 .doit = nl80211_get_wowlan,
8338 .policy = nl80211_policy,
8339 /* can be retrieved by unprivileged users */
8340 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8341 NL80211_FLAG_NEED_RTNL,
8342 },
8343 {
8344 .cmd = NL80211_CMD_SET_WOWLAN,
8345 .doit = nl80211_set_wowlan,
8346 .policy = nl80211_policy,
8347 .flags = GENL_ADMIN_PERM,
8348 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8349 NL80211_FLAG_NEED_RTNL,
8350 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008351#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008352 {
8353 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8354 .doit = nl80211_set_rekey_data,
8355 .policy = nl80211_policy,
8356 .flags = GENL_ADMIN_PERM,
8357 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8358 NL80211_FLAG_NEED_RTNL,
8359 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008360 {
8361 .cmd = NL80211_CMD_TDLS_MGMT,
8362 .doit = nl80211_tdls_mgmt,
8363 .policy = nl80211_policy,
8364 .flags = GENL_ADMIN_PERM,
8365 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8366 NL80211_FLAG_NEED_RTNL,
8367 },
8368 {
8369 .cmd = NL80211_CMD_TDLS_OPER,
8370 .doit = nl80211_tdls_oper,
8371 .policy = nl80211_policy,
8372 .flags = GENL_ADMIN_PERM,
8373 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8374 NL80211_FLAG_NEED_RTNL,
8375 },
Johannes Berg28946da2011-11-04 11:18:12 +01008376 {
8377 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8378 .doit = nl80211_register_unexpected_frame,
8379 .policy = nl80211_policy,
8380 .flags = GENL_ADMIN_PERM,
8381 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8382 NL80211_FLAG_NEED_RTNL,
8383 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008384 {
8385 .cmd = NL80211_CMD_PROBE_CLIENT,
8386 .doit = nl80211_probe_client,
8387 .policy = nl80211_policy,
8388 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008389 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008390 NL80211_FLAG_NEED_RTNL,
8391 },
Johannes Berg5e760232011-11-04 11:18:17 +01008392 {
8393 .cmd = NL80211_CMD_REGISTER_BEACONS,
8394 .doit = nl80211_register_beacons,
8395 .policy = nl80211_policy,
8396 .flags = GENL_ADMIN_PERM,
8397 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8398 NL80211_FLAG_NEED_RTNL,
8399 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008400 {
8401 .cmd = NL80211_CMD_SET_NOACK_MAP,
8402 .doit = nl80211_set_noack_map,
8403 .policy = nl80211_policy,
8404 .flags = GENL_ADMIN_PERM,
8405 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8406 NL80211_FLAG_NEED_RTNL,
8407 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008408 {
8409 .cmd = NL80211_CMD_START_P2P_DEVICE,
8410 .doit = nl80211_start_p2p_device,
8411 .policy = nl80211_policy,
8412 .flags = GENL_ADMIN_PERM,
8413 .internal_flags = NL80211_FLAG_NEED_WDEV |
8414 NL80211_FLAG_NEED_RTNL,
8415 },
8416 {
8417 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8418 .doit = nl80211_stop_p2p_device,
8419 .policy = nl80211_policy,
8420 .flags = GENL_ADMIN_PERM,
8421 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8422 NL80211_FLAG_NEED_RTNL,
8423 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008424 {
8425 .cmd = NL80211_CMD_SET_MCAST_RATE,
8426 .doit = nl80211_set_mcast_rate,
8427 .policy = nl80211_policy,
8428 .flags = GENL_ADMIN_PERM,
8429 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8430 NL80211_FLAG_NEED_RTNL,
8431 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308432 {
8433 .cmd = NL80211_CMD_SET_MAC_ACL,
8434 .doit = nl80211_set_mac_acl,
8435 .policy = nl80211_policy,
8436 .flags = GENL_ADMIN_PERM,
8437 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8438 NL80211_FLAG_NEED_RTNL,
8439 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008440 {
8441 .cmd = NL80211_CMD_RADAR_DETECT,
8442 .doit = nl80211_start_radar_detection,
8443 .policy = nl80211_policy,
8444 .flags = GENL_ADMIN_PERM,
8445 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8446 NL80211_FLAG_NEED_RTNL,
8447 },
Johannes Berg55682962007-09-20 13:09:35 -04008448};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008449
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008450static struct genl_multicast_group nl80211_mlme_mcgrp = {
8451 .name = "mlme",
8452};
Johannes Berg55682962007-09-20 13:09:35 -04008453
8454/* multicast groups */
8455static struct genl_multicast_group nl80211_config_mcgrp = {
8456 .name = "config",
8457};
Johannes Berg2a519312009-02-10 21:25:55 +01008458static struct genl_multicast_group nl80211_scan_mcgrp = {
8459 .name = "scan",
8460};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008461static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8462 .name = "regulatory",
8463};
Johannes Berg55682962007-09-20 13:09:35 -04008464
8465/* notification functions */
8466
8467void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8468{
8469 struct sk_buff *msg;
8470
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008471 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008472 if (!msg)
8473 return;
8474
8475 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8476 nlmsg_free(msg);
8477 return;
8478 }
8479
Johannes Berg463d0182009-07-14 00:33:35 +02008480 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8481 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008482}
8483
Johannes Berg362a4152009-05-24 16:43:15 +02008484static int nl80211_add_scan_req(struct sk_buff *msg,
8485 struct cfg80211_registered_device *rdev)
8486{
8487 struct cfg80211_scan_request *req = rdev->scan_req;
8488 struct nlattr *nest;
8489 int i;
8490
Johannes Bergf9f47522013-03-19 15:04:07 +01008491 lockdep_assert_held(&rdev->sched_scan_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +02008492
Johannes Berg362a4152009-05-24 16:43:15 +02008493 if (WARN_ON(!req))
8494 return 0;
8495
8496 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8497 if (!nest)
8498 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008499 for (i = 0; i < req->n_ssids; i++) {
8500 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8501 goto nla_put_failure;
8502 }
Johannes Berg362a4152009-05-24 16:43:15 +02008503 nla_nest_end(msg, nest);
8504
8505 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8506 if (!nest)
8507 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008508 for (i = 0; i < req->n_channels; i++) {
8509 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8510 goto nla_put_failure;
8511 }
Johannes Berg362a4152009-05-24 16:43:15 +02008512 nla_nest_end(msg, nest);
8513
David S. Miller9360ffd2012-03-29 04:41:26 -04008514 if (req->ie &&
8515 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8516 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008517
Sam Lefflered4737712012-10-11 21:03:31 -07008518 if (req->flags)
8519 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8520
Johannes Berg362a4152009-05-24 16:43:15 +02008521 return 0;
8522 nla_put_failure:
8523 return -ENOBUFS;
8524}
8525
Johannes Berga538e2d2009-06-16 19:56:42 +02008526static int nl80211_send_scan_msg(struct sk_buff *msg,
8527 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008528 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008529 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008530 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008531{
8532 void *hdr;
8533
Eric W. Biederman15e47302012-09-07 20:12:54 +00008534 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008535 if (!hdr)
8536 return -1;
8537
David S. Miller9360ffd2012-03-29 04:41:26 -04008538 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008539 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8540 wdev->netdev->ifindex)) ||
8541 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008542 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008543
Johannes Berg362a4152009-05-24 16:43:15 +02008544 /* ignore errors and send incomplete event anyway */
8545 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008546
8547 return genlmsg_end(msg, hdr);
8548
8549 nla_put_failure:
8550 genlmsg_cancel(msg, hdr);
8551 return -EMSGSIZE;
8552}
8553
Luciano Coelho807f8a82011-05-11 17:09:35 +03008554static int
8555nl80211_send_sched_scan_msg(struct sk_buff *msg,
8556 struct cfg80211_registered_device *rdev,
8557 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008558 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008559{
8560 void *hdr;
8561
Eric W. Biederman15e47302012-09-07 20:12:54 +00008562 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008563 if (!hdr)
8564 return -1;
8565
David S. Miller9360ffd2012-03-29 04:41:26 -04008566 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8567 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8568 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008569
8570 return genlmsg_end(msg, hdr);
8571
8572 nla_put_failure:
8573 genlmsg_cancel(msg, hdr);
8574 return -EMSGSIZE;
8575}
8576
Johannes Berga538e2d2009-06-16 19:56:42 +02008577void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008578 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008579{
8580 struct sk_buff *msg;
8581
Thomas Graf58050fc2012-06-28 03:57:45 +00008582 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008583 if (!msg)
8584 return;
8585
Johannes Bergfd014282012-06-18 19:17:03 +02008586 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008587 NL80211_CMD_TRIGGER_SCAN) < 0) {
8588 nlmsg_free(msg);
8589 return;
8590 }
8591
Johannes Berg463d0182009-07-14 00:33:35 +02008592 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8593 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008594}
8595
Johannes Berg2a519312009-02-10 21:25:55 +01008596void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008597 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008598{
8599 struct sk_buff *msg;
8600
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008601 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008602 if (!msg)
8603 return;
8604
Johannes Bergfd014282012-06-18 19:17:03 +02008605 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008606 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008607 nlmsg_free(msg);
8608 return;
8609 }
8610
Johannes Berg463d0182009-07-14 00:33:35 +02008611 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8612 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008613}
8614
8615void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008616 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008617{
8618 struct sk_buff *msg;
8619
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008620 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008621 if (!msg)
8622 return;
8623
Johannes Bergfd014282012-06-18 19:17:03 +02008624 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008625 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008626 nlmsg_free(msg);
8627 return;
8628 }
8629
Johannes Berg463d0182009-07-14 00:33:35 +02008630 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8631 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008632}
8633
Luciano Coelho807f8a82011-05-11 17:09:35 +03008634void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8635 struct net_device *netdev)
8636{
8637 struct sk_buff *msg;
8638
8639 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8640 if (!msg)
8641 return;
8642
8643 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8644 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8645 nlmsg_free(msg);
8646 return;
8647 }
8648
8649 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8650 nl80211_scan_mcgrp.id, GFP_KERNEL);
8651}
8652
8653void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8654 struct net_device *netdev, u32 cmd)
8655{
8656 struct sk_buff *msg;
8657
Thomas Graf58050fc2012-06-28 03:57:45 +00008658 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008659 if (!msg)
8660 return;
8661
8662 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8663 nlmsg_free(msg);
8664 return;
8665 }
8666
8667 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8668 nl80211_scan_mcgrp.id, GFP_KERNEL);
8669}
8670
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008671/*
8672 * This can happen on global regulatory changes or device specific settings
8673 * based on custom world regulatory domains.
8674 */
8675void nl80211_send_reg_change_event(struct regulatory_request *request)
8676{
8677 struct sk_buff *msg;
8678 void *hdr;
8679
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008680 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008681 if (!msg)
8682 return;
8683
8684 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8685 if (!hdr) {
8686 nlmsg_free(msg);
8687 return;
8688 }
8689
8690 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008691 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8692 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008693
David S. Miller9360ffd2012-03-29 04:41:26 -04008694 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8695 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8696 NL80211_REGDOM_TYPE_WORLD))
8697 goto nla_put_failure;
8698 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8699 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8700 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8701 goto nla_put_failure;
8702 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8703 request->intersect) {
8704 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8705 NL80211_REGDOM_TYPE_INTERSECTION))
8706 goto nla_put_failure;
8707 } else {
8708 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8709 NL80211_REGDOM_TYPE_COUNTRY) ||
8710 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8711 request->alpha2))
8712 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008713 }
8714
Johannes Bergf4173762012-12-03 18:23:37 +01008715 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008716 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8717 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008718
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008719 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008720
Johannes Bergbc43b282009-07-25 10:54:13 +02008721 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008722 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008723 GFP_ATOMIC);
8724 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008725
8726 return;
8727
8728nla_put_failure:
8729 genlmsg_cancel(msg, hdr);
8730 nlmsg_free(msg);
8731}
8732
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008733static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8734 struct net_device *netdev,
8735 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008736 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008737{
8738 struct sk_buff *msg;
8739 void *hdr;
8740
Johannes Berge6d6e342009-07-01 21:26:47 +02008741 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008742 if (!msg)
8743 return;
8744
8745 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8746 if (!hdr) {
8747 nlmsg_free(msg);
8748 return;
8749 }
8750
David S. Miller9360ffd2012-03-29 04:41:26 -04008751 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8752 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8753 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8754 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008755
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008756 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008757
Johannes Berg463d0182009-07-14 00:33:35 +02008758 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8759 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008760 return;
8761
8762 nla_put_failure:
8763 genlmsg_cancel(msg, hdr);
8764 nlmsg_free(msg);
8765}
8766
8767void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008768 struct net_device *netdev, const u8 *buf,
8769 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008770{
8771 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008772 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008773}
8774
8775void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8776 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008777 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008778{
Johannes Berge6d6e342009-07-01 21:26:47 +02008779 nl80211_send_mlme_event(rdev, netdev, buf, len,
8780 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008781}
8782
Jouni Malinen53b46b82009-03-27 20:53:56 +02008783void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008784 struct net_device *netdev, const u8 *buf,
8785 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008786{
8787 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008788 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008789}
8790
Jouni Malinen53b46b82009-03-27 20:53:56 +02008791void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8792 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008793 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008794{
8795 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008796 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008797}
8798
Jouni Malinencf4e5942010-12-16 00:52:40 +02008799void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8800 struct net_device *netdev, const u8 *buf,
8801 size_t len, gfp_t gfp)
8802{
8803 nl80211_send_mlme_event(rdev, netdev, buf, len,
8804 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8805}
8806
8807void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8808 struct net_device *netdev, const u8 *buf,
8809 size_t len, gfp_t gfp)
8810{
8811 nl80211_send_mlme_event(rdev, netdev, buf, len,
8812 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8813}
8814
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008815static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8816 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008817 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008818{
8819 struct sk_buff *msg;
8820 void *hdr;
8821
Johannes Berge6d6e342009-07-01 21:26:47 +02008822 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008823 if (!msg)
8824 return;
8825
8826 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8827 if (!hdr) {
8828 nlmsg_free(msg);
8829 return;
8830 }
8831
David S. Miller9360ffd2012-03-29 04:41:26 -04008832 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8833 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8834 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8835 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8836 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008837
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008838 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008839
Johannes Berg463d0182009-07-14 00:33:35 +02008840 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8841 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008842 return;
8843
8844 nla_put_failure:
8845 genlmsg_cancel(msg, hdr);
8846 nlmsg_free(msg);
8847}
8848
8849void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008850 struct net_device *netdev, const u8 *addr,
8851 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008852{
8853 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008854 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008855}
8856
8857void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008858 struct net_device *netdev, const u8 *addr,
8859 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008860{
Johannes Berge6d6e342009-07-01 21:26:47 +02008861 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8862 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008863}
8864
Samuel Ortizb23aa672009-07-01 21:26:54 +02008865void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8866 struct net_device *netdev, const u8 *bssid,
8867 const u8 *req_ie, size_t req_ie_len,
8868 const u8 *resp_ie, size_t resp_ie_len,
8869 u16 status, gfp_t gfp)
8870{
8871 struct sk_buff *msg;
8872 void *hdr;
8873
Thomas Graf58050fc2012-06-28 03:57:45 +00008874 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008875 if (!msg)
8876 return;
8877
8878 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8879 if (!hdr) {
8880 nlmsg_free(msg);
8881 return;
8882 }
8883
David S. Miller9360ffd2012-03-29 04:41:26 -04008884 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8885 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8886 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8887 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8888 (req_ie &&
8889 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8890 (resp_ie &&
8891 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8892 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008893
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008894 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008895
Johannes Berg463d0182009-07-14 00:33:35 +02008896 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8897 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008898 return;
8899
8900 nla_put_failure:
8901 genlmsg_cancel(msg, hdr);
8902 nlmsg_free(msg);
8903
8904}
8905
8906void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8907 struct net_device *netdev, const u8 *bssid,
8908 const u8 *req_ie, size_t req_ie_len,
8909 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8910{
8911 struct sk_buff *msg;
8912 void *hdr;
8913
Thomas Graf58050fc2012-06-28 03:57:45 +00008914 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008915 if (!msg)
8916 return;
8917
8918 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8919 if (!hdr) {
8920 nlmsg_free(msg);
8921 return;
8922 }
8923
David S. Miller9360ffd2012-03-29 04:41:26 -04008924 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8925 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8926 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8927 (req_ie &&
8928 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8929 (resp_ie &&
8930 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8931 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008932
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008933 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008934
Johannes Berg463d0182009-07-14 00:33:35 +02008935 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8936 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008937 return;
8938
8939 nla_put_failure:
8940 genlmsg_cancel(msg, hdr);
8941 nlmsg_free(msg);
8942
8943}
8944
8945void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8946 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008947 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008948{
8949 struct sk_buff *msg;
8950 void *hdr;
8951
Thomas Graf58050fc2012-06-28 03:57:45 +00008952 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008953 if (!msg)
8954 return;
8955
8956 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8957 if (!hdr) {
8958 nlmsg_free(msg);
8959 return;
8960 }
8961
David S. Miller9360ffd2012-03-29 04:41:26 -04008962 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8963 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8964 (from_ap && reason &&
8965 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8966 (from_ap &&
8967 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8968 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8969 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008970
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008971 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008972
Johannes Berg463d0182009-07-14 00:33:35 +02008973 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8974 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008975 return;
8976
8977 nla_put_failure:
8978 genlmsg_cancel(msg, hdr);
8979 nlmsg_free(msg);
8980
8981}
8982
Johannes Berg04a773a2009-04-19 21:24:32 +02008983void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8984 struct net_device *netdev, const u8 *bssid,
8985 gfp_t gfp)
8986{
8987 struct sk_buff *msg;
8988 void *hdr;
8989
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008990 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008991 if (!msg)
8992 return;
8993
8994 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8995 if (!hdr) {
8996 nlmsg_free(msg);
8997 return;
8998 }
8999
David S. Miller9360ffd2012-03-29 04:41:26 -04009000 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9001 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9002 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9003 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009004
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009005 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009006
Johannes Berg463d0182009-07-14 00:33:35 +02009007 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9008 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009009 return;
9010
9011 nla_put_failure:
9012 genlmsg_cancel(msg, hdr);
9013 nlmsg_free(msg);
9014}
9015
Javier Cardonac93b5e72011-04-07 15:08:34 -07009016void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
9017 struct net_device *netdev,
9018 const u8 *macaddr, const u8* ie, u8 ie_len,
9019 gfp_t gfp)
9020{
9021 struct sk_buff *msg;
9022 void *hdr;
9023
9024 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9025 if (!msg)
9026 return;
9027
9028 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9029 if (!hdr) {
9030 nlmsg_free(msg);
9031 return;
9032 }
9033
David S. Miller9360ffd2012-03-29 04:41:26 -04009034 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9035 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9036 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
9037 (ie_len && ie &&
9038 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9039 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009040
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009041 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009042
9043 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9044 nl80211_mlme_mcgrp.id, gfp);
9045 return;
9046
9047 nla_put_failure:
9048 genlmsg_cancel(msg, hdr);
9049 nlmsg_free(msg);
9050}
9051
Jouni Malinena3b8b052009-03-27 21:59:49 +02009052void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9053 struct net_device *netdev, const u8 *addr,
9054 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009055 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009056{
9057 struct sk_buff *msg;
9058 void *hdr;
9059
Johannes Berge6d6e342009-07-01 21:26:47 +02009060 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009061 if (!msg)
9062 return;
9063
9064 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9065 if (!hdr) {
9066 nlmsg_free(msg);
9067 return;
9068 }
9069
David S. Miller9360ffd2012-03-29 04:41:26 -04009070 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9071 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9072 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9073 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9074 (key_id != -1 &&
9075 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9076 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9077 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009078
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009079 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009080
Johannes Berg463d0182009-07-14 00:33:35 +02009081 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9082 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009083 return;
9084
9085 nla_put_failure:
9086 genlmsg_cancel(msg, hdr);
9087 nlmsg_free(msg);
9088}
9089
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009090void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9091 struct ieee80211_channel *channel_before,
9092 struct ieee80211_channel *channel_after)
9093{
9094 struct sk_buff *msg;
9095 void *hdr;
9096 struct nlattr *nl_freq;
9097
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009098 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009099 if (!msg)
9100 return;
9101
9102 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9103 if (!hdr) {
9104 nlmsg_free(msg);
9105 return;
9106 }
9107
9108 /*
9109 * Since we are applying the beacon hint to a wiphy we know its
9110 * wiphy_idx is valid
9111 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009112 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9113 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009114
9115 /* Before */
9116 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9117 if (!nl_freq)
9118 goto nla_put_failure;
9119 if (nl80211_msg_put_channel(msg, channel_before))
9120 goto nla_put_failure;
9121 nla_nest_end(msg, nl_freq);
9122
9123 /* After */
9124 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9125 if (!nl_freq)
9126 goto nla_put_failure;
9127 if (nl80211_msg_put_channel(msg, channel_after))
9128 goto nla_put_failure;
9129 nla_nest_end(msg, nl_freq);
9130
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009131 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009132
Johannes Berg463d0182009-07-14 00:33:35 +02009133 rcu_read_lock();
9134 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9135 GFP_ATOMIC);
9136 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009137
9138 return;
9139
9140nla_put_failure:
9141 genlmsg_cancel(msg, hdr);
9142 nlmsg_free(msg);
9143}
9144
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009145static void nl80211_send_remain_on_chan_event(
9146 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009147 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009148 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009149 unsigned int duration, gfp_t gfp)
9150{
9151 struct sk_buff *msg;
9152 void *hdr;
9153
9154 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9155 if (!msg)
9156 return;
9157
9158 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9159 if (!hdr) {
9160 nlmsg_free(msg);
9161 return;
9162 }
9163
David S. Miller9360ffd2012-03-29 04:41:26 -04009164 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009165 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9166 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009167 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009168 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009169 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9170 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009171 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9172 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009173
David S. Miller9360ffd2012-03-29 04:41:26 -04009174 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9175 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9176 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009177
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009178 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009179
9180 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9181 nl80211_mlme_mcgrp.id, gfp);
9182 return;
9183
9184 nla_put_failure:
9185 genlmsg_cancel(msg, hdr);
9186 nlmsg_free(msg);
9187}
9188
9189void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009190 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009191 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009192 unsigned int duration, gfp_t gfp)
9193{
9194 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009195 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009196 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009197}
9198
9199void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009200 struct cfg80211_registered_device *rdev,
9201 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009202 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009203{
9204 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009205 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009206}
9207
Johannes Berg98b62182009-12-23 13:15:44 +01009208void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9209 struct net_device *dev, const u8 *mac_addr,
9210 struct station_info *sinfo, gfp_t gfp)
9211{
9212 struct sk_buff *msg;
9213
Thomas Graf58050fc2012-06-28 03:57:45 +00009214 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009215 if (!msg)
9216 return;
9217
John W. Linville66266b32012-03-15 13:25:41 -04009218 if (nl80211_send_station(msg, 0, 0, 0,
9219 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009220 nlmsg_free(msg);
9221 return;
9222 }
9223
9224 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9225 nl80211_mlme_mcgrp.id, gfp);
9226}
9227
Jouni Malinenec15e682011-03-23 15:29:52 +02009228void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9229 struct net_device *dev, const u8 *mac_addr,
9230 gfp_t gfp)
9231{
9232 struct sk_buff *msg;
9233 void *hdr;
9234
Thomas Graf58050fc2012-06-28 03:57:45 +00009235 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009236 if (!msg)
9237 return;
9238
9239 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9240 if (!hdr) {
9241 nlmsg_free(msg);
9242 return;
9243 }
9244
David S. Miller9360ffd2012-03-29 04:41:26 -04009245 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9246 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9247 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009248
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009249 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009250
9251 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9252 nl80211_mlme_mcgrp.id, gfp);
9253 return;
9254
9255 nla_put_failure:
9256 genlmsg_cancel(msg, hdr);
9257 nlmsg_free(msg);
9258}
9259
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309260void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9261 struct net_device *dev, const u8 *mac_addr,
9262 enum nl80211_connect_failed_reason reason,
9263 gfp_t gfp)
9264{
9265 struct sk_buff *msg;
9266 void *hdr;
9267
9268 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9269 if (!msg)
9270 return;
9271
9272 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9273 if (!hdr) {
9274 nlmsg_free(msg);
9275 return;
9276 }
9277
9278 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9279 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9280 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9281 goto nla_put_failure;
9282
9283 genlmsg_end(msg, hdr);
9284
9285 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9286 nl80211_mlme_mcgrp.id, gfp);
9287 return;
9288
9289 nla_put_failure:
9290 genlmsg_cancel(msg, hdr);
9291 nlmsg_free(msg);
9292}
9293
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009294static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9295 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009296{
9297 struct wireless_dev *wdev = dev->ieee80211_ptr;
9298 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9299 struct sk_buff *msg;
9300 void *hdr;
9301 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009302 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009303
Eric W. Biederman15e47302012-09-07 20:12:54 +00009304 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009305 return false;
9306
9307 msg = nlmsg_new(100, gfp);
9308 if (!msg)
9309 return true;
9310
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009311 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009312 if (!hdr) {
9313 nlmsg_free(msg);
9314 return true;
9315 }
9316
David S. Miller9360ffd2012-03-29 04:41:26 -04009317 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9318 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9319 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9320 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009321
9322 err = genlmsg_end(msg, hdr);
9323 if (err < 0) {
9324 nlmsg_free(msg);
9325 return true;
9326 }
9327
Eric W. Biederman15e47302012-09-07 20:12:54 +00009328 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009329 return true;
9330
9331 nla_put_failure:
9332 genlmsg_cancel(msg, hdr);
9333 nlmsg_free(msg);
9334 return true;
9335}
9336
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009337bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9338{
9339 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9340 addr, gfp);
9341}
9342
9343bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9344 const u8 *addr, gfp_t gfp)
9345{
9346 return __nl80211_unexpected_frame(dev,
9347 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9348 addr, gfp);
9349}
9350
Johannes Berg2e161f72010-08-12 15:38:38 +02009351int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009352 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009353 int freq, int sig_dbm,
9354 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009355{
Johannes Berg71bbc992012-06-15 15:30:18 +02009356 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009357 struct sk_buff *msg;
9358 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009359
9360 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9361 if (!msg)
9362 return -ENOMEM;
9363
Johannes Berg2e161f72010-08-12 15:38:38 +02009364 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009365 if (!hdr) {
9366 nlmsg_free(msg);
9367 return -ENOMEM;
9368 }
9369
David S. Miller9360ffd2012-03-29 04:41:26 -04009370 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009371 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9372 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009373 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9374 (sig_dbm &&
9375 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9376 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9377 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009378
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009379 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009380
Eric W. Biederman15e47302012-09-07 20:12:54 +00009381 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009382
9383 nla_put_failure:
9384 genlmsg_cancel(msg, hdr);
9385 nlmsg_free(msg);
9386 return -ENOBUFS;
9387}
9388
Johannes Berg2e161f72010-08-12 15:38:38 +02009389void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009390 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009391 const u8 *buf, size_t len, bool ack,
9392 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009393{
Johannes Berg71bbc992012-06-15 15:30:18 +02009394 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009395 struct sk_buff *msg;
9396 void *hdr;
9397
9398 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9399 if (!msg)
9400 return;
9401
Johannes Berg2e161f72010-08-12 15:38:38 +02009402 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009403 if (!hdr) {
9404 nlmsg_free(msg);
9405 return;
9406 }
9407
David S. Miller9360ffd2012-03-29 04:41:26 -04009408 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009409 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9410 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009411 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9412 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9413 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9414 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009415
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009416 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009417
9418 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9419 return;
9420
9421 nla_put_failure:
9422 genlmsg_cancel(msg, hdr);
9423 nlmsg_free(msg);
9424}
9425
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009426void
9427nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9428 struct net_device *netdev,
9429 enum nl80211_cqm_rssi_threshold_event rssi_event,
9430 gfp_t gfp)
9431{
9432 struct sk_buff *msg;
9433 struct nlattr *pinfoattr;
9434 void *hdr;
9435
Thomas Graf58050fc2012-06-28 03:57:45 +00009436 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009437 if (!msg)
9438 return;
9439
9440 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9441 if (!hdr) {
9442 nlmsg_free(msg);
9443 return;
9444 }
9445
David S. Miller9360ffd2012-03-29 04:41:26 -04009446 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9447 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9448 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009449
9450 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9451 if (!pinfoattr)
9452 goto nla_put_failure;
9453
David S. Miller9360ffd2012-03-29 04:41:26 -04009454 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9455 rssi_event))
9456 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009457
9458 nla_nest_end(msg, pinfoattr);
9459
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009460 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009461
9462 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9463 nl80211_mlme_mcgrp.id, gfp);
9464 return;
9465
9466 nla_put_failure:
9467 genlmsg_cancel(msg, hdr);
9468 nlmsg_free(msg);
9469}
9470
Johannes Berge5497d72011-07-05 16:35:40 +02009471void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9472 struct net_device *netdev, const u8 *bssid,
9473 const u8 *replay_ctr, gfp_t gfp)
9474{
9475 struct sk_buff *msg;
9476 struct nlattr *rekey_attr;
9477 void *hdr;
9478
Thomas Graf58050fc2012-06-28 03:57:45 +00009479 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009480 if (!msg)
9481 return;
9482
9483 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9484 if (!hdr) {
9485 nlmsg_free(msg);
9486 return;
9487 }
9488
David S. Miller9360ffd2012-03-29 04:41:26 -04009489 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9490 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9491 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9492 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009493
9494 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9495 if (!rekey_attr)
9496 goto nla_put_failure;
9497
David S. Miller9360ffd2012-03-29 04:41:26 -04009498 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9499 NL80211_REPLAY_CTR_LEN, replay_ctr))
9500 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009501
9502 nla_nest_end(msg, rekey_attr);
9503
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009504 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009505
9506 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9507 nl80211_mlme_mcgrp.id, gfp);
9508 return;
9509
9510 nla_put_failure:
9511 genlmsg_cancel(msg, hdr);
9512 nlmsg_free(msg);
9513}
9514
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009515void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9516 struct net_device *netdev, int index,
9517 const u8 *bssid, bool preauth, gfp_t gfp)
9518{
9519 struct sk_buff *msg;
9520 struct nlattr *attr;
9521 void *hdr;
9522
Thomas Graf58050fc2012-06-28 03:57:45 +00009523 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009524 if (!msg)
9525 return;
9526
9527 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9528 if (!hdr) {
9529 nlmsg_free(msg);
9530 return;
9531 }
9532
David S. Miller9360ffd2012-03-29 04:41:26 -04009533 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9534 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9535 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009536
9537 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9538 if (!attr)
9539 goto nla_put_failure;
9540
David S. Miller9360ffd2012-03-29 04:41:26 -04009541 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9542 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9543 (preauth &&
9544 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9545 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009546
9547 nla_nest_end(msg, attr);
9548
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009549 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009550
9551 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9552 nl80211_mlme_mcgrp.id, gfp);
9553 return;
9554
9555 nla_put_failure:
9556 genlmsg_cancel(msg, hdr);
9557 nlmsg_free(msg);
9558}
9559
Thomas Pedersen53145262012-04-06 13:35:47 -07009560void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009561 struct net_device *netdev,
9562 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009563{
9564 struct sk_buff *msg;
9565 void *hdr;
9566
Thomas Graf58050fc2012-06-28 03:57:45 +00009567 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009568 if (!msg)
9569 return;
9570
9571 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9572 if (!hdr) {
9573 nlmsg_free(msg);
9574 return;
9575 }
9576
Johannes Berg683b6d32012-11-08 21:25:48 +01009577 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9578 goto nla_put_failure;
9579
9580 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009581 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009582
9583 genlmsg_end(msg, hdr);
9584
9585 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9586 nl80211_mlme_mcgrp.id, gfp);
9587 return;
9588
9589 nla_put_failure:
9590 genlmsg_cancel(msg, hdr);
9591 nlmsg_free(msg);
9592}
9593
Johannes Bergc063dbf2010-11-24 08:10:05 +01009594void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009595nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9596 struct net_device *netdev, const u8 *peer,
9597 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9598{
9599 struct sk_buff *msg;
9600 struct nlattr *pinfoattr;
9601 void *hdr;
9602
9603 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9604 if (!msg)
9605 return;
9606
9607 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9608 if (!hdr) {
9609 nlmsg_free(msg);
9610 return;
9611 }
9612
9613 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9614 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9615 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9616 goto nla_put_failure;
9617
9618 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9619 if (!pinfoattr)
9620 goto nla_put_failure;
9621
9622 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9623 goto nla_put_failure;
9624
9625 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9626 goto nla_put_failure;
9627
9628 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9629 goto nla_put_failure;
9630
9631 nla_nest_end(msg, pinfoattr);
9632
9633 genlmsg_end(msg, hdr);
9634
9635 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9636 nl80211_mlme_mcgrp.id, gfp);
9637 return;
9638
9639 nla_put_failure:
9640 genlmsg_cancel(msg, hdr);
9641 nlmsg_free(msg);
9642}
9643
9644void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009645nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9646 struct cfg80211_chan_def *chandef,
9647 enum nl80211_radar_event event,
9648 struct net_device *netdev, gfp_t gfp)
9649{
9650 struct sk_buff *msg;
9651 void *hdr;
9652
9653 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9654 if (!msg)
9655 return;
9656
9657 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9658 if (!hdr) {
9659 nlmsg_free(msg);
9660 return;
9661 }
9662
9663 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9664 goto nla_put_failure;
9665
9666 /* NOP and radar events don't need a netdev parameter */
9667 if (netdev) {
9668 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9669
9670 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9671 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9672 goto nla_put_failure;
9673 }
9674
9675 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9676 goto nla_put_failure;
9677
9678 if (nl80211_send_chandef(msg, chandef))
9679 goto nla_put_failure;
9680
9681 if (genlmsg_end(msg, hdr) < 0) {
9682 nlmsg_free(msg);
9683 return;
9684 }
9685
9686 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9687 nl80211_mlme_mcgrp.id, gfp);
9688 return;
9689
9690 nla_put_failure:
9691 genlmsg_cancel(msg, hdr);
9692 nlmsg_free(msg);
9693}
9694
9695void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009696nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9697 struct net_device *netdev, const u8 *peer,
9698 u32 num_packets, gfp_t gfp)
9699{
9700 struct sk_buff *msg;
9701 struct nlattr *pinfoattr;
9702 void *hdr;
9703
Thomas Graf58050fc2012-06-28 03:57:45 +00009704 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009705 if (!msg)
9706 return;
9707
9708 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9709 if (!hdr) {
9710 nlmsg_free(msg);
9711 return;
9712 }
9713
David S. Miller9360ffd2012-03-29 04:41:26 -04009714 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9715 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9716 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9717 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009718
9719 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9720 if (!pinfoattr)
9721 goto nla_put_failure;
9722
David S. Miller9360ffd2012-03-29 04:41:26 -04009723 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9724 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009725
9726 nla_nest_end(msg, pinfoattr);
9727
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009728 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009729
9730 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9731 nl80211_mlme_mcgrp.id, gfp);
9732 return;
9733
9734 nla_put_failure:
9735 genlmsg_cancel(msg, hdr);
9736 nlmsg_free(msg);
9737}
9738
Johannes Berg7f6cf312011-11-04 11:18:15 +01009739void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9740 u64 cookie, bool acked, gfp_t gfp)
9741{
9742 struct wireless_dev *wdev = dev->ieee80211_ptr;
9743 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9744 struct sk_buff *msg;
9745 void *hdr;
9746 int err;
9747
Beni Lev4ee3e062012-08-27 12:49:39 +03009748 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9749
Thomas Graf58050fc2012-06-28 03:57:45 +00009750 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009751
Johannes Berg7f6cf312011-11-04 11:18:15 +01009752 if (!msg)
9753 return;
9754
9755 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9756 if (!hdr) {
9757 nlmsg_free(msg);
9758 return;
9759 }
9760
David S. Miller9360ffd2012-03-29 04:41:26 -04009761 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9762 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9763 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9764 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9765 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9766 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009767
9768 err = genlmsg_end(msg, hdr);
9769 if (err < 0) {
9770 nlmsg_free(msg);
9771 return;
9772 }
9773
9774 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9775 nl80211_mlme_mcgrp.id, gfp);
9776 return;
9777
9778 nla_put_failure:
9779 genlmsg_cancel(msg, hdr);
9780 nlmsg_free(msg);
9781}
9782EXPORT_SYMBOL(cfg80211_probe_status);
9783
Johannes Berg5e760232011-11-04 11:18:17 +01009784void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9785 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009786 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009787{
9788 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9789 struct sk_buff *msg;
9790 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009791 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009792
Beni Lev4ee3e062012-08-27 12:49:39 +03009793 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9794
Ben Greear37c73b52012-10-26 14:49:25 -07009795 spin_lock_bh(&rdev->beacon_registrations_lock);
9796 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9797 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9798 if (!msg) {
9799 spin_unlock_bh(&rdev->beacon_registrations_lock);
9800 return;
9801 }
Johannes Berg5e760232011-11-04 11:18:17 +01009802
Ben Greear37c73b52012-10-26 14:49:25 -07009803 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9804 if (!hdr)
9805 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009806
Ben Greear37c73b52012-10-26 14:49:25 -07009807 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9808 (freq &&
9809 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9810 (sig_dbm &&
9811 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9812 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9813 goto nla_put_failure;
9814
9815 genlmsg_end(msg, hdr);
9816
9817 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009818 }
Ben Greear37c73b52012-10-26 14:49:25 -07009819 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009820 return;
9821
9822 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009823 spin_unlock_bh(&rdev->beacon_registrations_lock);
9824 if (hdr)
9825 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009826 nlmsg_free(msg);
9827}
9828EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9829
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009830#ifdef CONFIG_PM
9831void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9832 struct cfg80211_wowlan_wakeup *wakeup,
9833 gfp_t gfp)
9834{
9835 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9836 struct sk_buff *msg;
9837 void *hdr;
9838 int err, size = 200;
9839
9840 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9841
9842 if (wakeup)
9843 size += wakeup->packet_present_len;
9844
9845 msg = nlmsg_new(size, gfp);
9846 if (!msg)
9847 return;
9848
9849 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9850 if (!hdr)
9851 goto free_msg;
9852
9853 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9854 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9855 goto free_msg;
9856
9857 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9858 wdev->netdev->ifindex))
9859 goto free_msg;
9860
9861 if (wakeup) {
9862 struct nlattr *reasons;
9863
9864 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9865
9866 if (wakeup->disconnect &&
9867 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9868 goto free_msg;
9869 if (wakeup->magic_pkt &&
9870 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9871 goto free_msg;
9872 if (wakeup->gtk_rekey_failure &&
9873 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9874 goto free_msg;
9875 if (wakeup->eap_identity_req &&
9876 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9877 goto free_msg;
9878 if (wakeup->four_way_handshake &&
9879 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9880 goto free_msg;
9881 if (wakeup->rfkill_release &&
9882 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9883 goto free_msg;
9884
9885 if (wakeup->pattern_idx >= 0 &&
9886 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9887 wakeup->pattern_idx))
9888 goto free_msg;
9889
Johannes Berg2a0e0472013-01-23 22:57:40 +01009890 if (wakeup->tcp_match)
9891 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
9892
9893 if (wakeup->tcp_connlost)
9894 nla_put_flag(msg,
9895 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
9896
9897 if (wakeup->tcp_nomoretokens)
9898 nla_put_flag(msg,
9899 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
9900
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009901 if (wakeup->packet) {
9902 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9903 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9904
9905 if (!wakeup->packet_80211) {
9906 pkt_attr =
9907 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9908 len_attr =
9909 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9910 }
9911
9912 if (wakeup->packet_len &&
9913 nla_put_u32(msg, len_attr, wakeup->packet_len))
9914 goto free_msg;
9915
9916 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9917 wakeup->packet))
9918 goto free_msg;
9919 }
9920
9921 nla_nest_end(msg, reasons);
9922 }
9923
9924 err = genlmsg_end(msg, hdr);
9925 if (err < 0)
9926 goto free_msg;
9927
9928 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9929 nl80211_mlme_mcgrp.id, gfp);
9930 return;
9931
9932 free_msg:
9933 nlmsg_free(msg);
9934}
9935EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9936#endif
9937
Jouni Malinen3475b092012-11-16 22:49:57 +02009938void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9939 enum nl80211_tdls_operation oper,
9940 u16 reason_code, gfp_t gfp)
9941{
9942 struct wireless_dev *wdev = dev->ieee80211_ptr;
9943 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9944 struct sk_buff *msg;
9945 void *hdr;
9946 int err;
9947
9948 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9949 reason_code);
9950
9951 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9952 if (!msg)
9953 return;
9954
9955 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9956 if (!hdr) {
9957 nlmsg_free(msg);
9958 return;
9959 }
9960
9961 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9962 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9963 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9964 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9965 (reason_code > 0 &&
9966 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9967 goto nla_put_failure;
9968
9969 err = genlmsg_end(msg, hdr);
9970 if (err < 0) {
9971 nlmsg_free(msg);
9972 return;
9973 }
9974
9975 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9976 nl80211_mlme_mcgrp.id, gfp);
9977 return;
9978
9979 nla_put_failure:
9980 genlmsg_cancel(msg, hdr);
9981 nlmsg_free(msg);
9982}
9983EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9984
Jouni Malinen026331c2010-02-15 12:53:10 +02009985static int nl80211_netlink_notify(struct notifier_block * nb,
9986 unsigned long state,
9987 void *_notify)
9988{
9989 struct netlink_notify *notify = _notify;
9990 struct cfg80211_registered_device *rdev;
9991 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009992 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009993
9994 if (state != NETLINK_URELEASE)
9995 return NOTIFY_DONE;
9996
9997 rcu_read_lock();
9998
Johannes Berg5e760232011-11-04 11:18:17 +01009999 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010000 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010001 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010002
10003 spin_lock_bh(&rdev->beacon_registrations_lock);
10004 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10005 list) {
10006 if (reg->nlportid == notify->portid) {
10007 list_del(&reg->list);
10008 kfree(reg);
10009 break;
10010 }
10011 }
10012 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010013 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010014
10015 rcu_read_unlock();
10016
10017 return NOTIFY_DONE;
10018}
10019
10020static struct notifier_block nl80211_netlink_notifier = {
10021 .notifier_call = nl80211_netlink_notify,
10022};
10023
Johannes Berg55682962007-09-20 13:09:35 -040010024/* initialisation/exit functions */
10025
10026int nl80211_init(void)
10027{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010028 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010029
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010030 err = genl_register_family_with_ops(&nl80211_fam,
10031 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010032 if (err)
10033 return err;
10034
Johannes Berg55682962007-09-20 13:09:35 -040010035 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10036 if (err)
10037 goto err_out;
10038
Johannes Berg2a519312009-02-10 21:25:55 +010010039 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10040 if (err)
10041 goto err_out;
10042
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010043 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10044 if (err)
10045 goto err_out;
10046
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010047 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10048 if (err)
10049 goto err_out;
10050
Johannes Bergaff89a92009-07-01 21:26:51 +020010051#ifdef CONFIG_NL80211_TESTMODE
10052 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10053 if (err)
10054 goto err_out;
10055#endif
10056
Jouni Malinen026331c2010-02-15 12:53:10 +020010057 err = netlink_register_notifier(&nl80211_netlink_notifier);
10058 if (err)
10059 goto err_out;
10060
Johannes Berg55682962007-09-20 13:09:35 -040010061 return 0;
10062 err_out:
10063 genl_unregister_family(&nl80211_fam);
10064 return err;
10065}
10066
10067void nl80211_exit(void)
10068{
Jouni Malinen026331c2010-02-15 12:53:10 +020010069 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010070 genl_unregister_family(&nl80211_fam);
10071}