blob: f187a920ec71c92e2f48cab164d9d997f437093c [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 Malinen36aedc902008-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 Berg3713b4e2013-02-14 16:19:38 +0100373 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Berg55682962007-09-20 13:09:35 -0400374};
375
Johannes Berge31b8212010-10-05 19:39:30 +0200376/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000377static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200378 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200379 [NL80211_KEY_IDX] = { .type = NLA_U8 },
380 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200381 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200382 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
383 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200384 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100385 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
386};
387
388/* policy for the key default flags */
389static const struct nla_policy
390nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
391 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
392 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200393};
394
Johannes Bergff1b6e62011-05-04 15:37:28 +0200395/* policy for WoWLAN attributes */
396static const struct nla_policy
397nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
398 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
400 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
401 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200402 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
403 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
404 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
405 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100406 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
407};
408
409static const struct nla_policy
410nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
411 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
412 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
413 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
414 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
415 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
416 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
417 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
418 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
419 },
420 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
421 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
422 },
423 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
424 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
425 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200426};
427
Johannes Berge5497d72011-07-05 16:35:40 +0200428/* policy for GTK rekey offload attributes */
429static const struct nla_policy
430nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
431 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
432 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
433 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
434};
435
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300436static const struct nla_policy
437nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200438 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300439 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700440 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300441};
442
Holger Schuriga0438972009-11-11 11:30:02 +0100443/* ifidx get helper */
444static int nl80211_get_ifidx(struct netlink_callback *cb)
445{
446 int res;
447
448 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
449 nl80211_fam.attrbuf, nl80211_fam.maxattr,
450 nl80211_policy);
451 if (res)
452 return res;
453
454 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
455 return -EINVAL;
456
457 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
458 if (!res)
459 return -EINVAL;
460 return res;
461}
462
Johannes Berg67748892010-10-04 21:14:06 +0200463static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
464 struct netlink_callback *cb,
465 struct cfg80211_registered_device **rdev,
466 struct net_device **dev)
467{
468 int ifidx = cb->args[0];
469 int err;
470
471 if (!ifidx)
472 ifidx = nl80211_get_ifidx(cb);
473 if (ifidx < 0)
474 return ifidx;
475
476 cb->args[0] = ifidx;
477
478 rtnl_lock();
479
480 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
481 if (!*dev) {
482 err = -ENODEV;
483 goto out_rtnl;
484 }
485
486 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100487 if (IS_ERR(*rdev)) {
488 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200489 goto out_rtnl;
490 }
491
492 return 0;
493 out_rtnl:
494 rtnl_unlock();
495 return err;
496}
497
498static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
499{
500 cfg80211_unlock_rdev(rdev);
501 rtnl_unlock();
502}
503
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100504/* IE validation */
505static bool is_valid_ie_attr(const struct nlattr *attr)
506{
507 const u8 *pos;
508 int len;
509
510 if (!attr)
511 return true;
512
513 pos = nla_data(attr);
514 len = nla_len(attr);
515
516 while (len) {
517 u8 elemlen;
518
519 if (len < 2)
520 return false;
521 len -= 2;
522
523 elemlen = pos[1];
524 if (elemlen > len)
525 return false;
526
527 len -= elemlen;
528 pos += 2 + elemlen;
529 }
530
531 return true;
532}
533
Johannes Berg55682962007-09-20 13:09:35 -0400534/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000535static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400536 int flags, u8 cmd)
537{
538 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000539 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400540}
541
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400542static int nl80211_msg_put_channel(struct sk_buff *msg,
543 struct ieee80211_channel *chan)
544{
David S. Miller9360ffd2012-03-29 04:41:26 -0400545 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
546 chan->center_freq))
547 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400548
David S. Miller9360ffd2012-03-29 04:41:26 -0400549 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
550 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
551 goto nla_put_failure;
552 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
553 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
554 goto nla_put_failure;
555 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
556 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
557 goto nla_put_failure;
Johannes Berg1c33a052013-02-18 23:44:38 +0100558 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
559 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
560 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400561
David S. Miller9360ffd2012-03-29 04:41:26 -0400562 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
563 DBM_TO_MBM(chan->max_power)))
564 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400565
566 return 0;
567
568 nla_put_failure:
569 return -ENOBUFS;
570}
571
Johannes Berg55682962007-09-20 13:09:35 -0400572/* netlink command implementations */
573
Johannes Bergb9454e82009-07-08 13:29:08 +0200574struct key_parse {
575 struct key_params p;
576 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200577 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200578 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100579 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200580};
581
582static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
583{
584 struct nlattr *tb[NL80211_KEY_MAX + 1];
585 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
586 nl80211_key_policy);
587 if (err)
588 return err;
589
590 k->def = !!tb[NL80211_KEY_DEFAULT];
591 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
592
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100593 if (k->def) {
594 k->def_uni = true;
595 k->def_multi = true;
596 }
597 if (k->defmgmt)
598 k->def_multi = true;
599
Johannes Bergb9454e82009-07-08 13:29:08 +0200600 if (tb[NL80211_KEY_IDX])
601 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
602
603 if (tb[NL80211_KEY_DATA]) {
604 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
605 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
606 }
607
608 if (tb[NL80211_KEY_SEQ]) {
609 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
610 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
611 }
612
613 if (tb[NL80211_KEY_CIPHER])
614 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
615
Johannes Berge31b8212010-10-05 19:39:30 +0200616 if (tb[NL80211_KEY_TYPE]) {
617 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
618 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
619 return -EINVAL;
620 }
621
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100622 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
623 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100624 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
625 tb[NL80211_KEY_DEFAULT_TYPES],
626 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100627 if (err)
628 return err;
629
630 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
631 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
632 }
633
Johannes Bergb9454e82009-07-08 13:29:08 +0200634 return 0;
635}
636
637static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
638{
639 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
640 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
641 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
642 }
643
644 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
645 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
646 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
647 }
648
649 if (info->attrs[NL80211_ATTR_KEY_IDX])
650 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
651
652 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
653 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
654
655 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
656 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
657
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100658 if (k->def) {
659 k->def_uni = true;
660 k->def_multi = true;
661 }
662 if (k->defmgmt)
663 k->def_multi = true;
664
Johannes Berge31b8212010-10-05 19:39:30 +0200665 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
666 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
667 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
668 return -EINVAL;
669 }
670
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100671 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
672 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
673 int err = nla_parse_nested(
674 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
675 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
676 nl80211_key_default_policy);
677 if (err)
678 return err;
679
680 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
681 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
682 }
683
Johannes Bergb9454e82009-07-08 13:29:08 +0200684 return 0;
685}
686
687static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
688{
689 int err;
690
691 memset(k, 0, sizeof(*k));
692 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200693 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200694
695 if (info->attrs[NL80211_ATTR_KEY])
696 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
697 else
698 err = nl80211_parse_key_old(info, k);
699
700 if (err)
701 return err;
702
703 if (k->def && k->defmgmt)
704 return -EINVAL;
705
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100706 if (k->defmgmt) {
707 if (k->def_uni || !k->def_multi)
708 return -EINVAL;
709 }
710
Johannes Bergb9454e82009-07-08 13:29:08 +0200711 if (k->idx != -1) {
712 if (k->defmgmt) {
713 if (k->idx < 4 || k->idx > 5)
714 return -EINVAL;
715 } else if (k->def) {
716 if (k->idx < 0 || k->idx > 3)
717 return -EINVAL;
718 } else {
719 if (k->idx < 0 || k->idx > 5)
720 return -EINVAL;
721 }
722 }
723
724 return 0;
725}
726
Johannes Bergfffd0932009-07-08 14:22:54 +0200727static struct cfg80211_cached_keys *
728nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530729 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200730{
731 struct key_parse parse;
732 struct nlattr *key;
733 struct cfg80211_cached_keys *result;
734 int rem, err, def = 0;
735
736 result = kzalloc(sizeof(*result), GFP_KERNEL);
737 if (!result)
738 return ERR_PTR(-ENOMEM);
739
740 result->def = -1;
741 result->defmgmt = -1;
742
743 nla_for_each_nested(key, keys, rem) {
744 memset(&parse, 0, sizeof(parse));
745 parse.idx = -1;
746
747 err = nl80211_parse_key_new(key, &parse);
748 if (err)
749 goto error;
750 err = -EINVAL;
751 if (!parse.p.key)
752 goto error;
753 if (parse.idx < 0 || parse.idx > 4)
754 goto error;
755 if (parse.def) {
756 if (def)
757 goto error;
758 def = 1;
759 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100760 if (!parse.def_uni || !parse.def_multi)
761 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200762 } else if (parse.defmgmt)
763 goto error;
764 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200765 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200766 if (err)
767 goto error;
768 result->params[parse.idx].cipher = parse.p.cipher;
769 result->params[parse.idx].key_len = parse.p.key_len;
770 result->params[parse.idx].key = result->data[parse.idx];
771 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530772
773 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
774 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
775 if (no_ht)
776 *no_ht = true;
777 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200778 }
779
780 return result;
781 error:
782 kfree(result);
783 return ERR_PTR(err);
784}
785
786static int nl80211_key_allowed(struct wireless_dev *wdev)
787{
788 ASSERT_WDEV_LOCK(wdev);
789
Johannes Bergfffd0932009-07-08 14:22:54 +0200790 switch (wdev->iftype) {
791 case NL80211_IFTYPE_AP:
792 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200793 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700794 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200795 break;
796 case NL80211_IFTYPE_ADHOC:
797 if (!wdev->current_bss)
798 return -ENOLINK;
799 break;
800 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200801 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200802 if (wdev->sme_state != CFG80211_SME_CONNECTED)
803 return -ENOLINK;
804 break;
805 default:
806 return -EINVAL;
807 }
808
809 return 0;
810}
811
Johannes Berg7527a782011-05-13 10:58:57 +0200812static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
813{
814 struct nlattr *nl_modes = nla_nest_start(msg, attr);
815 int i;
816
817 if (!nl_modes)
818 goto nla_put_failure;
819
820 i = 0;
821 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400822 if ((ifmodes & 1) && nla_put_flag(msg, i))
823 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200824 ifmodes >>= 1;
825 i++;
826 }
827
828 nla_nest_end(msg, nl_modes);
829 return 0;
830
831nla_put_failure:
832 return -ENOBUFS;
833}
834
835static int nl80211_put_iface_combinations(struct wiphy *wiphy,
836 struct sk_buff *msg)
837{
838 struct nlattr *nl_combis;
839 int i, j;
840
841 nl_combis = nla_nest_start(msg,
842 NL80211_ATTR_INTERFACE_COMBINATIONS);
843 if (!nl_combis)
844 goto nla_put_failure;
845
846 for (i = 0; i < wiphy->n_iface_combinations; i++) {
847 const struct ieee80211_iface_combination *c;
848 struct nlattr *nl_combi, *nl_limits;
849
850 c = &wiphy->iface_combinations[i];
851
852 nl_combi = nla_nest_start(msg, i + 1);
853 if (!nl_combi)
854 goto nla_put_failure;
855
856 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
857 if (!nl_limits)
858 goto nla_put_failure;
859
860 for (j = 0; j < c->n_limits; j++) {
861 struct nlattr *nl_limit;
862
863 nl_limit = nla_nest_start(msg, j + 1);
864 if (!nl_limit)
865 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400866 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
867 c->limits[j].max))
868 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200869 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
870 c->limits[j].types))
871 goto nla_put_failure;
872 nla_nest_end(msg, nl_limit);
873 }
874
875 nla_nest_end(msg, nl_limits);
876
David S. Miller9360ffd2012-03-29 04:41:26 -0400877 if (c->beacon_int_infra_match &&
878 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
879 goto nla_put_failure;
880 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
881 c->num_different_channels) ||
882 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
883 c->max_interfaces))
884 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200885
886 nla_nest_end(msg, nl_combi);
887 }
888
889 nla_nest_end(msg, nl_combis);
890
891 return 0;
892nla_put_failure:
893 return -ENOBUFS;
894}
895
Johannes Berg3713b4e2013-02-14 16:19:38 +0100896#ifdef CONFIG_PM
897static int nl80211_send_wowlan(struct sk_buff *msg,
898 struct cfg80211_registered_device *dev)
899{
900 struct nlattr *nl_wowlan;
901
902 if (!dev->wiphy.wowlan.flags && !dev->wiphy.wowlan.n_patterns)
903 return 0;
904
905 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
906 if (!nl_wowlan)
907 return -ENOBUFS;
908
909 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
910 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
911 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
912 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
913 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
914 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
915 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
916 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
917 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
918 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
919 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
920 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
921 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
922 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
923 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
924 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
925 return -ENOBUFS;
926
927 if (dev->wiphy.wowlan.n_patterns) {
928 struct nl80211_wowlan_pattern_support pat = {
929 .max_patterns = dev->wiphy.wowlan.n_patterns,
930 .min_pattern_len = dev->wiphy.wowlan.pattern_min_len,
931 .max_pattern_len = dev->wiphy.wowlan.pattern_max_len,
932 .max_pkt_offset = dev->wiphy.wowlan.max_pkt_offset,
933 };
934
935 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
936 sizeof(pat), &pat))
937 return -ENOBUFS;
938 }
939
940 nla_nest_end(msg, nl_wowlan);
941
942 return 0;
943}
944#endif
945
946static int nl80211_send_band_rateinfo(struct sk_buff *msg,
947 struct ieee80211_supported_band *sband)
948{
949 struct nlattr *nl_rates, *nl_rate;
950 struct ieee80211_rate *rate;
951 int i;
952
953 /* add HT info */
954 if (sband->ht_cap.ht_supported &&
955 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
956 sizeof(sband->ht_cap.mcs),
957 &sband->ht_cap.mcs) ||
958 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
959 sband->ht_cap.cap) ||
960 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
961 sband->ht_cap.ampdu_factor) ||
962 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
963 sband->ht_cap.ampdu_density)))
964 return -ENOBUFS;
965
966 /* add VHT info */
967 if (sband->vht_cap.vht_supported &&
968 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
969 sizeof(sband->vht_cap.vht_mcs),
970 &sband->vht_cap.vht_mcs) ||
971 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
972 sband->vht_cap.cap)))
973 return -ENOBUFS;
974
975 /* add bitrates */
976 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
977 if (!nl_rates)
978 return -ENOBUFS;
979
980 for (i = 0; i < sband->n_bitrates; i++) {
981 nl_rate = nla_nest_start(msg, i);
982 if (!nl_rate)
983 return -ENOBUFS;
984
985 rate = &sband->bitrates[i];
986 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
987 rate->bitrate))
988 return -ENOBUFS;
989 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
990 nla_put_flag(msg,
991 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
992 return -ENOBUFS;
993
994 nla_nest_end(msg, nl_rate);
995 }
996
997 nla_nest_end(msg, nl_rates);
998
999 return 0;
1000}
1001
1002static int
1003nl80211_send_mgmt_stypes(struct sk_buff *msg,
1004 const struct ieee80211_txrx_stypes *mgmt_stypes)
1005{
1006 u16 stypes;
1007 struct nlattr *nl_ftypes, *nl_ifs;
1008 enum nl80211_iftype ift;
1009 int i;
1010
1011 if (!mgmt_stypes)
1012 return 0;
1013
1014 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1015 if (!nl_ifs)
1016 return -ENOBUFS;
1017
1018 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1019 nl_ftypes = nla_nest_start(msg, ift);
1020 if (!nl_ftypes)
1021 return -ENOBUFS;
1022 i = 0;
1023 stypes = mgmt_stypes[ift].tx;
1024 while (stypes) {
1025 if ((stypes & 1) &&
1026 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1027 (i << 4) | IEEE80211_FTYPE_MGMT))
1028 return -ENOBUFS;
1029 stypes >>= 1;
1030 i++;
1031 }
1032 nla_nest_end(msg, nl_ftypes);
1033 }
1034
1035 nla_nest_end(msg, nl_ifs);
1036
1037 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1038 if (!nl_ifs)
1039 return -ENOBUFS;
1040
1041 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1042 nl_ftypes = nla_nest_start(msg, ift);
1043 if (!nl_ftypes)
1044 return -ENOBUFS;
1045 i = 0;
1046 stypes = mgmt_stypes[ift].rx;
1047 while (stypes) {
1048 if ((stypes & 1) &&
1049 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1050 (i << 4) | IEEE80211_FTYPE_MGMT))
1051 return -ENOBUFS;
1052 stypes >>= 1;
1053 i++;
1054 }
1055 nla_nest_end(msg, nl_ftypes);
1056 }
1057 nla_nest_end(msg, nl_ifs);
1058
1059 return 0;
1060}
1061
1062static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1063 struct sk_buff *msg, u32 portid, u32 seq,
1064 int flags, bool split, long *split_start,
1065 long *band_start, long *chan_start)
Johannes Berg55682962007-09-20 13:09:35 -04001066{
1067 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001068 struct nlattr *nl_bands, *nl_band;
1069 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001070 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001071 enum ieee80211_band band;
1072 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001073 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001074 const struct ieee80211_txrx_stypes *mgmt_stypes =
1075 dev->wiphy.mgmt_stypes;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001076 long start = 0, start_chan = 0, start_band = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001077
Eric W. Biederman15e47302012-09-07 20:12:54 +00001078 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001079 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001080 return -ENOBUFS;
1081
1082 /* allow always using the variables */
1083 if (!split) {
1084 split_start = &start;
1085 band_start = &start_band;
1086 chan_start = &start_chan;
1087 }
Johannes Berg55682962007-09-20 13:09:35 -04001088
David S. Miller9360ffd2012-03-29 04:41:26 -04001089 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001090 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1091 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001092 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001093 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001094 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001095
Johannes Berg3713b4e2013-02-14 16:19:38 +01001096 switch (*split_start) {
1097 case 0:
1098 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1099 dev->wiphy.retry_short) ||
1100 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1101 dev->wiphy.retry_long) ||
1102 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1103 dev->wiphy.frag_threshold) ||
1104 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1105 dev->wiphy.rts_threshold) ||
1106 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1107 dev->wiphy.coverage_class) ||
1108 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1109 dev->wiphy.max_scan_ssids) ||
1110 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1111 dev->wiphy.max_sched_scan_ssids) ||
1112 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1113 dev->wiphy.max_scan_ie_len) ||
1114 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1115 dev->wiphy.max_sched_scan_ie_len) ||
1116 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1117 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001118 goto nla_put_failure;
1119
Johannes Berg3713b4e2013-02-14 16:19:38 +01001120 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1121 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1122 goto nla_put_failure;
1123 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1124 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1125 goto nla_put_failure;
1126 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1127 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1128 goto nla_put_failure;
1129 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1130 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1131 goto nla_put_failure;
1132 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1133 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1134 goto nla_put_failure;
1135 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1136 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001137 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001138
Johannes Berg3713b4e2013-02-14 16:19:38 +01001139 (*split_start)++;
1140 if (split)
1141 break;
1142 case 1:
1143 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1144 sizeof(u32) * dev->wiphy.n_cipher_suites,
1145 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001146 goto nla_put_failure;
1147
Johannes Berg3713b4e2013-02-14 16:19:38 +01001148 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1149 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001150 goto nla_put_failure;
1151
Johannes Berg3713b4e2013-02-14 16:19:38 +01001152 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1153 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1154 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001155
Johannes Berg3713b4e2013-02-14 16:19:38 +01001156 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1157 dev->wiphy.available_antennas_tx) ||
1158 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1159 dev->wiphy.available_antennas_rx))
1160 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001161
Johannes Berg3713b4e2013-02-14 16:19:38 +01001162 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1163 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1164 dev->wiphy.probe_resp_offload))
1165 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001166
Johannes Berg3713b4e2013-02-14 16:19:38 +01001167 if ((dev->wiphy.available_antennas_tx ||
1168 dev->wiphy.available_antennas_rx) &&
1169 dev->ops->get_antenna) {
1170 u32 tx_ant = 0, rx_ant = 0;
1171 int res;
1172 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1173 if (!res) {
1174 if (nla_put_u32(msg,
1175 NL80211_ATTR_WIPHY_ANTENNA_TX,
1176 tx_ant) ||
1177 nla_put_u32(msg,
1178 NL80211_ATTR_WIPHY_ANTENNA_RX,
1179 rx_ant))
1180 goto nla_put_failure;
1181 }
Johannes Bergee688b002008-01-24 19:38:39 +01001182 }
1183
Johannes Berg3713b4e2013-02-14 16:19:38 +01001184 (*split_start)++;
1185 if (split)
1186 break;
1187 case 2:
1188 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1189 dev->wiphy.interface_modes))
1190 goto nla_put_failure;
1191 (*split_start)++;
1192 if (split)
1193 break;
1194 case 3:
1195 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1196 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001197 goto nla_put_failure;
1198
Johannes Berg3713b4e2013-02-14 16:19:38 +01001199 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1200 struct ieee80211_supported_band *sband;
1201
1202 sband = dev->wiphy.bands[band];
1203
1204 if (!sband)
1205 continue;
1206
1207 nl_band = nla_nest_start(msg, band);
1208 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001209 goto nla_put_failure;
1210
Johannes Berg3713b4e2013-02-14 16:19:38 +01001211 switch (*chan_start) {
1212 case 0:
1213 if (nl80211_send_band_rateinfo(msg, sband))
1214 goto nla_put_failure;
1215 (*chan_start)++;
1216 if (split)
1217 break;
1218 default:
1219 /* add frequencies */
1220 nl_freqs = nla_nest_start(
1221 msg, NL80211_BAND_ATTR_FREQS);
1222 if (!nl_freqs)
1223 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001224
Johannes Berg3713b4e2013-02-14 16:19:38 +01001225 for (i = *chan_start - 1;
1226 i < sband->n_channels;
1227 i++) {
1228 nl_freq = nla_nest_start(msg, i);
1229 if (!nl_freq)
1230 goto nla_put_failure;
1231
1232 chan = &sband->channels[i];
1233
1234 if (nl80211_msg_put_channel(msg, chan))
1235 goto nla_put_failure;
1236
1237 nla_nest_end(msg, nl_freq);
1238 if (split)
1239 break;
1240 }
1241 if (i < sband->n_channels)
1242 *chan_start = i + 2;
1243 else
1244 *chan_start = 0;
1245 nla_nest_end(msg, nl_freqs);
1246 }
1247
1248 nla_nest_end(msg, nl_band);
1249
1250 if (split) {
1251 /* start again here */
1252 if (*chan_start)
1253 band--;
1254 break;
1255 }
Johannes Bergee688b002008-01-24 19:38:39 +01001256 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001257 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001258
Johannes Berg3713b4e2013-02-14 16:19:38 +01001259 if (band < IEEE80211_NUM_BANDS)
1260 *band_start = band + 1;
1261 else
1262 *band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001263
Johannes Berg3713b4e2013-02-14 16:19:38 +01001264 /* if bands & channels are done, continue outside */
1265 if (*band_start == 0 && *chan_start == 0)
1266 (*split_start)++;
1267 if (split)
1268 break;
1269 case 4:
1270 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1271 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001272 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001273
1274 i = 0;
1275#define CMD(op, n) \
1276 do { \
1277 if (dev->ops->op) { \
1278 i++; \
1279 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1280 goto nla_put_failure; \
1281 } \
1282 } while (0)
1283
1284 CMD(add_virtual_intf, NEW_INTERFACE);
1285 CMD(change_virtual_intf, SET_INTERFACE);
1286 CMD(add_key, NEW_KEY);
1287 CMD(start_ap, START_AP);
1288 CMD(add_station, NEW_STATION);
1289 CMD(add_mpath, NEW_MPATH);
1290 CMD(update_mesh_config, SET_MESH_CONFIG);
1291 CMD(change_bss, SET_BSS);
1292 CMD(auth, AUTHENTICATE);
1293 CMD(assoc, ASSOCIATE);
1294 CMD(deauth, DEAUTHENTICATE);
1295 CMD(disassoc, DISASSOCIATE);
1296 CMD(join_ibss, JOIN_IBSS);
1297 CMD(join_mesh, JOIN_MESH);
1298 CMD(set_pmksa, SET_PMKSA);
1299 CMD(del_pmksa, DEL_PMKSA);
1300 CMD(flush_pmksa, FLUSH_PMKSA);
1301 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1302 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1303 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1304 CMD(mgmt_tx, FRAME);
1305 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1306 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1307 i++;
1308 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1309 goto nla_put_failure;
1310 }
1311 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1312 dev->ops->join_mesh) {
1313 i++;
1314 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1315 goto nla_put_failure;
1316 }
1317 CMD(set_wds_peer, SET_WDS_PEER);
1318 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1319 CMD(tdls_mgmt, TDLS_MGMT);
1320 CMD(tdls_oper, TDLS_OPER);
1321 }
1322 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1323 CMD(sched_scan_start, START_SCHED_SCAN);
1324 CMD(probe_client, PROBE_CLIENT);
1325 CMD(set_noack_map, SET_NOACK_MAP);
1326 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1327 i++;
1328 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1329 goto nla_put_failure;
1330 }
1331 CMD(start_p2p_device, START_P2P_DEVICE);
1332 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001333
Kalle Valo4745fc02011-11-17 19:06:10 +02001334#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001335 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001336#endif
1337
Johannes Berg8fdc6212009-03-14 09:34:01 +01001338#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001339
Johannes Berg3713b4e2013-02-14 16:19:38 +01001340 if (dev->ops->connect || dev->ops->auth) {
1341 i++;
1342 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001343 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001344 }
1345
Johannes Berg3713b4e2013-02-14 16:19:38 +01001346 if (dev->ops->disconnect || dev->ops->deauth) {
1347 i++;
1348 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1349 goto nla_put_failure;
1350 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001351
Johannes Berg3713b4e2013-02-14 16:19:38 +01001352 nla_nest_end(msg, nl_cmds);
1353 (*split_start)++;
1354 if (split)
1355 break;
1356 case 5:
1357 if (dev->ops->remain_on_channel &&
1358 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1359 nla_put_u32(msg,
1360 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1361 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001362 goto nla_put_failure;
1363
Johannes Berg3713b4e2013-02-14 16:19:38 +01001364 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1365 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1366 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001367
Johannes Berg3713b4e2013-02-14 16:19:38 +01001368 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1369 goto nla_put_failure;
1370 (*split_start)++;
1371 if (split)
1372 break;
1373 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001374#ifdef CONFIG_PM
Johannes Berg3713b4e2013-02-14 16:19:38 +01001375 if (nl80211_send_wowlan(msg, dev))
1376 goto nla_put_failure;
1377 (*split_start)++;
1378 if (split)
1379 break;
1380#else
1381 (*split_start)++;
1382#endif
1383 case 7:
1384 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1385 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001386 goto nla_put_failure;
1387
Johannes Berg3713b4e2013-02-14 16:19:38 +01001388 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1389 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001390
Johannes Berg3713b4e2013-02-14 16:19:38 +01001391 (*split_start)++;
1392 if (split)
1393 break;
1394 case 8:
1395 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1396 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1397 dev->wiphy.ap_sme_capa))
1398 goto nla_put_failure;
1399
1400 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1401 dev->wiphy.features))
1402 goto nla_put_failure;
1403
1404 if (dev->wiphy.ht_capa_mod_mask &&
1405 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1406 sizeof(*dev->wiphy.ht_capa_mod_mask),
1407 dev->wiphy.ht_capa_mod_mask))
1408 goto nla_put_failure;
1409
1410 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1411 dev->wiphy.max_acl_mac_addrs &&
1412 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1413 dev->wiphy.max_acl_mac_addrs))
1414 goto nla_put_failure;
1415
1416 /*
1417 * Any information below this point is only available to
1418 * applications that can deal with it being split. This
1419 * helps ensure that newly added capabilities don't break
1420 * older tools by overrunning their buffers.
1421 *
1422 * We still increment split_start so that in the split
1423 * case we'll continue with more data in the next round,
1424 * but break unconditionally so unsplit data stops here.
1425 */
1426 (*split_start)++;
1427 break;
1428 case 9:
1429 /* placeholder */
1430
1431 /* done */
1432 *split_start = 0;
1433 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001434 }
Johannes Berg55682962007-09-20 13:09:35 -04001435 return genlmsg_end(msg, hdr);
1436
1437 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001438 genlmsg_cancel(msg, hdr);
1439 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001440}
1441
1442static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1443{
Johannes Berg645e77d2013-03-01 14:03:49 +01001444 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001445 int start = cb->args[0];
1446 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001447 s64 filter_wiphy = -1;
1448 bool split = false;
1449 struct nlattr **tb = nl80211_fam.attrbuf;
1450 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001451
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001452 mutex_lock(&cfg80211_mutex);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001453 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1454 tb, nl80211_fam.maxattr, nl80211_policy);
1455 if (res == 0) {
1456 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1457 if (tb[NL80211_ATTR_WIPHY])
1458 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1459 if (tb[NL80211_ATTR_WDEV])
1460 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1461 if (tb[NL80211_ATTR_IFINDEX]) {
1462 struct net_device *netdev;
1463 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1464
1465 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1466 if (!netdev) {
1467 mutex_unlock(&cfg80211_mutex);
1468 return -ENODEV;
1469 }
1470 if (netdev->ieee80211_ptr) {
1471 dev = wiphy_to_dev(
1472 netdev->ieee80211_ptr->wiphy);
1473 filter_wiphy = dev->wiphy_idx;
1474 }
1475 dev_put(netdev);
1476 }
1477 }
1478
Johannes Berg79c97e92009-07-07 03:56:12 +02001479 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001480 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1481 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001482 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001483 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001484 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1485 continue;
1486 /* attempt to fit multiple wiphy data chunks into the skb */
1487 do {
1488 ret = nl80211_send_wiphy(dev, skb,
1489 NETLINK_CB(cb->skb).portid,
1490 cb->nlh->nlmsg_seq,
1491 NLM_F_MULTI,
1492 split, &cb->args[1],
1493 &cb->args[2],
1494 &cb->args[3]);
1495 if (ret < 0) {
1496 /*
1497 * If sending the wiphy data didn't fit (ENOBUFS
1498 * or EMSGSIZE returned), this SKB is still
1499 * empty (so it's not too big because another
1500 * wiphy dataset is already in the skb) and
1501 * we've not tried to adjust the dump allocation
1502 * yet ... then adjust the alloc size to be
1503 * bigger, and return 1 but with the empty skb.
1504 * This results in an empty message being RX'ed
1505 * in userspace, but that is ignored.
1506 *
1507 * We can then retry with the larger buffer.
1508 */
1509 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1510 !skb->len &&
1511 cb->min_dump_alloc < 4096) {
1512 cb->min_dump_alloc = 4096;
1513 mutex_unlock(&cfg80211_mutex);
1514 return 1;
1515 }
1516 idx--;
1517 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001518 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001519 } while (cb->args[1] > 0);
1520 break;
Johannes Berg55682962007-09-20 13:09:35 -04001521 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001522 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001523
1524 cb->args[0] = idx;
1525
1526 return skb->len;
1527}
1528
1529static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1530{
1531 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001532 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001533
Johannes Berg645e77d2013-03-01 14:03:49 +01001534 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001535 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001536 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001537
Johannes Berg3713b4e2013-02-14 16:19:38 +01001538 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1539 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001540 nlmsg_free(msg);
1541 return -ENOBUFS;
1542 }
Johannes Berg55682962007-09-20 13:09:35 -04001543
Johannes Berg134e6372009-07-10 09:51:34 +00001544 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001545}
1546
Jouni Malinen31888482008-10-30 16:59:24 +02001547static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1548 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1549 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1550 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1551 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1552 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1553};
1554
1555static int parse_txq_params(struct nlattr *tb[],
1556 struct ieee80211_txq_params *txq_params)
1557{
Johannes Berga3304b02012-03-28 11:04:24 +02001558 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001559 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1560 !tb[NL80211_TXQ_ATTR_AIFS])
1561 return -EINVAL;
1562
Johannes Berga3304b02012-03-28 11:04:24 +02001563 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001564 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1565 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1566 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1567 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1568
Johannes Berga3304b02012-03-28 11:04:24 +02001569 if (txq_params->ac >= NL80211_NUM_ACS)
1570 return -EINVAL;
1571
Jouni Malinen31888482008-10-30 16:59:24 +02001572 return 0;
1573}
1574
Johannes Bergf444de02010-05-05 15:25:02 +02001575static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1576{
1577 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001578 * You can only set the channel explicitly for WDS interfaces,
1579 * all others have their channel managed via their respective
1580 * "establish a connection" command (connect, join, ...)
1581 *
1582 * For AP/GO and mesh mode, the channel can be set with the
1583 * channel userspace API, but is only stored and passed to the
1584 * low-level driver when the AP starts or the mesh is joined.
1585 * This is for backward compatibility, userspace can also give
1586 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001587 *
1588 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001589 * whatever else is going on, so they have their own special
1590 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001591 */
1592 return !wdev ||
1593 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001594 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001595 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1596 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001597}
1598
Johannes Berg683b6d32012-11-08 21:25:48 +01001599static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1600 struct genl_info *info,
1601 struct cfg80211_chan_def *chandef)
1602{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301603 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001604
1605 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1606 return -EINVAL;
1607
1608 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1609
1610 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001611 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1612 chandef->center_freq1 = control_freq;
1613 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001614
1615 /* Primary channel not allowed */
1616 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1617 return -EINVAL;
1618
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001619 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1620 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001621
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001622 chantype = nla_get_u32(
1623 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1624
1625 switch (chantype) {
1626 case NL80211_CHAN_NO_HT:
1627 case NL80211_CHAN_HT20:
1628 case NL80211_CHAN_HT40PLUS:
1629 case NL80211_CHAN_HT40MINUS:
1630 cfg80211_chandef_create(chandef, chandef->chan,
1631 chantype);
1632 break;
1633 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001634 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001635 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001636 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1637 chandef->width =
1638 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1639 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1640 chandef->center_freq1 =
1641 nla_get_u32(
1642 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1643 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1644 chandef->center_freq2 =
1645 nla_get_u32(
1646 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1647 }
1648
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001649 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001650 return -EINVAL;
1651
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001652 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1653 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001654 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001655
Johannes Berg683b6d32012-11-08 21:25:48 +01001656 return 0;
1657}
1658
Johannes Bergf444de02010-05-05 15:25:02 +02001659static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1660 struct wireless_dev *wdev,
1661 struct genl_info *info)
1662{
Johannes Berg683b6d32012-11-08 21:25:48 +01001663 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001664 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001665 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1666
1667 if (wdev)
1668 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001669
Johannes Bergf444de02010-05-05 15:25:02 +02001670 if (!nl80211_can_set_dev_channel(wdev))
1671 return -EOPNOTSUPP;
1672
Johannes Berg683b6d32012-11-08 21:25:48 +01001673 result = nl80211_parse_chandef(rdev, info, &chandef);
1674 if (result)
1675 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001676
1677 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001678 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001679 case NL80211_IFTYPE_AP:
1680 case NL80211_IFTYPE_P2P_GO:
1681 if (wdev->beacon_interval) {
1682 result = -EBUSY;
1683 break;
1684 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001685 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001686 result = -EINVAL;
1687 break;
1688 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001689 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001690 result = 0;
1691 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001692 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001693 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001694 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001695 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001696 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001697 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001698 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001699 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001700 }
1701 mutex_unlock(&rdev->devlist_mtx);
1702
1703 return result;
1704}
1705
1706static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1707{
Johannes Berg4c476992010-10-04 21:36:35 +02001708 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1709 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001710
Johannes Berg4c476992010-10-04 21:36:35 +02001711 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001712}
1713
Bill Jordane8347eb2010-10-01 13:54:28 -04001714static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1715{
Johannes Berg43b19952010-10-07 13:10:30 +02001716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1717 struct net_device *dev = info->user_ptr[1];
1718 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001719 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001720
1721 if (!info->attrs[NL80211_ATTR_MAC])
1722 return -EINVAL;
1723
Johannes Berg43b19952010-10-07 13:10:30 +02001724 if (netif_running(dev))
1725 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001726
Johannes Berg43b19952010-10-07 13:10:30 +02001727 if (!rdev->ops->set_wds_peer)
1728 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001729
Johannes Berg43b19952010-10-07 13:10:30 +02001730 if (wdev->iftype != NL80211_IFTYPE_WDS)
1731 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001732
1733 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001734 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001735}
1736
1737
Johannes Berg55682962007-09-20 13:09:35 -04001738static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1739{
1740 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001741 struct net_device *netdev = NULL;
1742 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001743 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001744 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001745 u32 changed;
1746 u8 retry_short = 0, retry_long = 0;
1747 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001748 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001749
Johannes Bergf444de02010-05-05 15:25:02 +02001750 /*
1751 * Try to find the wiphy and netdev. Normally this
1752 * function shouldn't need the netdev, but this is
1753 * done for backward compatibility -- previously
1754 * setting the channel was done per wiphy, but now
1755 * it is per netdev. Previous userland like hostapd
1756 * also passed a netdev to set_wiphy, so that it is
1757 * possible to let that go to the right netdev!
1758 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001759 mutex_lock(&cfg80211_mutex);
1760
Johannes Bergf444de02010-05-05 15:25:02 +02001761 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1762 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1763
1764 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1765 if (netdev && netdev->ieee80211_ptr) {
1766 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1767 mutex_lock(&rdev->mtx);
1768 } else
1769 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001770 }
1771
Johannes Bergf444de02010-05-05 15:25:02 +02001772 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001773 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1774 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001775 if (IS_ERR(rdev)) {
1776 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001777 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001778 }
1779 wdev = NULL;
1780 netdev = NULL;
1781 result = 0;
1782
1783 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001784 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001785 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001786
1787 /*
1788 * end workaround code, by now the rdev is available
1789 * and locked, and wdev may or may not be NULL.
1790 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001791
1792 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001793 result = cfg80211_dev_rename(
1794 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001795
1796 mutex_unlock(&cfg80211_mutex);
1797
1798 if (result)
1799 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001800
Jouni Malinen31888482008-10-30 16:59:24 +02001801 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1802 struct ieee80211_txq_params txq_params;
1803 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1804
1805 if (!rdev->ops->set_txq_params) {
1806 result = -EOPNOTSUPP;
1807 goto bad_res;
1808 }
1809
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001810 if (!netdev) {
1811 result = -EINVAL;
1812 goto bad_res;
1813 }
1814
Johannes Berg133a3ff2011-11-03 14:50:13 +01001815 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1816 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1817 result = -EINVAL;
1818 goto bad_res;
1819 }
1820
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001821 if (!netif_running(netdev)) {
1822 result = -ENETDOWN;
1823 goto bad_res;
1824 }
1825
Jouni Malinen31888482008-10-30 16:59:24 +02001826 nla_for_each_nested(nl_txq_params,
1827 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1828 rem_txq_params) {
1829 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1830 nla_data(nl_txq_params),
1831 nla_len(nl_txq_params),
1832 txq_params_policy);
1833 result = parse_txq_params(tb, &txq_params);
1834 if (result)
1835 goto bad_res;
1836
Hila Gonene35e4d22012-06-27 17:19:42 +03001837 result = rdev_set_txq_params(rdev, netdev,
1838 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001839 if (result)
1840 goto bad_res;
1841 }
1842 }
1843
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001844 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001845 result = __nl80211_set_channel(rdev,
1846 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1847 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001848 if (result)
1849 goto bad_res;
1850 }
1851
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001852 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001853 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001854 enum nl80211_tx_power_setting type;
1855 int idx, mbm = 0;
1856
Johannes Bergc8442112012-10-24 10:17:18 +02001857 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1858 txp_wdev = NULL;
1859
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001860 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001861 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001862 goto bad_res;
1863 }
1864
1865 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1866 type = nla_get_u32(info->attrs[idx]);
1867
1868 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1869 (type != NL80211_TX_POWER_AUTOMATIC)) {
1870 result = -EINVAL;
1871 goto bad_res;
1872 }
1873
1874 if (type != NL80211_TX_POWER_AUTOMATIC) {
1875 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1876 mbm = nla_get_u32(info->attrs[idx]);
1877 }
1878
Johannes Bergc8442112012-10-24 10:17:18 +02001879 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001880 if (result)
1881 goto bad_res;
1882 }
1883
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001884 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1885 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1886 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001887 if ((!rdev->wiphy.available_antennas_tx &&
1888 !rdev->wiphy.available_antennas_rx) ||
1889 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001890 result = -EOPNOTSUPP;
1891 goto bad_res;
1892 }
1893
1894 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1895 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1896
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001897 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001898 * available antenna masks, except for the "all" mask */
1899 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1900 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001901 result = -EINVAL;
1902 goto bad_res;
1903 }
1904
Bruno Randolf7f531e02010-12-16 11:30:22 +09001905 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1906 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001907
Hila Gonene35e4d22012-06-27 17:19:42 +03001908 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001909 if (result)
1910 goto bad_res;
1911 }
1912
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001913 changed = 0;
1914
1915 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1916 retry_short = nla_get_u8(
1917 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1918 if (retry_short == 0) {
1919 result = -EINVAL;
1920 goto bad_res;
1921 }
1922 changed |= WIPHY_PARAM_RETRY_SHORT;
1923 }
1924
1925 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1926 retry_long = nla_get_u8(
1927 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1928 if (retry_long == 0) {
1929 result = -EINVAL;
1930 goto bad_res;
1931 }
1932 changed |= WIPHY_PARAM_RETRY_LONG;
1933 }
1934
1935 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1936 frag_threshold = nla_get_u32(
1937 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1938 if (frag_threshold < 256) {
1939 result = -EINVAL;
1940 goto bad_res;
1941 }
1942 if (frag_threshold != (u32) -1) {
1943 /*
1944 * Fragments (apart from the last one) are required to
1945 * have even length. Make the fragmentation code
1946 * simpler by stripping LSB should someone try to use
1947 * odd threshold value.
1948 */
1949 frag_threshold &= ~0x1;
1950 }
1951 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1952 }
1953
1954 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1955 rts_threshold = nla_get_u32(
1956 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1957 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1958 }
1959
Lukáš Turek81077e82009-12-21 22:50:47 +01001960 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1961 coverage_class = nla_get_u8(
1962 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1963 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1964 }
1965
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001966 if (changed) {
1967 u8 old_retry_short, old_retry_long;
1968 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001969 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001970
1971 if (!rdev->ops->set_wiphy_params) {
1972 result = -EOPNOTSUPP;
1973 goto bad_res;
1974 }
1975
1976 old_retry_short = rdev->wiphy.retry_short;
1977 old_retry_long = rdev->wiphy.retry_long;
1978 old_frag_threshold = rdev->wiphy.frag_threshold;
1979 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001980 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001981
1982 if (changed & WIPHY_PARAM_RETRY_SHORT)
1983 rdev->wiphy.retry_short = retry_short;
1984 if (changed & WIPHY_PARAM_RETRY_LONG)
1985 rdev->wiphy.retry_long = retry_long;
1986 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1987 rdev->wiphy.frag_threshold = frag_threshold;
1988 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1989 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001990 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1991 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001992
Hila Gonene35e4d22012-06-27 17:19:42 +03001993 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001994 if (result) {
1995 rdev->wiphy.retry_short = old_retry_short;
1996 rdev->wiphy.retry_long = old_retry_long;
1997 rdev->wiphy.frag_threshold = old_frag_threshold;
1998 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001999 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002000 }
2001 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002002
Johannes Berg306d6112008-12-08 12:39:04 +01002003 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002004 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02002005 if (netdev)
2006 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002007 return result;
2008}
2009
Johannes Berg71bbc992012-06-15 15:30:18 +02002010static inline u64 wdev_id(struct wireless_dev *wdev)
2011{
2012 return (u64)wdev->identifier |
2013 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2014}
Johannes Berg55682962007-09-20 13:09:35 -04002015
Johannes Berg683b6d32012-11-08 21:25:48 +01002016static int nl80211_send_chandef(struct sk_buff *msg,
2017 struct cfg80211_chan_def *chandef)
2018{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002019 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002020
Johannes Berg683b6d32012-11-08 21:25:48 +01002021 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2022 chandef->chan->center_freq))
2023 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002024 switch (chandef->width) {
2025 case NL80211_CHAN_WIDTH_20_NOHT:
2026 case NL80211_CHAN_WIDTH_20:
2027 case NL80211_CHAN_WIDTH_40:
2028 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2029 cfg80211_get_chandef_type(chandef)))
2030 return -ENOBUFS;
2031 break;
2032 default:
2033 break;
2034 }
2035 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2036 return -ENOBUFS;
2037 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2038 return -ENOBUFS;
2039 if (chandef->center_freq2 &&
2040 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002041 return -ENOBUFS;
2042 return 0;
2043}
2044
Eric W. Biederman15e47302012-09-07 20:12:54 +00002045static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002046 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002047 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002048{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002049 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002050 void *hdr;
2051
Eric W. Biederman15e47302012-09-07 20:12:54 +00002052 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002053 if (!hdr)
2054 return -1;
2055
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002056 if (dev &&
2057 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002058 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002059 goto nla_put_failure;
2060
2061 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2062 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002063 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002064 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002065 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2066 rdev->devlist_generation ^
2067 (cfg80211_rdev_list_generation << 2)))
2068 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002069
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002070 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002071 int ret;
2072 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002073
Johannes Berg683b6d32012-11-08 21:25:48 +01002074 ret = rdev_get_channel(rdev, wdev, &chandef);
2075 if (ret == 0) {
2076 if (nl80211_send_chandef(msg, &chandef))
2077 goto nla_put_failure;
2078 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002079 }
2080
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002081 if (wdev->ssid_len) {
2082 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2083 goto nla_put_failure;
2084 }
2085
Johannes Berg55682962007-09-20 13:09:35 -04002086 return genlmsg_end(msg, hdr);
2087
2088 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002089 genlmsg_cancel(msg, hdr);
2090 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002091}
2092
2093static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2094{
2095 int wp_idx = 0;
2096 int if_idx = 0;
2097 int wp_start = cb->args[0];
2098 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002099 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002100 struct wireless_dev *wdev;
2101
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002102 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02002103 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2104 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002105 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002106 if (wp_idx < wp_start) {
2107 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002108 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002109 }
Johannes Berg55682962007-09-20 13:09:35 -04002110 if_idx = 0;
2111
Johannes Bergf5ea9122009-08-07 16:17:38 +02002112 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02002113 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002114 if (if_idx < if_start) {
2115 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002116 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002117 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002118 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002119 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002120 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002121 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002122 goto out;
2123 }
2124 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002125 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002126 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002127
2128 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002129 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002130 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002131 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002132
2133 cb->args[0] = wp_idx;
2134 cb->args[1] = if_idx;
2135
2136 return skb->len;
2137}
2138
2139static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2140{
2141 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002142 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002143 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002144
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002145 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002146 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002147 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002148
Eric W. Biederman15e47302012-09-07 20:12:54 +00002149 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002150 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002151 nlmsg_free(msg);
2152 return -ENOBUFS;
2153 }
Johannes Berg55682962007-09-20 13:09:35 -04002154
Johannes Berg134e6372009-07-10 09:51:34 +00002155 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002156}
2157
Michael Wu66f7ac52008-01-31 19:48:22 +01002158static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2159 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2160 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2161 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2162 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2163 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2164};
2165
2166static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2167{
2168 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2169 int flag;
2170
2171 *mntrflags = 0;
2172
2173 if (!nla)
2174 return -EINVAL;
2175
2176 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2177 nla, mntr_flags_policy))
2178 return -EINVAL;
2179
2180 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2181 if (flags[flag])
2182 *mntrflags |= (1<<flag);
2183
2184 return 0;
2185}
2186
Johannes Berg9bc383d2009-11-19 11:55:19 +01002187static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002188 struct net_device *netdev, u8 use_4addr,
2189 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002190{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002191 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002192 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002193 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002194 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002195 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002196
2197 switch (iftype) {
2198 case NL80211_IFTYPE_AP_VLAN:
2199 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2200 return 0;
2201 break;
2202 case NL80211_IFTYPE_STATION:
2203 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2204 return 0;
2205 break;
2206 default:
2207 break;
2208 }
2209
2210 return -EOPNOTSUPP;
2211}
2212
Johannes Berg55682962007-09-20 13:09:35 -04002213static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2214{
Johannes Berg4c476992010-10-04 21:36:35 +02002215 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002216 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002217 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002218 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002219 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002220 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002221 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002222
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002223 memset(&params, 0, sizeof(params));
2224
Johannes Berg04a773a2009-04-19 21:24:32 +02002225 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002226
Johannes Berg723b0382008-09-16 20:22:09 +02002227 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002228 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002229 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002230 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002231 if (ntype > NL80211_IFTYPE_MAX)
2232 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002233 }
2234
Johannes Berg92ffe052008-09-16 20:39:36 +02002235 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002236 struct wireless_dev *wdev = dev->ieee80211_ptr;
2237
Johannes Berg4c476992010-10-04 21:36:35 +02002238 if (ntype != NL80211_IFTYPE_MESH_POINT)
2239 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002240 if (netif_running(dev))
2241 return -EBUSY;
2242
2243 wdev_lock(wdev);
2244 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2245 IEEE80211_MAX_MESH_ID_LEN);
2246 wdev->mesh_id_up_len =
2247 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2248 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2249 wdev->mesh_id_up_len);
2250 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002251 }
2252
Felix Fietkau8b787642009-11-10 18:53:10 +01002253 if (info->attrs[NL80211_ATTR_4ADDR]) {
2254 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2255 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002256 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002257 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002258 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002259 } else {
2260 params.use_4addr = -1;
2261 }
2262
Johannes Berg92ffe052008-09-16 20:39:36 +02002263 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002264 if (ntype != NL80211_IFTYPE_MONITOR)
2265 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002266 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2267 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002268 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002269 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002270
2271 flags = &_flags;
2272 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002273 }
Johannes Berg3b858752009-03-12 09:55:09 +01002274
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002275 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002276 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002277 else
2278 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002279
Johannes Berg9bc383d2009-11-19 11:55:19 +01002280 if (!err && params.use_4addr != -1)
2281 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2282
Johannes Berg55682962007-09-20 13:09:35 -04002283 return err;
2284}
2285
2286static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2287{
Johannes Berg4c476992010-10-04 21:36:35 +02002288 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002289 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002290 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002291 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002292 int err;
2293 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002294 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002295
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002296 memset(&params, 0, sizeof(params));
2297
Johannes Berg55682962007-09-20 13:09:35 -04002298 if (!info->attrs[NL80211_ATTR_IFNAME])
2299 return -EINVAL;
2300
2301 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2302 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2303 if (type > NL80211_IFTYPE_MAX)
2304 return -EINVAL;
2305 }
2306
Johannes Berg79c97e92009-07-07 03:56:12 +02002307 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002308 !(rdev->wiphy.interface_modes & (1 << type)))
2309 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002310
Arend van Spriel1c18f142013-01-08 10:17:27 +01002311 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2312 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2313 ETH_ALEN);
2314 if (!is_valid_ether_addr(params.macaddr))
2315 return -EADDRNOTAVAIL;
2316 }
2317
Johannes Berg9bc383d2009-11-19 11:55:19 +01002318 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002319 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002320 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002321 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002322 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002323 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002324
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002325 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2326 if (!msg)
2327 return -ENOMEM;
2328
Michael Wu66f7ac52008-01-31 19:48:22 +01002329 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2330 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2331 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002332 wdev = rdev_add_virtual_intf(rdev,
2333 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2334 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002335 if (IS_ERR(wdev)) {
2336 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002337 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002338 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002339
Johannes Berg98104fde2012-06-16 00:19:54 +02002340 switch (type) {
2341 case NL80211_IFTYPE_MESH_POINT:
2342 if (!info->attrs[NL80211_ATTR_MESH_ID])
2343 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002344 wdev_lock(wdev);
2345 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2346 IEEE80211_MAX_MESH_ID_LEN);
2347 wdev->mesh_id_up_len =
2348 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2349 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2350 wdev->mesh_id_up_len);
2351 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002352 break;
2353 case NL80211_IFTYPE_P2P_DEVICE:
2354 /*
2355 * P2P Device doesn't have a netdev, so doesn't go
2356 * through the netdev notifier and must be added here
2357 */
2358 mutex_init(&wdev->mtx);
2359 INIT_LIST_HEAD(&wdev->event_list);
2360 spin_lock_init(&wdev->event_lock);
2361 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2362 spin_lock_init(&wdev->mgmt_registrations_lock);
2363
2364 mutex_lock(&rdev->devlist_mtx);
2365 wdev->identifier = ++rdev->wdev_id;
2366 list_add_rcu(&wdev->list, &rdev->wdev_list);
2367 rdev->devlist_generation++;
2368 mutex_unlock(&rdev->devlist_mtx);
2369 break;
2370 default:
2371 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002372 }
2373
Eric W. Biederman15e47302012-09-07 20:12:54 +00002374 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002375 rdev, wdev) < 0) {
2376 nlmsg_free(msg);
2377 return -ENOBUFS;
2378 }
2379
2380 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002381}
2382
2383static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2384{
Johannes Berg4c476992010-10-04 21:36:35 +02002385 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002386 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002387
Johannes Berg4c476992010-10-04 21:36:35 +02002388 if (!rdev->ops->del_virtual_intf)
2389 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002390
Johannes Berg84efbb82012-06-16 00:00:26 +02002391 /*
2392 * If we remove a wireless device without a netdev then clear
2393 * user_ptr[1] so that nl80211_post_doit won't dereference it
2394 * to check if it needs to do dev_put(). Otherwise it crashes
2395 * since the wdev has been freed, unlike with a netdev where
2396 * we need the dev_put() for the netdev to really be freed.
2397 */
2398 if (!wdev->netdev)
2399 info->user_ptr[1] = NULL;
2400
Hila Gonene35e4d22012-06-27 17:19:42 +03002401 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002402}
2403
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002404static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2405{
2406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2407 struct net_device *dev = info->user_ptr[1];
2408 u16 noack_map;
2409
2410 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2411 return -EINVAL;
2412
2413 if (!rdev->ops->set_noack_map)
2414 return -EOPNOTSUPP;
2415
2416 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2417
Hila Gonene35e4d22012-06-27 17:19:42 +03002418 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002419}
2420
Johannes Berg41ade002007-12-19 02:03:29 +01002421struct get_key_cookie {
2422 struct sk_buff *msg;
2423 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002424 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002425};
2426
2427static void get_key_callback(void *c, struct key_params *params)
2428{
Johannes Bergb9454e82009-07-08 13:29:08 +02002429 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002430 struct get_key_cookie *cookie = c;
2431
David S. Miller9360ffd2012-03-29 04:41:26 -04002432 if ((params->key &&
2433 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2434 params->key_len, params->key)) ||
2435 (params->seq &&
2436 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2437 params->seq_len, params->seq)) ||
2438 (params->cipher &&
2439 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2440 params->cipher)))
2441 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002442
Johannes Bergb9454e82009-07-08 13:29:08 +02002443 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2444 if (!key)
2445 goto nla_put_failure;
2446
David S. Miller9360ffd2012-03-29 04:41:26 -04002447 if ((params->key &&
2448 nla_put(cookie->msg, NL80211_KEY_DATA,
2449 params->key_len, params->key)) ||
2450 (params->seq &&
2451 nla_put(cookie->msg, NL80211_KEY_SEQ,
2452 params->seq_len, params->seq)) ||
2453 (params->cipher &&
2454 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2455 params->cipher)))
2456 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002457
David S. Miller9360ffd2012-03-29 04:41:26 -04002458 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2459 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002460
2461 nla_nest_end(cookie->msg, key);
2462
Johannes Berg41ade002007-12-19 02:03:29 +01002463 return;
2464 nla_put_failure:
2465 cookie->error = 1;
2466}
2467
2468static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2469{
Johannes Berg4c476992010-10-04 21:36:35 +02002470 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002471 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002472 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002473 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002474 const u8 *mac_addr = NULL;
2475 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002476 struct get_key_cookie cookie = {
2477 .error = 0,
2478 };
2479 void *hdr;
2480 struct sk_buff *msg;
2481
2482 if (info->attrs[NL80211_ATTR_KEY_IDX])
2483 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2484
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002485 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002486 return -EINVAL;
2487
2488 if (info->attrs[NL80211_ATTR_MAC])
2489 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2490
Johannes Berge31b8212010-10-05 19:39:30 +02002491 pairwise = !!mac_addr;
2492 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2493 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2494 if (kt >= NUM_NL80211_KEYTYPES)
2495 return -EINVAL;
2496 if (kt != NL80211_KEYTYPE_GROUP &&
2497 kt != NL80211_KEYTYPE_PAIRWISE)
2498 return -EINVAL;
2499 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2500 }
2501
Johannes Berg4c476992010-10-04 21:36:35 +02002502 if (!rdev->ops->get_key)
2503 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002504
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002505 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002506 if (!msg)
2507 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002508
Eric W. Biederman15e47302012-09-07 20:12:54 +00002509 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002510 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002511 if (IS_ERR(hdr))
2512 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002513
2514 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002515 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002516
David S. Miller9360ffd2012-03-29 04:41:26 -04002517 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2518 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2519 goto nla_put_failure;
2520 if (mac_addr &&
2521 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2522 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002523
Johannes Berge31b8212010-10-05 19:39:30 +02002524 if (pairwise && mac_addr &&
2525 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2526 return -ENOENT;
2527
Hila Gonene35e4d22012-06-27 17:19:42 +03002528 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2529 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002530
2531 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002532 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002533
2534 if (cookie.error)
2535 goto nla_put_failure;
2536
2537 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002538 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002539
2540 nla_put_failure:
2541 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002542 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002543 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002544 return err;
2545}
2546
2547static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2548{
Johannes Berg4c476992010-10-04 21:36:35 +02002549 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002550 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002551 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002552 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002553
Johannes Bergb9454e82009-07-08 13:29:08 +02002554 err = nl80211_parse_key(info, &key);
2555 if (err)
2556 return err;
2557
2558 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002559 return -EINVAL;
2560
Johannes Bergb9454e82009-07-08 13:29:08 +02002561 /* only support setting default key */
2562 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002563 return -EINVAL;
2564
Johannes Bergfffd0932009-07-08 14:22:54 +02002565 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002566
2567 if (key.def) {
2568 if (!rdev->ops->set_default_key) {
2569 err = -EOPNOTSUPP;
2570 goto out;
2571 }
2572
2573 err = nl80211_key_allowed(dev->ieee80211_ptr);
2574 if (err)
2575 goto out;
2576
Hila Gonene35e4d22012-06-27 17:19:42 +03002577 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002578 key.def_uni, key.def_multi);
2579
2580 if (err)
2581 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002582
Johannes Berg3d23e342009-09-29 23:27:28 +02002583#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002584 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002585#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002586 } else {
2587 if (key.def_uni || !key.def_multi) {
2588 err = -EINVAL;
2589 goto out;
2590 }
2591
2592 if (!rdev->ops->set_default_mgmt_key) {
2593 err = -EOPNOTSUPP;
2594 goto out;
2595 }
2596
2597 err = nl80211_key_allowed(dev->ieee80211_ptr);
2598 if (err)
2599 goto out;
2600
Hila Gonene35e4d22012-06-27 17:19:42 +03002601 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002602 if (err)
2603 goto out;
2604
2605#ifdef CONFIG_CFG80211_WEXT
2606 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2607#endif
2608 }
2609
2610 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002611 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002612
Johannes Berg41ade002007-12-19 02:03:29 +01002613 return err;
2614}
2615
2616static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2617{
Johannes Berg4c476992010-10-04 21:36:35 +02002618 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002619 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002620 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002621 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002622 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002623
Johannes Bergb9454e82009-07-08 13:29:08 +02002624 err = nl80211_parse_key(info, &key);
2625 if (err)
2626 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002627
Johannes Bergb9454e82009-07-08 13:29:08 +02002628 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002629 return -EINVAL;
2630
Johannes Berg41ade002007-12-19 02:03:29 +01002631 if (info->attrs[NL80211_ATTR_MAC])
2632 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2633
Johannes Berge31b8212010-10-05 19:39:30 +02002634 if (key.type == -1) {
2635 if (mac_addr)
2636 key.type = NL80211_KEYTYPE_PAIRWISE;
2637 else
2638 key.type = NL80211_KEYTYPE_GROUP;
2639 }
2640
2641 /* for now */
2642 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2643 key.type != NL80211_KEYTYPE_GROUP)
2644 return -EINVAL;
2645
Johannes Berg4c476992010-10-04 21:36:35 +02002646 if (!rdev->ops->add_key)
2647 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002648
Johannes Berge31b8212010-10-05 19:39:30 +02002649 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2650 key.type == NL80211_KEYTYPE_PAIRWISE,
2651 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002652 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002653
2654 wdev_lock(dev->ieee80211_ptr);
2655 err = nl80211_key_allowed(dev->ieee80211_ptr);
2656 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002657 err = rdev_add_key(rdev, dev, key.idx,
2658 key.type == NL80211_KEYTYPE_PAIRWISE,
2659 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002660 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002661
Johannes Berg41ade002007-12-19 02:03:29 +01002662 return err;
2663}
2664
2665static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2666{
Johannes Berg4c476992010-10-04 21:36:35 +02002667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002668 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002669 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002670 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002671 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002672
Johannes Bergb9454e82009-07-08 13:29:08 +02002673 err = nl80211_parse_key(info, &key);
2674 if (err)
2675 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002676
2677 if (info->attrs[NL80211_ATTR_MAC])
2678 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2679
Johannes Berge31b8212010-10-05 19:39:30 +02002680 if (key.type == -1) {
2681 if (mac_addr)
2682 key.type = NL80211_KEYTYPE_PAIRWISE;
2683 else
2684 key.type = NL80211_KEYTYPE_GROUP;
2685 }
2686
2687 /* for now */
2688 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2689 key.type != NL80211_KEYTYPE_GROUP)
2690 return -EINVAL;
2691
Johannes Berg4c476992010-10-04 21:36:35 +02002692 if (!rdev->ops->del_key)
2693 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002694
Johannes Bergfffd0932009-07-08 14:22:54 +02002695 wdev_lock(dev->ieee80211_ptr);
2696 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002697
2698 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2699 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2700 err = -ENOENT;
2701
Johannes Bergfffd0932009-07-08 14:22:54 +02002702 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002703 err = rdev_del_key(rdev, dev, key.idx,
2704 key.type == NL80211_KEYTYPE_PAIRWISE,
2705 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002706
Johannes Berg3d23e342009-09-29 23:27:28 +02002707#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002708 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002709 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002710 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002711 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002712 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2713 }
2714#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002715 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002716
Johannes Berg41ade002007-12-19 02:03:29 +01002717 return err;
2718}
2719
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302720/* This function returns an error or the number of nested attributes */
2721static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2722{
2723 struct nlattr *attr;
2724 int n_entries = 0, tmp;
2725
2726 nla_for_each_nested(attr, nl_attr, tmp) {
2727 if (nla_len(attr) != ETH_ALEN)
2728 return -EINVAL;
2729
2730 n_entries++;
2731 }
2732
2733 return n_entries;
2734}
2735
2736/*
2737 * This function parses ACL information and allocates memory for ACL data.
2738 * On successful return, the calling function is responsible to free the
2739 * ACL buffer returned by this function.
2740 */
2741static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2742 struct genl_info *info)
2743{
2744 enum nl80211_acl_policy acl_policy;
2745 struct nlattr *attr;
2746 struct cfg80211_acl_data *acl;
2747 int i = 0, n_entries, tmp;
2748
2749 if (!wiphy->max_acl_mac_addrs)
2750 return ERR_PTR(-EOPNOTSUPP);
2751
2752 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2753 return ERR_PTR(-EINVAL);
2754
2755 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2756 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2757 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2758 return ERR_PTR(-EINVAL);
2759
2760 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2761 return ERR_PTR(-EINVAL);
2762
2763 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2764 if (n_entries < 0)
2765 return ERR_PTR(n_entries);
2766
2767 if (n_entries > wiphy->max_acl_mac_addrs)
2768 return ERR_PTR(-ENOTSUPP);
2769
2770 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2771 GFP_KERNEL);
2772 if (!acl)
2773 return ERR_PTR(-ENOMEM);
2774
2775 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2776 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2777 i++;
2778 }
2779
2780 acl->n_acl_entries = n_entries;
2781 acl->acl_policy = acl_policy;
2782
2783 return acl;
2784}
2785
2786static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2787{
2788 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2789 struct net_device *dev = info->user_ptr[1];
2790 struct cfg80211_acl_data *acl;
2791 int err;
2792
2793 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2794 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2795 return -EOPNOTSUPP;
2796
2797 if (!dev->ieee80211_ptr->beacon_interval)
2798 return -EINVAL;
2799
2800 acl = parse_acl_data(&rdev->wiphy, info);
2801 if (IS_ERR(acl))
2802 return PTR_ERR(acl);
2803
2804 err = rdev_set_mac_acl(rdev, dev, acl);
2805
2806 kfree(acl);
2807
2808 return err;
2809}
2810
Johannes Berg88600202012-02-13 15:17:18 +01002811static int nl80211_parse_beacon(struct genl_info *info,
2812 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002813{
Johannes Berg88600202012-02-13 15:17:18 +01002814 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002815
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002816 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2817 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2818 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2819 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002820 return -EINVAL;
2821
Johannes Berg88600202012-02-13 15:17:18 +01002822 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002823
Johannes Berged1b6cc2007-12-19 02:03:32 +01002824 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002825 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2826 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2827 if (!bcn->head_len)
2828 return -EINVAL;
2829 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002830 }
2831
2832 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002833 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2834 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002835 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002836 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002837 }
2838
Johannes Berg4c476992010-10-04 21:36:35 +02002839 if (!haveinfo)
2840 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002841
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002842 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002843 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2844 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002845 }
2846
2847 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002848 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002849 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002850 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002851 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2852 }
2853
2854 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002855 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002856 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002857 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002858 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2859 }
2860
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002861 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002862 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002863 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002864 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002865 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2866 }
2867
Johannes Berg88600202012-02-13 15:17:18 +01002868 return 0;
2869}
2870
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002871static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2872 struct cfg80211_ap_settings *params)
2873{
2874 struct wireless_dev *wdev;
2875 bool ret = false;
2876
2877 mutex_lock(&rdev->devlist_mtx);
2878
Johannes Berg89a54e42012-06-15 14:33:17 +02002879 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002880 if (wdev->iftype != NL80211_IFTYPE_AP &&
2881 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2882 continue;
2883
Johannes Berg683b6d32012-11-08 21:25:48 +01002884 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002885 continue;
2886
Johannes Berg683b6d32012-11-08 21:25:48 +01002887 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002888 ret = true;
2889 break;
2890 }
2891
2892 mutex_unlock(&rdev->devlist_mtx);
2893
2894 return ret;
2895}
2896
Jouni Malinene39e5b52012-09-30 19:29:39 +03002897static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2898 enum nl80211_auth_type auth_type,
2899 enum nl80211_commands cmd)
2900{
2901 if (auth_type > NL80211_AUTHTYPE_MAX)
2902 return false;
2903
2904 switch (cmd) {
2905 case NL80211_CMD_AUTHENTICATE:
2906 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2907 auth_type == NL80211_AUTHTYPE_SAE)
2908 return false;
2909 return true;
2910 case NL80211_CMD_CONNECT:
2911 case NL80211_CMD_START_AP:
2912 /* SAE not supported yet */
2913 if (auth_type == NL80211_AUTHTYPE_SAE)
2914 return false;
2915 return true;
2916 default:
2917 return false;
2918 }
2919}
2920
Johannes Berg88600202012-02-13 15:17:18 +01002921static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2922{
2923 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2924 struct net_device *dev = info->user_ptr[1];
2925 struct wireless_dev *wdev = dev->ieee80211_ptr;
2926 struct cfg80211_ap_settings params;
2927 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002928 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002929
2930 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2931 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2932 return -EOPNOTSUPP;
2933
2934 if (!rdev->ops->start_ap)
2935 return -EOPNOTSUPP;
2936
2937 if (wdev->beacon_interval)
2938 return -EALREADY;
2939
2940 memset(&params, 0, sizeof(params));
2941
2942 /* these are required for START_AP */
2943 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2944 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2945 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2946 return -EINVAL;
2947
2948 err = nl80211_parse_beacon(info, &params.beacon);
2949 if (err)
2950 return err;
2951
2952 params.beacon_interval =
2953 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2954 params.dtim_period =
2955 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2956
2957 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2958 if (err)
2959 return err;
2960
2961 /*
2962 * In theory, some of these attributes should be required here
2963 * but since they were not used when the command was originally
2964 * added, keep them optional for old user space programs to let
2965 * them continue to work with drivers that do not need the
2966 * additional information -- drivers must check!
2967 */
2968 if (info->attrs[NL80211_ATTR_SSID]) {
2969 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2970 params.ssid_len =
2971 nla_len(info->attrs[NL80211_ATTR_SSID]);
2972 if (params.ssid_len == 0 ||
2973 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2974 return -EINVAL;
2975 }
2976
2977 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2978 params.hidden_ssid = nla_get_u32(
2979 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2980 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2981 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2982 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2983 return -EINVAL;
2984 }
2985
2986 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2987
2988 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2989 params.auth_type = nla_get_u32(
2990 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002991 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2992 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002993 return -EINVAL;
2994 } else
2995 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2996
2997 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2998 NL80211_MAX_NR_CIPHER_SUITES);
2999 if (err)
3000 return err;
3001
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303002 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3003 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3004 return -EOPNOTSUPP;
3005 params.inactivity_timeout = nla_get_u16(
3006 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3007 }
3008
Johannes Berg53cabad2012-11-14 15:17:28 +01003009 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3010 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3011 return -EINVAL;
3012 params.p2p_ctwindow =
3013 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3014 if (params.p2p_ctwindow > 127)
3015 return -EINVAL;
3016 if (params.p2p_ctwindow != 0 &&
3017 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3018 return -EINVAL;
3019 }
3020
3021 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3022 u8 tmp;
3023
3024 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3025 return -EINVAL;
3026 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3027 if (tmp > 1)
3028 return -EINVAL;
3029 params.p2p_opp_ps = tmp;
3030 if (params.p2p_opp_ps != 0 &&
3031 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3032 return -EINVAL;
3033 }
3034
Johannes Bergaa430da2012-05-16 23:50:18 +02003035 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003036 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3037 if (err)
3038 return err;
3039 } else if (wdev->preset_chandef.chan) {
3040 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003041 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003042 return -EINVAL;
3043
Johannes Berg683b6d32012-11-08 21:25:48 +01003044 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003045 return -EINVAL;
3046
Simon Wunderlich04f39042013-02-08 18:16:19 +01003047 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3048 if (err < 0)
3049 return err;
3050 if (err) {
3051 radar_detect_width = BIT(params.chandef.width);
3052 params.radar_required = true;
3053 }
3054
Michal Kaziore4e32452012-06-29 12:47:08 +02003055 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003056 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3057 params.chandef.chan,
3058 CHAN_MODE_SHARED,
3059 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003060 mutex_unlock(&rdev->devlist_mtx);
3061
3062 if (err)
3063 return err;
3064
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303065 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3066 params.acl = parse_acl_data(&rdev->wiphy, info);
3067 if (IS_ERR(params.acl))
3068 return PTR_ERR(params.acl);
3069 }
3070
Hila Gonene35e4d22012-06-27 17:19:42 +03003071 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003072 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003073 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003074 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003075 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003076 wdev->ssid_len = params.ssid_len;
3077 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003078 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303079
3080 kfree(params.acl);
3081
Johannes Berg56d18932011-05-09 18:41:15 +02003082 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003083}
3084
Johannes Berg88600202012-02-13 15:17:18 +01003085static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3086{
3087 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3088 struct net_device *dev = info->user_ptr[1];
3089 struct wireless_dev *wdev = dev->ieee80211_ptr;
3090 struct cfg80211_beacon_data params;
3091 int err;
3092
3093 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3094 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3095 return -EOPNOTSUPP;
3096
3097 if (!rdev->ops->change_beacon)
3098 return -EOPNOTSUPP;
3099
3100 if (!wdev->beacon_interval)
3101 return -EINVAL;
3102
3103 err = nl80211_parse_beacon(info, &params);
3104 if (err)
3105 return err;
3106
Hila Gonene35e4d22012-06-27 17:19:42 +03003107 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003108}
3109
3110static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003111{
Johannes Berg4c476992010-10-04 21:36:35 +02003112 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3113 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003114
Michal Kazior60771782012-06-29 12:46:56 +02003115 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003116}
3117
Johannes Berg5727ef12007-12-19 02:03:34 +01003118static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3119 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3120 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3121 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003122 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003123 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003124 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003125};
3126
Johannes Bergeccb8e82009-05-11 21:57:56 +03003127static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003128 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003129 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003130{
3131 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003132 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003133 int flag;
3134
Johannes Bergeccb8e82009-05-11 21:57:56 +03003135 /*
3136 * Try parsing the new attribute first so userspace
3137 * can specify both for older kernels.
3138 */
3139 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3140 if (nla) {
3141 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003142
Johannes Bergeccb8e82009-05-11 21:57:56 +03003143 sta_flags = nla_data(nla);
3144 params->sta_flags_mask = sta_flags->mask;
3145 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003146 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003147 if ((params->sta_flags_mask |
3148 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3149 return -EINVAL;
3150 return 0;
3151 }
3152
3153 /* if present, parse the old attribute */
3154
3155 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003156 if (!nla)
3157 return 0;
3158
3159 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3160 nla, sta_flags_policy))
3161 return -EINVAL;
3162
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003163 /*
3164 * Only allow certain flags for interface types so that
3165 * other attributes are silently ignored. Remember that
3166 * this is backward compatibility code with old userspace
3167 * and shouldn't be hit in other cases anyway.
3168 */
3169 switch (iftype) {
3170 case NL80211_IFTYPE_AP:
3171 case NL80211_IFTYPE_AP_VLAN:
3172 case NL80211_IFTYPE_P2P_GO:
3173 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3174 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3175 BIT(NL80211_STA_FLAG_WME) |
3176 BIT(NL80211_STA_FLAG_MFP);
3177 break;
3178 case NL80211_IFTYPE_P2P_CLIENT:
3179 case NL80211_IFTYPE_STATION:
3180 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3181 BIT(NL80211_STA_FLAG_TDLS_PEER);
3182 break;
3183 case NL80211_IFTYPE_MESH_POINT:
3184 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3185 BIT(NL80211_STA_FLAG_MFP) |
3186 BIT(NL80211_STA_FLAG_AUTHORIZED);
3187 default:
3188 return -EINVAL;
3189 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003190
Johannes Berg3383b5a2012-05-10 20:14:43 +02003191 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3192 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003193 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003194
Johannes Berg3383b5a2012-05-10 20:14:43 +02003195 /* no longer support new API additions in old API */
3196 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3197 return -EINVAL;
3198 }
3199 }
3200
Johannes Berg5727ef12007-12-19 02:03:34 +01003201 return 0;
3202}
3203
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003204static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3205 int attr)
3206{
3207 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003208 u32 bitrate;
3209 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003210
3211 rate = nla_nest_start(msg, attr);
3212 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003213 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003214
3215 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3216 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003217 /* report 16-bit bitrate only if we can */
3218 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003219 if (bitrate > 0 &&
3220 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3221 return false;
3222 if (bitrate_compat > 0 &&
3223 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3224 return false;
3225
3226 if (info->flags & RATE_INFO_FLAGS_MCS) {
3227 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3228 return false;
3229 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3230 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3231 return false;
3232 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3233 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3234 return false;
3235 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3236 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3237 return false;
3238 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3239 return false;
3240 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3241 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3242 return false;
3243 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3244 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3245 return false;
3246 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3247 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3248 return false;
3249 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3250 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3251 return false;
3252 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3253 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3254 return false;
3255 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003256
3257 nla_nest_end(msg, rate);
3258 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003259}
3260
Eric W. Biederman15e47302012-09-07 20:12:54 +00003261static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003262 int flags,
3263 struct cfg80211_registered_device *rdev,
3264 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003265 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003266{
3267 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003268 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003269
Eric W. Biederman15e47302012-09-07 20:12:54 +00003270 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003271 if (!hdr)
3272 return -1;
3273
David S. Miller9360ffd2012-03-29 04:41:26 -04003274 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3275 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3276 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3277 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003278
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003279 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3280 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003281 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003282 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3283 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3284 sinfo->connected_time))
3285 goto nla_put_failure;
3286 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3287 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3288 sinfo->inactive_time))
3289 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003290 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3291 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003292 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003293 (u32)sinfo->rx_bytes))
3294 goto nla_put_failure;
3295 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3296 NL80211_STA_INFO_TX_BYTES64)) &&
3297 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3298 (u32)sinfo->tx_bytes))
3299 goto nla_put_failure;
3300 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3301 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003302 sinfo->rx_bytes))
3303 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003304 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3305 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003306 sinfo->tx_bytes))
3307 goto nla_put_failure;
3308 if ((sinfo->filled & STATION_INFO_LLID) &&
3309 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3310 goto nla_put_failure;
3311 if ((sinfo->filled & STATION_INFO_PLID) &&
3312 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3313 goto nla_put_failure;
3314 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3315 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3316 sinfo->plink_state))
3317 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003318 switch (rdev->wiphy.signal_type) {
3319 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003320 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3321 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3322 sinfo->signal))
3323 goto nla_put_failure;
3324 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3325 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3326 sinfo->signal_avg))
3327 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003328 break;
3329 default:
3330 break;
3331 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003332 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003333 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3334 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003335 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003336 }
3337 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3338 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3339 NL80211_STA_INFO_RX_BITRATE))
3340 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003341 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003342 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3343 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3344 sinfo->rx_packets))
3345 goto nla_put_failure;
3346 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3347 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3348 sinfo->tx_packets))
3349 goto nla_put_failure;
3350 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3351 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3352 sinfo->tx_retries))
3353 goto nla_put_failure;
3354 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3355 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3356 sinfo->tx_failed))
3357 goto nla_put_failure;
3358 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3359 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3360 sinfo->beacon_loss_count))
3361 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003362 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3363 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3364 sinfo->local_pm))
3365 goto nla_put_failure;
3366 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3367 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3368 sinfo->peer_pm))
3369 goto nla_put_failure;
3370 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3371 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3372 sinfo->nonpeer_pm))
3373 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003374 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3375 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3376 if (!bss_param)
3377 goto nla_put_failure;
3378
David S. Miller9360ffd2012-03-29 04:41:26 -04003379 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3380 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3381 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3382 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3383 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3384 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3385 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3386 sinfo->bss_param.dtim_period) ||
3387 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3388 sinfo->bss_param.beacon_interval))
3389 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003390
3391 nla_nest_end(msg, bss_param);
3392 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003393 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3394 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3395 sizeof(struct nl80211_sta_flag_update),
3396 &sinfo->sta_flags))
3397 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003398 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3399 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3400 sinfo->t_offset))
3401 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003402 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003403
David S. Miller9360ffd2012-03-29 04:41:26 -04003404 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3405 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3406 sinfo->assoc_req_ies))
3407 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003408
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003409 return genlmsg_end(msg, hdr);
3410
3411 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003412 genlmsg_cancel(msg, hdr);
3413 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003414}
3415
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003416static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003417 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003418{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003419 struct station_info sinfo;
3420 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003421 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003422 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003423 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003424 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003425
Johannes Berg67748892010-10-04 21:14:06 +02003426 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3427 if (err)
3428 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003429
3430 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003431 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003432 goto out_err;
3433 }
3434
Johannes Bergbba95fe2008-07-29 13:22:51 +02003435 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003436 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003437 err = rdev_dump_station(dev, netdev, sta_idx,
3438 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003439 if (err == -ENOENT)
3440 break;
3441 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003442 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003443
3444 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003445 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003446 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003447 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003448 &sinfo) < 0)
3449 goto out;
3450
3451 sta_idx++;
3452 }
3453
3454
3455 out:
3456 cb->args[1] = sta_idx;
3457 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003458 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003459 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003460
3461 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003462}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003463
Johannes Berg5727ef12007-12-19 02:03:34 +01003464static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3465{
Johannes Berg4c476992010-10-04 21:36:35 +02003466 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3467 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003468 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003469 struct sk_buff *msg;
3470 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003471 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003472
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003473 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003474
3475 if (!info->attrs[NL80211_ATTR_MAC])
3476 return -EINVAL;
3477
3478 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3479
Johannes Berg4c476992010-10-04 21:36:35 +02003480 if (!rdev->ops->get_station)
3481 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003482
Hila Gonene35e4d22012-06-27 17:19:42 +03003483 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003484 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003485 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003486
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003487 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003488 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003489 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003490
Eric W. Biederman15e47302012-09-07 20:12:54 +00003491 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003492 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003493 nlmsg_free(msg);
3494 return -ENOBUFS;
3495 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003496
Johannes Berg4c476992010-10-04 21:36:35 +02003497 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003498}
3499
Johannes Berg77ee7c82013-02-15 00:48:33 +01003500int cfg80211_check_station_change(struct wiphy *wiphy,
3501 struct station_parameters *params,
3502 enum cfg80211_station_type statype)
3503{
3504 if (params->listen_interval != -1)
3505 return -EINVAL;
3506 if (params->aid)
3507 return -EINVAL;
3508
3509 /* When you run into this, adjust the code below for the new flag */
3510 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3511
3512 switch (statype) {
3513 case CFG80211_STA_MESH_PEER_NONSEC:
3514 case CFG80211_STA_MESH_PEER_SECURE:
3515 /*
3516 * No ignoring the TDLS flag here -- the userspace mesh
3517 * code doesn't have the bug of including TDLS in the
3518 * mask everywhere.
3519 */
3520 if (params->sta_flags_mask &
3521 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3522 BIT(NL80211_STA_FLAG_MFP) |
3523 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3524 return -EINVAL;
3525 break;
3526 case CFG80211_STA_TDLS_PEER_SETUP:
3527 case CFG80211_STA_TDLS_PEER_ACTIVE:
3528 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3529 return -EINVAL;
3530 /* ignore since it can't change */
3531 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3532 break;
3533 default:
3534 /* disallow mesh-specific things */
3535 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3536 return -EINVAL;
3537 if (params->local_pm)
3538 return -EINVAL;
3539 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3540 return -EINVAL;
3541 }
3542
3543 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3544 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3545 /* TDLS can't be set, ... */
3546 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3547 return -EINVAL;
3548 /*
3549 * ... but don't bother the driver with it. This works around
3550 * a hostapd/wpa_supplicant issue -- it always includes the
3551 * TLDS_PEER flag in the mask even for AP mode.
3552 */
3553 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3554 }
3555
3556 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3557 /* reject other things that can't change */
3558 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3559 return -EINVAL;
3560 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3561 return -EINVAL;
3562 if (params->supported_rates)
3563 return -EINVAL;
3564 if (params->ext_capab || params->ht_capa || params->vht_capa)
3565 return -EINVAL;
3566 }
3567
3568 if (statype != CFG80211_STA_AP_CLIENT) {
3569 if (params->vlan)
3570 return -EINVAL;
3571 }
3572
3573 switch (statype) {
3574 case CFG80211_STA_AP_MLME_CLIENT:
3575 /* Use this only for authorizing/unauthorizing a station */
3576 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3577 return -EOPNOTSUPP;
3578 break;
3579 case CFG80211_STA_AP_CLIENT:
3580 /* accept only the listed bits */
3581 if (params->sta_flags_mask &
3582 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3583 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3584 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3585 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3586 BIT(NL80211_STA_FLAG_WME) |
3587 BIT(NL80211_STA_FLAG_MFP)))
3588 return -EINVAL;
3589
3590 /* but authenticated/associated only if driver handles it */
3591 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3592 params->sta_flags_mask &
3593 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3594 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3595 return -EINVAL;
3596 break;
3597 case CFG80211_STA_IBSS:
3598 case CFG80211_STA_AP_STA:
3599 /* reject any changes other than AUTHORIZED */
3600 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3601 return -EINVAL;
3602 break;
3603 case CFG80211_STA_TDLS_PEER_SETUP:
3604 /* reject any changes other than AUTHORIZED or WME */
3605 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3606 BIT(NL80211_STA_FLAG_WME)))
3607 return -EINVAL;
3608 /* force (at least) rates when authorizing */
3609 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3610 !params->supported_rates)
3611 return -EINVAL;
3612 break;
3613 case CFG80211_STA_TDLS_PEER_ACTIVE:
3614 /* reject any changes */
3615 return -EINVAL;
3616 case CFG80211_STA_MESH_PEER_NONSEC:
3617 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3618 return -EINVAL;
3619 break;
3620 case CFG80211_STA_MESH_PEER_SECURE:
3621 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3622 return -EINVAL;
3623 break;
3624 }
3625
3626 return 0;
3627}
3628EXPORT_SYMBOL(cfg80211_check_station_change);
3629
Johannes Berg5727ef12007-12-19 02:03:34 +01003630/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003631 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003632 */
Johannes Berg80b99892011-11-18 16:23:01 +01003633static struct net_device *get_vlan(struct genl_info *info,
3634 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003635{
Johannes Berg463d0182009-07-14 00:33:35 +02003636 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003637 struct net_device *v;
3638 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003639
Johannes Berg80b99892011-11-18 16:23:01 +01003640 if (!vlanattr)
3641 return NULL;
3642
3643 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3644 if (!v)
3645 return ERR_PTR(-ENODEV);
3646
3647 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3648 ret = -EINVAL;
3649 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003650 }
Johannes Berg80b99892011-11-18 16:23:01 +01003651
Johannes Berg77ee7c82013-02-15 00:48:33 +01003652 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3653 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3654 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3655 ret = -EINVAL;
3656 goto error;
3657 }
3658
Johannes Berg80b99892011-11-18 16:23:01 +01003659 if (!netif_running(v)) {
3660 ret = -ENETDOWN;
3661 goto error;
3662 }
3663
3664 return v;
3665 error:
3666 dev_put(v);
3667 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003668}
3669
Jouni Malinendf881292013-02-14 21:10:54 +02003670static struct nla_policy
3671nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3672 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3673 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3674};
3675
Johannes Bergff276692013-02-15 00:09:01 +01003676static int nl80211_parse_sta_wme(struct genl_info *info,
3677 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003678{
Jouni Malinendf881292013-02-14 21:10:54 +02003679 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3680 struct nlattr *nla;
3681 int err;
3682
Jouni Malinendf881292013-02-14 21:10:54 +02003683 /* parse WME attributes if present */
3684 if (!info->attrs[NL80211_ATTR_STA_WME])
3685 return 0;
3686
3687 nla = info->attrs[NL80211_ATTR_STA_WME];
3688 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3689 nl80211_sta_wme_policy);
3690 if (err)
3691 return err;
3692
3693 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3694 params->uapsd_queues = nla_get_u8(
3695 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3696 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3697 return -EINVAL;
3698
3699 if (tb[NL80211_STA_WME_MAX_SP])
3700 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3701
3702 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3703 return -EINVAL;
3704
3705 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3706
3707 return 0;
3708}
3709
Johannes Bergff276692013-02-15 00:09:01 +01003710static int nl80211_set_station_tdls(struct genl_info *info,
3711 struct station_parameters *params)
3712{
3713 /* Dummy STA entry gets updated once the peer capabilities are known */
3714 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3715 params->ht_capa =
3716 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3717 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3718 params->vht_capa =
3719 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3720
3721 return nl80211_parse_sta_wme(info, params);
3722}
3723
Johannes Berg5727ef12007-12-19 02:03:34 +01003724static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3725{
Johannes Berg4c476992010-10-04 21:36:35 +02003726 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003727 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003728 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003729 u8 *mac_addr;
3730 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003731
3732 memset(&params, 0, sizeof(params));
3733
3734 params.listen_interval = -1;
3735
Johannes Berg77ee7c82013-02-15 00:48:33 +01003736 if (!rdev->ops->change_station)
3737 return -EOPNOTSUPP;
3738
Johannes Berg5727ef12007-12-19 02:03:34 +01003739 if (info->attrs[NL80211_ATTR_STA_AID])
3740 return -EINVAL;
3741
3742 if (!info->attrs[NL80211_ATTR_MAC])
3743 return -EINVAL;
3744
3745 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3746
3747 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3748 params.supported_rates =
3749 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3750 params.supported_rates_len =
3751 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3752 }
3753
Jouni Malinen9d62a982013-02-14 21:10:13 +02003754 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3755 params.capability =
3756 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3757 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3758 }
3759
3760 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3761 params.ext_capab =
3762 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3763 params.ext_capab_len =
3764 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3765 }
3766
Jouni Malinendf881292013-02-14 21:10:54 +02003767 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003768 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03003769
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003770 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003771 return -EINVAL;
3772
Johannes Bergf8bacc22013-02-14 23:27:01 +01003773 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003774 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003775 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3776 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3777 return -EINVAL;
3778 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003779
Johannes Bergf8bacc22013-02-14 23:27:01 +01003780 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003781 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003782 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3783 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3784 return -EINVAL;
3785 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3786 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003787
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003788 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3789 enum nl80211_mesh_power_mode pm = nla_get_u32(
3790 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3791
3792 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3793 pm > NL80211_MESH_POWER_MAX)
3794 return -EINVAL;
3795
3796 params.local_pm = pm;
3797 }
3798
Johannes Berg77ee7c82013-02-15 00:48:33 +01003799 /* Include parameters for TDLS peer (will check later) */
3800 err = nl80211_set_station_tdls(info, &params);
3801 if (err)
3802 return err;
3803
3804 params.vlan = get_vlan(info, rdev);
3805 if (IS_ERR(params.vlan))
3806 return PTR_ERR(params.vlan);
3807
Johannes Berga97f4422009-06-18 17:23:43 +02003808 switch (dev->ieee80211_ptr->iftype) {
3809 case NL80211_IFTYPE_AP:
3810 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003811 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003812 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003813 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003814 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003815 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003816 break;
3817 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003818 err = -EOPNOTSUPP;
3819 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003820 }
3821
Johannes Berg77ee7c82013-02-15 00:48:33 +01003822 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003823 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003824
Johannes Berg77ee7c82013-02-15 00:48:33 +01003825 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003826 if (params.vlan)
3827 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003828
Johannes Berg5727ef12007-12-19 02:03:34 +01003829 return err;
3830}
3831
3832static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3833{
Johannes Berg4c476992010-10-04 21:36:35 +02003834 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003835 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003836 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003837 struct station_parameters params;
3838 u8 *mac_addr = NULL;
3839
3840 memset(&params, 0, sizeof(params));
3841
Johannes Berg984c3112013-02-14 23:43:25 +01003842 if (!rdev->ops->add_station)
3843 return -EOPNOTSUPP;
3844
Johannes Berg5727ef12007-12-19 02:03:34 +01003845 if (!info->attrs[NL80211_ATTR_MAC])
3846 return -EINVAL;
3847
Johannes Berg5727ef12007-12-19 02:03:34 +01003848 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3849 return -EINVAL;
3850
3851 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3852 return -EINVAL;
3853
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003854 if (!info->attrs[NL80211_ATTR_STA_AID])
3855 return -EINVAL;
3856
Johannes Berg5727ef12007-12-19 02:03:34 +01003857 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3858 params.supported_rates =
3859 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3860 params.supported_rates_len =
3861 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3862 params.listen_interval =
3863 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003864
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003865 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3866 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3867 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003868
Jouni Malinen9d62a982013-02-14 21:10:13 +02003869 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3870 params.capability =
3871 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3872 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3873 }
3874
3875 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3876 params.ext_capab =
3877 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3878 params.ext_capab_len =
3879 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3880 }
3881
Jouni Malinen36aedc902008-08-25 11:58:58 +03003882 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3883 params.ht_capa =
3884 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003885
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003886 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3887 params.vht_capa =
3888 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3889
Johannes Bergf8bacc22013-02-14 23:27:01 +01003890 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07003891 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003892 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3893 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3894 return -EINVAL;
3895 }
Javier Cardona96b78df2011-04-07 15:08:33 -07003896
Johannes Bergff276692013-02-15 00:09:01 +01003897 err = nl80211_parse_sta_wme(info, &params);
3898 if (err)
3899 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003900
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003901 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003902 return -EINVAL;
3903
Johannes Berg77ee7c82013-02-15 00:48:33 +01003904 /* When you run into this, adjust the code below for the new flag */
3905 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3906
Johannes Bergbdd90d52011-12-14 12:20:27 +01003907 switch (dev->ieee80211_ptr->iftype) {
3908 case NL80211_IFTYPE_AP:
3909 case NL80211_IFTYPE_AP_VLAN:
3910 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01003911 /* ignore WME attributes if iface/sta is not capable */
3912 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
3913 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
3914 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003915
Johannes Bergbdd90d52011-12-14 12:20:27 +01003916 /* TDLS peers cannot be added */
3917 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003918 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003919 /* but don't bother the driver with it */
3920 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003921
Johannes Bergd582cff2012-10-26 17:53:44 +02003922 /* allow authenticated/associated only if driver handles it */
3923 if (!(rdev->wiphy.features &
3924 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3925 params.sta_flags_mask &
3926 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3927 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3928 return -EINVAL;
3929
Johannes Bergbdd90d52011-12-14 12:20:27 +01003930 /* must be last in here for error handling */
3931 params.vlan = get_vlan(info, rdev);
3932 if (IS_ERR(params.vlan))
3933 return PTR_ERR(params.vlan);
3934 break;
3935 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01003936 /* ignore uAPSD data */
3937 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
3938
Johannes Bergd582cff2012-10-26 17:53:44 +02003939 /* associated is disallowed */
3940 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3941 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003942 /* TDLS peers cannot be added */
3943 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003944 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003945 break;
3946 case NL80211_IFTYPE_STATION:
Johannes Berg984c3112013-02-14 23:43:25 +01003947 /* ignore uAPSD data */
3948 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
3949
Johannes Berg77ee7c82013-02-15 00:48:33 +01003950 /* these are disallowed */
3951 if (params.sta_flags_mask &
3952 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
3953 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02003954 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003955 /* Only TDLS peers can be added */
3956 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3957 return -EINVAL;
3958 /* Can only add if TDLS ... */
3959 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3960 return -EOPNOTSUPP;
3961 /* ... with external setup is supported */
3962 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3963 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003964 /*
3965 * Older wpa_supplicant versions always mark the TDLS peer
3966 * as authorized, but it shouldn't yet be.
3967 */
3968 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01003969 break;
3970 default:
3971 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003972 }
3973
Johannes Bergbdd90d52011-12-14 12:20:27 +01003974 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003975
Hila Gonene35e4d22012-06-27 17:19:42 +03003976 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003977
Johannes Berg5727ef12007-12-19 02:03:34 +01003978 if (params.vlan)
3979 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003980 return err;
3981}
3982
3983static int nl80211_del_station(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];
Johannes Berg5727ef12007-12-19 02:03:34 +01003987 u8 *mac_addr = NULL;
3988
3989 if (info->attrs[NL80211_ATTR_MAC])
3990 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3991
Johannes Berge80cf852009-05-11 14:43:13 +02003992 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003993 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003994 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003995 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3996 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003997
Johannes Berg4c476992010-10-04 21:36:35 +02003998 if (!rdev->ops->del_station)
3999 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004000
Hila Gonene35e4d22012-06-27 17:19:42 +03004001 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004002}
4003
Eric W. Biederman15e47302012-09-07 20:12:54 +00004004static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004005 int flags, struct net_device *dev,
4006 u8 *dst, u8 *next_hop,
4007 struct mpath_info *pinfo)
4008{
4009 void *hdr;
4010 struct nlattr *pinfoattr;
4011
Eric W. Biederman15e47302012-09-07 20:12:54 +00004012 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004013 if (!hdr)
4014 return -1;
4015
David S. Miller9360ffd2012-03-29 04:41:26 -04004016 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4017 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4018 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4019 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4020 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004021
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004022 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4023 if (!pinfoattr)
4024 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004025 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4026 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4027 pinfo->frame_qlen))
4028 goto nla_put_failure;
4029 if (((pinfo->filled & MPATH_INFO_SN) &&
4030 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4031 ((pinfo->filled & MPATH_INFO_METRIC) &&
4032 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4033 pinfo->metric)) ||
4034 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4035 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4036 pinfo->exptime)) ||
4037 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4038 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4039 pinfo->flags)) ||
4040 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4041 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4042 pinfo->discovery_timeout)) ||
4043 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4044 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4045 pinfo->discovery_retries)))
4046 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004047
4048 nla_nest_end(msg, pinfoattr);
4049
4050 return genlmsg_end(msg, hdr);
4051
4052 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004053 genlmsg_cancel(msg, hdr);
4054 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004055}
4056
4057static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004058 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004059{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004060 struct mpath_info pinfo;
4061 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004062 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004063 u8 dst[ETH_ALEN];
4064 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02004065 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004066 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004067
Johannes Berg67748892010-10-04 21:14:06 +02004068 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4069 if (err)
4070 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004071
4072 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004073 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004074 goto out_err;
4075 }
4076
Jouni Malineneec60b02009-03-20 21:21:19 +02004077 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
4078 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004079 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004080 }
4081
Johannes Bergbba95fe2008-07-29 13:22:51 +02004082 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03004083 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
4084 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004085 if (err == -ENOENT)
4086 break;
4087 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004088 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004089
Eric W. Biederman15e47302012-09-07 20:12:54 +00004090 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004091 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4092 netdev, dst, next_hop,
4093 &pinfo) < 0)
4094 goto out;
4095
4096 path_idx++;
4097 }
4098
4099
4100 out:
4101 cb->args[1] = path_idx;
4102 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004103 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004104 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004105 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004106}
4107
4108static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4109{
Johannes Berg4c476992010-10-04 21:36:35 +02004110 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004111 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004112 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004113 struct mpath_info pinfo;
4114 struct sk_buff *msg;
4115 u8 *dst = NULL;
4116 u8 next_hop[ETH_ALEN];
4117
4118 memset(&pinfo, 0, sizeof(pinfo));
4119
4120 if (!info->attrs[NL80211_ATTR_MAC])
4121 return -EINVAL;
4122
4123 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4124
Johannes Berg4c476992010-10-04 21:36:35 +02004125 if (!rdev->ops->get_mpath)
4126 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004127
Johannes Berg4c476992010-10-04 21:36:35 +02004128 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4129 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004130
Hila Gonene35e4d22012-06-27 17:19:42 +03004131 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004132 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004133 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004134
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004135 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004136 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004137 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004138
Eric W. Biederman15e47302012-09-07 20:12:54 +00004139 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004140 dev, dst, next_hop, &pinfo) < 0) {
4141 nlmsg_free(msg);
4142 return -ENOBUFS;
4143 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004144
Johannes Berg4c476992010-10-04 21:36:35 +02004145 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004146}
4147
4148static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4149{
Johannes Berg4c476992010-10-04 21:36:35 +02004150 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4151 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004152 u8 *dst = NULL;
4153 u8 *next_hop = NULL;
4154
4155 if (!info->attrs[NL80211_ATTR_MAC])
4156 return -EINVAL;
4157
4158 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4159 return -EINVAL;
4160
4161 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4162 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4163
Johannes Berg4c476992010-10-04 21:36:35 +02004164 if (!rdev->ops->change_mpath)
4165 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004166
Johannes Berg4c476992010-10-04 21:36:35 +02004167 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4168 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004169
Hila Gonene35e4d22012-06-27 17:19:42 +03004170 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004171}
Johannes Berg4c476992010-10-04 21:36:35 +02004172
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004173static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4174{
Johannes Berg4c476992010-10-04 21:36:35 +02004175 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4176 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004177 u8 *dst = NULL;
4178 u8 *next_hop = NULL;
4179
4180 if (!info->attrs[NL80211_ATTR_MAC])
4181 return -EINVAL;
4182
4183 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4184 return -EINVAL;
4185
4186 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4187 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4188
Johannes Berg4c476992010-10-04 21:36:35 +02004189 if (!rdev->ops->add_mpath)
4190 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004191
Johannes Berg4c476992010-10-04 21:36:35 +02004192 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4193 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004194
Hila Gonene35e4d22012-06-27 17:19:42 +03004195 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004196}
4197
4198static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4199{
Johannes Berg4c476992010-10-04 21:36:35 +02004200 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4201 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004202 u8 *dst = NULL;
4203
4204 if (info->attrs[NL80211_ATTR_MAC])
4205 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4206
Johannes Berg4c476992010-10-04 21:36:35 +02004207 if (!rdev->ops->del_mpath)
4208 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004209
Hila Gonene35e4d22012-06-27 17:19:42 +03004210 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004211}
4212
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004213static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4214{
Johannes Berg4c476992010-10-04 21:36:35 +02004215 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4216 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004217 struct bss_parameters params;
4218
4219 memset(&params, 0, sizeof(params));
4220 /* default to not changing parameters */
4221 params.use_cts_prot = -1;
4222 params.use_short_preamble = -1;
4223 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004224 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004225 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004226 params.p2p_ctwindow = -1;
4227 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004228
4229 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4230 params.use_cts_prot =
4231 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4232 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4233 params.use_short_preamble =
4234 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4235 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4236 params.use_short_slot_time =
4237 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004238 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4239 params.basic_rates =
4240 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4241 params.basic_rates_len =
4242 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4243 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004244 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4245 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004246 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4247 params.ht_opmode =
4248 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004249
Johannes Berg53cabad2012-11-14 15:17:28 +01004250 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4251 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4252 return -EINVAL;
4253 params.p2p_ctwindow =
4254 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4255 if (params.p2p_ctwindow < 0)
4256 return -EINVAL;
4257 if (params.p2p_ctwindow != 0 &&
4258 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4259 return -EINVAL;
4260 }
4261
4262 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4263 u8 tmp;
4264
4265 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4266 return -EINVAL;
4267 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4268 if (tmp > 1)
4269 return -EINVAL;
4270 params.p2p_opp_ps = tmp;
4271 if (params.p2p_opp_ps &&
4272 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4273 return -EINVAL;
4274 }
4275
Johannes Berg4c476992010-10-04 21:36:35 +02004276 if (!rdev->ops->change_bss)
4277 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004278
Johannes Berg074ac8d2010-09-16 14:58:22 +02004279 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004280 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4281 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004282
Hila Gonene35e4d22012-06-27 17:19:42 +03004283 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004284}
4285
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004286static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004287 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4288 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4289 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4290 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4291 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4292 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4293};
4294
4295static int parse_reg_rule(struct nlattr *tb[],
4296 struct ieee80211_reg_rule *reg_rule)
4297{
4298 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4299 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4300
4301 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4302 return -EINVAL;
4303 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4304 return -EINVAL;
4305 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4306 return -EINVAL;
4307 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4308 return -EINVAL;
4309 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4310 return -EINVAL;
4311
4312 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4313
4314 freq_range->start_freq_khz =
4315 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4316 freq_range->end_freq_khz =
4317 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4318 freq_range->max_bandwidth_khz =
4319 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4320
4321 power_rule->max_eirp =
4322 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4323
4324 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4325 power_rule->max_antenna_gain =
4326 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4327
4328 return 0;
4329}
4330
4331static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4332{
4333 int r;
4334 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004335 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004336
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004337 /*
4338 * You should only get this when cfg80211 hasn't yet initialized
4339 * completely when built-in to the kernel right between the time
4340 * window between nl80211_init() and regulatory_init(), if that is
4341 * even possible.
4342 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004343 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004344 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004345
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004346 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4347 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004348
4349 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4350
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004351 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4352 user_reg_hint_type =
4353 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4354 else
4355 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4356
4357 switch (user_reg_hint_type) {
4358 case NL80211_USER_REG_HINT_USER:
4359 case NL80211_USER_REG_HINT_CELL_BASE:
4360 break;
4361 default:
4362 return -EINVAL;
4363 }
4364
4365 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004366
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004367 return r;
4368}
4369
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004370static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004371 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004372{
Johannes Berg4c476992010-10-04 21:36:35 +02004373 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004374 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004375 struct wireless_dev *wdev = dev->ieee80211_ptr;
4376 struct mesh_config cur_params;
4377 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004378 void *hdr;
4379 struct nlattr *pinfoattr;
4380 struct sk_buff *msg;
4381
Johannes Berg29cbe682010-12-03 09:20:44 +01004382 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4383 return -EOPNOTSUPP;
4384
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004385 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004386 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004387
Johannes Berg29cbe682010-12-03 09:20:44 +01004388 wdev_lock(wdev);
4389 /* If not connected, get default parameters */
4390 if (!wdev->mesh_id_len)
4391 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4392 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004393 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004394 wdev_unlock(wdev);
4395
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004396 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004397 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004398
4399 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004400 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004401 if (!msg)
4402 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004403 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004404 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004405 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004406 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004407 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004408 if (!pinfoattr)
4409 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004410 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4411 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4412 cur_params.dot11MeshRetryTimeout) ||
4413 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4414 cur_params.dot11MeshConfirmTimeout) ||
4415 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4416 cur_params.dot11MeshHoldingTimeout) ||
4417 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4418 cur_params.dot11MeshMaxPeerLinks) ||
4419 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4420 cur_params.dot11MeshMaxRetries) ||
4421 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4422 cur_params.dot11MeshTTL) ||
4423 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4424 cur_params.element_ttl) ||
4425 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4426 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004427 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4428 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004429 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4430 cur_params.dot11MeshHWMPmaxPREQretries) ||
4431 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4432 cur_params.path_refresh_time) ||
4433 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4434 cur_params.min_discovery_timeout) ||
4435 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4436 cur_params.dot11MeshHWMPactivePathTimeout) ||
4437 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4438 cur_params.dot11MeshHWMPpreqMinInterval) ||
4439 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4440 cur_params.dot11MeshHWMPperrMinInterval) ||
4441 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4442 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4443 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4444 cur_params.dot11MeshHWMPRootMode) ||
4445 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4446 cur_params.dot11MeshHWMPRannInterval) ||
4447 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4448 cur_params.dot11MeshGateAnnouncementProtocol) ||
4449 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4450 cur_params.dot11MeshForwarding) ||
4451 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004452 cur_params.rssi_threshold) ||
4453 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004454 cur_params.ht_opmode) ||
4455 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4456 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4457 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004458 cur_params.dot11MeshHWMProotInterval) ||
4459 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004460 cur_params.dot11MeshHWMPconfirmationInterval) ||
4461 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4462 cur_params.power_mode) ||
4463 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4464 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004465 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004466 nla_nest_end(msg, pinfoattr);
4467 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004468 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004469
Johannes Berg3b858752009-03-12 09:55:09 +01004470 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004471 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004472 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004473 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004474 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004475}
4476
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004477static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004478 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4479 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4480 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4481 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4482 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4483 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004484 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004485 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004486 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004487 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4488 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4489 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4490 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4491 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004492 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004493 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004494 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004495 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004496 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004497 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004498 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4499 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004500 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4501 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004502 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004503 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4504 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004505};
4506
Javier Cardonac80d5452010-12-16 17:37:49 -08004507static const struct nla_policy
4508 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004509 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004510 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4511 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004512 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004513 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004514 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004515 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004516};
4517
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004518static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004519 struct mesh_config *cfg,
4520 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004521{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004522 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004523 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004524
Marco Porschea54fba2013-01-07 16:04:48 +01004525#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4526do { \
4527 if (tb[attr]) { \
4528 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4529 return -EINVAL; \
4530 cfg->param = fn(tb[attr]); \
4531 mask |= (1 << (attr - 1)); \
4532 } \
4533} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004534
4535
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004536 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004537 return -EINVAL;
4538 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004539 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004540 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004541 return -EINVAL;
4542
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004543 /* This makes sure that there aren't more than 32 mesh config
4544 * parameters (otherwise our bitfield scheme would not work.) */
4545 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4546
4547 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004548 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004549 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4550 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004551 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004552 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4553 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004554 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004555 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4556 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004557 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004558 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4559 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004560 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004561 mask, NL80211_MESHCONF_MAX_RETRIES,
4562 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004563 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004564 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004565 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004566 mask, NL80211_MESHCONF_ELEMENT_TTL,
4567 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004568 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004569 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4570 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004571 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4572 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004573 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4574 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004575 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004576 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4577 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004578 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004579 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4580 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004581 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004582 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4583 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004584 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4585 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004586 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4587 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004588 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004589 1, 65535, mask,
4590 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004591 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004592 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004593 1, 65535, mask,
4594 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004595 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004596 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004597 dot11MeshHWMPnetDiameterTraversalTime,
4598 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004599 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4600 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004601 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4602 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4603 nla_get_u8);
4604 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4605 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004606 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004607 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004608 dot11MeshGateAnnouncementProtocol, 0, 1,
4609 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004610 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004611 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004612 mask, NL80211_MESHCONF_FORWARDING,
4613 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004614 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004615 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4616 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004617 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004618 mask, NL80211_MESHCONF_HT_OPMODE,
4619 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004620 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004621 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004622 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4623 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004624 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004625 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4626 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004627 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004628 dot11MeshHWMPconfirmationInterval,
4629 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004630 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4631 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004632 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4633 NL80211_MESH_POWER_ACTIVE,
4634 NL80211_MESH_POWER_MAX,
4635 mask, NL80211_MESHCONF_POWER_MODE,
4636 nla_get_u32);
4637 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4638 0, 65535, mask,
4639 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004640 if (mask_out)
4641 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004642
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004643 return 0;
4644
4645#undef FILL_IN_MESH_PARAM_IF_SET
4646}
4647
Javier Cardonac80d5452010-12-16 17:37:49 -08004648static int nl80211_parse_mesh_setup(struct genl_info *info,
4649 struct mesh_setup *setup)
4650{
4651 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4652
4653 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4654 return -EINVAL;
4655 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4656 info->attrs[NL80211_ATTR_MESH_SETUP],
4657 nl80211_mesh_setup_params_policy))
4658 return -EINVAL;
4659
Javier Cardonad299a1f2012-03-31 11:31:33 -07004660 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4661 setup->sync_method =
4662 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4663 IEEE80211_SYNC_METHOD_VENDOR :
4664 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4665
Javier Cardonac80d5452010-12-16 17:37:49 -08004666 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4667 setup->path_sel_proto =
4668 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4669 IEEE80211_PATH_PROTOCOL_VENDOR :
4670 IEEE80211_PATH_PROTOCOL_HWMP;
4671
4672 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4673 setup->path_metric =
4674 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4675 IEEE80211_PATH_METRIC_VENDOR :
4676 IEEE80211_PATH_METRIC_AIRTIME;
4677
Javier Cardona581a8b02011-04-07 15:08:27 -07004678
4679 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004680 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004681 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004682 if (!is_valid_ie_attr(ieattr))
4683 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004684 setup->ie = nla_data(ieattr);
4685 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004686 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004687 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4688 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004689
4690 return 0;
4691}
4692
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004693static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004694 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004695{
4696 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4697 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004698 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004699 struct mesh_config cfg;
4700 u32 mask;
4701 int err;
4702
Johannes Berg29cbe682010-12-03 09:20:44 +01004703 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4704 return -EOPNOTSUPP;
4705
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004706 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004707 return -EOPNOTSUPP;
4708
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004709 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004710 if (err)
4711 return err;
4712
Johannes Berg29cbe682010-12-03 09:20:44 +01004713 wdev_lock(wdev);
4714 if (!wdev->mesh_id_len)
4715 err = -ENOLINK;
4716
4717 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004718 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004719
4720 wdev_unlock(wdev);
4721
4722 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004723}
4724
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004725static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4726{
Johannes Berg458f4f92012-12-06 15:47:38 +01004727 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004728 struct sk_buff *msg;
4729 void *hdr = NULL;
4730 struct nlattr *nl_reg_rules;
4731 unsigned int i;
4732 int err = -EINVAL;
4733
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004734 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004735
4736 if (!cfg80211_regdomain)
4737 goto out;
4738
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004739 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004740 if (!msg) {
4741 err = -ENOBUFS;
4742 goto out;
4743 }
4744
Eric W. Biederman15e47302012-09-07 20:12:54 +00004745 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004746 NL80211_CMD_GET_REG);
4747 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004748 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004749
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004750 if (reg_last_request_cell_base() &&
4751 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4752 NL80211_USER_REG_HINT_CELL_BASE))
4753 goto nla_put_failure;
4754
Johannes Berg458f4f92012-12-06 15:47:38 +01004755 rcu_read_lock();
4756 regdom = rcu_dereference(cfg80211_regdomain);
4757
4758 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4759 (regdom->dfs_region &&
4760 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4761 goto nla_put_failure_rcu;
4762
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004763 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4764 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004765 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004766
Johannes Berg458f4f92012-12-06 15:47:38 +01004767 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004768 struct nlattr *nl_reg_rule;
4769 const struct ieee80211_reg_rule *reg_rule;
4770 const struct ieee80211_freq_range *freq_range;
4771 const struct ieee80211_power_rule *power_rule;
4772
Johannes Berg458f4f92012-12-06 15:47:38 +01004773 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004774 freq_range = &reg_rule->freq_range;
4775 power_rule = &reg_rule->power_rule;
4776
4777 nl_reg_rule = nla_nest_start(msg, i);
4778 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004779 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004780
David S. Miller9360ffd2012-03-29 04:41:26 -04004781 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4782 reg_rule->flags) ||
4783 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4784 freq_range->start_freq_khz) ||
4785 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4786 freq_range->end_freq_khz) ||
4787 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4788 freq_range->max_bandwidth_khz) ||
4789 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4790 power_rule->max_antenna_gain) ||
4791 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4792 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004793 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004794
4795 nla_nest_end(msg, nl_reg_rule);
4796 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004797 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004798
4799 nla_nest_end(msg, nl_reg_rules);
4800
4801 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004802 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004803 goto out;
4804
Johannes Berg458f4f92012-12-06 15:47:38 +01004805nla_put_failure_rcu:
4806 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004807nla_put_failure:
4808 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004809put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004810 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004811 err = -EMSGSIZE;
4812out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004813 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004814 return err;
4815}
4816
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004817static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4818{
4819 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4820 struct nlattr *nl_reg_rule;
4821 char *alpha2 = NULL;
4822 int rem_reg_rules = 0, r = 0;
4823 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004824 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004825 struct ieee80211_regdomain *rd = NULL;
4826
4827 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4828 return -EINVAL;
4829
4830 if (!info->attrs[NL80211_ATTR_REG_RULES])
4831 return -EINVAL;
4832
4833 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4834
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004835 if (info->attrs[NL80211_ATTR_DFS_REGION])
4836 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4837
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004838 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004839 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004840 num_rules++;
4841 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004842 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004843 }
4844
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004845 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004846 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004847
4848 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004849 if (!rd)
4850 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004851
4852 rd->n_reg_rules = num_rules;
4853 rd->alpha2[0] = alpha2[0];
4854 rd->alpha2[1] = alpha2[1];
4855
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004856 /*
4857 * Disable DFS master mode if the DFS region was
4858 * not supported or known on this kernel.
4859 */
4860 if (reg_supported_dfs_region(dfs_region))
4861 rd->dfs_region = dfs_region;
4862
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004863 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004864 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004865 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004866 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4867 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004868 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4869 if (r)
4870 goto bad_reg;
4871
4872 rule_idx++;
4873
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004874 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4875 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004876 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004877 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004878 }
4879
Johannes Berg6913b492012-12-04 00:48:59 +01004880 mutex_lock(&cfg80211_mutex);
4881
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004882 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004883 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004884 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004885 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004886
Johannes Bergd2372b32008-10-24 20:32:20 +02004887 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004888 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004889 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004890}
4891
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004892static int validate_scan_freqs(struct nlattr *freqs)
4893{
4894 struct nlattr *attr1, *attr2;
4895 int n_channels = 0, tmp1, tmp2;
4896
4897 nla_for_each_nested(attr1, freqs, tmp1) {
4898 n_channels++;
4899 /*
4900 * Some hardware has a limited channel list for
4901 * scanning, and it is pretty much nonsensical
4902 * to scan for a channel twice, so disallow that
4903 * and don't require drivers to check that the
4904 * channel list they get isn't longer than what
4905 * they can scan, as long as they can scan all
4906 * the channels they registered at once.
4907 */
4908 nla_for_each_nested(attr2, freqs, tmp2)
4909 if (attr1 != attr2 &&
4910 nla_get_u32(attr1) == nla_get_u32(attr2))
4911 return 0;
4912 }
4913
4914 return n_channels;
4915}
4916
Johannes Berg2a519312009-02-10 21:25:55 +01004917static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4918{
Johannes Berg4c476992010-10-04 21:36:35 +02004919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004920 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004921 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004922 struct nlattr *attr;
4923 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004924 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004925 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004926
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004927 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4928 return -EINVAL;
4929
Johannes Berg79c97e92009-07-07 03:56:12 +02004930 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004931
Johannes Berg4c476992010-10-04 21:36:35 +02004932 if (!rdev->ops->scan)
4933 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004934
Johannes Berg4c476992010-10-04 21:36:35 +02004935 if (rdev->scan_req)
4936 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004937
4938 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004939 n_channels = validate_scan_freqs(
4940 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004941 if (!n_channels)
4942 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004943 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004944 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004945 n_channels = 0;
4946
Johannes Berg2a519312009-02-10 21:25:55 +01004947 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4948 if (wiphy->bands[band])
4949 n_channels += wiphy->bands[band]->n_channels;
4950 }
4951
4952 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4953 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4954 n_ssids++;
4955
Johannes Berg4c476992010-10-04 21:36:35 +02004956 if (n_ssids > wiphy->max_scan_ssids)
4957 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004958
Jouni Malinen70692ad2009-02-16 19:39:13 +02004959 if (info->attrs[NL80211_ATTR_IE])
4960 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4961 else
4962 ie_len = 0;
4963
Johannes Berg4c476992010-10-04 21:36:35 +02004964 if (ie_len > wiphy->max_scan_ie_len)
4965 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004966
Johannes Berg2a519312009-02-10 21:25:55 +01004967 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004968 + sizeof(*request->ssids) * n_ssids
4969 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004970 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004971 if (!request)
4972 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004973
Johannes Berg2a519312009-02-10 21:25:55 +01004974 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004975 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004976 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004977 if (ie_len) {
4978 if (request->ssids)
4979 request->ie = (void *)(request->ssids + n_ssids);
4980 else
4981 request->ie = (void *)(request->channels + n_channels);
4982 }
Johannes Berg2a519312009-02-10 21:25:55 +01004983
Johannes Berg584991d2009-11-02 13:32:03 +01004984 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004985 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4986 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004987 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004988 struct ieee80211_channel *chan;
4989
4990 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4991
4992 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004993 err = -EINVAL;
4994 goto out_free;
4995 }
Johannes Berg584991d2009-11-02 13:32:03 +01004996
4997 /* ignore disabled channels */
4998 if (chan->flags & IEEE80211_CHAN_DISABLED)
4999 continue;
5000
5001 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005002 i++;
5003 }
5004 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005005 enum ieee80211_band band;
5006
Johannes Berg2a519312009-02-10 21:25:55 +01005007 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005008 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5009 int j;
5010 if (!wiphy->bands[band])
5011 continue;
5012 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005013 struct ieee80211_channel *chan;
5014
5015 chan = &wiphy->bands[band]->channels[j];
5016
5017 if (chan->flags & IEEE80211_CHAN_DISABLED)
5018 continue;
5019
5020 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005021 i++;
5022 }
5023 }
5024 }
5025
Johannes Berg584991d2009-11-02 13:32:03 +01005026 if (!i) {
5027 err = -EINVAL;
5028 goto out_free;
5029 }
5030
5031 request->n_channels = i;
5032
Johannes Berg2a519312009-02-10 21:25:55 +01005033 i = 0;
5034 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5035 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005036 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005037 err = -EINVAL;
5038 goto out_free;
5039 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005040 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005041 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005042 i++;
5043 }
5044 }
5045
Jouni Malinen70692ad2009-02-16 19:39:13 +02005046 if (info->attrs[NL80211_ATTR_IE]) {
5047 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005048 memcpy((void *)request->ie,
5049 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005050 request->ie_len);
5051 }
5052
Johannes Berg34850ab2011-07-18 18:08:35 +02005053 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005054 if (wiphy->bands[i])
5055 request->rates[i] =
5056 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005057
5058 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5059 nla_for_each_nested(attr,
5060 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5061 tmp) {
5062 enum ieee80211_band band = nla_type(attr);
5063
Dan Carpenter84404622011-07-29 11:52:18 +03005064 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005065 err = -EINVAL;
5066 goto out_free;
5067 }
5068 err = ieee80211_get_ratemask(wiphy->bands[band],
5069 nla_data(attr),
5070 nla_len(attr),
5071 &request->rates[band]);
5072 if (err)
5073 goto out_free;
5074 }
5075 }
5076
Sam Leffler46856bb2012-10-11 21:03:32 -07005077 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005078 request->flags = nla_get_u32(
5079 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005080 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5081 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5082 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5083 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005084 err = -EOPNOTSUPP;
5085 goto out_free;
5086 }
5087 }
Sam Lefflered4737712012-10-11 21:03:31 -07005088
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305089 request->no_cck =
5090 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5091
Johannes Bergfd014282012-06-18 19:17:03 +02005092 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005093 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005094 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005095
Johannes Berg79c97e92009-07-07 03:56:12 +02005096 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005097 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005098
Johannes Berg463d0182009-07-14 00:33:35 +02005099 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005100 nl80211_send_scan_start(rdev, wdev);
5101 if (wdev->netdev)
5102 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005103 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005104 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005105 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005106 kfree(request);
5107 }
Johannes Berg3b858752009-03-12 09:55:09 +01005108
Johannes Berg2a519312009-02-10 21:25:55 +01005109 return err;
5110}
5111
Luciano Coelho807f8a82011-05-11 17:09:35 +03005112static int nl80211_start_sched_scan(struct sk_buff *skb,
5113 struct genl_info *info)
5114{
5115 struct cfg80211_sched_scan_request *request;
5116 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5117 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005118 struct nlattr *attr;
5119 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005120 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005121 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005122 enum ieee80211_band band;
5123 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005124 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005125
5126 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5127 !rdev->ops->sched_scan_start)
5128 return -EOPNOTSUPP;
5129
5130 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5131 return -EINVAL;
5132
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005133 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5134 return -EINVAL;
5135
5136 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5137 if (interval == 0)
5138 return -EINVAL;
5139
Luciano Coelho807f8a82011-05-11 17:09:35 +03005140 wiphy = &rdev->wiphy;
5141
5142 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5143 n_channels = validate_scan_freqs(
5144 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5145 if (!n_channels)
5146 return -EINVAL;
5147 } else {
5148 n_channels = 0;
5149
5150 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5151 if (wiphy->bands[band])
5152 n_channels += wiphy->bands[band]->n_channels;
5153 }
5154
5155 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5156 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5157 tmp)
5158 n_ssids++;
5159
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005160 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005161 return -EINVAL;
5162
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005163 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5164 nla_for_each_nested(attr,
5165 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5166 tmp)
5167 n_match_sets++;
5168
5169 if (n_match_sets > wiphy->max_match_sets)
5170 return -EINVAL;
5171
Luciano Coelho807f8a82011-05-11 17:09:35 +03005172 if (info->attrs[NL80211_ATTR_IE])
5173 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5174 else
5175 ie_len = 0;
5176
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005177 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005178 return -EINVAL;
5179
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005180 mutex_lock(&rdev->sched_scan_mtx);
5181
5182 if (rdev->sched_scan_req) {
5183 err = -EINPROGRESS;
5184 goto out;
5185 }
5186
Luciano Coelho807f8a82011-05-11 17:09:35 +03005187 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005188 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005189 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005190 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005191 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005192 if (!request) {
5193 err = -ENOMEM;
5194 goto out;
5195 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005196
5197 if (n_ssids)
5198 request->ssids = (void *)&request->channels[n_channels];
5199 request->n_ssids = n_ssids;
5200 if (ie_len) {
5201 if (request->ssids)
5202 request->ie = (void *)(request->ssids + n_ssids);
5203 else
5204 request->ie = (void *)(request->channels + n_channels);
5205 }
5206
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005207 if (n_match_sets) {
5208 if (request->ie)
5209 request->match_sets = (void *)(request->ie + ie_len);
5210 else if (request->ssids)
5211 request->match_sets =
5212 (void *)(request->ssids + n_ssids);
5213 else
5214 request->match_sets =
5215 (void *)(request->channels + n_channels);
5216 }
5217 request->n_match_sets = n_match_sets;
5218
Luciano Coelho807f8a82011-05-11 17:09:35 +03005219 i = 0;
5220 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5221 /* user specified, bail out if channel not found */
5222 nla_for_each_nested(attr,
5223 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5224 tmp) {
5225 struct ieee80211_channel *chan;
5226
5227 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5228
5229 if (!chan) {
5230 err = -EINVAL;
5231 goto out_free;
5232 }
5233
5234 /* ignore disabled channels */
5235 if (chan->flags & IEEE80211_CHAN_DISABLED)
5236 continue;
5237
5238 request->channels[i] = chan;
5239 i++;
5240 }
5241 } else {
5242 /* all channels */
5243 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5244 int j;
5245 if (!wiphy->bands[band])
5246 continue;
5247 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5248 struct ieee80211_channel *chan;
5249
5250 chan = &wiphy->bands[band]->channels[j];
5251
5252 if (chan->flags & IEEE80211_CHAN_DISABLED)
5253 continue;
5254
5255 request->channels[i] = chan;
5256 i++;
5257 }
5258 }
5259 }
5260
5261 if (!i) {
5262 err = -EINVAL;
5263 goto out_free;
5264 }
5265
5266 request->n_channels = i;
5267
5268 i = 0;
5269 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5270 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5271 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005272 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005273 err = -EINVAL;
5274 goto out_free;
5275 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005276 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005277 memcpy(request->ssids[i].ssid, nla_data(attr),
5278 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005279 i++;
5280 }
5281 }
5282
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005283 i = 0;
5284 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5285 nla_for_each_nested(attr,
5286 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5287 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005288 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005289
5290 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5291 nla_data(attr), nla_len(attr),
5292 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005293 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005294 if (ssid) {
5295 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5296 err = -EINVAL;
5297 goto out_free;
5298 }
5299 memcpy(request->match_sets[i].ssid.ssid,
5300 nla_data(ssid), nla_len(ssid));
5301 request->match_sets[i].ssid.ssid_len =
5302 nla_len(ssid);
5303 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005304 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5305 if (rssi)
5306 request->rssi_thold = nla_get_u32(rssi);
5307 else
5308 request->rssi_thold =
5309 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005310 i++;
5311 }
5312 }
5313
Luciano Coelho807f8a82011-05-11 17:09:35 +03005314 if (info->attrs[NL80211_ATTR_IE]) {
5315 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5316 memcpy((void *)request->ie,
5317 nla_data(info->attrs[NL80211_ATTR_IE]),
5318 request->ie_len);
5319 }
5320
Sam Leffler46856bb2012-10-11 21:03:32 -07005321 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005322 request->flags = nla_get_u32(
5323 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005324 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5325 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5326 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5327 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005328 err = -EOPNOTSUPP;
5329 goto out_free;
5330 }
5331 }
Sam Lefflered4737712012-10-11 21:03:31 -07005332
Luciano Coelho807f8a82011-05-11 17:09:35 +03005333 request->dev = dev;
5334 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005335 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005336 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005337
Hila Gonene35e4d22012-06-27 17:19:42 +03005338 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005339 if (!err) {
5340 rdev->sched_scan_req = request;
5341 nl80211_send_sched_scan(rdev, dev,
5342 NL80211_CMD_START_SCHED_SCAN);
5343 goto out;
5344 }
5345
5346out_free:
5347 kfree(request);
5348out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005349 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005350 return err;
5351}
5352
5353static int nl80211_stop_sched_scan(struct sk_buff *skb,
5354 struct genl_info *info)
5355{
5356 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005357 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005358
5359 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5360 !rdev->ops->sched_scan_stop)
5361 return -EOPNOTSUPP;
5362
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005363 mutex_lock(&rdev->sched_scan_mtx);
5364 err = __cfg80211_stop_sched_scan(rdev, false);
5365 mutex_unlock(&rdev->sched_scan_mtx);
5366
5367 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005368}
5369
Simon Wunderlich04f39042013-02-08 18:16:19 +01005370static int nl80211_start_radar_detection(struct sk_buff *skb,
5371 struct genl_info *info)
5372{
5373 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5374 struct net_device *dev = info->user_ptr[1];
5375 struct wireless_dev *wdev = dev->ieee80211_ptr;
5376 struct cfg80211_chan_def chandef;
5377 int err;
5378
5379 err = nl80211_parse_chandef(rdev, info, &chandef);
5380 if (err)
5381 return err;
5382
5383 if (wdev->cac_started)
5384 return -EBUSY;
5385
5386 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5387 if (err < 0)
5388 return err;
5389
5390 if (err == 0)
5391 return -EINVAL;
5392
5393 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5394 return -EINVAL;
5395
5396 if (!rdev->ops->start_radar_detection)
5397 return -EOPNOTSUPP;
5398
5399 mutex_lock(&rdev->devlist_mtx);
5400 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5401 chandef.chan, CHAN_MODE_SHARED,
5402 BIT(chandef.width));
5403 if (err)
5404 goto err_locked;
5405
5406 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5407 if (!err) {
5408 wdev->channel = chandef.chan;
5409 wdev->cac_started = true;
5410 wdev->cac_start_time = jiffies;
5411 }
5412err_locked:
5413 mutex_unlock(&rdev->devlist_mtx);
5414
5415 return err;
5416}
5417
Johannes Berg9720bb32011-06-21 09:45:33 +02005418static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5419 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005420 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005421 struct wireless_dev *wdev,
5422 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005423{
Johannes Berg48ab9052009-07-10 18:42:31 +02005424 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005425 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005426 void *hdr;
5427 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005428 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005429
5430 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005431
Eric W. Biederman15e47302012-09-07 20:12:54 +00005432 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005433 NL80211_CMD_NEW_SCAN_RESULTS);
5434 if (!hdr)
5435 return -1;
5436
Johannes Berg9720bb32011-06-21 09:45:33 +02005437 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5438
David S. Miller9360ffd2012-03-29 04:41:26 -04005439 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5440 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5441 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005442
5443 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5444 if (!bss)
5445 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005446 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005447 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005448 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005449
5450 rcu_read_lock();
5451 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005452 if (ies) {
5453 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5454 goto fail_unlock_rcu;
5455 tsf = true;
5456 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5457 ies->len, ies->data))
5458 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005459 }
5460 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005461 if (ies) {
5462 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5463 goto fail_unlock_rcu;
5464 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5465 ies->len, ies->data))
5466 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005467 }
5468 rcu_read_unlock();
5469
David S. Miller9360ffd2012-03-29 04:41:26 -04005470 if (res->beacon_interval &&
5471 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5472 goto nla_put_failure;
5473 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5474 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5475 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5476 jiffies_to_msecs(jiffies - intbss->ts)))
5477 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005478
Johannes Berg77965c92009-02-18 18:45:06 +01005479 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005480 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005481 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5482 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005483 break;
5484 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005485 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5486 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005487 break;
5488 default:
5489 break;
5490 }
5491
Johannes Berg48ab9052009-07-10 18:42:31 +02005492 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005493 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005494 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005495 if (intbss == wdev->current_bss &&
5496 nla_put_u32(msg, NL80211_BSS_STATUS,
5497 NL80211_BSS_STATUS_ASSOCIATED))
5498 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005499 break;
5500 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005501 if (intbss == wdev->current_bss &&
5502 nla_put_u32(msg, NL80211_BSS_STATUS,
5503 NL80211_BSS_STATUS_IBSS_JOINED))
5504 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005505 break;
5506 default:
5507 break;
5508 }
5509
Johannes Berg2a519312009-02-10 21:25:55 +01005510 nla_nest_end(msg, bss);
5511
5512 return genlmsg_end(msg, hdr);
5513
Johannes Berg8cef2c92013-02-05 16:54:31 +01005514 fail_unlock_rcu:
5515 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005516 nla_put_failure:
5517 genlmsg_cancel(msg, hdr);
5518 return -EMSGSIZE;
5519}
5520
5521static int nl80211_dump_scan(struct sk_buff *skb,
5522 struct netlink_callback *cb)
5523{
Johannes Berg48ab9052009-07-10 18:42:31 +02005524 struct cfg80211_registered_device *rdev;
5525 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005526 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005527 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005528 int start = cb->args[1], idx = 0;
5529 int err;
5530
Johannes Berg67748892010-10-04 21:14:06 +02005531 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5532 if (err)
5533 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005534
Johannes Berg48ab9052009-07-10 18:42:31 +02005535 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005536
Johannes Berg48ab9052009-07-10 18:42:31 +02005537 wdev_lock(wdev);
5538 spin_lock_bh(&rdev->bss_lock);
5539 cfg80211_bss_expire(rdev);
5540
Johannes Berg9720bb32011-06-21 09:45:33 +02005541 cb->seq = rdev->bss_generation;
5542
Johannes Berg48ab9052009-07-10 18:42:31 +02005543 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005544 if (++idx <= start)
5545 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005546 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005547 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005548 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005549 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005550 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005551 }
5552 }
5553
Johannes Berg48ab9052009-07-10 18:42:31 +02005554 spin_unlock_bh(&rdev->bss_lock);
5555 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005556
5557 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005558 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005559
Johannes Berg67748892010-10-04 21:14:06 +02005560 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005561}
5562
Eric W. Biederman15e47302012-09-07 20:12:54 +00005563static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005564 int flags, struct net_device *dev,
5565 struct survey_info *survey)
5566{
5567 void *hdr;
5568 struct nlattr *infoattr;
5569
Eric W. Biederman15e47302012-09-07 20:12:54 +00005570 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005571 NL80211_CMD_NEW_SURVEY_RESULTS);
5572 if (!hdr)
5573 return -ENOMEM;
5574
David S. Miller9360ffd2012-03-29 04:41:26 -04005575 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5576 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005577
5578 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5579 if (!infoattr)
5580 goto nla_put_failure;
5581
David S. Miller9360ffd2012-03-29 04:41:26 -04005582 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5583 survey->channel->center_freq))
5584 goto nla_put_failure;
5585
5586 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5587 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5588 goto nla_put_failure;
5589 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5590 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5591 goto nla_put_failure;
5592 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5593 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5594 survey->channel_time))
5595 goto nla_put_failure;
5596 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5597 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5598 survey->channel_time_busy))
5599 goto nla_put_failure;
5600 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5601 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5602 survey->channel_time_ext_busy))
5603 goto nla_put_failure;
5604 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5605 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5606 survey->channel_time_rx))
5607 goto nla_put_failure;
5608 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5609 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5610 survey->channel_time_tx))
5611 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005612
5613 nla_nest_end(msg, infoattr);
5614
5615 return genlmsg_end(msg, hdr);
5616
5617 nla_put_failure:
5618 genlmsg_cancel(msg, hdr);
5619 return -EMSGSIZE;
5620}
5621
5622static int nl80211_dump_survey(struct sk_buff *skb,
5623 struct netlink_callback *cb)
5624{
5625 struct survey_info survey;
5626 struct cfg80211_registered_device *dev;
5627 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005628 int survey_idx = cb->args[1];
5629 int res;
5630
Johannes Berg67748892010-10-04 21:14:06 +02005631 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5632 if (res)
5633 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005634
5635 if (!dev->ops->dump_survey) {
5636 res = -EOPNOTSUPP;
5637 goto out_err;
5638 }
5639
5640 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005641 struct ieee80211_channel *chan;
5642
Hila Gonene35e4d22012-06-27 17:19:42 +03005643 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005644 if (res == -ENOENT)
5645 break;
5646 if (res)
5647 goto out_err;
5648
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005649 /* Survey without a channel doesn't make sense */
5650 if (!survey.channel) {
5651 res = -EINVAL;
5652 goto out;
5653 }
5654
5655 chan = ieee80211_get_channel(&dev->wiphy,
5656 survey.channel->center_freq);
5657 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5658 survey_idx++;
5659 continue;
5660 }
5661
Holger Schurig61fa7132009-11-11 12:25:40 +01005662 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005663 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005664 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5665 netdev,
5666 &survey) < 0)
5667 goto out;
5668 survey_idx++;
5669 }
5670
5671 out:
5672 cb->args[1] = survey_idx;
5673 res = skb->len;
5674 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005675 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005676 return res;
5677}
5678
Samuel Ortizb23aa672009-07-01 21:26:54 +02005679static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5680{
5681 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5682 NL80211_WPA_VERSION_2));
5683}
5684
Jouni Malinen636a5d32009-03-19 13:39:22 +02005685static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5686{
Johannes Berg4c476992010-10-04 21:36:35 +02005687 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5688 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005689 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005690 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5691 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005692 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005693 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005694 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005695
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005696 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5697 return -EINVAL;
5698
5699 if (!info->attrs[NL80211_ATTR_MAC])
5700 return -EINVAL;
5701
Jouni Malinen17780922009-03-27 20:52:47 +02005702 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5703 return -EINVAL;
5704
Johannes Berg19957bb2009-07-02 17:20:43 +02005705 if (!info->attrs[NL80211_ATTR_SSID])
5706 return -EINVAL;
5707
5708 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5709 return -EINVAL;
5710
Johannes Bergfffd0932009-07-08 14:22:54 +02005711 err = nl80211_parse_key(info, &key);
5712 if (err)
5713 return err;
5714
5715 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005716 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5717 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005718 if (!key.p.key || !key.p.key_len)
5719 return -EINVAL;
5720 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5721 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5722 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5723 key.p.key_len != WLAN_KEY_LEN_WEP104))
5724 return -EINVAL;
5725 if (key.idx > 4)
5726 return -EINVAL;
5727 } else {
5728 key.p.key_len = 0;
5729 key.p.key = NULL;
5730 }
5731
Johannes Bergafea0b72010-08-10 09:46:42 +02005732 if (key.idx >= 0) {
5733 int i;
5734 bool ok = false;
5735 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5736 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5737 ok = true;
5738 break;
5739 }
5740 }
Johannes Berg4c476992010-10-04 21:36:35 +02005741 if (!ok)
5742 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005743 }
5744
Johannes Berg4c476992010-10-04 21:36:35 +02005745 if (!rdev->ops->auth)
5746 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005747
Johannes Berg074ac8d2010-09-16 14:58:22 +02005748 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005749 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5750 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005751
Johannes Berg19957bb2009-07-02 17:20:43 +02005752 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005753 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005754 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005755 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5756 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005757
Johannes Berg19957bb2009-07-02 17:20:43 +02005758 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5759 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5760
5761 if (info->attrs[NL80211_ATTR_IE]) {
5762 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5763 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5764 }
5765
5766 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005767 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005768 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005769
Jouni Malinene39e5b52012-09-30 19:29:39 +03005770 if (auth_type == NL80211_AUTHTYPE_SAE &&
5771 !info->attrs[NL80211_ATTR_SAE_DATA])
5772 return -EINVAL;
5773
5774 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5775 if (auth_type != NL80211_AUTHTYPE_SAE)
5776 return -EINVAL;
5777 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5778 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5779 /* need to include at least Auth Transaction and Status Code */
5780 if (sae_data_len < 4)
5781 return -EINVAL;
5782 }
5783
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005784 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5785
Johannes Berg95de8172012-01-20 13:55:25 +01005786 /*
5787 * Since we no longer track auth state, ignore
5788 * requests to only change local state.
5789 */
5790 if (local_state_change)
5791 return 0;
5792
Johannes Berg4c476992010-10-04 21:36:35 +02005793 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5794 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005795 key.p.key, key.p.key_len, key.idx,
5796 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005797}
5798
Johannes Bergc0692b82010-08-27 14:26:53 +03005799static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5800 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005801 struct cfg80211_crypto_settings *settings,
5802 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005803{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005804 memset(settings, 0, sizeof(*settings));
5805
Samuel Ortizb23aa672009-07-01 21:26:54 +02005806 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5807
Johannes Bergc0692b82010-08-27 14:26:53 +03005808 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5809 u16 proto;
5810 proto = nla_get_u16(
5811 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5812 settings->control_port_ethertype = cpu_to_be16(proto);
5813 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5814 proto != ETH_P_PAE)
5815 return -EINVAL;
5816 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5817 settings->control_port_no_encrypt = true;
5818 } else
5819 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5820
Samuel Ortizb23aa672009-07-01 21:26:54 +02005821 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5822 void *data;
5823 int len, i;
5824
5825 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5826 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5827 settings->n_ciphers_pairwise = len / sizeof(u32);
5828
5829 if (len % sizeof(u32))
5830 return -EINVAL;
5831
Johannes Berg3dc27d22009-07-02 21:36:37 +02005832 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005833 return -EINVAL;
5834
5835 memcpy(settings->ciphers_pairwise, data, len);
5836
5837 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005838 if (!cfg80211_supported_cipher_suite(
5839 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005840 settings->ciphers_pairwise[i]))
5841 return -EINVAL;
5842 }
5843
5844 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5845 settings->cipher_group =
5846 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005847 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5848 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005849 return -EINVAL;
5850 }
5851
5852 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5853 settings->wpa_versions =
5854 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5855 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5856 return -EINVAL;
5857 }
5858
5859 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5860 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005861 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005862
5863 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5864 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5865 settings->n_akm_suites = len / sizeof(u32);
5866
5867 if (len % sizeof(u32))
5868 return -EINVAL;
5869
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005870 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5871 return -EINVAL;
5872
Samuel Ortizb23aa672009-07-01 21:26:54 +02005873 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005874 }
5875
5876 return 0;
5877}
5878
Jouni Malinen636a5d32009-03-19 13:39:22 +02005879static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5880{
Johannes Berg4c476992010-10-04 21:36:35 +02005881 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5882 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005883 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005884 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005885 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005886 int err, ssid_len, ie_len = 0;
5887 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005888 u32 flags = 0;
5889 struct ieee80211_ht_cap *ht_capa = NULL;
5890 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005891
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005892 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5893 return -EINVAL;
5894
5895 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005896 !info->attrs[NL80211_ATTR_SSID] ||
5897 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005898 return -EINVAL;
5899
Johannes Berg4c476992010-10-04 21:36:35 +02005900 if (!rdev->ops->assoc)
5901 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005902
Johannes Berg074ac8d2010-09-16 14:58:22 +02005903 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005904 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5905 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005906
Johannes Berg19957bb2009-07-02 17:20:43 +02005907 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005908
Johannes Berg19957bb2009-07-02 17:20:43 +02005909 chan = ieee80211_get_channel(&rdev->wiphy,
5910 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005911 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5912 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005913
Johannes Berg19957bb2009-07-02 17:20:43 +02005914 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5915 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005916
5917 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005918 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5919 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005920 }
5921
Jouni Malinendc6382c2009-05-06 22:09:37 +03005922 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005923 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005924 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005925 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005926 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005927 else if (mfp != NL80211_MFP_NO)
5928 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005929 }
5930
Johannes Berg3e5d7642009-07-07 14:37:26 +02005931 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5932 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5933
Ben Greear7e7c8922011-11-18 11:31:59 -08005934 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5935 flags |= ASSOC_REQ_DISABLE_HT;
5936
5937 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5938 ht_capa_mask =
5939 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5940
5941 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5942 if (!ht_capa_mask)
5943 return -EINVAL;
5944 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5945 }
5946
Johannes Bergc0692b82010-08-27 14:26:53 +03005947 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005948 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005949 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5950 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005951 &crypto, flags, ht_capa,
5952 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005953
Jouni Malinen636a5d32009-03-19 13:39:22 +02005954 return err;
5955}
5956
5957static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5958{
Johannes Berg4c476992010-10-04 21:36:35 +02005959 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5960 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005961 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005962 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005963 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005964 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005965
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005966 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5967 return -EINVAL;
5968
5969 if (!info->attrs[NL80211_ATTR_MAC])
5970 return -EINVAL;
5971
5972 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5973 return -EINVAL;
5974
Johannes Berg4c476992010-10-04 21:36:35 +02005975 if (!rdev->ops->deauth)
5976 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005977
Johannes Berg074ac8d2010-09-16 14:58:22 +02005978 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005979 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5980 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005981
Johannes Berg19957bb2009-07-02 17:20:43 +02005982 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005983
Johannes Berg19957bb2009-07-02 17:20:43 +02005984 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5985 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005986 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005987 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005988 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005989
5990 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005991 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5992 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005993 }
5994
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005995 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5996
Johannes Berg4c476992010-10-04 21:36:35 +02005997 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5998 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005999}
6000
6001static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6002{
Johannes Berg4c476992010-10-04 21:36:35 +02006003 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6004 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006005 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006006 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006007 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006008 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006009
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006010 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6011 return -EINVAL;
6012
6013 if (!info->attrs[NL80211_ATTR_MAC])
6014 return -EINVAL;
6015
6016 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6017 return -EINVAL;
6018
Johannes Berg4c476992010-10-04 21:36:35 +02006019 if (!rdev->ops->disassoc)
6020 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006021
Johannes Berg074ac8d2010-09-16 14:58:22 +02006022 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006023 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6024 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006025
Johannes Berg19957bb2009-07-02 17:20:43 +02006026 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006027
Johannes Berg19957bb2009-07-02 17:20:43 +02006028 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6029 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006030 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006031 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006032 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006033
6034 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006035 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6036 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006037 }
6038
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006039 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6040
Johannes Berg4c476992010-10-04 21:36:35 +02006041 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6042 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006043}
6044
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006045static bool
6046nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6047 int mcast_rate[IEEE80211_NUM_BANDS],
6048 int rateval)
6049{
6050 struct wiphy *wiphy = &rdev->wiphy;
6051 bool found = false;
6052 int band, i;
6053
6054 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6055 struct ieee80211_supported_band *sband;
6056
6057 sband = wiphy->bands[band];
6058 if (!sband)
6059 continue;
6060
6061 for (i = 0; i < sband->n_bitrates; i++) {
6062 if (sband->bitrates[i].bitrate == rateval) {
6063 mcast_rate[band] = i + 1;
6064 found = true;
6065 break;
6066 }
6067 }
6068 }
6069
6070 return found;
6071}
6072
Johannes Berg04a773a2009-04-19 21:24:32 +02006073static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6074{
Johannes Berg4c476992010-10-04 21:36:35 +02006075 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6076 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006077 struct cfg80211_ibss_params ibss;
6078 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006079 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006080 int err;
6081
Johannes Berg8e30bc52009-04-22 17:45:38 +02006082 memset(&ibss, 0, sizeof(ibss));
6083
Johannes Berg04a773a2009-04-19 21:24:32 +02006084 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6085 return -EINVAL;
6086
Johannes Berg683b6d32012-11-08 21:25:48 +01006087 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006088 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6089 return -EINVAL;
6090
Johannes Berg8e30bc52009-04-22 17:45:38 +02006091 ibss.beacon_interval = 100;
6092
6093 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6094 ibss.beacon_interval =
6095 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6096 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6097 return -EINVAL;
6098 }
6099
Johannes Berg4c476992010-10-04 21:36:35 +02006100 if (!rdev->ops->join_ibss)
6101 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006102
Johannes Berg4c476992010-10-04 21:36:35 +02006103 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6104 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006105
Johannes Berg79c97e92009-07-07 03:56:12 +02006106 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006107
Johannes Berg39193492011-09-16 13:45:25 +02006108 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006109 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006110
6111 if (!is_valid_ether_addr(ibss.bssid))
6112 return -EINVAL;
6113 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006114 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6115 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6116
6117 if (info->attrs[NL80211_ATTR_IE]) {
6118 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6119 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6120 }
6121
Johannes Berg683b6d32012-11-08 21:25:48 +01006122 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6123 if (err)
6124 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006125
Johannes Berg683b6d32012-11-08 21:25:48 +01006126 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006127 return -EINVAL;
6128
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006129 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6130 return -EINVAL;
6131 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6132 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006133 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006134
Johannes Berg04a773a2009-04-19 21:24:32 +02006135 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006136 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006137
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006138 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6139 u8 *rates =
6140 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6141 int n_rates =
6142 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6143 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006144 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006145
Johannes Berg34850ab2011-07-18 18:08:35 +02006146 err = ieee80211_get_ratemask(sband, rates, n_rates,
6147 &ibss.basic_rates);
6148 if (err)
6149 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006150 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006151
6152 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6153 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6154 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6155 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006156
Johannes Berg4c476992010-10-04 21:36:35 +02006157 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306158 bool no_ht = false;
6159
Johannes Berg4c476992010-10-04 21:36:35 +02006160 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306161 info->attrs[NL80211_ATTR_KEYS],
6162 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006163 if (IS_ERR(connkeys))
6164 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306165
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006166 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6167 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306168 kfree(connkeys);
6169 return -EINVAL;
6170 }
Johannes Berg4c476992010-10-04 21:36:35 +02006171 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006172
Antonio Quartulli267335d2012-01-31 20:25:47 +01006173 ibss.control_port =
6174 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6175
Johannes Berg4c476992010-10-04 21:36:35 +02006176 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006177 if (err)
6178 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006179 return err;
6180}
6181
6182static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6183{
Johannes Berg4c476992010-10-04 21:36:35 +02006184 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6185 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006186
Johannes Berg4c476992010-10-04 21:36:35 +02006187 if (!rdev->ops->leave_ibss)
6188 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006189
Johannes Berg4c476992010-10-04 21:36:35 +02006190 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6191 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006192
Johannes Berg4c476992010-10-04 21:36:35 +02006193 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006194}
6195
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006196static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6197{
6198 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6199 struct net_device *dev = info->user_ptr[1];
6200 int mcast_rate[IEEE80211_NUM_BANDS];
6201 u32 nla_rate;
6202 int err;
6203
6204 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6205 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6206 return -EOPNOTSUPP;
6207
6208 if (!rdev->ops->set_mcast_rate)
6209 return -EOPNOTSUPP;
6210
6211 memset(mcast_rate, 0, sizeof(mcast_rate));
6212
6213 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6214 return -EINVAL;
6215
6216 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6217 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6218 return -EINVAL;
6219
6220 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6221
6222 return err;
6223}
6224
6225
Johannes Bergaff89a92009-07-01 21:26:51 +02006226#ifdef CONFIG_NL80211_TESTMODE
6227static struct genl_multicast_group nl80211_testmode_mcgrp = {
6228 .name = "testmode",
6229};
6230
6231static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6232{
Johannes Berg4c476992010-10-04 21:36:35 +02006233 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006234 int err;
6235
6236 if (!info->attrs[NL80211_ATTR_TESTDATA])
6237 return -EINVAL;
6238
Johannes Bergaff89a92009-07-01 21:26:51 +02006239 err = -EOPNOTSUPP;
6240 if (rdev->ops->testmode_cmd) {
6241 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006242 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006243 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6244 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6245 rdev->testmode_info = NULL;
6246 }
6247
Johannes Bergaff89a92009-07-01 21:26:51 +02006248 return err;
6249}
6250
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006251static int nl80211_testmode_dump(struct sk_buff *skb,
6252 struct netlink_callback *cb)
6253{
Johannes Berg00918d32011-12-13 17:22:05 +01006254 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006255 int err;
6256 long phy_idx;
6257 void *data = NULL;
6258 int data_len = 0;
6259
6260 if (cb->args[0]) {
6261 /*
6262 * 0 is a valid index, but not valid for args[0],
6263 * so we need to offset by 1.
6264 */
6265 phy_idx = cb->args[0] - 1;
6266 } else {
6267 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6268 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6269 nl80211_policy);
6270 if (err)
6271 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006272
Johannes Berg2bd7e352012-06-15 14:23:16 +02006273 mutex_lock(&cfg80211_mutex);
6274 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6275 nl80211_fam.attrbuf);
6276 if (IS_ERR(rdev)) {
6277 mutex_unlock(&cfg80211_mutex);
6278 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006279 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006280 phy_idx = rdev->wiphy_idx;
6281 rdev = NULL;
6282 mutex_unlock(&cfg80211_mutex);
6283
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006284 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6285 cb->args[1] =
6286 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6287 }
6288
6289 if (cb->args[1]) {
6290 data = nla_data((void *)cb->args[1]);
6291 data_len = nla_len((void *)cb->args[1]);
6292 }
6293
6294 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006295 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6296 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006297 mutex_unlock(&cfg80211_mutex);
6298 return -ENOENT;
6299 }
Johannes Berg00918d32011-12-13 17:22:05 +01006300 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006301 mutex_unlock(&cfg80211_mutex);
6302
Johannes Berg00918d32011-12-13 17:22:05 +01006303 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006304 err = -EOPNOTSUPP;
6305 goto out_err;
6306 }
6307
6308 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006309 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006310 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6311 NL80211_CMD_TESTMODE);
6312 struct nlattr *tmdata;
6313
David S. Miller9360ffd2012-03-29 04:41:26 -04006314 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006315 genlmsg_cancel(skb, hdr);
6316 break;
6317 }
6318
6319 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6320 if (!tmdata) {
6321 genlmsg_cancel(skb, hdr);
6322 break;
6323 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006324 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006325 nla_nest_end(skb, tmdata);
6326
6327 if (err == -ENOBUFS || err == -ENOENT) {
6328 genlmsg_cancel(skb, hdr);
6329 break;
6330 } else if (err) {
6331 genlmsg_cancel(skb, hdr);
6332 goto out_err;
6333 }
6334
6335 genlmsg_end(skb, hdr);
6336 }
6337
6338 err = skb->len;
6339 /* see above */
6340 cb->args[0] = phy_idx + 1;
6341 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006342 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006343 return err;
6344}
6345
Johannes Bergaff89a92009-07-01 21:26:51 +02006346static struct sk_buff *
6347__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006348 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006349{
6350 struct sk_buff *skb;
6351 void *hdr;
6352 struct nlattr *data;
6353
6354 skb = nlmsg_new(approxlen + 100, gfp);
6355 if (!skb)
6356 return NULL;
6357
Eric W. Biederman15e47302012-09-07 20:12:54 +00006358 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006359 if (!hdr) {
6360 kfree_skb(skb);
6361 return NULL;
6362 }
6363
David S. Miller9360ffd2012-03-29 04:41:26 -04006364 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6365 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006366 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6367
6368 ((void **)skb->cb)[0] = rdev;
6369 ((void **)skb->cb)[1] = hdr;
6370 ((void **)skb->cb)[2] = data;
6371
6372 return skb;
6373
6374 nla_put_failure:
6375 kfree_skb(skb);
6376 return NULL;
6377}
6378
6379struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6380 int approxlen)
6381{
6382 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6383
6384 if (WARN_ON(!rdev->testmode_info))
6385 return NULL;
6386
6387 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006388 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006389 rdev->testmode_info->snd_seq,
6390 GFP_KERNEL);
6391}
6392EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6393
6394int cfg80211_testmode_reply(struct sk_buff *skb)
6395{
6396 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6397 void *hdr = ((void **)skb->cb)[1];
6398 struct nlattr *data = ((void **)skb->cb)[2];
6399
6400 if (WARN_ON(!rdev->testmode_info)) {
6401 kfree_skb(skb);
6402 return -EINVAL;
6403 }
6404
6405 nla_nest_end(skb, data);
6406 genlmsg_end(skb, hdr);
6407 return genlmsg_reply(skb, rdev->testmode_info);
6408}
6409EXPORT_SYMBOL(cfg80211_testmode_reply);
6410
6411struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6412 int approxlen, gfp_t gfp)
6413{
6414 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6415
6416 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6417}
6418EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6419
6420void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6421{
6422 void *hdr = ((void **)skb->cb)[1];
6423 struct nlattr *data = ((void **)skb->cb)[2];
6424
6425 nla_nest_end(skb, data);
6426 genlmsg_end(skb, hdr);
6427 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6428}
6429EXPORT_SYMBOL(cfg80211_testmode_event);
6430#endif
6431
Samuel Ortizb23aa672009-07-01 21:26:54 +02006432static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6433{
Johannes Berg4c476992010-10-04 21:36:35 +02006434 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6435 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006436 struct cfg80211_connect_params connect;
6437 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006438 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006439 int err;
6440
6441 memset(&connect, 0, sizeof(connect));
6442
6443 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6444 return -EINVAL;
6445
6446 if (!info->attrs[NL80211_ATTR_SSID] ||
6447 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6448 return -EINVAL;
6449
6450 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6451 connect.auth_type =
6452 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006453 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6454 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006455 return -EINVAL;
6456 } else
6457 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6458
6459 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6460
Johannes Bergc0692b82010-08-27 14:26:53 +03006461 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006462 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006463 if (err)
6464 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006465
Johannes Berg074ac8d2010-09-16 14:58:22 +02006466 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006467 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6468 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006469
Johannes Berg79c97e92009-07-07 03:56:12 +02006470 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006471
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306472 connect.bg_scan_period = -1;
6473 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6474 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6475 connect.bg_scan_period =
6476 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6477 }
6478
Samuel Ortizb23aa672009-07-01 21:26:54 +02006479 if (info->attrs[NL80211_ATTR_MAC])
6480 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6481 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6482 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6483
6484 if (info->attrs[NL80211_ATTR_IE]) {
6485 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6486 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6487 }
6488
Jouni Malinencee00a92013-01-15 17:15:57 +02006489 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6490 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6491 if (connect.mfp != NL80211_MFP_REQUIRED &&
6492 connect.mfp != NL80211_MFP_NO)
6493 return -EINVAL;
6494 } else {
6495 connect.mfp = NL80211_MFP_NO;
6496 }
6497
Samuel Ortizb23aa672009-07-01 21:26:54 +02006498 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6499 connect.channel =
6500 ieee80211_get_channel(wiphy,
6501 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6502 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006503 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6504 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006505 }
6506
Johannes Bergfffd0932009-07-08 14:22:54 +02006507 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6508 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306509 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006510 if (IS_ERR(connkeys))
6511 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006512 }
6513
Ben Greear7e7c8922011-11-18 11:31:59 -08006514 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6515 connect.flags |= ASSOC_REQ_DISABLE_HT;
6516
6517 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6518 memcpy(&connect.ht_capa_mask,
6519 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6520 sizeof(connect.ht_capa_mask));
6521
6522 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006523 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6524 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006525 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006526 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006527 memcpy(&connect.ht_capa,
6528 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6529 sizeof(connect.ht_capa));
6530 }
6531
Johannes Bergfffd0932009-07-08 14:22:54 +02006532 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006533 if (err)
6534 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006535 return err;
6536}
6537
6538static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6539{
Johannes Berg4c476992010-10-04 21:36:35 +02006540 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6541 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006542 u16 reason;
6543
6544 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6545 reason = WLAN_REASON_DEAUTH_LEAVING;
6546 else
6547 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6548
6549 if (reason == 0)
6550 return -EINVAL;
6551
Johannes Berg074ac8d2010-09-16 14:58:22 +02006552 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006553 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6554 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006555
Johannes Berg4c476992010-10-04 21:36:35 +02006556 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006557}
6558
Johannes Berg463d0182009-07-14 00:33:35 +02006559static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6560{
Johannes Berg4c476992010-10-04 21:36:35 +02006561 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006562 struct net *net;
6563 int err;
6564 u32 pid;
6565
6566 if (!info->attrs[NL80211_ATTR_PID])
6567 return -EINVAL;
6568
6569 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6570
Johannes Berg463d0182009-07-14 00:33:35 +02006571 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006572 if (IS_ERR(net))
6573 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006574
6575 err = 0;
6576
6577 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006578 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6579 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006580
Johannes Berg463d0182009-07-14 00:33:35 +02006581 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006582 return err;
6583}
6584
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006585static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6586{
Johannes Berg4c476992010-10-04 21:36:35 +02006587 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006588 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6589 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006590 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006591 struct cfg80211_pmksa pmksa;
6592
6593 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6594
6595 if (!info->attrs[NL80211_ATTR_MAC])
6596 return -EINVAL;
6597
6598 if (!info->attrs[NL80211_ATTR_PMKID])
6599 return -EINVAL;
6600
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006601 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6602 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6603
Johannes Berg074ac8d2010-09-16 14:58:22 +02006604 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006605 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6606 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006607
6608 switch (info->genlhdr->cmd) {
6609 case NL80211_CMD_SET_PMKSA:
6610 rdev_ops = rdev->ops->set_pmksa;
6611 break;
6612 case NL80211_CMD_DEL_PMKSA:
6613 rdev_ops = rdev->ops->del_pmksa;
6614 break;
6615 default:
6616 WARN_ON(1);
6617 break;
6618 }
6619
Johannes Berg4c476992010-10-04 21:36:35 +02006620 if (!rdev_ops)
6621 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006622
Johannes Berg4c476992010-10-04 21:36:35 +02006623 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006624}
6625
6626static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6627{
Johannes Berg4c476992010-10-04 21:36:35 +02006628 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6629 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006630
Johannes Berg074ac8d2010-09-16 14:58:22 +02006631 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006632 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6633 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006634
Johannes Berg4c476992010-10-04 21:36:35 +02006635 if (!rdev->ops->flush_pmksa)
6636 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006637
Hila Gonene35e4d22012-06-27 17:19:42 +03006638 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006639}
6640
Arik Nemtsov109086c2011-09-28 14:12:50 +03006641static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6642{
6643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6644 struct net_device *dev = info->user_ptr[1];
6645 u8 action_code, dialog_token;
6646 u16 status_code;
6647 u8 *peer;
6648
6649 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6650 !rdev->ops->tdls_mgmt)
6651 return -EOPNOTSUPP;
6652
6653 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6654 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6655 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6656 !info->attrs[NL80211_ATTR_IE] ||
6657 !info->attrs[NL80211_ATTR_MAC])
6658 return -EINVAL;
6659
6660 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6661 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6662 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6663 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6664
Hila Gonene35e4d22012-06-27 17:19:42 +03006665 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6666 dialog_token, status_code,
6667 nla_data(info->attrs[NL80211_ATTR_IE]),
6668 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006669}
6670
6671static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6672{
6673 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6674 struct net_device *dev = info->user_ptr[1];
6675 enum nl80211_tdls_operation operation;
6676 u8 *peer;
6677
6678 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6679 !rdev->ops->tdls_oper)
6680 return -EOPNOTSUPP;
6681
6682 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6683 !info->attrs[NL80211_ATTR_MAC])
6684 return -EINVAL;
6685
6686 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6687 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6688
Hila Gonene35e4d22012-06-27 17:19:42 +03006689 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006690}
6691
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006692static int nl80211_remain_on_channel(struct sk_buff *skb,
6693 struct genl_info *info)
6694{
Johannes Berg4c476992010-10-04 21:36:35 +02006695 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006696 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006697 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006698 struct sk_buff *msg;
6699 void *hdr;
6700 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006701 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006702 int err;
6703
6704 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6705 !info->attrs[NL80211_ATTR_DURATION])
6706 return -EINVAL;
6707
6708 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6709
Johannes Berg7c4ef712011-11-18 15:33:48 +01006710 if (!rdev->ops->remain_on_channel ||
6711 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006712 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006713
Johannes Bergebf348f2012-06-01 12:50:54 +02006714 /*
6715 * We should be on that channel for at least a minimum amount of
6716 * time (10ms) but no longer than the driver supports.
6717 */
6718 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6719 duration > rdev->wiphy.max_remain_on_channel_duration)
6720 return -EINVAL;
6721
Johannes Berg683b6d32012-11-08 21:25:48 +01006722 err = nl80211_parse_chandef(rdev, info, &chandef);
6723 if (err)
6724 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006725
6726 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006727 if (!msg)
6728 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006729
Eric W. Biederman15e47302012-09-07 20:12:54 +00006730 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006731 NL80211_CMD_REMAIN_ON_CHANNEL);
6732
6733 if (IS_ERR(hdr)) {
6734 err = PTR_ERR(hdr);
6735 goto free_msg;
6736 }
6737
Johannes Berg683b6d32012-11-08 21:25:48 +01006738 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6739 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006740
6741 if (err)
6742 goto free_msg;
6743
David S. Miller9360ffd2012-03-29 04:41:26 -04006744 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6745 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006746
6747 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006748
6749 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006750
6751 nla_put_failure:
6752 err = -ENOBUFS;
6753 free_msg:
6754 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006755 return err;
6756}
6757
6758static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6759 struct genl_info *info)
6760{
Johannes Berg4c476992010-10-04 21:36:35 +02006761 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006762 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006763 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006764
6765 if (!info->attrs[NL80211_ATTR_COOKIE])
6766 return -EINVAL;
6767
Johannes Berg4c476992010-10-04 21:36:35 +02006768 if (!rdev->ops->cancel_remain_on_channel)
6769 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006770
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006771 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6772
Hila Gonene35e4d22012-06-27 17:19:42 +03006773 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006774}
6775
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006776static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6777 u8 *rates, u8 rates_len)
6778{
6779 u8 i;
6780 u32 mask = 0;
6781
6782 for (i = 0; i < rates_len; i++) {
6783 int rate = (rates[i] & 0x7f) * 5;
6784 int ridx;
6785 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6786 struct ieee80211_rate *srate =
6787 &sband->bitrates[ridx];
6788 if (rate == srate->bitrate) {
6789 mask |= 1 << ridx;
6790 break;
6791 }
6792 }
6793 if (ridx == sband->n_bitrates)
6794 return 0; /* rate not found */
6795 }
6796
6797 return mask;
6798}
6799
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006800static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6801 u8 *rates, u8 rates_len,
6802 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6803{
6804 u8 i;
6805
6806 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6807
6808 for (i = 0; i < rates_len; i++) {
6809 int ridx, rbit;
6810
6811 ridx = rates[i] / 8;
6812 rbit = BIT(rates[i] % 8);
6813
6814 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006815 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006816 return false;
6817
6818 /* check availability */
6819 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6820 mcs[ridx] |= rbit;
6821 else
6822 return false;
6823 }
6824
6825 return true;
6826}
6827
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006828static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006829 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6830 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006831 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6832 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006833};
6834
6835static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6836 struct genl_info *info)
6837{
6838 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006839 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006840 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006841 int rem, i;
6842 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006843 struct nlattr *tx_rates;
6844 struct ieee80211_supported_band *sband;
6845
6846 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6847 return -EINVAL;
6848
Johannes Berg4c476992010-10-04 21:36:35 +02006849 if (!rdev->ops->set_bitrate_mask)
6850 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006851
6852 memset(&mask, 0, sizeof(mask));
6853 /* Default to all rates enabled */
6854 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6855 sband = rdev->wiphy.bands[i];
6856 mask.control[i].legacy =
6857 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006858 if (sband)
6859 memcpy(mask.control[i].mcs,
6860 sband->ht_cap.mcs.rx_mask,
6861 sizeof(mask.control[i].mcs));
6862 else
6863 memset(mask.control[i].mcs, 0,
6864 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006865 }
6866
6867 /*
6868 * The nested attribute uses enum nl80211_band as the index. This maps
6869 * directly to the enum ieee80211_band values used in cfg80211.
6870 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006871 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006872 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6873 {
6874 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006875 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6876 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006877 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006878 if (sband == NULL)
6879 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006880 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6881 nla_len(tx_rates), nl80211_txattr_policy);
6882 if (tb[NL80211_TXRATE_LEGACY]) {
6883 mask.control[band].legacy = rateset_to_mask(
6884 sband,
6885 nla_data(tb[NL80211_TXRATE_LEGACY]),
6886 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306887 if ((mask.control[band].legacy == 0) &&
6888 nla_len(tb[NL80211_TXRATE_LEGACY]))
6889 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006890 }
6891 if (tb[NL80211_TXRATE_MCS]) {
6892 if (!ht_rateset_to_mask(
6893 sband,
6894 nla_data(tb[NL80211_TXRATE_MCS]),
6895 nla_len(tb[NL80211_TXRATE_MCS]),
6896 mask.control[band].mcs))
6897 return -EINVAL;
6898 }
6899
6900 if (mask.control[band].legacy == 0) {
6901 /* don't allow empty legacy rates if HT
6902 * is not even supported. */
6903 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6904 return -EINVAL;
6905
6906 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6907 if (mask.control[band].mcs[i])
6908 break;
6909
6910 /* legacy and mcs rates may not be both empty */
6911 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006912 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006913 }
6914 }
6915
Hila Gonene35e4d22012-06-27 17:19:42 +03006916 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006917}
6918
Johannes Berg2e161f72010-08-12 15:38:38 +02006919static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006920{
Johannes Berg4c476992010-10-04 21:36:35 +02006921 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006922 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006923 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006924
6925 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6926 return -EINVAL;
6927
Johannes Berg2e161f72010-08-12 15:38:38 +02006928 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6929 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006930
Johannes Berg71bbc992012-06-15 15:30:18 +02006931 switch (wdev->iftype) {
6932 case NL80211_IFTYPE_STATION:
6933 case NL80211_IFTYPE_ADHOC:
6934 case NL80211_IFTYPE_P2P_CLIENT:
6935 case NL80211_IFTYPE_AP:
6936 case NL80211_IFTYPE_AP_VLAN:
6937 case NL80211_IFTYPE_MESH_POINT:
6938 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006939 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006940 break;
6941 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006942 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006943 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006944
6945 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006946 if (!rdev->ops->mgmt_tx)
6947 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006948
Eric W. Biederman15e47302012-09-07 20:12:54 +00006949 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006950 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6951 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006952}
6953
Johannes Berg2e161f72010-08-12 15:38:38 +02006954static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006955{
Johannes Berg4c476992010-10-04 21:36:35 +02006956 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006957 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006958 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006959 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006960 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006961 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006962 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006963 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006964 bool offchan, no_cck, dont_wait_for_ack;
6965
6966 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006967
Johannes Berg683b6d32012-11-08 21:25:48 +01006968 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006969 return -EINVAL;
6970
Johannes Berg4c476992010-10-04 21:36:35 +02006971 if (!rdev->ops->mgmt_tx)
6972 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006973
Johannes Berg71bbc992012-06-15 15:30:18 +02006974 switch (wdev->iftype) {
6975 case NL80211_IFTYPE_STATION:
6976 case NL80211_IFTYPE_ADHOC:
6977 case NL80211_IFTYPE_P2P_CLIENT:
6978 case NL80211_IFTYPE_AP:
6979 case NL80211_IFTYPE_AP_VLAN:
6980 case NL80211_IFTYPE_MESH_POINT:
6981 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006982 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006983 break;
6984 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006985 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006986 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006987
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006988 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006989 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006990 return -EINVAL;
6991 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006992
6993 /*
6994 * We should wait on the channel for at least a minimum amount
6995 * of time (10ms) but no longer than the driver supports.
6996 */
6997 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6998 wait > rdev->wiphy.max_remain_on_channel_duration)
6999 return -EINVAL;
7000
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007001 }
7002
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007003 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7004
Johannes Berg7c4ef712011-11-18 15:33:48 +01007005 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7006 return -EINVAL;
7007
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307008 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7009
Johannes Berg683b6d32012-11-08 21:25:48 +01007010 err = nl80211_parse_chandef(rdev, info, &chandef);
7011 if (err)
7012 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007013
Johannes Berge247bd902011-11-04 11:18:21 +01007014 if (!dont_wait_for_ack) {
7015 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7016 if (!msg)
7017 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007018
Eric W. Biederman15e47302012-09-07 20:12:54 +00007019 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007020 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007021
Johannes Berge247bd902011-11-04 11:18:21 +01007022 if (IS_ERR(hdr)) {
7023 err = PTR_ERR(hdr);
7024 goto free_msg;
7025 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007026 }
Johannes Berge247bd902011-11-04 11:18:21 +01007027
Johannes Berg683b6d32012-11-08 21:25:48 +01007028 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007029 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7030 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007031 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007032 if (err)
7033 goto free_msg;
7034
Johannes Berge247bd902011-11-04 11:18:21 +01007035 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007036 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7037 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007038
Johannes Berge247bd902011-11-04 11:18:21 +01007039 genlmsg_end(msg, hdr);
7040 return genlmsg_reply(msg, info);
7041 }
7042
7043 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007044
7045 nla_put_failure:
7046 err = -ENOBUFS;
7047 free_msg:
7048 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007049 return err;
7050}
7051
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007052static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7053{
7054 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007055 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007056 u64 cookie;
7057
7058 if (!info->attrs[NL80211_ATTR_COOKIE])
7059 return -EINVAL;
7060
7061 if (!rdev->ops->mgmt_tx_cancel_wait)
7062 return -EOPNOTSUPP;
7063
Johannes Berg71bbc992012-06-15 15:30:18 +02007064 switch (wdev->iftype) {
7065 case NL80211_IFTYPE_STATION:
7066 case NL80211_IFTYPE_ADHOC:
7067 case NL80211_IFTYPE_P2P_CLIENT:
7068 case NL80211_IFTYPE_AP:
7069 case NL80211_IFTYPE_AP_VLAN:
7070 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007071 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007072 break;
7073 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007074 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007075 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007076
7077 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7078
Hila Gonene35e4d22012-06-27 17:19:42 +03007079 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007080}
7081
Kalle Valoffb9eb32010-02-17 17:58:10 +02007082static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7083{
Johannes Berg4c476992010-10-04 21:36:35 +02007084 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007085 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007086 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007087 u8 ps_state;
7088 bool state;
7089 int err;
7090
Johannes Berg4c476992010-10-04 21:36:35 +02007091 if (!info->attrs[NL80211_ATTR_PS_STATE])
7092 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007093
7094 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7095
Johannes Berg4c476992010-10-04 21:36:35 +02007096 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7097 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007098
7099 wdev = dev->ieee80211_ptr;
7100
Johannes Berg4c476992010-10-04 21:36:35 +02007101 if (!rdev->ops->set_power_mgmt)
7102 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007103
7104 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7105
7106 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007107 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007108
Hila Gonene35e4d22012-06-27 17:19:42 +03007109 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007110 if (!err)
7111 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007112 return err;
7113}
7114
7115static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7116{
Johannes Berg4c476992010-10-04 21:36:35 +02007117 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007118 enum nl80211_ps_state ps_state;
7119 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007120 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007121 struct sk_buff *msg;
7122 void *hdr;
7123 int err;
7124
Kalle Valoffb9eb32010-02-17 17:58:10 +02007125 wdev = dev->ieee80211_ptr;
7126
Johannes Berg4c476992010-10-04 21:36:35 +02007127 if (!rdev->ops->set_power_mgmt)
7128 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007129
7130 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007131 if (!msg)
7132 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007133
Eric W. Biederman15e47302012-09-07 20:12:54 +00007134 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007135 NL80211_CMD_GET_POWER_SAVE);
7136 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007137 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007138 goto free_msg;
7139 }
7140
7141 if (wdev->ps)
7142 ps_state = NL80211_PS_ENABLED;
7143 else
7144 ps_state = NL80211_PS_DISABLED;
7145
David S. Miller9360ffd2012-03-29 04:41:26 -04007146 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7147 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007148
7149 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007150 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007151
Johannes Berg4c476992010-10-04 21:36:35 +02007152 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007153 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007154 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007155 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007156 return err;
7157}
7158
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007159static struct nla_policy
7160nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7161 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7162 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7163 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007164 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7165 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7166 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007167};
7168
Thomas Pedersen84f10702012-07-12 16:17:33 -07007169static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007170 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007171{
7172 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7173 struct wireless_dev *wdev;
7174 struct net_device *dev = info->user_ptr[1];
7175
Johannes Bergd9d8b012012-11-26 12:51:52 +01007176 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007177 return -EINVAL;
7178
7179 wdev = dev->ieee80211_ptr;
7180
7181 if (!rdev->ops->set_cqm_txe_config)
7182 return -EOPNOTSUPP;
7183
7184 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7185 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7186 return -EOPNOTSUPP;
7187
Hila Gonene35e4d22012-06-27 17:19:42 +03007188 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007189}
7190
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007191static int nl80211_set_cqm_rssi(struct genl_info *info,
7192 s32 threshold, u32 hysteresis)
7193{
Johannes Berg4c476992010-10-04 21:36:35 +02007194 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007195 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007196 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007197
7198 if (threshold > 0)
7199 return -EINVAL;
7200
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007201 wdev = dev->ieee80211_ptr;
7202
Johannes Berg4c476992010-10-04 21:36:35 +02007203 if (!rdev->ops->set_cqm_rssi_config)
7204 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007205
Johannes Berg074ac8d2010-09-16 14:58:22 +02007206 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007207 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7208 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007209
Hila Gonene35e4d22012-06-27 17:19:42 +03007210 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007211}
7212
7213static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7214{
7215 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7216 struct nlattr *cqm;
7217 int err;
7218
7219 cqm = info->attrs[NL80211_ATTR_CQM];
7220 if (!cqm) {
7221 err = -EINVAL;
7222 goto out;
7223 }
7224
7225 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7226 nl80211_attr_cqm_policy);
7227 if (err)
7228 goto out;
7229
7230 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7231 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7232 s32 threshold;
7233 u32 hysteresis;
7234 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7235 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7236 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007237 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7238 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7239 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7240 u32 rate, pkts, intvl;
7241 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7242 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7243 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7244 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007245 } else
7246 err = -EINVAL;
7247
7248out:
7249 return err;
7250}
7251
Johannes Berg29cbe682010-12-03 09:20:44 +01007252static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7253{
7254 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7255 struct net_device *dev = info->user_ptr[1];
7256 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007257 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007258 int err;
7259
7260 /* start with default */
7261 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007262 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007263
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007264 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007265 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007266 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007267 if (err)
7268 return err;
7269 }
7270
7271 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7272 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7273 return -EINVAL;
7274
Javier Cardonac80d5452010-12-16 17:37:49 -08007275 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7276 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7277
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007278 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7279 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7280 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7281 return -EINVAL;
7282
Marco Porsch9bdbf042013-01-07 16:04:51 +01007283 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7284 setup.beacon_interval =
7285 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7286 if (setup.beacon_interval < 10 ||
7287 setup.beacon_interval > 10000)
7288 return -EINVAL;
7289 }
7290
7291 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7292 setup.dtim_period =
7293 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7294 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7295 return -EINVAL;
7296 }
7297
Javier Cardonac80d5452010-12-16 17:37:49 -08007298 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7299 /* parse additional setup parameters if given */
7300 err = nl80211_parse_mesh_setup(info, &setup);
7301 if (err)
7302 return err;
7303 }
7304
Johannes Bergcc1d2802012-05-16 23:50:20 +02007305 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007306 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7307 if (err)
7308 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007309 } else {
7310 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007311 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007312 }
7313
Javier Cardonac80d5452010-12-16 17:37:49 -08007314 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007315}
7316
7317static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7318{
7319 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7320 struct net_device *dev = info->user_ptr[1];
7321
7322 return cfg80211_leave_mesh(rdev, dev);
7323}
7324
Johannes Bergdfb89c52012-06-27 09:23:48 +02007325#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007326static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7327 struct cfg80211_registered_device *rdev)
7328{
7329 struct nlattr *nl_pats, *nl_pat;
7330 int i, pat_len;
7331
7332 if (!rdev->wowlan->n_patterns)
7333 return 0;
7334
7335 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7336 if (!nl_pats)
7337 return -ENOBUFS;
7338
7339 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7340 nl_pat = nla_nest_start(msg, i + 1);
7341 if (!nl_pat)
7342 return -ENOBUFS;
7343 pat_len = rdev->wowlan->patterns[i].pattern_len;
7344 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7345 DIV_ROUND_UP(pat_len, 8),
7346 rdev->wowlan->patterns[i].mask) ||
7347 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7348 pat_len, rdev->wowlan->patterns[i].pattern) ||
7349 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7350 rdev->wowlan->patterns[i].pkt_offset))
7351 return -ENOBUFS;
7352 nla_nest_end(msg, nl_pat);
7353 }
7354 nla_nest_end(msg, nl_pats);
7355
7356 return 0;
7357}
7358
Johannes Berg2a0e0472013-01-23 22:57:40 +01007359static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7360 struct cfg80211_wowlan_tcp *tcp)
7361{
7362 struct nlattr *nl_tcp;
7363
7364 if (!tcp)
7365 return 0;
7366
7367 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7368 if (!nl_tcp)
7369 return -ENOBUFS;
7370
7371 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7372 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7373 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7374 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7375 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7376 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7377 tcp->payload_len, tcp->payload) ||
7378 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7379 tcp->data_interval) ||
7380 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7381 tcp->wake_len, tcp->wake_data) ||
7382 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7383 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7384 return -ENOBUFS;
7385
7386 if (tcp->payload_seq.len &&
7387 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7388 sizeof(tcp->payload_seq), &tcp->payload_seq))
7389 return -ENOBUFS;
7390
7391 if (tcp->payload_tok.len &&
7392 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7393 sizeof(tcp->payload_tok) + tcp->tokens_size,
7394 &tcp->payload_tok))
7395 return -ENOBUFS;
7396
7397 return 0;
7398}
7399
Johannes Bergff1b6e62011-05-04 15:37:28 +02007400static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7401{
7402 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7403 struct sk_buff *msg;
7404 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007405 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007406
Johannes Berg2a0e0472013-01-23 22:57:40 +01007407 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7408 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007409 return -EOPNOTSUPP;
7410
Johannes Berg2a0e0472013-01-23 22:57:40 +01007411 if (rdev->wowlan && rdev->wowlan->tcp) {
7412 /* adjust size to have room for all the data */
7413 size += rdev->wowlan->tcp->tokens_size +
7414 rdev->wowlan->tcp->payload_len +
7415 rdev->wowlan->tcp->wake_len +
7416 rdev->wowlan->tcp->wake_len / 8;
7417 }
7418
7419 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007420 if (!msg)
7421 return -ENOMEM;
7422
Eric W. Biederman15e47302012-09-07 20:12:54 +00007423 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007424 NL80211_CMD_GET_WOWLAN);
7425 if (!hdr)
7426 goto nla_put_failure;
7427
7428 if (rdev->wowlan) {
7429 struct nlattr *nl_wowlan;
7430
7431 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7432 if (!nl_wowlan)
7433 goto nla_put_failure;
7434
David S. Miller9360ffd2012-03-29 04:41:26 -04007435 if ((rdev->wowlan->any &&
7436 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7437 (rdev->wowlan->disconnect &&
7438 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7439 (rdev->wowlan->magic_pkt &&
7440 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7441 (rdev->wowlan->gtk_rekey_failure &&
7442 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7443 (rdev->wowlan->eap_identity_req &&
7444 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7445 (rdev->wowlan->four_way_handshake &&
7446 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7447 (rdev->wowlan->rfkill_release &&
7448 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7449 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007450
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007451 if (nl80211_send_wowlan_patterns(msg, rdev))
7452 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007453
7454 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7455 goto nla_put_failure;
7456
Johannes Bergff1b6e62011-05-04 15:37:28 +02007457 nla_nest_end(msg, nl_wowlan);
7458 }
7459
7460 genlmsg_end(msg, hdr);
7461 return genlmsg_reply(msg, info);
7462
7463nla_put_failure:
7464 nlmsg_free(msg);
7465 return -ENOBUFS;
7466}
7467
Johannes Berg2a0e0472013-01-23 22:57:40 +01007468static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7469 struct nlattr *attr,
7470 struct cfg80211_wowlan *trig)
7471{
7472 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7473 struct cfg80211_wowlan_tcp *cfg;
7474 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7475 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7476 u32 size;
7477 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7478 int err, port;
7479
7480 if (!rdev->wiphy.wowlan.tcp)
7481 return -EINVAL;
7482
7483 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7484 nla_data(attr), nla_len(attr),
7485 nl80211_wowlan_tcp_policy);
7486 if (err)
7487 return err;
7488
7489 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7490 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7491 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7492 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7493 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7494 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7495 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7496 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7497 return -EINVAL;
7498
7499 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7500 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7501 return -EINVAL;
7502
7503 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7504 rdev->wiphy.wowlan.tcp->data_interval_max)
7505 return -EINVAL;
7506
7507 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7508 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7509 return -EINVAL;
7510
7511 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7512 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7513 return -EINVAL;
7514
7515 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7516 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7517
7518 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7519 tokens_size = tokln - sizeof(*tok);
7520
7521 if (!tok->len || tokens_size % tok->len)
7522 return -EINVAL;
7523 if (!rdev->wiphy.wowlan.tcp->tok)
7524 return -EINVAL;
7525 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7526 return -EINVAL;
7527 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7528 return -EINVAL;
7529 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7530 return -EINVAL;
7531 if (tok->offset + tok->len > data_size)
7532 return -EINVAL;
7533 }
7534
7535 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7536 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7537 if (!rdev->wiphy.wowlan.tcp->seq)
7538 return -EINVAL;
7539 if (seq->len == 0 || seq->len > 4)
7540 return -EINVAL;
7541 if (seq->len + seq->offset > data_size)
7542 return -EINVAL;
7543 }
7544
7545 size = sizeof(*cfg);
7546 size += data_size;
7547 size += wake_size + wake_mask_size;
7548 size += tokens_size;
7549
7550 cfg = kzalloc(size, GFP_KERNEL);
7551 if (!cfg)
7552 return -ENOMEM;
7553 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7554 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7555 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7556 ETH_ALEN);
7557 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7558 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7559 else
7560 port = 0;
7561#ifdef CONFIG_INET
7562 /* allocate a socket and port for it and use it */
7563 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7564 IPPROTO_TCP, &cfg->sock, 1);
7565 if (err) {
7566 kfree(cfg);
7567 return err;
7568 }
7569 if (inet_csk_get_port(cfg->sock->sk, port)) {
7570 sock_release(cfg->sock);
7571 kfree(cfg);
7572 return -EADDRINUSE;
7573 }
7574 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7575#else
7576 if (!port) {
7577 kfree(cfg);
7578 return -EINVAL;
7579 }
7580 cfg->src_port = port;
7581#endif
7582
7583 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7584 cfg->payload_len = data_size;
7585 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7586 memcpy((void *)cfg->payload,
7587 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7588 data_size);
7589 if (seq)
7590 cfg->payload_seq = *seq;
7591 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7592 cfg->wake_len = wake_size;
7593 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7594 memcpy((void *)cfg->wake_data,
7595 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7596 wake_size);
7597 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7598 data_size + wake_size;
7599 memcpy((void *)cfg->wake_mask,
7600 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7601 wake_mask_size);
7602 if (tok) {
7603 cfg->tokens_size = tokens_size;
7604 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7605 }
7606
7607 trig->tcp = cfg;
7608
7609 return 0;
7610}
7611
Johannes Bergff1b6e62011-05-04 15:37:28 +02007612static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7613{
7614 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7615 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007616 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007617 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007618 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7619 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007620 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007621
Johannes Berg2a0e0472013-01-23 22:57:40 +01007622 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7623 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007624 return -EOPNOTSUPP;
7625
Johannes Bergae33bd82012-07-12 16:25:02 +02007626 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7627 cfg80211_rdev_free_wowlan(rdev);
7628 rdev->wowlan = NULL;
7629 goto set_wakeup;
7630 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007631
7632 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7633 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7634 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7635 nl80211_wowlan_policy);
7636 if (err)
7637 return err;
7638
7639 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7640 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7641 return -EINVAL;
7642 new_triggers.any = true;
7643 }
7644
7645 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7646 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7647 return -EINVAL;
7648 new_triggers.disconnect = true;
7649 }
7650
7651 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7652 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7653 return -EINVAL;
7654 new_triggers.magic_pkt = true;
7655 }
7656
Johannes Berg77dbbb12011-07-13 10:48:55 +02007657 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7658 return -EINVAL;
7659
7660 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7661 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7662 return -EINVAL;
7663 new_triggers.gtk_rekey_failure = true;
7664 }
7665
7666 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7667 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7668 return -EINVAL;
7669 new_triggers.eap_identity_req = true;
7670 }
7671
7672 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7673 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7674 return -EINVAL;
7675 new_triggers.four_way_handshake = true;
7676 }
7677
7678 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7679 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7680 return -EINVAL;
7681 new_triggers.rfkill_release = true;
7682 }
7683
Johannes Bergff1b6e62011-05-04 15:37:28 +02007684 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7685 struct nlattr *pat;
7686 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007687 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007688 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7689
7690 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7691 rem)
7692 n_patterns++;
7693 if (n_patterns > wowlan->n_patterns)
7694 return -EINVAL;
7695
7696 new_triggers.patterns = kcalloc(n_patterns,
7697 sizeof(new_triggers.patterns[0]),
7698 GFP_KERNEL);
7699 if (!new_triggers.patterns)
7700 return -ENOMEM;
7701
7702 new_triggers.n_patterns = n_patterns;
7703 i = 0;
7704
7705 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7706 rem) {
7707 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7708 nla_data(pat), nla_len(pat), NULL);
7709 err = -EINVAL;
7710 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7711 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7712 goto error;
7713 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7714 mask_len = DIV_ROUND_UP(pat_len, 8);
7715 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7716 mask_len)
7717 goto error;
7718 if (pat_len > wowlan->pattern_max_len ||
7719 pat_len < wowlan->pattern_min_len)
7720 goto error;
7721
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007722 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7723 pkt_offset = 0;
7724 else
7725 pkt_offset = nla_get_u32(
7726 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7727 if (pkt_offset > wowlan->max_pkt_offset)
7728 goto error;
7729 new_triggers.patterns[i].pkt_offset = pkt_offset;
7730
Johannes Bergff1b6e62011-05-04 15:37:28 +02007731 new_triggers.patterns[i].mask =
7732 kmalloc(mask_len + pat_len, GFP_KERNEL);
7733 if (!new_triggers.patterns[i].mask) {
7734 err = -ENOMEM;
7735 goto error;
7736 }
7737 new_triggers.patterns[i].pattern =
7738 new_triggers.patterns[i].mask + mask_len;
7739 memcpy(new_triggers.patterns[i].mask,
7740 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7741 mask_len);
7742 new_triggers.patterns[i].pattern_len = pat_len;
7743 memcpy(new_triggers.patterns[i].pattern,
7744 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7745 pat_len);
7746 i++;
7747 }
7748 }
7749
Johannes Berg2a0e0472013-01-23 22:57:40 +01007750 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7751 err = nl80211_parse_wowlan_tcp(
7752 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7753 &new_triggers);
7754 if (err)
7755 goto error;
7756 }
7757
Johannes Bergae33bd82012-07-12 16:25:02 +02007758 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7759 if (!ntrig) {
7760 err = -ENOMEM;
7761 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007762 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007763 cfg80211_rdev_free_wowlan(rdev);
7764 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007765
Johannes Bergae33bd82012-07-12 16:25:02 +02007766 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007767 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007768 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007769
Johannes Bergff1b6e62011-05-04 15:37:28 +02007770 return 0;
7771 error:
7772 for (i = 0; i < new_triggers.n_patterns; i++)
7773 kfree(new_triggers.patterns[i].mask);
7774 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007775 if (new_triggers.tcp && new_triggers.tcp->sock)
7776 sock_release(new_triggers.tcp->sock);
7777 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007778 return err;
7779}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007780#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007781
Johannes Berge5497d72011-07-05 16:35:40 +02007782static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7783{
7784 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7785 struct net_device *dev = info->user_ptr[1];
7786 struct wireless_dev *wdev = dev->ieee80211_ptr;
7787 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7788 struct cfg80211_gtk_rekey_data rekey_data;
7789 int err;
7790
7791 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7792 return -EINVAL;
7793
7794 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7795 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7796 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7797 nl80211_rekey_policy);
7798 if (err)
7799 return err;
7800
7801 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7802 return -ERANGE;
7803 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7804 return -ERANGE;
7805 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7806 return -ERANGE;
7807
7808 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7809 NL80211_KEK_LEN);
7810 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7811 NL80211_KCK_LEN);
7812 memcpy(rekey_data.replay_ctr,
7813 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7814 NL80211_REPLAY_CTR_LEN);
7815
7816 wdev_lock(wdev);
7817 if (!wdev->current_bss) {
7818 err = -ENOTCONN;
7819 goto out;
7820 }
7821
7822 if (!rdev->ops->set_rekey_data) {
7823 err = -EOPNOTSUPP;
7824 goto out;
7825 }
7826
Hila Gonene35e4d22012-06-27 17:19:42 +03007827 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007828 out:
7829 wdev_unlock(wdev);
7830 return err;
7831}
7832
Johannes Berg28946da2011-11-04 11:18:12 +01007833static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7834 struct genl_info *info)
7835{
7836 struct net_device *dev = info->user_ptr[1];
7837 struct wireless_dev *wdev = dev->ieee80211_ptr;
7838
7839 if (wdev->iftype != NL80211_IFTYPE_AP &&
7840 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7841 return -EINVAL;
7842
Eric W. Biederman15e47302012-09-07 20:12:54 +00007843 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007844 return -EBUSY;
7845
Eric W. Biederman15e47302012-09-07 20:12:54 +00007846 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007847 return 0;
7848}
7849
Johannes Berg7f6cf312011-11-04 11:18:15 +01007850static int nl80211_probe_client(struct sk_buff *skb,
7851 struct genl_info *info)
7852{
7853 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7854 struct net_device *dev = info->user_ptr[1];
7855 struct wireless_dev *wdev = dev->ieee80211_ptr;
7856 struct sk_buff *msg;
7857 void *hdr;
7858 const u8 *addr;
7859 u64 cookie;
7860 int err;
7861
7862 if (wdev->iftype != NL80211_IFTYPE_AP &&
7863 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7864 return -EOPNOTSUPP;
7865
7866 if (!info->attrs[NL80211_ATTR_MAC])
7867 return -EINVAL;
7868
7869 if (!rdev->ops->probe_client)
7870 return -EOPNOTSUPP;
7871
7872 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7873 if (!msg)
7874 return -ENOMEM;
7875
Eric W. Biederman15e47302012-09-07 20:12:54 +00007876 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007877 NL80211_CMD_PROBE_CLIENT);
7878
7879 if (IS_ERR(hdr)) {
7880 err = PTR_ERR(hdr);
7881 goto free_msg;
7882 }
7883
7884 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7885
Hila Gonene35e4d22012-06-27 17:19:42 +03007886 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007887 if (err)
7888 goto free_msg;
7889
David S. Miller9360ffd2012-03-29 04:41:26 -04007890 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7891 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007892
7893 genlmsg_end(msg, hdr);
7894
7895 return genlmsg_reply(msg, info);
7896
7897 nla_put_failure:
7898 err = -ENOBUFS;
7899 free_msg:
7900 nlmsg_free(msg);
7901 return err;
7902}
7903
Johannes Berg5e7602302011-11-04 11:18:17 +01007904static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7905{
7906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007907 struct cfg80211_beacon_registration *reg, *nreg;
7908 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007909
7910 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7911 return -EOPNOTSUPP;
7912
Ben Greear37c73b52012-10-26 14:49:25 -07007913 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7914 if (!nreg)
7915 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01007916
Ben Greear37c73b52012-10-26 14:49:25 -07007917 /* First, check if already registered. */
7918 spin_lock_bh(&rdev->beacon_registrations_lock);
7919 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7920 if (reg->nlportid == info->snd_portid) {
7921 rv = -EALREADY;
7922 goto out_err;
7923 }
7924 }
7925 /* Add it to the list */
7926 nreg->nlportid = info->snd_portid;
7927 list_add(&nreg->list, &rdev->beacon_registrations);
7928
7929 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01007930
7931 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007932out_err:
7933 spin_unlock_bh(&rdev->beacon_registrations_lock);
7934 kfree(nreg);
7935 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007936}
7937
Johannes Berg98104fde2012-06-16 00:19:54 +02007938static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7939{
7940 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7941 struct wireless_dev *wdev = info->user_ptr[1];
7942 int err;
7943
7944 if (!rdev->ops->start_p2p_device)
7945 return -EOPNOTSUPP;
7946
7947 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7948 return -EOPNOTSUPP;
7949
7950 if (wdev->p2p_started)
7951 return 0;
7952
7953 mutex_lock(&rdev->devlist_mtx);
7954 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7955 mutex_unlock(&rdev->devlist_mtx);
7956 if (err)
7957 return err;
7958
Johannes Bergeeb126e2012-10-23 15:16:50 +02007959 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007960 if (err)
7961 return err;
7962
7963 wdev->p2p_started = true;
7964 mutex_lock(&rdev->devlist_mtx);
7965 rdev->opencount++;
7966 mutex_unlock(&rdev->devlist_mtx);
7967
7968 return 0;
7969}
7970
7971static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7972{
7973 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7974 struct wireless_dev *wdev = info->user_ptr[1];
7975
7976 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7977 return -EOPNOTSUPP;
7978
7979 if (!rdev->ops->stop_p2p_device)
7980 return -EOPNOTSUPP;
7981
7982 if (!wdev->p2p_started)
7983 return 0;
7984
Johannes Bergeeb126e2012-10-23 15:16:50 +02007985 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007986 wdev->p2p_started = false;
7987
7988 mutex_lock(&rdev->devlist_mtx);
7989 rdev->opencount--;
7990 mutex_unlock(&rdev->devlist_mtx);
7991
7992 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7993 rdev->scan_req->aborted = true;
7994 ___cfg80211_scan_done(rdev, true);
7995 }
7996
7997 return 0;
7998}
7999
Johannes Berg3713b4e2013-02-14 16:19:38 +01008000static int nl80211_get_protocol_features(struct sk_buff *skb,
8001 struct genl_info *info)
8002{
8003 void *hdr;
8004 struct sk_buff *msg;
8005
8006 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8007 if (!msg)
8008 return -ENOMEM;
8009
8010 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8011 NL80211_CMD_GET_PROTOCOL_FEATURES);
8012 if (!hdr)
8013 goto nla_put_failure;
8014
8015 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8016 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8017 goto nla_put_failure;
8018
8019 genlmsg_end(msg, hdr);
8020 return genlmsg_reply(msg, info);
8021
8022 nla_put_failure:
8023 kfree_skb(msg);
8024 return -ENOBUFS;
8025}
8026
Johannes Berg4c476992010-10-04 21:36:35 +02008027#define NL80211_FLAG_NEED_WIPHY 0x01
8028#define NL80211_FLAG_NEED_NETDEV 0x02
8029#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008030#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8031#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8032 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008033#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008034/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008035#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8036 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008037
8038static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8039 struct genl_info *info)
8040{
8041 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008042 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008043 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008044 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8045
8046 if (rtnl)
8047 rtnl_lock();
8048
8049 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008050 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008051 if (IS_ERR(rdev)) {
8052 if (rtnl)
8053 rtnl_unlock();
8054 return PTR_ERR(rdev);
8055 }
8056 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008057 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8058 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008059 mutex_lock(&cfg80211_mutex);
8060 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8061 info->attrs);
8062 if (IS_ERR(wdev)) {
8063 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02008064 if (rtnl)
8065 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008066 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008067 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008068
Johannes Berg89a54e42012-06-15 14:33:17 +02008069 dev = wdev->netdev;
8070 rdev = wiphy_to_dev(wdev->wiphy);
8071
Johannes Berg1bf614e2012-06-15 15:23:36 +02008072 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8073 if (!dev) {
8074 mutex_unlock(&cfg80211_mutex);
8075 if (rtnl)
8076 rtnl_unlock();
8077 return -EINVAL;
8078 }
8079
8080 info->user_ptr[1] = dev;
8081 } else {
8082 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008083 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008084
Johannes Berg1bf614e2012-06-15 15:23:36 +02008085 if (dev) {
8086 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8087 !netif_running(dev)) {
8088 mutex_unlock(&cfg80211_mutex);
8089 if (rtnl)
8090 rtnl_unlock();
8091 return -ENETDOWN;
8092 }
8093
8094 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008095 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8096 if (!wdev->p2p_started) {
8097 mutex_unlock(&cfg80211_mutex);
8098 if (rtnl)
8099 rtnl_unlock();
8100 return -ENETDOWN;
8101 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008102 }
8103
Johannes Berg89a54e42012-06-15 14:33:17 +02008104 cfg80211_lock_rdev(rdev);
8105
8106 mutex_unlock(&cfg80211_mutex);
8107
Johannes Berg4c476992010-10-04 21:36:35 +02008108 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008109 }
8110
8111 return 0;
8112}
8113
8114static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8115 struct genl_info *info)
8116{
8117 if (info->user_ptr[0])
8118 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02008119 if (info->user_ptr[1]) {
8120 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8121 struct wireless_dev *wdev = info->user_ptr[1];
8122
8123 if (wdev->netdev)
8124 dev_put(wdev->netdev);
8125 } else {
8126 dev_put(info->user_ptr[1]);
8127 }
8128 }
Johannes Berg4c476992010-10-04 21:36:35 +02008129 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8130 rtnl_unlock();
8131}
8132
Johannes Berg55682962007-09-20 13:09:35 -04008133static struct genl_ops nl80211_ops[] = {
8134 {
8135 .cmd = NL80211_CMD_GET_WIPHY,
8136 .doit = nl80211_get_wiphy,
8137 .dumpit = nl80211_dump_wiphy,
8138 .policy = nl80211_policy,
8139 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008140 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04008141 },
8142 {
8143 .cmd = NL80211_CMD_SET_WIPHY,
8144 .doit = nl80211_set_wiphy,
8145 .policy = nl80211_policy,
8146 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008147 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008148 },
8149 {
8150 .cmd = NL80211_CMD_GET_INTERFACE,
8151 .doit = nl80211_get_interface,
8152 .dumpit = nl80211_dump_interface,
8153 .policy = nl80211_policy,
8154 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02008155 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04008156 },
8157 {
8158 .cmd = NL80211_CMD_SET_INTERFACE,
8159 .doit = nl80211_set_interface,
8160 .policy = nl80211_policy,
8161 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008162 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8163 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008164 },
8165 {
8166 .cmd = NL80211_CMD_NEW_INTERFACE,
8167 .doit = nl80211_new_interface,
8168 .policy = nl80211_policy,
8169 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008170 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8171 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008172 },
8173 {
8174 .cmd = NL80211_CMD_DEL_INTERFACE,
8175 .doit = nl80211_del_interface,
8176 .policy = nl80211_policy,
8177 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008178 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008179 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008180 },
Johannes Berg41ade002007-12-19 02:03:29 +01008181 {
8182 .cmd = NL80211_CMD_GET_KEY,
8183 .doit = nl80211_get_key,
8184 .policy = nl80211_policy,
8185 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008186 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008187 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008188 },
8189 {
8190 .cmd = NL80211_CMD_SET_KEY,
8191 .doit = nl80211_set_key,
8192 .policy = nl80211_policy,
8193 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008194 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008195 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008196 },
8197 {
8198 .cmd = NL80211_CMD_NEW_KEY,
8199 .doit = nl80211_new_key,
8200 .policy = nl80211_policy,
8201 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008202 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008203 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008204 },
8205 {
8206 .cmd = NL80211_CMD_DEL_KEY,
8207 .doit = nl80211_del_key,
8208 .policy = nl80211_policy,
8209 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008210 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008211 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008212 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008213 {
8214 .cmd = NL80211_CMD_SET_BEACON,
8215 .policy = nl80211_policy,
8216 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008217 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008218 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008219 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008220 },
8221 {
Johannes Berg88600202012-02-13 15:17:18 +01008222 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008223 .policy = nl80211_policy,
8224 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008225 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008226 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008227 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008228 },
8229 {
Johannes Berg88600202012-02-13 15:17:18 +01008230 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008231 .policy = nl80211_policy,
8232 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008233 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008234 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008235 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008236 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008237 {
8238 .cmd = NL80211_CMD_GET_STATION,
8239 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008240 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008241 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008242 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8243 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008244 },
8245 {
8246 .cmd = NL80211_CMD_SET_STATION,
8247 .doit = nl80211_set_station,
8248 .policy = nl80211_policy,
8249 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008250 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008251 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008252 },
8253 {
8254 .cmd = NL80211_CMD_NEW_STATION,
8255 .doit = nl80211_new_station,
8256 .policy = nl80211_policy,
8257 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008258 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008259 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008260 },
8261 {
8262 .cmd = NL80211_CMD_DEL_STATION,
8263 .doit = nl80211_del_station,
8264 .policy = nl80211_policy,
8265 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008266 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008267 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008268 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008269 {
8270 .cmd = NL80211_CMD_GET_MPATH,
8271 .doit = nl80211_get_mpath,
8272 .dumpit = nl80211_dump_mpath,
8273 .policy = nl80211_policy,
8274 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008275 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008276 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008277 },
8278 {
8279 .cmd = NL80211_CMD_SET_MPATH,
8280 .doit = nl80211_set_mpath,
8281 .policy = nl80211_policy,
8282 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008283 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008284 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008285 },
8286 {
8287 .cmd = NL80211_CMD_NEW_MPATH,
8288 .doit = nl80211_new_mpath,
8289 .policy = nl80211_policy,
8290 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008291 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008292 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008293 },
8294 {
8295 .cmd = NL80211_CMD_DEL_MPATH,
8296 .doit = nl80211_del_mpath,
8297 .policy = nl80211_policy,
8298 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008299 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008300 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008301 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008302 {
8303 .cmd = NL80211_CMD_SET_BSS,
8304 .doit = nl80211_set_bss,
8305 .policy = nl80211_policy,
8306 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008307 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008308 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008309 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008310 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008311 .cmd = NL80211_CMD_GET_REG,
8312 .doit = nl80211_get_reg,
8313 .policy = nl80211_policy,
8314 /* can be retrieved by unprivileged users */
8315 },
8316 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008317 .cmd = NL80211_CMD_SET_REG,
8318 .doit = nl80211_set_reg,
8319 .policy = nl80211_policy,
8320 .flags = GENL_ADMIN_PERM,
8321 },
8322 {
8323 .cmd = NL80211_CMD_REQ_SET_REG,
8324 .doit = nl80211_req_set_reg,
8325 .policy = nl80211_policy,
8326 .flags = GENL_ADMIN_PERM,
8327 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008328 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008329 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8330 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008331 .policy = nl80211_policy,
8332 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008333 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008334 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008335 },
8336 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008337 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8338 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008339 .policy = nl80211_policy,
8340 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008341 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008342 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008343 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008344 {
Johannes Berg2a519312009-02-10 21:25:55 +01008345 .cmd = NL80211_CMD_TRIGGER_SCAN,
8346 .doit = nl80211_trigger_scan,
8347 .policy = nl80211_policy,
8348 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008349 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008350 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008351 },
8352 {
8353 .cmd = NL80211_CMD_GET_SCAN,
8354 .policy = nl80211_policy,
8355 .dumpit = nl80211_dump_scan,
8356 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008357 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008358 .cmd = NL80211_CMD_START_SCHED_SCAN,
8359 .doit = nl80211_start_sched_scan,
8360 .policy = nl80211_policy,
8361 .flags = GENL_ADMIN_PERM,
8362 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8363 NL80211_FLAG_NEED_RTNL,
8364 },
8365 {
8366 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8367 .doit = nl80211_stop_sched_scan,
8368 .policy = nl80211_policy,
8369 .flags = GENL_ADMIN_PERM,
8370 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8371 NL80211_FLAG_NEED_RTNL,
8372 },
8373 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008374 .cmd = NL80211_CMD_AUTHENTICATE,
8375 .doit = nl80211_authenticate,
8376 .policy = nl80211_policy,
8377 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008378 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008379 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008380 },
8381 {
8382 .cmd = NL80211_CMD_ASSOCIATE,
8383 .doit = nl80211_associate,
8384 .policy = nl80211_policy,
8385 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008386 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008387 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008388 },
8389 {
8390 .cmd = NL80211_CMD_DEAUTHENTICATE,
8391 .doit = nl80211_deauthenticate,
8392 .policy = nl80211_policy,
8393 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008394 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008395 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008396 },
8397 {
8398 .cmd = NL80211_CMD_DISASSOCIATE,
8399 .doit = nl80211_disassociate,
8400 .policy = nl80211_policy,
8401 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008402 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008403 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008404 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008405 {
8406 .cmd = NL80211_CMD_JOIN_IBSS,
8407 .doit = nl80211_join_ibss,
8408 .policy = nl80211_policy,
8409 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008410 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008411 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008412 },
8413 {
8414 .cmd = NL80211_CMD_LEAVE_IBSS,
8415 .doit = nl80211_leave_ibss,
8416 .policy = nl80211_policy,
8417 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008418 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008419 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008420 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008421#ifdef CONFIG_NL80211_TESTMODE
8422 {
8423 .cmd = NL80211_CMD_TESTMODE,
8424 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008425 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008426 .policy = nl80211_policy,
8427 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008428 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8429 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008430 },
8431#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008432 {
8433 .cmd = NL80211_CMD_CONNECT,
8434 .doit = nl80211_connect,
8435 .policy = nl80211_policy,
8436 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008437 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008438 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008439 },
8440 {
8441 .cmd = NL80211_CMD_DISCONNECT,
8442 .doit = nl80211_disconnect,
8443 .policy = nl80211_policy,
8444 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008445 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008446 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008447 },
Johannes Berg463d0182009-07-14 00:33:35 +02008448 {
8449 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8450 .doit = nl80211_wiphy_netns,
8451 .policy = nl80211_policy,
8452 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008453 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8454 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008455 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008456 {
8457 .cmd = NL80211_CMD_GET_SURVEY,
8458 .policy = nl80211_policy,
8459 .dumpit = nl80211_dump_survey,
8460 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008461 {
8462 .cmd = NL80211_CMD_SET_PMKSA,
8463 .doit = nl80211_setdel_pmksa,
8464 .policy = nl80211_policy,
8465 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008466 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008467 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008468 },
8469 {
8470 .cmd = NL80211_CMD_DEL_PMKSA,
8471 .doit = nl80211_setdel_pmksa,
8472 .policy = nl80211_policy,
8473 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008474 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008475 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008476 },
8477 {
8478 .cmd = NL80211_CMD_FLUSH_PMKSA,
8479 .doit = nl80211_flush_pmksa,
8480 .policy = nl80211_policy,
8481 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008482 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008483 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008484 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008485 {
8486 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8487 .doit = nl80211_remain_on_channel,
8488 .policy = nl80211_policy,
8489 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008490 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008491 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008492 },
8493 {
8494 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8495 .doit = nl80211_cancel_remain_on_channel,
8496 .policy = nl80211_policy,
8497 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008498 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008499 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008500 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008501 {
8502 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8503 .doit = nl80211_set_tx_bitrate_mask,
8504 .policy = nl80211_policy,
8505 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008506 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8507 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008508 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008509 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008510 .cmd = NL80211_CMD_REGISTER_FRAME,
8511 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008512 .policy = nl80211_policy,
8513 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008514 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008515 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008516 },
8517 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008518 .cmd = NL80211_CMD_FRAME,
8519 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008520 .policy = nl80211_policy,
8521 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008522 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008523 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008524 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008525 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008526 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8527 .doit = nl80211_tx_mgmt_cancel_wait,
8528 .policy = nl80211_policy,
8529 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008530 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008531 NL80211_FLAG_NEED_RTNL,
8532 },
8533 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008534 .cmd = NL80211_CMD_SET_POWER_SAVE,
8535 .doit = nl80211_set_power_save,
8536 .policy = nl80211_policy,
8537 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008538 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8539 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008540 },
8541 {
8542 .cmd = NL80211_CMD_GET_POWER_SAVE,
8543 .doit = nl80211_get_power_save,
8544 .policy = nl80211_policy,
8545 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008546 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8547 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008548 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008549 {
8550 .cmd = NL80211_CMD_SET_CQM,
8551 .doit = nl80211_set_cqm,
8552 .policy = nl80211_policy,
8553 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008554 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8555 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008556 },
Johannes Bergf444de02010-05-05 15:25:02 +02008557 {
8558 .cmd = NL80211_CMD_SET_CHANNEL,
8559 .doit = nl80211_set_channel,
8560 .policy = nl80211_policy,
8561 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008562 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8563 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008564 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008565 {
8566 .cmd = NL80211_CMD_SET_WDS_PEER,
8567 .doit = nl80211_set_wds_peer,
8568 .policy = nl80211_policy,
8569 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008570 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8571 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008572 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008573 {
8574 .cmd = NL80211_CMD_JOIN_MESH,
8575 .doit = nl80211_join_mesh,
8576 .policy = nl80211_policy,
8577 .flags = GENL_ADMIN_PERM,
8578 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8579 NL80211_FLAG_NEED_RTNL,
8580 },
8581 {
8582 .cmd = NL80211_CMD_LEAVE_MESH,
8583 .doit = nl80211_leave_mesh,
8584 .policy = nl80211_policy,
8585 .flags = GENL_ADMIN_PERM,
8586 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8587 NL80211_FLAG_NEED_RTNL,
8588 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008589#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008590 {
8591 .cmd = NL80211_CMD_GET_WOWLAN,
8592 .doit = nl80211_get_wowlan,
8593 .policy = nl80211_policy,
8594 /* can be retrieved by unprivileged users */
8595 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8596 NL80211_FLAG_NEED_RTNL,
8597 },
8598 {
8599 .cmd = NL80211_CMD_SET_WOWLAN,
8600 .doit = nl80211_set_wowlan,
8601 .policy = nl80211_policy,
8602 .flags = GENL_ADMIN_PERM,
8603 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8604 NL80211_FLAG_NEED_RTNL,
8605 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008606#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008607 {
8608 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8609 .doit = nl80211_set_rekey_data,
8610 .policy = nl80211_policy,
8611 .flags = GENL_ADMIN_PERM,
8612 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8613 NL80211_FLAG_NEED_RTNL,
8614 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008615 {
8616 .cmd = NL80211_CMD_TDLS_MGMT,
8617 .doit = nl80211_tdls_mgmt,
8618 .policy = nl80211_policy,
8619 .flags = GENL_ADMIN_PERM,
8620 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8621 NL80211_FLAG_NEED_RTNL,
8622 },
8623 {
8624 .cmd = NL80211_CMD_TDLS_OPER,
8625 .doit = nl80211_tdls_oper,
8626 .policy = nl80211_policy,
8627 .flags = GENL_ADMIN_PERM,
8628 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8629 NL80211_FLAG_NEED_RTNL,
8630 },
Johannes Berg28946da2011-11-04 11:18:12 +01008631 {
8632 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8633 .doit = nl80211_register_unexpected_frame,
8634 .policy = nl80211_policy,
8635 .flags = GENL_ADMIN_PERM,
8636 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8637 NL80211_FLAG_NEED_RTNL,
8638 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008639 {
8640 .cmd = NL80211_CMD_PROBE_CLIENT,
8641 .doit = nl80211_probe_client,
8642 .policy = nl80211_policy,
8643 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008644 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008645 NL80211_FLAG_NEED_RTNL,
8646 },
Johannes Berg5e7602302011-11-04 11:18:17 +01008647 {
8648 .cmd = NL80211_CMD_REGISTER_BEACONS,
8649 .doit = nl80211_register_beacons,
8650 .policy = nl80211_policy,
8651 .flags = GENL_ADMIN_PERM,
8652 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8653 NL80211_FLAG_NEED_RTNL,
8654 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008655 {
8656 .cmd = NL80211_CMD_SET_NOACK_MAP,
8657 .doit = nl80211_set_noack_map,
8658 .policy = nl80211_policy,
8659 .flags = GENL_ADMIN_PERM,
8660 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8661 NL80211_FLAG_NEED_RTNL,
8662 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008663 {
8664 .cmd = NL80211_CMD_START_P2P_DEVICE,
8665 .doit = nl80211_start_p2p_device,
8666 .policy = nl80211_policy,
8667 .flags = GENL_ADMIN_PERM,
8668 .internal_flags = NL80211_FLAG_NEED_WDEV |
8669 NL80211_FLAG_NEED_RTNL,
8670 },
8671 {
8672 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8673 .doit = nl80211_stop_p2p_device,
8674 .policy = nl80211_policy,
8675 .flags = GENL_ADMIN_PERM,
8676 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8677 NL80211_FLAG_NEED_RTNL,
8678 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008679 {
8680 .cmd = NL80211_CMD_SET_MCAST_RATE,
8681 .doit = nl80211_set_mcast_rate,
8682 .policy = nl80211_policy,
8683 .flags = GENL_ADMIN_PERM,
8684 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8685 NL80211_FLAG_NEED_RTNL,
8686 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308687 {
8688 .cmd = NL80211_CMD_SET_MAC_ACL,
8689 .doit = nl80211_set_mac_acl,
8690 .policy = nl80211_policy,
8691 .flags = GENL_ADMIN_PERM,
8692 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8693 NL80211_FLAG_NEED_RTNL,
8694 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008695 {
8696 .cmd = NL80211_CMD_RADAR_DETECT,
8697 .doit = nl80211_start_radar_detection,
8698 .policy = nl80211_policy,
8699 .flags = GENL_ADMIN_PERM,
8700 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8701 NL80211_FLAG_NEED_RTNL,
8702 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01008703 {
8704 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8705 .doit = nl80211_get_protocol_features,
8706 .policy = nl80211_policy,
8707 },
Johannes Berg55682962007-09-20 13:09:35 -04008708};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008709
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008710static struct genl_multicast_group nl80211_mlme_mcgrp = {
8711 .name = "mlme",
8712};
Johannes Berg55682962007-09-20 13:09:35 -04008713
8714/* multicast groups */
8715static struct genl_multicast_group nl80211_config_mcgrp = {
8716 .name = "config",
8717};
Johannes Berg2a519312009-02-10 21:25:55 +01008718static struct genl_multicast_group nl80211_scan_mcgrp = {
8719 .name = "scan",
8720};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008721static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8722 .name = "regulatory",
8723};
Johannes Berg55682962007-09-20 13:09:35 -04008724
8725/* notification functions */
8726
8727void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8728{
8729 struct sk_buff *msg;
8730
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008731 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008732 if (!msg)
8733 return;
8734
Johannes Berg3713b4e2013-02-14 16:19:38 +01008735 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
8736 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04008737 nlmsg_free(msg);
8738 return;
8739 }
8740
Johannes Berg463d0182009-07-14 00:33:35 +02008741 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8742 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008743}
8744
Johannes Berg362a4152009-05-24 16:43:15 +02008745static int nl80211_add_scan_req(struct sk_buff *msg,
8746 struct cfg80211_registered_device *rdev)
8747{
8748 struct cfg80211_scan_request *req = rdev->scan_req;
8749 struct nlattr *nest;
8750 int i;
8751
Johannes Berg667503dd2009-07-07 03:56:11 +02008752 ASSERT_RDEV_LOCK(rdev);
8753
Johannes Berg362a4152009-05-24 16:43:15 +02008754 if (WARN_ON(!req))
8755 return 0;
8756
8757 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8758 if (!nest)
8759 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008760 for (i = 0; i < req->n_ssids; i++) {
8761 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8762 goto nla_put_failure;
8763 }
Johannes Berg362a4152009-05-24 16:43:15 +02008764 nla_nest_end(msg, nest);
8765
8766 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8767 if (!nest)
8768 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008769 for (i = 0; i < req->n_channels; i++) {
8770 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8771 goto nla_put_failure;
8772 }
Johannes Berg362a4152009-05-24 16:43:15 +02008773 nla_nest_end(msg, nest);
8774
David S. Miller9360ffd2012-03-29 04:41:26 -04008775 if (req->ie &&
8776 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8777 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008778
Sam Lefflered4737712012-10-11 21:03:31 -07008779 if (req->flags)
8780 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8781
Johannes Berg362a4152009-05-24 16:43:15 +02008782 return 0;
8783 nla_put_failure:
8784 return -ENOBUFS;
8785}
8786
Johannes Berga538e2d2009-06-16 19:56:42 +02008787static int nl80211_send_scan_msg(struct sk_buff *msg,
8788 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008789 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008790 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008791 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008792{
8793 void *hdr;
8794
Eric W. Biederman15e47302012-09-07 20:12:54 +00008795 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008796 if (!hdr)
8797 return -1;
8798
David S. Miller9360ffd2012-03-29 04:41:26 -04008799 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008800 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8801 wdev->netdev->ifindex)) ||
8802 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008803 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008804
Johannes Berg362a4152009-05-24 16:43:15 +02008805 /* ignore errors and send incomplete event anyway */
8806 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008807
8808 return genlmsg_end(msg, hdr);
8809
8810 nla_put_failure:
8811 genlmsg_cancel(msg, hdr);
8812 return -EMSGSIZE;
8813}
8814
Luciano Coelho807f8a82011-05-11 17:09:35 +03008815static int
8816nl80211_send_sched_scan_msg(struct sk_buff *msg,
8817 struct cfg80211_registered_device *rdev,
8818 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008819 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008820{
8821 void *hdr;
8822
Eric W. Biederman15e47302012-09-07 20:12:54 +00008823 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008824 if (!hdr)
8825 return -1;
8826
David S. Miller9360ffd2012-03-29 04:41:26 -04008827 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8828 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8829 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008830
8831 return genlmsg_end(msg, hdr);
8832
8833 nla_put_failure:
8834 genlmsg_cancel(msg, hdr);
8835 return -EMSGSIZE;
8836}
8837
Johannes Berga538e2d2009-06-16 19:56:42 +02008838void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008839 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008840{
8841 struct sk_buff *msg;
8842
Thomas Graf58050fc2012-06-28 03:57:45 +00008843 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008844 if (!msg)
8845 return;
8846
Johannes Bergfd014282012-06-18 19:17:03 +02008847 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008848 NL80211_CMD_TRIGGER_SCAN) < 0) {
8849 nlmsg_free(msg);
8850 return;
8851 }
8852
Johannes Berg463d0182009-07-14 00:33:35 +02008853 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8854 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008855}
8856
Johannes Berg2a519312009-02-10 21:25:55 +01008857void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008858 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008859{
8860 struct sk_buff *msg;
8861
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008863 if (!msg)
8864 return;
8865
Johannes Bergfd014282012-06-18 19:17:03 +02008866 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008867 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008868 nlmsg_free(msg);
8869 return;
8870 }
8871
Johannes Berg463d0182009-07-14 00:33:35 +02008872 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8873 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008874}
8875
8876void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008877 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008878{
8879 struct sk_buff *msg;
8880
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008881 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008882 if (!msg)
8883 return;
8884
Johannes Bergfd014282012-06-18 19:17:03 +02008885 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008886 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008887 nlmsg_free(msg);
8888 return;
8889 }
8890
Johannes Berg463d0182009-07-14 00:33:35 +02008891 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8892 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008893}
8894
Luciano Coelho807f8a82011-05-11 17:09:35 +03008895void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8896 struct net_device *netdev)
8897{
8898 struct sk_buff *msg;
8899
8900 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8901 if (!msg)
8902 return;
8903
8904 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8905 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8906 nlmsg_free(msg);
8907 return;
8908 }
8909
8910 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8911 nl80211_scan_mcgrp.id, GFP_KERNEL);
8912}
8913
8914void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8915 struct net_device *netdev, u32 cmd)
8916{
8917 struct sk_buff *msg;
8918
Thomas Graf58050fc2012-06-28 03:57:45 +00008919 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008920 if (!msg)
8921 return;
8922
8923 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8924 nlmsg_free(msg);
8925 return;
8926 }
8927
8928 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8929 nl80211_scan_mcgrp.id, GFP_KERNEL);
8930}
8931
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008932/*
8933 * This can happen on global regulatory changes or device specific settings
8934 * based on custom world regulatory domains.
8935 */
8936void nl80211_send_reg_change_event(struct regulatory_request *request)
8937{
8938 struct sk_buff *msg;
8939 void *hdr;
8940
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008941 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008942 if (!msg)
8943 return;
8944
8945 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8946 if (!hdr) {
8947 nlmsg_free(msg);
8948 return;
8949 }
8950
8951 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008952 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8953 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008954
David S. Miller9360ffd2012-03-29 04:41:26 -04008955 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8956 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8957 NL80211_REGDOM_TYPE_WORLD))
8958 goto nla_put_failure;
8959 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8960 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8961 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8962 goto nla_put_failure;
8963 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8964 request->intersect) {
8965 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8966 NL80211_REGDOM_TYPE_INTERSECTION))
8967 goto nla_put_failure;
8968 } else {
8969 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8970 NL80211_REGDOM_TYPE_COUNTRY) ||
8971 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8972 request->alpha2))
8973 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008974 }
8975
Johannes Bergf4173762012-12-03 18:23:37 +01008976 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008977 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8978 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008979
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008980 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008981
Johannes Bergbc43b282009-07-25 10:54:13 +02008982 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008983 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008984 GFP_ATOMIC);
8985 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008986
8987 return;
8988
8989nla_put_failure:
8990 genlmsg_cancel(msg, hdr);
8991 nlmsg_free(msg);
8992}
8993
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008994static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8995 struct net_device *netdev,
8996 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008997 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008998{
8999 struct sk_buff *msg;
9000 void *hdr;
9001
Johannes Berge6d6e342009-07-01 21:26:47 +02009002 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009003 if (!msg)
9004 return;
9005
9006 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9007 if (!hdr) {
9008 nlmsg_free(msg);
9009 return;
9010 }
9011
David S. Miller9360ffd2012-03-29 04:41:26 -04009012 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9013 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9014 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9015 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009016
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009017 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009018
Johannes Berg463d0182009-07-14 00:33:35 +02009019 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9020 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009021 return;
9022
9023 nla_put_failure:
9024 genlmsg_cancel(msg, hdr);
9025 nlmsg_free(msg);
9026}
9027
9028void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009029 struct net_device *netdev, const u8 *buf,
9030 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009031{
9032 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009033 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009034}
9035
9036void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9037 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009038 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009039{
Johannes Berge6d6e342009-07-01 21:26:47 +02009040 nl80211_send_mlme_event(rdev, netdev, buf, len,
9041 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009042}
9043
Jouni Malinen53b46b82009-03-27 20:53:56 +02009044void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009045 struct net_device *netdev, const u8 *buf,
9046 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009047{
9048 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009049 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009050}
9051
Jouni Malinen53b46b82009-03-27 20:53:56 +02009052void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9053 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009054 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009055{
9056 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009057 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009058}
9059
Jouni Malinencf4e5942010-12-16 00:52:40 +02009060void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
9061 struct net_device *netdev, const u8 *buf,
9062 size_t len, gfp_t gfp)
9063{
9064 nl80211_send_mlme_event(rdev, netdev, buf, len,
9065 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
9066}
9067
9068void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
9069 struct net_device *netdev, const u8 *buf,
9070 size_t len, gfp_t gfp)
9071{
9072 nl80211_send_mlme_event(rdev, netdev, buf, len,
9073 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
9074}
9075
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009076static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9077 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009078 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009079{
9080 struct sk_buff *msg;
9081 void *hdr;
9082
Johannes Berge6d6e342009-07-01 21:26:47 +02009083 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009084 if (!msg)
9085 return;
9086
9087 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9088 if (!hdr) {
9089 nlmsg_free(msg);
9090 return;
9091 }
9092
David S. Miller9360ffd2012-03-29 04:41:26 -04009093 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9094 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9095 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9096 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9097 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009098
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009099 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009100
Johannes Berg463d0182009-07-14 00:33:35 +02009101 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9102 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009103 return;
9104
9105 nla_put_failure:
9106 genlmsg_cancel(msg, hdr);
9107 nlmsg_free(msg);
9108}
9109
9110void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009111 struct net_device *netdev, const u8 *addr,
9112 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009113{
9114 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009115 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009116}
9117
9118void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009119 struct net_device *netdev, const u8 *addr,
9120 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009121{
Johannes Berge6d6e342009-07-01 21:26:47 +02009122 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9123 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009124}
9125
Samuel Ortizb23aa672009-07-01 21:26:54 +02009126void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9127 struct net_device *netdev, const u8 *bssid,
9128 const u8 *req_ie, size_t req_ie_len,
9129 const u8 *resp_ie, size_t resp_ie_len,
9130 u16 status, gfp_t gfp)
9131{
9132 struct sk_buff *msg;
9133 void *hdr;
9134
Thomas Graf58050fc2012-06-28 03:57:45 +00009135 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009136 if (!msg)
9137 return;
9138
9139 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9140 if (!hdr) {
9141 nlmsg_free(msg);
9142 return;
9143 }
9144
David S. Miller9360ffd2012-03-29 04:41:26 -04009145 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9146 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9147 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9148 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9149 (req_ie &&
9150 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9151 (resp_ie &&
9152 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9153 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009154
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009155 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009156
Johannes Berg463d0182009-07-14 00:33:35 +02009157 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9158 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009159 return;
9160
9161 nla_put_failure:
9162 genlmsg_cancel(msg, hdr);
9163 nlmsg_free(msg);
9164
9165}
9166
9167void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9168 struct net_device *netdev, const u8 *bssid,
9169 const u8 *req_ie, size_t req_ie_len,
9170 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9171{
9172 struct sk_buff *msg;
9173 void *hdr;
9174
Thomas Graf58050fc2012-06-28 03:57:45 +00009175 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009176 if (!msg)
9177 return;
9178
9179 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9180 if (!hdr) {
9181 nlmsg_free(msg);
9182 return;
9183 }
9184
David S. Miller9360ffd2012-03-29 04:41:26 -04009185 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9186 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9187 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9188 (req_ie &&
9189 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9190 (resp_ie &&
9191 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9192 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009193
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009194 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009195
Johannes Berg463d0182009-07-14 00:33:35 +02009196 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9197 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009198 return;
9199
9200 nla_put_failure:
9201 genlmsg_cancel(msg, hdr);
9202 nlmsg_free(msg);
9203
9204}
9205
9206void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9207 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02009208 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009209{
9210 struct sk_buff *msg;
9211 void *hdr;
9212
Thomas Graf58050fc2012-06-28 03:57:45 +00009213 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009214 if (!msg)
9215 return;
9216
9217 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9218 if (!hdr) {
9219 nlmsg_free(msg);
9220 return;
9221 }
9222
David S. Miller9360ffd2012-03-29 04:41:26 -04009223 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9224 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9225 (from_ap && reason &&
9226 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9227 (from_ap &&
9228 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9229 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9230 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009231
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009232 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009233
Johannes Berg463d0182009-07-14 00:33:35 +02009234 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9235 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009236 return;
9237
9238 nla_put_failure:
9239 genlmsg_cancel(msg, hdr);
9240 nlmsg_free(msg);
9241
9242}
9243
Johannes Berg04a773a2009-04-19 21:24:32 +02009244void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9245 struct net_device *netdev, const u8 *bssid,
9246 gfp_t gfp)
9247{
9248 struct sk_buff *msg;
9249 void *hdr;
9250
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009251 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009252 if (!msg)
9253 return;
9254
9255 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9256 if (!hdr) {
9257 nlmsg_free(msg);
9258 return;
9259 }
9260
David S. Miller9360ffd2012-03-29 04:41:26 -04009261 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9262 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9263 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9264 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009265
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009266 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009267
Johannes Berg463d0182009-07-14 00:33:35 +02009268 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9269 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009270 return;
9271
9272 nla_put_failure:
9273 genlmsg_cancel(msg, hdr);
9274 nlmsg_free(msg);
9275}
9276
Javier Cardonac93b5e72011-04-07 15:08:34 -07009277void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
9278 struct net_device *netdev,
9279 const u8 *macaddr, const u8* ie, u8 ie_len,
9280 gfp_t gfp)
9281{
9282 struct sk_buff *msg;
9283 void *hdr;
9284
9285 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9286 if (!msg)
9287 return;
9288
9289 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9290 if (!hdr) {
9291 nlmsg_free(msg);
9292 return;
9293 }
9294
David S. Miller9360ffd2012-03-29 04:41:26 -04009295 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9296 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9297 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
9298 (ie_len && ie &&
9299 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9300 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009301
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009302 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009303
9304 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9305 nl80211_mlme_mcgrp.id, gfp);
9306 return;
9307
9308 nla_put_failure:
9309 genlmsg_cancel(msg, hdr);
9310 nlmsg_free(msg);
9311}
9312
Jouni Malinena3b8b052009-03-27 21:59:49 +02009313void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9314 struct net_device *netdev, const u8 *addr,
9315 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009316 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009317{
9318 struct sk_buff *msg;
9319 void *hdr;
9320
Johannes Berge6d6e342009-07-01 21:26:47 +02009321 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009322 if (!msg)
9323 return;
9324
9325 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9326 if (!hdr) {
9327 nlmsg_free(msg);
9328 return;
9329 }
9330
David S. Miller9360ffd2012-03-29 04:41:26 -04009331 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9332 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9333 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9334 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9335 (key_id != -1 &&
9336 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9337 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9338 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009339
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009340 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009341
Johannes Berg463d0182009-07-14 00:33:35 +02009342 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9343 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009344 return;
9345
9346 nla_put_failure:
9347 genlmsg_cancel(msg, hdr);
9348 nlmsg_free(msg);
9349}
9350
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009351void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9352 struct ieee80211_channel *channel_before,
9353 struct ieee80211_channel *channel_after)
9354{
9355 struct sk_buff *msg;
9356 void *hdr;
9357 struct nlattr *nl_freq;
9358
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009360 if (!msg)
9361 return;
9362
9363 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9364 if (!hdr) {
9365 nlmsg_free(msg);
9366 return;
9367 }
9368
9369 /*
9370 * Since we are applying the beacon hint to a wiphy we know its
9371 * wiphy_idx is valid
9372 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009373 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9374 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009375
9376 /* Before */
9377 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9378 if (!nl_freq)
9379 goto nla_put_failure;
9380 if (nl80211_msg_put_channel(msg, channel_before))
9381 goto nla_put_failure;
9382 nla_nest_end(msg, nl_freq);
9383
9384 /* After */
9385 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9386 if (!nl_freq)
9387 goto nla_put_failure;
9388 if (nl80211_msg_put_channel(msg, channel_after))
9389 goto nla_put_failure;
9390 nla_nest_end(msg, nl_freq);
9391
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009392 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009393
Johannes Berg463d0182009-07-14 00:33:35 +02009394 rcu_read_lock();
9395 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9396 GFP_ATOMIC);
9397 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009398
9399 return;
9400
9401nla_put_failure:
9402 genlmsg_cancel(msg, hdr);
9403 nlmsg_free(msg);
9404}
9405
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009406static void nl80211_send_remain_on_chan_event(
9407 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009408 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009409 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009410 unsigned int duration, gfp_t gfp)
9411{
9412 struct sk_buff *msg;
9413 void *hdr;
9414
9415 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9416 if (!msg)
9417 return;
9418
9419 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9420 if (!hdr) {
9421 nlmsg_free(msg);
9422 return;
9423 }
9424
David S. Miller9360ffd2012-03-29 04:41:26 -04009425 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009426 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9427 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009428 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009429 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009430 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9431 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009432 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9433 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009434
David S. Miller9360ffd2012-03-29 04:41:26 -04009435 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9436 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9437 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009438
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009439 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009440
9441 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9442 nl80211_mlme_mcgrp.id, gfp);
9443 return;
9444
9445 nla_put_failure:
9446 genlmsg_cancel(msg, hdr);
9447 nlmsg_free(msg);
9448}
9449
9450void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009451 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009452 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009453 unsigned int duration, gfp_t gfp)
9454{
9455 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009456 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009457 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009458}
9459
9460void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009461 struct cfg80211_registered_device *rdev,
9462 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009463 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009464{
9465 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009466 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009467}
9468
Johannes Berg98b62182009-12-23 13:15:44 +01009469void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9470 struct net_device *dev, const u8 *mac_addr,
9471 struct station_info *sinfo, gfp_t gfp)
9472{
9473 struct sk_buff *msg;
9474
Thomas Graf58050fc2012-06-28 03:57:45 +00009475 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009476 if (!msg)
9477 return;
9478
John W. Linville66266b32012-03-15 13:25:41 -04009479 if (nl80211_send_station(msg, 0, 0, 0,
9480 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009481 nlmsg_free(msg);
9482 return;
9483 }
9484
9485 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9486 nl80211_mlme_mcgrp.id, gfp);
9487}
9488
Jouni Malinenec15e682011-03-23 15:29:52 +02009489void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9490 struct net_device *dev, const u8 *mac_addr,
9491 gfp_t gfp)
9492{
9493 struct sk_buff *msg;
9494 void *hdr;
9495
Thomas Graf58050fc2012-06-28 03:57:45 +00009496 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009497 if (!msg)
9498 return;
9499
9500 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9501 if (!hdr) {
9502 nlmsg_free(msg);
9503 return;
9504 }
9505
David S. Miller9360ffd2012-03-29 04:41:26 -04009506 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9507 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9508 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009509
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009510 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009511
9512 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9513 nl80211_mlme_mcgrp.id, gfp);
9514 return;
9515
9516 nla_put_failure:
9517 genlmsg_cancel(msg, hdr);
9518 nlmsg_free(msg);
9519}
9520
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309521void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9522 struct net_device *dev, const u8 *mac_addr,
9523 enum nl80211_connect_failed_reason reason,
9524 gfp_t gfp)
9525{
9526 struct sk_buff *msg;
9527 void *hdr;
9528
9529 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9530 if (!msg)
9531 return;
9532
9533 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9534 if (!hdr) {
9535 nlmsg_free(msg);
9536 return;
9537 }
9538
9539 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9540 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9541 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9542 goto nla_put_failure;
9543
9544 genlmsg_end(msg, hdr);
9545
9546 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9547 nl80211_mlme_mcgrp.id, gfp);
9548 return;
9549
9550 nla_put_failure:
9551 genlmsg_cancel(msg, hdr);
9552 nlmsg_free(msg);
9553}
9554
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009555static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9556 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009557{
9558 struct wireless_dev *wdev = dev->ieee80211_ptr;
9559 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9560 struct sk_buff *msg;
9561 void *hdr;
9562 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009563 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009564
Eric W. Biederman15e47302012-09-07 20:12:54 +00009565 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009566 return false;
9567
9568 msg = nlmsg_new(100, gfp);
9569 if (!msg)
9570 return true;
9571
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009572 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009573 if (!hdr) {
9574 nlmsg_free(msg);
9575 return true;
9576 }
9577
David S. Miller9360ffd2012-03-29 04:41:26 -04009578 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9579 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9580 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9581 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009582
9583 err = genlmsg_end(msg, hdr);
9584 if (err < 0) {
9585 nlmsg_free(msg);
9586 return true;
9587 }
9588
Eric W. Biederman15e47302012-09-07 20:12:54 +00009589 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009590 return true;
9591
9592 nla_put_failure:
9593 genlmsg_cancel(msg, hdr);
9594 nlmsg_free(msg);
9595 return true;
9596}
9597
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009598bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9599{
9600 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9601 addr, gfp);
9602}
9603
9604bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9605 const u8 *addr, gfp_t gfp)
9606{
9607 return __nl80211_unexpected_frame(dev,
9608 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9609 addr, gfp);
9610}
9611
Johannes Berg2e161f72010-08-12 15:38:38 +02009612int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009613 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009614 int freq, int sig_dbm,
9615 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009616{
Johannes Berg71bbc992012-06-15 15:30:18 +02009617 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009618 struct sk_buff *msg;
9619 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009620
9621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9622 if (!msg)
9623 return -ENOMEM;
9624
Johannes Berg2e161f72010-08-12 15:38:38 +02009625 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009626 if (!hdr) {
9627 nlmsg_free(msg);
9628 return -ENOMEM;
9629 }
9630
David S. Miller9360ffd2012-03-29 04:41:26 -04009631 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009632 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9633 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009634 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9635 (sig_dbm &&
9636 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9637 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9638 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009639
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009640 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009641
Eric W. Biederman15e47302012-09-07 20:12:54 +00009642 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009643
9644 nla_put_failure:
9645 genlmsg_cancel(msg, hdr);
9646 nlmsg_free(msg);
9647 return -ENOBUFS;
9648}
9649
Johannes Berg2e161f72010-08-12 15:38:38 +02009650void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009651 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009652 const u8 *buf, size_t len, bool ack,
9653 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009654{
Johannes Berg71bbc992012-06-15 15:30:18 +02009655 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009656 struct sk_buff *msg;
9657 void *hdr;
9658
9659 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9660 if (!msg)
9661 return;
9662
Johannes Berg2e161f72010-08-12 15:38:38 +02009663 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009664 if (!hdr) {
9665 nlmsg_free(msg);
9666 return;
9667 }
9668
David S. Miller9360ffd2012-03-29 04:41:26 -04009669 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009670 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9671 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009672 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9673 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9674 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9675 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009676
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009677 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009678
9679 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9680 return;
9681
9682 nla_put_failure:
9683 genlmsg_cancel(msg, hdr);
9684 nlmsg_free(msg);
9685}
9686
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009687void
9688nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9689 struct net_device *netdev,
9690 enum nl80211_cqm_rssi_threshold_event rssi_event,
9691 gfp_t gfp)
9692{
9693 struct sk_buff *msg;
9694 struct nlattr *pinfoattr;
9695 void *hdr;
9696
Thomas Graf58050fc2012-06-28 03:57:45 +00009697 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009698 if (!msg)
9699 return;
9700
9701 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9702 if (!hdr) {
9703 nlmsg_free(msg);
9704 return;
9705 }
9706
David S. Miller9360ffd2012-03-29 04:41:26 -04009707 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9708 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9709 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009710
9711 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9712 if (!pinfoattr)
9713 goto nla_put_failure;
9714
David S. Miller9360ffd2012-03-29 04:41:26 -04009715 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9716 rssi_event))
9717 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009718
9719 nla_nest_end(msg, pinfoattr);
9720
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009721 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009722
9723 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9724 nl80211_mlme_mcgrp.id, gfp);
9725 return;
9726
9727 nla_put_failure:
9728 genlmsg_cancel(msg, hdr);
9729 nlmsg_free(msg);
9730}
9731
Johannes Berge5497d72011-07-05 16:35:40 +02009732void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9733 struct net_device *netdev, const u8 *bssid,
9734 const u8 *replay_ctr, gfp_t gfp)
9735{
9736 struct sk_buff *msg;
9737 struct nlattr *rekey_attr;
9738 void *hdr;
9739
Thomas Graf58050fc2012-06-28 03:57:45 +00009740 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009741 if (!msg)
9742 return;
9743
9744 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9745 if (!hdr) {
9746 nlmsg_free(msg);
9747 return;
9748 }
9749
David S. Miller9360ffd2012-03-29 04:41:26 -04009750 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9751 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9752 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9753 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009754
9755 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9756 if (!rekey_attr)
9757 goto nla_put_failure;
9758
David S. Miller9360ffd2012-03-29 04:41:26 -04009759 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9760 NL80211_REPLAY_CTR_LEN, replay_ctr))
9761 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009762
9763 nla_nest_end(msg, rekey_attr);
9764
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009765 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009766
9767 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9768 nl80211_mlme_mcgrp.id, gfp);
9769 return;
9770
9771 nla_put_failure:
9772 genlmsg_cancel(msg, hdr);
9773 nlmsg_free(msg);
9774}
9775
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009776void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9777 struct net_device *netdev, int index,
9778 const u8 *bssid, bool preauth, gfp_t gfp)
9779{
9780 struct sk_buff *msg;
9781 struct nlattr *attr;
9782 void *hdr;
9783
Thomas Graf58050fc2012-06-28 03:57:45 +00009784 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009785 if (!msg)
9786 return;
9787
9788 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9789 if (!hdr) {
9790 nlmsg_free(msg);
9791 return;
9792 }
9793
David S. Miller9360ffd2012-03-29 04:41:26 -04009794 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9795 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9796 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009797
9798 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9799 if (!attr)
9800 goto nla_put_failure;
9801
David S. Miller9360ffd2012-03-29 04:41:26 -04009802 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9803 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9804 (preauth &&
9805 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9806 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009807
9808 nla_nest_end(msg, attr);
9809
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009810 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009811
9812 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9813 nl80211_mlme_mcgrp.id, gfp);
9814 return;
9815
9816 nla_put_failure:
9817 genlmsg_cancel(msg, hdr);
9818 nlmsg_free(msg);
9819}
9820
Thomas Pedersen53145262012-04-06 13:35:47 -07009821void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009822 struct net_device *netdev,
9823 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009824{
9825 struct sk_buff *msg;
9826 void *hdr;
9827
Thomas Graf58050fc2012-06-28 03:57:45 +00009828 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009829 if (!msg)
9830 return;
9831
9832 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9833 if (!hdr) {
9834 nlmsg_free(msg);
9835 return;
9836 }
9837
Johannes Berg683b6d32012-11-08 21:25:48 +01009838 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9839 goto nla_put_failure;
9840
9841 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009842 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009843
9844 genlmsg_end(msg, hdr);
9845
9846 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9847 nl80211_mlme_mcgrp.id, gfp);
9848 return;
9849
9850 nla_put_failure:
9851 genlmsg_cancel(msg, hdr);
9852 nlmsg_free(msg);
9853}
9854
Johannes Bergc063dbf2010-11-24 08:10:05 +01009855void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009856nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9857 struct net_device *netdev, const u8 *peer,
9858 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9859{
9860 struct sk_buff *msg;
9861 struct nlattr *pinfoattr;
9862 void *hdr;
9863
9864 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9865 if (!msg)
9866 return;
9867
9868 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9869 if (!hdr) {
9870 nlmsg_free(msg);
9871 return;
9872 }
9873
9874 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9875 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9876 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9877 goto nla_put_failure;
9878
9879 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9880 if (!pinfoattr)
9881 goto nla_put_failure;
9882
9883 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9884 goto nla_put_failure;
9885
9886 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9887 goto nla_put_failure;
9888
9889 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9890 goto nla_put_failure;
9891
9892 nla_nest_end(msg, pinfoattr);
9893
9894 genlmsg_end(msg, hdr);
9895
9896 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9897 nl80211_mlme_mcgrp.id, gfp);
9898 return;
9899
9900 nla_put_failure:
9901 genlmsg_cancel(msg, hdr);
9902 nlmsg_free(msg);
9903}
9904
9905void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009906nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9907 struct cfg80211_chan_def *chandef,
9908 enum nl80211_radar_event event,
9909 struct net_device *netdev, gfp_t gfp)
9910{
9911 struct sk_buff *msg;
9912 void *hdr;
9913
9914 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9915 if (!msg)
9916 return;
9917
9918 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9919 if (!hdr) {
9920 nlmsg_free(msg);
9921 return;
9922 }
9923
9924 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9925 goto nla_put_failure;
9926
9927 /* NOP and radar events don't need a netdev parameter */
9928 if (netdev) {
9929 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9930
9931 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9932 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9933 goto nla_put_failure;
9934 }
9935
9936 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9937 goto nla_put_failure;
9938
9939 if (nl80211_send_chandef(msg, chandef))
9940 goto nla_put_failure;
9941
9942 if (genlmsg_end(msg, hdr) < 0) {
9943 nlmsg_free(msg);
9944 return;
9945 }
9946
9947 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9948 nl80211_mlme_mcgrp.id, gfp);
9949 return;
9950
9951 nla_put_failure:
9952 genlmsg_cancel(msg, hdr);
9953 nlmsg_free(msg);
9954}
9955
9956void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009957nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9958 struct net_device *netdev, const u8 *peer,
9959 u32 num_packets, gfp_t gfp)
9960{
9961 struct sk_buff *msg;
9962 struct nlattr *pinfoattr;
9963 void *hdr;
9964
Thomas Graf58050fc2012-06-28 03:57:45 +00009965 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009966 if (!msg)
9967 return;
9968
9969 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9970 if (!hdr) {
9971 nlmsg_free(msg);
9972 return;
9973 }
9974
David S. Miller9360ffd2012-03-29 04:41:26 -04009975 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9976 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9977 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9978 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009979
9980 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9981 if (!pinfoattr)
9982 goto nla_put_failure;
9983
David S. Miller9360ffd2012-03-29 04:41:26 -04009984 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9985 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009986
9987 nla_nest_end(msg, pinfoattr);
9988
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009989 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009990
9991 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9992 nl80211_mlme_mcgrp.id, gfp);
9993 return;
9994
9995 nla_put_failure:
9996 genlmsg_cancel(msg, hdr);
9997 nlmsg_free(msg);
9998}
9999
Johannes Berg7f6cf312011-11-04 11:18:15 +010010000void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10001 u64 cookie, bool acked, gfp_t gfp)
10002{
10003 struct wireless_dev *wdev = dev->ieee80211_ptr;
10004 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10005 struct sk_buff *msg;
10006 void *hdr;
10007 int err;
10008
Beni Lev4ee3e062012-08-27 12:49:39 +030010009 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10010
Thomas Graf58050fc2012-06-28 03:57:45 +000010011 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010012
Johannes Berg7f6cf312011-11-04 11:18:15 +010010013 if (!msg)
10014 return;
10015
10016 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10017 if (!hdr) {
10018 nlmsg_free(msg);
10019 return;
10020 }
10021
David S. Miller9360ffd2012-03-29 04:41:26 -040010022 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10023 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10024 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10025 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10026 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10027 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010028
10029 err = genlmsg_end(msg, hdr);
10030 if (err < 0) {
10031 nlmsg_free(msg);
10032 return;
10033 }
10034
10035 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10036 nl80211_mlme_mcgrp.id, gfp);
10037 return;
10038
10039 nla_put_failure:
10040 genlmsg_cancel(msg, hdr);
10041 nlmsg_free(msg);
10042}
10043EXPORT_SYMBOL(cfg80211_probe_status);
10044
Johannes Berg5e7602302011-11-04 11:18:17 +010010045void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10046 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010047 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010010048{
10049 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10050 struct sk_buff *msg;
10051 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010052 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010010053
Beni Lev4ee3e062012-08-27 12:49:39 +030010054 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10055
Ben Greear37c73b52012-10-26 14:49:25 -070010056 spin_lock_bh(&rdev->beacon_registrations_lock);
10057 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10058 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10059 if (!msg) {
10060 spin_unlock_bh(&rdev->beacon_registrations_lock);
10061 return;
10062 }
Johannes Berg5e7602302011-11-04 11:18:17 +010010063
Ben Greear37c73b52012-10-26 14:49:25 -070010064 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10065 if (!hdr)
10066 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010010067
Ben Greear37c73b52012-10-26 14:49:25 -070010068 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10069 (freq &&
10070 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10071 (sig_dbm &&
10072 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10073 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10074 goto nla_put_failure;
10075
10076 genlmsg_end(msg, hdr);
10077
10078 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010010079 }
Ben Greear37c73b52012-10-26 14:49:25 -070010080 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010081 return;
10082
10083 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010084 spin_unlock_bh(&rdev->beacon_registrations_lock);
10085 if (hdr)
10086 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010010087 nlmsg_free(msg);
10088}
10089EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10090
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010091#ifdef CONFIG_PM
10092void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10093 struct cfg80211_wowlan_wakeup *wakeup,
10094 gfp_t gfp)
10095{
10096 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10097 struct sk_buff *msg;
10098 void *hdr;
10099 int err, size = 200;
10100
10101 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10102
10103 if (wakeup)
10104 size += wakeup->packet_present_len;
10105
10106 msg = nlmsg_new(size, gfp);
10107 if (!msg)
10108 return;
10109
10110 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10111 if (!hdr)
10112 goto free_msg;
10113
10114 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10115 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10116 goto free_msg;
10117
10118 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10119 wdev->netdev->ifindex))
10120 goto free_msg;
10121
10122 if (wakeup) {
10123 struct nlattr *reasons;
10124
10125 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10126
10127 if (wakeup->disconnect &&
10128 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10129 goto free_msg;
10130 if (wakeup->magic_pkt &&
10131 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10132 goto free_msg;
10133 if (wakeup->gtk_rekey_failure &&
10134 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10135 goto free_msg;
10136 if (wakeup->eap_identity_req &&
10137 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10138 goto free_msg;
10139 if (wakeup->four_way_handshake &&
10140 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10141 goto free_msg;
10142 if (wakeup->rfkill_release &&
10143 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10144 goto free_msg;
10145
10146 if (wakeup->pattern_idx >= 0 &&
10147 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10148 wakeup->pattern_idx))
10149 goto free_msg;
10150
Johannes Berg2a0e0472013-01-23 22:57:40 +010010151 if (wakeup->tcp_match)
10152 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10153
10154 if (wakeup->tcp_connlost)
10155 nla_put_flag(msg,
10156 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10157
10158 if (wakeup->tcp_nomoretokens)
10159 nla_put_flag(msg,
10160 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10161
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010162 if (wakeup->packet) {
10163 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10164 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10165
10166 if (!wakeup->packet_80211) {
10167 pkt_attr =
10168 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10169 len_attr =
10170 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10171 }
10172
10173 if (wakeup->packet_len &&
10174 nla_put_u32(msg, len_attr, wakeup->packet_len))
10175 goto free_msg;
10176
10177 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10178 wakeup->packet))
10179 goto free_msg;
10180 }
10181
10182 nla_nest_end(msg, reasons);
10183 }
10184
10185 err = genlmsg_end(msg, hdr);
10186 if (err < 0)
10187 goto free_msg;
10188
10189 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10190 nl80211_mlme_mcgrp.id, gfp);
10191 return;
10192
10193 free_msg:
10194 nlmsg_free(msg);
10195}
10196EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10197#endif
10198
Jouni Malinen3475b092012-11-16 22:49:57 +020010199void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10200 enum nl80211_tdls_operation oper,
10201 u16 reason_code, gfp_t gfp)
10202{
10203 struct wireless_dev *wdev = dev->ieee80211_ptr;
10204 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10205 struct sk_buff *msg;
10206 void *hdr;
10207 int err;
10208
10209 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10210 reason_code);
10211
10212 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10213 if (!msg)
10214 return;
10215
10216 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10217 if (!hdr) {
10218 nlmsg_free(msg);
10219 return;
10220 }
10221
10222 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10223 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10224 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10225 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10226 (reason_code > 0 &&
10227 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10228 goto nla_put_failure;
10229
10230 err = genlmsg_end(msg, hdr);
10231 if (err < 0) {
10232 nlmsg_free(msg);
10233 return;
10234 }
10235
10236 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10237 nl80211_mlme_mcgrp.id, gfp);
10238 return;
10239
10240 nla_put_failure:
10241 genlmsg_cancel(msg, hdr);
10242 nlmsg_free(msg);
10243}
10244EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10245
Jouni Malinen026331c2010-02-15 12:53:10 +020010246static int nl80211_netlink_notify(struct notifier_block * nb,
10247 unsigned long state,
10248 void *_notify)
10249{
10250 struct netlink_notify *notify = _notify;
10251 struct cfg80211_registered_device *rdev;
10252 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010253 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010254
10255 if (state != NETLINK_URELEASE)
10256 return NOTIFY_DONE;
10257
10258 rcu_read_lock();
10259
Johannes Berg5e7602302011-11-04 11:18:17 +010010260 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010261 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010262 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010263
10264 spin_lock_bh(&rdev->beacon_registrations_lock);
10265 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10266 list) {
10267 if (reg->nlportid == notify->portid) {
10268 list_del(&reg->list);
10269 kfree(reg);
10270 break;
10271 }
10272 }
10273 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010274 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010275
10276 rcu_read_unlock();
10277
10278 return NOTIFY_DONE;
10279}
10280
10281static struct notifier_block nl80211_netlink_notifier = {
10282 .notifier_call = nl80211_netlink_notify,
10283};
10284
Johannes Berg55682962007-09-20 13:09:35 -040010285/* initialisation/exit functions */
10286
10287int nl80211_init(void)
10288{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010289 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010290
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010291 err = genl_register_family_with_ops(&nl80211_fam,
10292 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010293 if (err)
10294 return err;
10295
Johannes Berg55682962007-09-20 13:09:35 -040010296 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10297 if (err)
10298 goto err_out;
10299
Johannes Berg2a519312009-02-10 21:25:55 +010010300 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10301 if (err)
10302 goto err_out;
10303
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010304 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10305 if (err)
10306 goto err_out;
10307
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010308 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10309 if (err)
10310 goto err_out;
10311
Johannes Bergaff89a92009-07-01 21:26:51 +020010312#ifdef CONFIG_NL80211_TESTMODE
10313 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10314 if (err)
10315 goto err_out;
10316#endif
10317
Jouni Malinen026331c2010-02-15 12:53:10 +020010318 err = netlink_register_notifier(&nl80211_netlink_notifier);
10319 if (err)
10320 goto err_out;
10321
Johannes Berg55682962007-09-20 13:09:35 -040010322 return 0;
10323 err_out:
10324 genl_unregister_family(&nl80211_fam);
10325 return err;
10326}
10327
10328void nl80211_exit(void)
10329{
Jouni Malinen026331c2010-02-15 12:53:10 +020010330 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010331 genl_unregister_family(&nl80211_fam);
10332}