blob: f687a8d0d0265ddbdfad92c250884e339a93fad9 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100373 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100374 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
375 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
376 .len = NL80211_VHT_CAPABILITY_LEN,
377 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200378 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
379 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
380 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg55682962007-09-20 13:09:35 -0400381};
382
Johannes Berge31b8212010-10-05 19:39:30 +0200383/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000384static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200385 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200386 [NL80211_KEY_IDX] = { .type = NLA_U8 },
387 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200388 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200389 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
390 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200391 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100392 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
393};
394
395/* policy for the key default flags */
396static const struct nla_policy
397nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
398 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
399 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200400};
401
Johannes Bergff1b6e62011-05-04 15:37:28 +0200402/* policy for WoWLAN attributes */
403static const struct nla_policy
404nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
405 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
406 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
407 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
408 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200409 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
410 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
411 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
412 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100413 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
414};
415
416static const struct nla_policy
417nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
418 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
419 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
420 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
421 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
422 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
423 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
425 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
426 },
427 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
428 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
429 },
430 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
431 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
432 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200433};
434
Johannes Berge5497d72011-07-05 16:35:40 +0200435/* policy for GTK rekey offload attributes */
436static const struct nla_policy
437nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
438 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
439 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
440 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
441};
442
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300443static const struct nla_policy
444nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200445 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300446 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700447 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300448};
449
Johannes Berg97990a02013-04-19 01:02:55 +0200450static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
451 struct netlink_callback *cb,
452 struct cfg80211_registered_device **rdev,
453 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100454{
Johannes Berg67748892010-10-04 21:14:06 +0200455 int err;
456
Johannes Berg67748892010-10-04 21:14:06 +0200457 rtnl_lock();
Johannes Berg97990a02013-04-19 01:02:55 +0200458 mutex_lock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200459
Johannes Berg97990a02013-04-19 01:02:55 +0200460 if (!cb->args[0]) {
461 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
462 nl80211_fam.attrbuf, nl80211_fam.maxattr,
463 nl80211_policy);
464 if (err)
465 goto out_unlock;
466
467 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
468 nl80211_fam.attrbuf);
469 if (IS_ERR(*wdev)) {
470 err = PTR_ERR(*wdev);
471 goto out_unlock;
472 }
473 *rdev = wiphy_to_dev((*wdev)->wiphy);
474 cb->args[0] = (*rdev)->wiphy_idx;
475 cb->args[1] = (*wdev)->identifier;
476 } else {
477 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0]);
478 struct wireless_dev *tmp;
479
480 if (!wiphy) {
481 err = -ENODEV;
482 goto out_unlock;
483 }
484 *rdev = wiphy_to_dev(wiphy);
485 *wdev = NULL;
486
487 mutex_lock(&(*rdev)->devlist_mtx);
488 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
489 if (tmp->identifier == cb->args[1]) {
490 *wdev = tmp;
491 break;
492 }
493 }
494 mutex_unlock(&(*rdev)->devlist_mtx);
495
496 if (!*wdev) {
497 err = -ENODEV;
498 goto out_unlock;
499 }
Johannes Berg67748892010-10-04 21:14:06 +0200500 }
501
Johannes Berg97990a02013-04-19 01:02:55 +0200502 cfg80211_lock_rdev(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200503
Johannes Berg97990a02013-04-19 01:02:55 +0200504 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200505 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200506 out_unlock:
507 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200508 rtnl_unlock();
509 return err;
510}
511
Johannes Berg97990a02013-04-19 01:02:55 +0200512static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200513{
514 cfg80211_unlock_rdev(rdev);
515 rtnl_unlock();
516}
517
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100518/* IE validation */
519static bool is_valid_ie_attr(const struct nlattr *attr)
520{
521 const u8 *pos;
522 int len;
523
524 if (!attr)
525 return true;
526
527 pos = nla_data(attr);
528 len = nla_len(attr);
529
530 while (len) {
531 u8 elemlen;
532
533 if (len < 2)
534 return false;
535 len -= 2;
536
537 elemlen = pos[1];
538 if (elemlen > len)
539 return false;
540
541 len -= elemlen;
542 pos += 2 + elemlen;
543 }
544
545 return true;
546}
547
Johannes Berg55682962007-09-20 13:09:35 -0400548/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000549static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400550 int flags, u8 cmd)
551{
552 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000553 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400554}
555
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400556static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100557 struct ieee80211_channel *chan,
558 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400559{
David S. Miller9360ffd2012-03-29 04:41:26 -0400560 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
561 chan->center_freq))
562 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400563
David S. Miller9360ffd2012-03-29 04:41:26 -0400564 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
565 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
566 goto nla_put_failure;
567 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
568 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
569 goto nla_put_failure;
570 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
571 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
572 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100573 if (chan->flags & IEEE80211_CHAN_RADAR) {
574 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
575 goto nla_put_failure;
576 if (large) {
577 u32 time;
578
579 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
580
581 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
582 chan->dfs_state))
583 goto nla_put_failure;
584 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
585 time))
586 goto nla_put_failure;
587 }
588 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400589
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100590 if (large) {
591 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
592 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
593 goto nla_put_failure;
594 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
595 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
596 goto nla_put_failure;
597 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
598 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
599 goto nla_put_failure;
600 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
601 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
602 goto nla_put_failure;
603 }
604
David S. Miller9360ffd2012-03-29 04:41:26 -0400605 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
606 DBM_TO_MBM(chan->max_power)))
607 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400608
609 return 0;
610
611 nla_put_failure:
612 return -ENOBUFS;
613}
614
Johannes Berg55682962007-09-20 13:09:35 -0400615/* netlink command implementations */
616
Johannes Bergb9454e82009-07-08 13:29:08 +0200617struct key_parse {
618 struct key_params p;
619 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200620 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200621 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100622 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200623};
624
625static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
626{
627 struct nlattr *tb[NL80211_KEY_MAX + 1];
628 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
629 nl80211_key_policy);
630 if (err)
631 return err;
632
633 k->def = !!tb[NL80211_KEY_DEFAULT];
634 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
635
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100636 if (k->def) {
637 k->def_uni = true;
638 k->def_multi = true;
639 }
640 if (k->defmgmt)
641 k->def_multi = true;
642
Johannes Bergb9454e82009-07-08 13:29:08 +0200643 if (tb[NL80211_KEY_IDX])
644 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
645
646 if (tb[NL80211_KEY_DATA]) {
647 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
648 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
649 }
650
651 if (tb[NL80211_KEY_SEQ]) {
652 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
653 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
654 }
655
656 if (tb[NL80211_KEY_CIPHER])
657 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
658
Johannes Berge31b8212010-10-05 19:39:30 +0200659 if (tb[NL80211_KEY_TYPE]) {
660 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
661 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
662 return -EINVAL;
663 }
664
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100665 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
666 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100667 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
668 tb[NL80211_KEY_DEFAULT_TYPES],
669 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100670 if (err)
671 return err;
672
673 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
674 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
675 }
676
Johannes Bergb9454e82009-07-08 13:29:08 +0200677 return 0;
678}
679
680static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
681{
682 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
683 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
684 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
685 }
686
687 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
688 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
689 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
690 }
691
692 if (info->attrs[NL80211_ATTR_KEY_IDX])
693 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
694
695 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
696 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
697
698 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
699 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
700
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100701 if (k->def) {
702 k->def_uni = true;
703 k->def_multi = true;
704 }
705 if (k->defmgmt)
706 k->def_multi = true;
707
Johannes Berge31b8212010-10-05 19:39:30 +0200708 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
709 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
710 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
711 return -EINVAL;
712 }
713
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100714 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
715 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
716 int err = nla_parse_nested(
717 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
718 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
719 nl80211_key_default_policy);
720 if (err)
721 return err;
722
723 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
724 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
725 }
726
Johannes Bergb9454e82009-07-08 13:29:08 +0200727 return 0;
728}
729
730static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
731{
732 int err;
733
734 memset(k, 0, sizeof(*k));
735 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200736 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200737
738 if (info->attrs[NL80211_ATTR_KEY])
739 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
740 else
741 err = nl80211_parse_key_old(info, k);
742
743 if (err)
744 return err;
745
746 if (k->def && k->defmgmt)
747 return -EINVAL;
748
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100749 if (k->defmgmt) {
750 if (k->def_uni || !k->def_multi)
751 return -EINVAL;
752 }
753
Johannes Bergb9454e82009-07-08 13:29:08 +0200754 if (k->idx != -1) {
755 if (k->defmgmt) {
756 if (k->idx < 4 || k->idx > 5)
757 return -EINVAL;
758 } else if (k->def) {
759 if (k->idx < 0 || k->idx > 3)
760 return -EINVAL;
761 } else {
762 if (k->idx < 0 || k->idx > 5)
763 return -EINVAL;
764 }
765 }
766
767 return 0;
768}
769
Johannes Bergfffd0932009-07-08 14:22:54 +0200770static struct cfg80211_cached_keys *
771nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530772 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200773{
774 struct key_parse parse;
775 struct nlattr *key;
776 struct cfg80211_cached_keys *result;
777 int rem, err, def = 0;
778
779 result = kzalloc(sizeof(*result), GFP_KERNEL);
780 if (!result)
781 return ERR_PTR(-ENOMEM);
782
783 result->def = -1;
784 result->defmgmt = -1;
785
786 nla_for_each_nested(key, keys, rem) {
787 memset(&parse, 0, sizeof(parse));
788 parse.idx = -1;
789
790 err = nl80211_parse_key_new(key, &parse);
791 if (err)
792 goto error;
793 err = -EINVAL;
794 if (!parse.p.key)
795 goto error;
796 if (parse.idx < 0 || parse.idx > 4)
797 goto error;
798 if (parse.def) {
799 if (def)
800 goto error;
801 def = 1;
802 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100803 if (!parse.def_uni || !parse.def_multi)
804 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200805 } else if (parse.defmgmt)
806 goto error;
807 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200808 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200809 if (err)
810 goto error;
811 result->params[parse.idx].cipher = parse.p.cipher;
812 result->params[parse.idx].key_len = parse.p.key_len;
813 result->params[parse.idx].key = result->data[parse.idx];
814 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530815
816 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
817 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
818 if (no_ht)
819 *no_ht = true;
820 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200821 }
822
823 return result;
824 error:
825 kfree(result);
826 return ERR_PTR(err);
827}
828
829static int nl80211_key_allowed(struct wireless_dev *wdev)
830{
831 ASSERT_WDEV_LOCK(wdev);
832
Johannes Bergfffd0932009-07-08 14:22:54 +0200833 switch (wdev->iftype) {
834 case NL80211_IFTYPE_AP:
835 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200836 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700837 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200838 break;
839 case NL80211_IFTYPE_ADHOC:
840 if (!wdev->current_bss)
841 return -ENOLINK;
842 break;
843 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200844 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200845 if (wdev->sme_state != CFG80211_SME_CONNECTED)
846 return -ENOLINK;
847 break;
848 default:
849 return -EINVAL;
850 }
851
852 return 0;
853}
854
Johannes Berg7527a782011-05-13 10:58:57 +0200855static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
856{
857 struct nlattr *nl_modes = nla_nest_start(msg, attr);
858 int i;
859
860 if (!nl_modes)
861 goto nla_put_failure;
862
863 i = 0;
864 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400865 if ((ifmodes & 1) && nla_put_flag(msg, i))
866 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200867 ifmodes >>= 1;
868 i++;
869 }
870
871 nla_nest_end(msg, nl_modes);
872 return 0;
873
874nla_put_failure:
875 return -ENOBUFS;
876}
877
878static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100879 struct sk_buff *msg,
880 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200881{
882 struct nlattr *nl_combis;
883 int i, j;
884
885 nl_combis = nla_nest_start(msg,
886 NL80211_ATTR_INTERFACE_COMBINATIONS);
887 if (!nl_combis)
888 goto nla_put_failure;
889
890 for (i = 0; i < wiphy->n_iface_combinations; i++) {
891 const struct ieee80211_iface_combination *c;
892 struct nlattr *nl_combi, *nl_limits;
893
894 c = &wiphy->iface_combinations[i];
895
896 nl_combi = nla_nest_start(msg, i + 1);
897 if (!nl_combi)
898 goto nla_put_failure;
899
900 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
901 if (!nl_limits)
902 goto nla_put_failure;
903
904 for (j = 0; j < c->n_limits; j++) {
905 struct nlattr *nl_limit;
906
907 nl_limit = nla_nest_start(msg, j + 1);
908 if (!nl_limit)
909 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400910 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
911 c->limits[j].max))
912 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200913 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
914 c->limits[j].types))
915 goto nla_put_failure;
916 nla_nest_end(msg, nl_limit);
917 }
918
919 nla_nest_end(msg, nl_limits);
920
David S. Miller9360ffd2012-03-29 04:41:26 -0400921 if (c->beacon_int_infra_match &&
922 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
923 goto nla_put_failure;
924 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
925 c->num_different_channels) ||
926 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
927 c->max_interfaces))
928 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100929 if (large &&
930 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
931 c->radar_detect_widths))
932 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200933
934 nla_nest_end(msg, nl_combi);
935 }
936
937 nla_nest_end(msg, nl_combis);
938
939 return 0;
940nla_put_failure:
941 return -ENOBUFS;
942}
943
Johannes Berg3713b4e2013-02-14 16:19:38 +0100944#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100945static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
946 struct sk_buff *msg)
947{
948 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
949 struct nlattr *nl_tcp;
950
951 if (!tcp)
952 return 0;
953
954 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
955 if (!nl_tcp)
956 return -ENOBUFS;
957
958 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
959 tcp->data_payload_max))
960 return -ENOBUFS;
961
962 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
963 tcp->data_payload_max))
964 return -ENOBUFS;
965
966 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
967 return -ENOBUFS;
968
969 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
970 sizeof(*tcp->tok), tcp->tok))
971 return -ENOBUFS;
972
973 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
974 tcp->data_interval_max))
975 return -ENOBUFS;
976
977 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
978 tcp->wake_payload_max))
979 return -ENOBUFS;
980
981 nla_nest_end(msg, nl_tcp);
982 return 0;
983}
984
Johannes Berg3713b4e2013-02-14 16:19:38 +0100985static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +0100986 struct cfg80211_registered_device *dev,
987 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100988{
989 struct nlattr *nl_wowlan;
990
991 if (!dev->wiphy.wowlan.flags && !dev->wiphy.wowlan.n_patterns)
992 return 0;
993
994 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
995 if (!nl_wowlan)
996 return -ENOBUFS;
997
998 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
999 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1000 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1001 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1002 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1003 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1004 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1005 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1006 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1007 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1008 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1009 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1010 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1011 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1012 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1013 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1014 return -ENOBUFS;
1015
1016 if (dev->wiphy.wowlan.n_patterns) {
1017 struct nl80211_wowlan_pattern_support pat = {
1018 .max_patterns = dev->wiphy.wowlan.n_patterns,
1019 .min_pattern_len = dev->wiphy.wowlan.pattern_min_len,
1020 .max_pattern_len = dev->wiphy.wowlan.pattern_max_len,
1021 .max_pkt_offset = dev->wiphy.wowlan.max_pkt_offset,
1022 };
1023
1024 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1025 sizeof(pat), &pat))
1026 return -ENOBUFS;
1027 }
1028
Johannes Bergb56cf722013-02-20 01:02:38 +01001029 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1030 return -ENOBUFS;
1031
Johannes Berg3713b4e2013-02-14 16:19:38 +01001032 nla_nest_end(msg, nl_wowlan);
1033
1034 return 0;
1035}
1036#endif
1037
1038static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1039 struct ieee80211_supported_band *sband)
1040{
1041 struct nlattr *nl_rates, *nl_rate;
1042 struct ieee80211_rate *rate;
1043 int i;
1044
1045 /* add HT info */
1046 if (sband->ht_cap.ht_supported &&
1047 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1048 sizeof(sband->ht_cap.mcs),
1049 &sband->ht_cap.mcs) ||
1050 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1051 sband->ht_cap.cap) ||
1052 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1053 sband->ht_cap.ampdu_factor) ||
1054 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1055 sband->ht_cap.ampdu_density)))
1056 return -ENOBUFS;
1057
1058 /* add VHT info */
1059 if (sband->vht_cap.vht_supported &&
1060 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1061 sizeof(sband->vht_cap.vht_mcs),
1062 &sband->vht_cap.vht_mcs) ||
1063 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1064 sband->vht_cap.cap)))
1065 return -ENOBUFS;
1066
1067 /* add bitrates */
1068 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1069 if (!nl_rates)
1070 return -ENOBUFS;
1071
1072 for (i = 0; i < sband->n_bitrates; i++) {
1073 nl_rate = nla_nest_start(msg, i);
1074 if (!nl_rate)
1075 return -ENOBUFS;
1076
1077 rate = &sband->bitrates[i];
1078 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1079 rate->bitrate))
1080 return -ENOBUFS;
1081 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1082 nla_put_flag(msg,
1083 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1084 return -ENOBUFS;
1085
1086 nla_nest_end(msg, nl_rate);
1087 }
1088
1089 nla_nest_end(msg, nl_rates);
1090
1091 return 0;
1092}
1093
1094static int
1095nl80211_send_mgmt_stypes(struct sk_buff *msg,
1096 const struct ieee80211_txrx_stypes *mgmt_stypes)
1097{
1098 u16 stypes;
1099 struct nlattr *nl_ftypes, *nl_ifs;
1100 enum nl80211_iftype ift;
1101 int i;
1102
1103 if (!mgmt_stypes)
1104 return 0;
1105
1106 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1107 if (!nl_ifs)
1108 return -ENOBUFS;
1109
1110 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1111 nl_ftypes = nla_nest_start(msg, ift);
1112 if (!nl_ftypes)
1113 return -ENOBUFS;
1114 i = 0;
1115 stypes = mgmt_stypes[ift].tx;
1116 while (stypes) {
1117 if ((stypes & 1) &&
1118 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1119 (i << 4) | IEEE80211_FTYPE_MGMT))
1120 return -ENOBUFS;
1121 stypes >>= 1;
1122 i++;
1123 }
1124 nla_nest_end(msg, nl_ftypes);
1125 }
1126
1127 nla_nest_end(msg, nl_ifs);
1128
1129 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1130 if (!nl_ifs)
1131 return -ENOBUFS;
1132
1133 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1134 nl_ftypes = nla_nest_start(msg, ift);
1135 if (!nl_ftypes)
1136 return -ENOBUFS;
1137 i = 0;
1138 stypes = mgmt_stypes[ift].rx;
1139 while (stypes) {
1140 if ((stypes & 1) &&
1141 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1142 (i << 4) | IEEE80211_FTYPE_MGMT))
1143 return -ENOBUFS;
1144 stypes >>= 1;
1145 i++;
1146 }
1147 nla_nest_end(msg, nl_ftypes);
1148 }
1149 nla_nest_end(msg, nl_ifs);
1150
1151 return 0;
1152}
1153
1154static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1155 struct sk_buff *msg, u32 portid, u32 seq,
1156 int flags, bool split, long *split_start,
1157 long *band_start, long *chan_start)
Johannes Berg55682962007-09-20 13:09:35 -04001158{
1159 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001160 struct nlattr *nl_bands, *nl_band;
1161 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001162 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001163 enum ieee80211_band band;
1164 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001165 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001166 const struct ieee80211_txrx_stypes *mgmt_stypes =
1167 dev->wiphy.mgmt_stypes;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001168 long start = 0, start_chan = 0, start_band = 0;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001169 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001170
Eric W. Biederman15e47302012-09-07 20:12:54 +00001171 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001172 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001173 return -ENOBUFS;
1174
1175 /* allow always using the variables */
1176 if (!split) {
1177 split_start = &start;
1178 band_start = &start_band;
1179 chan_start = &start_chan;
1180 }
Johannes Berg55682962007-09-20 13:09:35 -04001181
David S. Miller9360ffd2012-03-29 04:41:26 -04001182 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001183 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1184 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001185 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001186 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001187 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001188
Johannes Berg3713b4e2013-02-14 16:19:38 +01001189 switch (*split_start) {
1190 case 0:
1191 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1192 dev->wiphy.retry_short) ||
1193 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1194 dev->wiphy.retry_long) ||
1195 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1196 dev->wiphy.frag_threshold) ||
1197 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1198 dev->wiphy.rts_threshold) ||
1199 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1200 dev->wiphy.coverage_class) ||
1201 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1202 dev->wiphy.max_scan_ssids) ||
1203 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1204 dev->wiphy.max_sched_scan_ssids) ||
1205 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1206 dev->wiphy.max_scan_ie_len) ||
1207 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1208 dev->wiphy.max_sched_scan_ie_len) ||
1209 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1210 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001211 goto nla_put_failure;
1212
Johannes Berg3713b4e2013-02-14 16:19:38 +01001213 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1214 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1215 goto nla_put_failure;
1216 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1217 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1218 goto nla_put_failure;
1219 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1220 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1221 goto nla_put_failure;
1222 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1223 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1224 goto nla_put_failure;
1225 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1226 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1227 goto nla_put_failure;
1228 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1229 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001230 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001231
Johannes Berg3713b4e2013-02-14 16:19:38 +01001232 (*split_start)++;
1233 if (split)
1234 break;
1235 case 1:
1236 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1237 sizeof(u32) * dev->wiphy.n_cipher_suites,
1238 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001239 goto nla_put_failure;
1240
Johannes Berg3713b4e2013-02-14 16:19:38 +01001241 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1242 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001243 goto nla_put_failure;
1244
Johannes Berg3713b4e2013-02-14 16:19:38 +01001245 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1246 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1247 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001248
Johannes Berg3713b4e2013-02-14 16:19:38 +01001249 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1250 dev->wiphy.available_antennas_tx) ||
1251 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1252 dev->wiphy.available_antennas_rx))
1253 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001254
Johannes Berg3713b4e2013-02-14 16:19:38 +01001255 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1256 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1257 dev->wiphy.probe_resp_offload))
1258 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001259
Johannes Berg3713b4e2013-02-14 16:19:38 +01001260 if ((dev->wiphy.available_antennas_tx ||
1261 dev->wiphy.available_antennas_rx) &&
1262 dev->ops->get_antenna) {
1263 u32 tx_ant = 0, rx_ant = 0;
1264 int res;
1265 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1266 if (!res) {
1267 if (nla_put_u32(msg,
1268 NL80211_ATTR_WIPHY_ANTENNA_TX,
1269 tx_ant) ||
1270 nla_put_u32(msg,
1271 NL80211_ATTR_WIPHY_ANTENNA_RX,
1272 rx_ant))
1273 goto nla_put_failure;
1274 }
Johannes Bergee688b002008-01-24 19:38:39 +01001275 }
1276
Johannes Berg3713b4e2013-02-14 16:19:38 +01001277 (*split_start)++;
1278 if (split)
1279 break;
1280 case 2:
1281 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1282 dev->wiphy.interface_modes))
1283 goto nla_put_failure;
1284 (*split_start)++;
1285 if (split)
1286 break;
1287 case 3:
1288 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1289 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001290 goto nla_put_failure;
1291
Johannes Berg3713b4e2013-02-14 16:19:38 +01001292 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1293 struct ieee80211_supported_band *sband;
1294
1295 sband = dev->wiphy.bands[band];
1296
1297 if (!sband)
1298 continue;
1299
1300 nl_band = nla_nest_start(msg, band);
1301 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001302 goto nla_put_failure;
1303
Johannes Berg3713b4e2013-02-14 16:19:38 +01001304 switch (*chan_start) {
1305 case 0:
1306 if (nl80211_send_band_rateinfo(msg, sband))
1307 goto nla_put_failure;
1308 (*chan_start)++;
1309 if (split)
1310 break;
1311 default:
1312 /* add frequencies */
1313 nl_freqs = nla_nest_start(
1314 msg, NL80211_BAND_ATTR_FREQS);
1315 if (!nl_freqs)
1316 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001317
Johannes Berg3713b4e2013-02-14 16:19:38 +01001318 for (i = *chan_start - 1;
1319 i < sband->n_channels;
1320 i++) {
1321 nl_freq = nla_nest_start(msg, i);
1322 if (!nl_freq)
1323 goto nla_put_failure;
1324
1325 chan = &sband->channels[i];
1326
Johannes Bergcdc89b92013-02-18 23:54:36 +01001327 if (nl80211_msg_put_channel(msg, chan,
1328 split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001329 goto nla_put_failure;
1330
1331 nla_nest_end(msg, nl_freq);
1332 if (split)
1333 break;
1334 }
1335 if (i < sband->n_channels)
1336 *chan_start = i + 2;
1337 else
1338 *chan_start = 0;
1339 nla_nest_end(msg, nl_freqs);
1340 }
1341
1342 nla_nest_end(msg, nl_band);
1343
1344 if (split) {
1345 /* start again here */
1346 if (*chan_start)
1347 band--;
1348 break;
1349 }
Johannes Bergee688b002008-01-24 19:38:39 +01001350 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001352
Johannes Berg3713b4e2013-02-14 16:19:38 +01001353 if (band < IEEE80211_NUM_BANDS)
1354 *band_start = band + 1;
1355 else
1356 *band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001357
Johannes Berg3713b4e2013-02-14 16:19:38 +01001358 /* if bands & channels are done, continue outside */
1359 if (*band_start == 0 && *chan_start == 0)
1360 (*split_start)++;
1361 if (split)
1362 break;
1363 case 4:
1364 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1365 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001366 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001367
1368 i = 0;
1369#define CMD(op, n) \
1370 do { \
1371 if (dev->ops->op) { \
1372 i++; \
1373 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1374 goto nla_put_failure; \
1375 } \
1376 } while (0)
1377
1378 CMD(add_virtual_intf, NEW_INTERFACE);
1379 CMD(change_virtual_intf, SET_INTERFACE);
1380 CMD(add_key, NEW_KEY);
1381 CMD(start_ap, START_AP);
1382 CMD(add_station, NEW_STATION);
1383 CMD(add_mpath, NEW_MPATH);
1384 CMD(update_mesh_config, SET_MESH_CONFIG);
1385 CMD(change_bss, SET_BSS);
1386 CMD(auth, AUTHENTICATE);
1387 CMD(assoc, ASSOCIATE);
1388 CMD(deauth, DEAUTHENTICATE);
1389 CMD(disassoc, DISASSOCIATE);
1390 CMD(join_ibss, JOIN_IBSS);
1391 CMD(join_mesh, JOIN_MESH);
1392 CMD(set_pmksa, SET_PMKSA);
1393 CMD(del_pmksa, DEL_PMKSA);
1394 CMD(flush_pmksa, FLUSH_PMKSA);
1395 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1396 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1397 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1398 CMD(mgmt_tx, FRAME);
1399 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1400 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1401 i++;
1402 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1403 goto nla_put_failure;
1404 }
1405 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1406 dev->ops->join_mesh) {
1407 i++;
1408 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1409 goto nla_put_failure;
1410 }
1411 CMD(set_wds_peer, SET_WDS_PEER);
1412 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1413 CMD(tdls_mgmt, TDLS_MGMT);
1414 CMD(tdls_oper, TDLS_OPER);
1415 }
1416 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1417 CMD(sched_scan_start, START_SCHED_SCAN);
1418 CMD(probe_client, PROBE_CLIENT);
1419 CMD(set_noack_map, SET_NOACK_MAP);
1420 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1421 i++;
1422 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1423 goto nla_put_failure;
1424 }
1425 CMD(start_p2p_device, START_P2P_DEVICE);
1426 CMD(set_mcast_rate, SET_MCAST_RATE);
Arend van Spriel5de17982013-04-18 15:49:00 +02001427 if (split) {
1428 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1429 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1430 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001431
Kalle Valo4745fc02011-11-17 19:06:10 +02001432#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001433 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001434#endif
1435
Johannes Berg8fdc6212009-03-14 09:34:01 +01001436#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001437
Johannes Berg3713b4e2013-02-14 16:19:38 +01001438 if (dev->ops->connect || dev->ops->auth) {
1439 i++;
1440 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001441 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001442 }
1443
Johannes Berg3713b4e2013-02-14 16:19:38 +01001444 if (dev->ops->disconnect || dev->ops->deauth) {
1445 i++;
1446 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1447 goto nla_put_failure;
1448 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001449
Johannes Berg3713b4e2013-02-14 16:19:38 +01001450 nla_nest_end(msg, nl_cmds);
1451 (*split_start)++;
1452 if (split)
1453 break;
1454 case 5:
1455 if (dev->ops->remain_on_channel &&
1456 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1457 nla_put_u32(msg,
1458 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1459 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001460 goto nla_put_failure;
1461
Johannes Berg3713b4e2013-02-14 16:19:38 +01001462 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1463 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1464 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001465
Johannes Berg3713b4e2013-02-14 16:19:38 +01001466 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1467 goto nla_put_failure;
1468 (*split_start)++;
1469 if (split)
1470 break;
1471 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001472#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001473 if (nl80211_send_wowlan(msg, dev, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001474 goto nla_put_failure;
1475 (*split_start)++;
1476 if (split)
1477 break;
1478#else
1479 (*split_start)++;
1480#endif
1481 case 7:
1482 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1483 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001484 goto nla_put_failure;
1485
Johannes Bergcdc89b92013-02-18 23:54:36 +01001486 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001487 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001488
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 (*split_start)++;
1490 if (split)
1491 break;
1492 case 8:
1493 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1494 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1495 dev->wiphy.ap_sme_capa))
1496 goto nla_put_failure;
1497
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001498 features = dev->wiphy.features;
1499 /*
1500 * We can only add the per-channel limit information if the
1501 * dump is split, otherwise it makes it too big. Therefore
1502 * only advertise it in that case.
1503 */
1504 if (split)
1505 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1506 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001507 goto nla_put_failure;
1508
1509 if (dev->wiphy.ht_capa_mod_mask &&
1510 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1511 sizeof(*dev->wiphy.ht_capa_mod_mask),
1512 dev->wiphy.ht_capa_mod_mask))
1513 goto nla_put_failure;
1514
1515 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1516 dev->wiphy.max_acl_mac_addrs &&
1517 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1518 dev->wiphy.max_acl_mac_addrs))
1519 goto nla_put_failure;
1520
1521 /*
1522 * Any information below this point is only available to
1523 * applications that can deal with it being split. This
1524 * helps ensure that newly added capabilities don't break
1525 * older tools by overrunning their buffers.
1526 *
1527 * We still increment split_start so that in the split
1528 * case we'll continue with more data in the next round,
1529 * but break unconditionally so unsplit data stops here.
1530 */
1531 (*split_start)++;
1532 break;
1533 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001534 if (dev->wiphy.extended_capabilities &&
1535 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1536 dev->wiphy.extended_capabilities_len,
1537 dev->wiphy.extended_capabilities) ||
1538 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1539 dev->wiphy.extended_capabilities_len,
1540 dev->wiphy.extended_capabilities_mask)))
1541 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001542
Johannes Bergee2aca32013-02-21 17:36:01 +01001543 if (dev->wiphy.vht_capa_mod_mask &&
1544 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1545 sizeof(*dev->wiphy.vht_capa_mod_mask),
1546 dev->wiphy.vht_capa_mod_mask))
1547 goto nla_put_failure;
1548
Johannes Berg3713b4e2013-02-14 16:19:38 +01001549 /* done */
1550 *split_start = 0;
1551 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001552 }
Johannes Berg55682962007-09-20 13:09:35 -04001553 return genlmsg_end(msg, hdr);
1554
1555 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001556 genlmsg_cancel(msg, hdr);
1557 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001558}
1559
1560static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1561{
Johannes Berg645e77d2013-03-01 14:03:49 +01001562 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001563 int start = cb->args[0];
1564 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001565 s64 filter_wiphy = -1;
1566 bool split = false;
1567 struct nlattr **tb = nl80211_fam.attrbuf;
1568 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001569
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001570 mutex_lock(&cfg80211_mutex);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001571 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1572 tb, nl80211_fam.maxattr, nl80211_policy);
1573 if (res == 0) {
1574 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1575 if (tb[NL80211_ATTR_WIPHY])
1576 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1577 if (tb[NL80211_ATTR_WDEV])
1578 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1579 if (tb[NL80211_ATTR_IFINDEX]) {
1580 struct net_device *netdev;
1581 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1582
1583 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1584 if (!netdev) {
1585 mutex_unlock(&cfg80211_mutex);
1586 return -ENODEV;
1587 }
1588 if (netdev->ieee80211_ptr) {
1589 dev = wiphy_to_dev(
1590 netdev->ieee80211_ptr->wiphy);
1591 filter_wiphy = dev->wiphy_idx;
1592 }
1593 dev_put(netdev);
1594 }
1595 }
1596
Johannes Berg79c97e92009-07-07 03:56:12 +02001597 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001598 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1599 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001600 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001601 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001602 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1603 continue;
1604 /* attempt to fit multiple wiphy data chunks into the skb */
1605 do {
1606 ret = nl80211_send_wiphy(dev, skb,
1607 NETLINK_CB(cb->skb).portid,
1608 cb->nlh->nlmsg_seq,
1609 NLM_F_MULTI,
1610 split, &cb->args[1],
1611 &cb->args[2],
1612 &cb->args[3]);
1613 if (ret < 0) {
1614 /*
1615 * If sending the wiphy data didn't fit (ENOBUFS
1616 * or EMSGSIZE returned), this SKB is still
1617 * empty (so it's not too big because another
1618 * wiphy dataset is already in the skb) and
1619 * we've not tried to adjust the dump allocation
1620 * yet ... then adjust the alloc size to be
1621 * bigger, and return 1 but with the empty skb.
1622 * This results in an empty message being RX'ed
1623 * in userspace, but that is ignored.
1624 *
1625 * We can then retry with the larger buffer.
1626 */
1627 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1628 !skb->len &&
1629 cb->min_dump_alloc < 4096) {
1630 cb->min_dump_alloc = 4096;
1631 mutex_unlock(&cfg80211_mutex);
1632 return 1;
1633 }
1634 idx--;
1635 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001636 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001637 } while (cb->args[1] > 0);
1638 break;
Johannes Berg55682962007-09-20 13:09:35 -04001639 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001640 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001641
1642 cb->args[0] = idx;
1643
1644 return skb->len;
1645}
1646
1647static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1648{
1649 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001650 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001651
Johannes Berg645e77d2013-03-01 14:03:49 +01001652 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001653 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001654 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001655
Johannes Berg3713b4e2013-02-14 16:19:38 +01001656 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1657 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001658 nlmsg_free(msg);
1659 return -ENOBUFS;
1660 }
Johannes Berg55682962007-09-20 13:09:35 -04001661
Johannes Berg134e6372009-07-10 09:51:34 +00001662 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001663}
1664
Jouni Malinen31888482008-10-30 16:59:24 +02001665static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1666 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1667 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1668 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1669 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1670 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1671};
1672
1673static int parse_txq_params(struct nlattr *tb[],
1674 struct ieee80211_txq_params *txq_params)
1675{
Johannes Berga3304b02012-03-28 11:04:24 +02001676 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001677 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1678 !tb[NL80211_TXQ_ATTR_AIFS])
1679 return -EINVAL;
1680
Johannes Berga3304b02012-03-28 11:04:24 +02001681 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001682 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1683 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1684 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1685 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1686
Johannes Berga3304b02012-03-28 11:04:24 +02001687 if (txq_params->ac >= NL80211_NUM_ACS)
1688 return -EINVAL;
1689
Jouni Malinen31888482008-10-30 16:59:24 +02001690 return 0;
1691}
1692
Johannes Bergf444de02010-05-05 15:25:02 +02001693static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1694{
1695 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001696 * You can only set the channel explicitly for WDS interfaces,
1697 * all others have their channel managed via their respective
1698 * "establish a connection" command (connect, join, ...)
1699 *
1700 * For AP/GO and mesh mode, the channel can be set with the
1701 * channel userspace API, but is only stored and passed to the
1702 * low-level driver when the AP starts or the mesh is joined.
1703 * This is for backward compatibility, userspace can also give
1704 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001705 *
1706 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001707 * whatever else is going on, so they have their own special
1708 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001709 */
1710 return !wdev ||
1711 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001712 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001713 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1714 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001715}
1716
Johannes Berg683b6d32012-11-08 21:25:48 +01001717static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1718 struct genl_info *info,
1719 struct cfg80211_chan_def *chandef)
1720{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301721 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001722
1723 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1724 return -EINVAL;
1725
1726 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1727
1728 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001729 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1730 chandef->center_freq1 = control_freq;
1731 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001732
1733 /* Primary channel not allowed */
1734 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1735 return -EINVAL;
1736
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001737 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1738 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001739
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001740 chantype = nla_get_u32(
1741 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1742
1743 switch (chantype) {
1744 case NL80211_CHAN_NO_HT:
1745 case NL80211_CHAN_HT20:
1746 case NL80211_CHAN_HT40PLUS:
1747 case NL80211_CHAN_HT40MINUS:
1748 cfg80211_chandef_create(chandef, chandef->chan,
1749 chantype);
1750 break;
1751 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001752 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001753 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001754 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1755 chandef->width =
1756 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1757 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1758 chandef->center_freq1 =
1759 nla_get_u32(
1760 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1761 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1762 chandef->center_freq2 =
1763 nla_get_u32(
1764 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1765 }
1766
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001767 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001768 return -EINVAL;
1769
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001770 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1771 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001772 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001773
Johannes Berg683b6d32012-11-08 21:25:48 +01001774 return 0;
1775}
1776
Johannes Bergf444de02010-05-05 15:25:02 +02001777static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1778 struct wireless_dev *wdev,
1779 struct genl_info *info)
1780{
Johannes Berg683b6d32012-11-08 21:25:48 +01001781 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001782 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001783 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1784
1785 if (wdev)
1786 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001787
Johannes Bergf444de02010-05-05 15:25:02 +02001788 if (!nl80211_can_set_dev_channel(wdev))
1789 return -EOPNOTSUPP;
1790
Johannes Berg683b6d32012-11-08 21:25:48 +01001791 result = nl80211_parse_chandef(rdev, info, &chandef);
1792 if (result)
1793 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001794
1795 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001796 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001797 case NL80211_IFTYPE_AP:
1798 case NL80211_IFTYPE_P2P_GO:
1799 if (wdev->beacon_interval) {
1800 result = -EBUSY;
1801 break;
1802 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001803 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001804 result = -EINVAL;
1805 break;
1806 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001807 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001808 result = 0;
1809 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001810 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001811 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001812 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001813 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001814 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001815 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001816 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001817 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001818 }
1819 mutex_unlock(&rdev->devlist_mtx);
1820
1821 return result;
1822}
1823
1824static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1825{
Johannes Berg4c476992010-10-04 21:36:35 +02001826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1827 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001828
Johannes Berg4c476992010-10-04 21:36:35 +02001829 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001830}
1831
Bill Jordane8347eb2010-10-01 13:54:28 -04001832static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1833{
Johannes Berg43b19952010-10-07 13:10:30 +02001834 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1835 struct net_device *dev = info->user_ptr[1];
1836 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001837 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001838
1839 if (!info->attrs[NL80211_ATTR_MAC])
1840 return -EINVAL;
1841
Johannes Berg43b19952010-10-07 13:10:30 +02001842 if (netif_running(dev))
1843 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001844
Johannes Berg43b19952010-10-07 13:10:30 +02001845 if (!rdev->ops->set_wds_peer)
1846 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001847
Johannes Berg43b19952010-10-07 13:10:30 +02001848 if (wdev->iftype != NL80211_IFTYPE_WDS)
1849 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001850
1851 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001852 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001853}
1854
1855
Johannes Berg55682962007-09-20 13:09:35 -04001856static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1857{
1858 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001859 struct net_device *netdev = NULL;
1860 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001861 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001862 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001863 u32 changed;
1864 u8 retry_short = 0, retry_long = 0;
1865 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001866 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001867
Johannes Bergf444de02010-05-05 15:25:02 +02001868 /*
1869 * Try to find the wiphy and netdev. Normally this
1870 * function shouldn't need the netdev, but this is
1871 * done for backward compatibility -- previously
1872 * setting the channel was done per wiphy, but now
1873 * it is per netdev. Previous userland like hostapd
1874 * also passed a netdev to set_wiphy, so that it is
1875 * possible to let that go to the right netdev!
1876 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001877 mutex_lock(&cfg80211_mutex);
1878
Johannes Bergf444de02010-05-05 15:25:02 +02001879 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1880 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1881
1882 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1883 if (netdev && netdev->ieee80211_ptr) {
1884 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1885 mutex_lock(&rdev->mtx);
1886 } else
1887 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001888 }
1889
Johannes Bergf444de02010-05-05 15:25:02 +02001890 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001891 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1892 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001893 if (IS_ERR(rdev)) {
1894 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001895 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001896 }
1897 wdev = NULL;
1898 netdev = NULL;
1899 result = 0;
1900
1901 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001902 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001903 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001904
1905 /*
1906 * end workaround code, by now the rdev is available
1907 * and locked, and wdev may or may not be NULL.
1908 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001909
1910 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001911 result = cfg80211_dev_rename(
1912 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001913
1914 mutex_unlock(&cfg80211_mutex);
1915
1916 if (result)
1917 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001918
Jouni Malinen31888482008-10-30 16:59:24 +02001919 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1920 struct ieee80211_txq_params txq_params;
1921 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1922
1923 if (!rdev->ops->set_txq_params) {
1924 result = -EOPNOTSUPP;
1925 goto bad_res;
1926 }
1927
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001928 if (!netdev) {
1929 result = -EINVAL;
1930 goto bad_res;
1931 }
1932
Johannes Berg133a3ff2011-11-03 14:50:13 +01001933 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1934 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1935 result = -EINVAL;
1936 goto bad_res;
1937 }
1938
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001939 if (!netif_running(netdev)) {
1940 result = -ENETDOWN;
1941 goto bad_res;
1942 }
1943
Jouni Malinen31888482008-10-30 16:59:24 +02001944 nla_for_each_nested(nl_txq_params,
1945 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1946 rem_txq_params) {
1947 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1948 nla_data(nl_txq_params),
1949 nla_len(nl_txq_params),
1950 txq_params_policy);
1951 result = parse_txq_params(tb, &txq_params);
1952 if (result)
1953 goto bad_res;
1954
Hila Gonene35e4d22012-06-27 17:19:42 +03001955 result = rdev_set_txq_params(rdev, netdev,
1956 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001957 if (result)
1958 goto bad_res;
1959 }
1960 }
1961
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001962 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001963 result = __nl80211_set_channel(rdev,
1964 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1965 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001966 if (result)
1967 goto bad_res;
1968 }
1969
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001970 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001971 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001972 enum nl80211_tx_power_setting type;
1973 int idx, mbm = 0;
1974
Johannes Bergc8442112012-10-24 10:17:18 +02001975 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1976 txp_wdev = NULL;
1977
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001978 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001979 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001980 goto bad_res;
1981 }
1982
1983 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1984 type = nla_get_u32(info->attrs[idx]);
1985
1986 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1987 (type != NL80211_TX_POWER_AUTOMATIC)) {
1988 result = -EINVAL;
1989 goto bad_res;
1990 }
1991
1992 if (type != NL80211_TX_POWER_AUTOMATIC) {
1993 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1994 mbm = nla_get_u32(info->attrs[idx]);
1995 }
1996
Johannes Bergc8442112012-10-24 10:17:18 +02001997 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001998 if (result)
1999 goto bad_res;
2000 }
2001
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002002 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2003 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2004 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002005 if ((!rdev->wiphy.available_antennas_tx &&
2006 !rdev->wiphy.available_antennas_rx) ||
2007 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002008 result = -EOPNOTSUPP;
2009 goto bad_res;
2010 }
2011
2012 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2013 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2014
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002015 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002016 * available antenna masks, except for the "all" mask */
2017 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2018 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002019 result = -EINVAL;
2020 goto bad_res;
2021 }
2022
Bruno Randolf7f531e02010-12-16 11:30:22 +09002023 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2024 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002025
Hila Gonene35e4d22012-06-27 17:19:42 +03002026 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002027 if (result)
2028 goto bad_res;
2029 }
2030
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002031 changed = 0;
2032
2033 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2034 retry_short = nla_get_u8(
2035 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2036 if (retry_short == 0) {
2037 result = -EINVAL;
2038 goto bad_res;
2039 }
2040 changed |= WIPHY_PARAM_RETRY_SHORT;
2041 }
2042
2043 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2044 retry_long = nla_get_u8(
2045 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2046 if (retry_long == 0) {
2047 result = -EINVAL;
2048 goto bad_res;
2049 }
2050 changed |= WIPHY_PARAM_RETRY_LONG;
2051 }
2052
2053 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2054 frag_threshold = nla_get_u32(
2055 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2056 if (frag_threshold < 256) {
2057 result = -EINVAL;
2058 goto bad_res;
2059 }
2060 if (frag_threshold != (u32) -1) {
2061 /*
2062 * Fragments (apart from the last one) are required to
2063 * have even length. Make the fragmentation code
2064 * simpler by stripping LSB should someone try to use
2065 * odd threshold value.
2066 */
2067 frag_threshold &= ~0x1;
2068 }
2069 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2070 }
2071
2072 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2073 rts_threshold = nla_get_u32(
2074 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2075 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2076 }
2077
Lukáš Turek81077e82009-12-21 22:50:47 +01002078 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2079 coverage_class = nla_get_u8(
2080 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2081 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2082 }
2083
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002084 if (changed) {
2085 u8 old_retry_short, old_retry_long;
2086 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002087 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002088
2089 if (!rdev->ops->set_wiphy_params) {
2090 result = -EOPNOTSUPP;
2091 goto bad_res;
2092 }
2093
2094 old_retry_short = rdev->wiphy.retry_short;
2095 old_retry_long = rdev->wiphy.retry_long;
2096 old_frag_threshold = rdev->wiphy.frag_threshold;
2097 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002098 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002099
2100 if (changed & WIPHY_PARAM_RETRY_SHORT)
2101 rdev->wiphy.retry_short = retry_short;
2102 if (changed & WIPHY_PARAM_RETRY_LONG)
2103 rdev->wiphy.retry_long = retry_long;
2104 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2105 rdev->wiphy.frag_threshold = frag_threshold;
2106 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2107 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002108 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2109 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002110
Hila Gonene35e4d22012-06-27 17:19:42 +03002111 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002112 if (result) {
2113 rdev->wiphy.retry_short = old_retry_short;
2114 rdev->wiphy.retry_long = old_retry_long;
2115 rdev->wiphy.frag_threshold = old_frag_threshold;
2116 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002117 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002118 }
2119 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002120
Johannes Berg306d6112008-12-08 12:39:04 +01002121 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002122 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02002123 if (netdev)
2124 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002125 return result;
2126}
2127
Johannes Berg71bbc992012-06-15 15:30:18 +02002128static inline u64 wdev_id(struct wireless_dev *wdev)
2129{
2130 return (u64)wdev->identifier |
2131 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2132}
Johannes Berg55682962007-09-20 13:09:35 -04002133
Johannes Berg683b6d32012-11-08 21:25:48 +01002134static int nl80211_send_chandef(struct sk_buff *msg,
2135 struct cfg80211_chan_def *chandef)
2136{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002137 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002138
Johannes Berg683b6d32012-11-08 21:25:48 +01002139 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2140 chandef->chan->center_freq))
2141 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002142 switch (chandef->width) {
2143 case NL80211_CHAN_WIDTH_20_NOHT:
2144 case NL80211_CHAN_WIDTH_20:
2145 case NL80211_CHAN_WIDTH_40:
2146 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2147 cfg80211_get_chandef_type(chandef)))
2148 return -ENOBUFS;
2149 break;
2150 default:
2151 break;
2152 }
2153 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2154 return -ENOBUFS;
2155 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2156 return -ENOBUFS;
2157 if (chandef->center_freq2 &&
2158 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002159 return -ENOBUFS;
2160 return 0;
2161}
2162
Eric W. Biederman15e47302012-09-07 20:12:54 +00002163static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002164 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002165 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002166{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002167 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002168 void *hdr;
2169
Eric W. Biederman15e47302012-09-07 20:12:54 +00002170 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002171 if (!hdr)
2172 return -1;
2173
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002174 if (dev &&
2175 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002176 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002177 goto nla_put_failure;
2178
2179 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2180 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002181 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002182 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002183 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2184 rdev->devlist_generation ^
2185 (cfg80211_rdev_list_generation << 2)))
2186 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002187
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002188 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002189 int ret;
2190 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002191
Johannes Berg683b6d32012-11-08 21:25:48 +01002192 ret = rdev_get_channel(rdev, wdev, &chandef);
2193 if (ret == 0) {
2194 if (nl80211_send_chandef(msg, &chandef))
2195 goto nla_put_failure;
2196 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002197 }
2198
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002199 if (wdev->ssid_len) {
2200 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2201 goto nla_put_failure;
2202 }
2203
Johannes Berg55682962007-09-20 13:09:35 -04002204 return genlmsg_end(msg, hdr);
2205
2206 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002207 genlmsg_cancel(msg, hdr);
2208 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002209}
2210
2211static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2212{
2213 int wp_idx = 0;
2214 int if_idx = 0;
2215 int wp_start = cb->args[0];
2216 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002217 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002218 struct wireless_dev *wdev;
2219
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002220 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02002221 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2222 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002223 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002224 if (wp_idx < wp_start) {
2225 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002226 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002227 }
Johannes Berg55682962007-09-20 13:09:35 -04002228 if_idx = 0;
2229
Johannes Bergf5ea9122009-08-07 16:17:38 +02002230 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02002231 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002232 if (if_idx < if_start) {
2233 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002234 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002235 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002236 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002237 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002238 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002239 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002240 goto out;
2241 }
2242 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002243 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002244 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002245
2246 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002247 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002248 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002249 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002250
2251 cb->args[0] = wp_idx;
2252 cb->args[1] = if_idx;
2253
2254 return skb->len;
2255}
2256
2257static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2258{
2259 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002260 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002261 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002262
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002264 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002265 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002266
Eric W. Biederman15e47302012-09-07 20:12:54 +00002267 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002268 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002269 nlmsg_free(msg);
2270 return -ENOBUFS;
2271 }
Johannes Berg55682962007-09-20 13:09:35 -04002272
Johannes Berg134e6372009-07-10 09:51:34 +00002273 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002274}
2275
Michael Wu66f7ac52008-01-31 19:48:22 +01002276static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2277 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2278 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2279 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2280 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2281 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2282};
2283
2284static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2285{
2286 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2287 int flag;
2288
2289 *mntrflags = 0;
2290
2291 if (!nla)
2292 return -EINVAL;
2293
2294 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2295 nla, mntr_flags_policy))
2296 return -EINVAL;
2297
2298 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2299 if (flags[flag])
2300 *mntrflags |= (1<<flag);
2301
2302 return 0;
2303}
2304
Johannes Berg9bc383d2009-11-19 11:55:19 +01002305static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002306 struct net_device *netdev, u8 use_4addr,
2307 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002308{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002309 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002310 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002311 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002312 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002313 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002314
2315 switch (iftype) {
2316 case NL80211_IFTYPE_AP_VLAN:
2317 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2318 return 0;
2319 break;
2320 case NL80211_IFTYPE_STATION:
2321 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2322 return 0;
2323 break;
2324 default:
2325 break;
2326 }
2327
2328 return -EOPNOTSUPP;
2329}
2330
Johannes Berg55682962007-09-20 13:09:35 -04002331static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2332{
Johannes Berg4c476992010-10-04 21:36:35 +02002333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002334 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002335 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002336 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002337 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002338 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002339 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002340
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002341 memset(&params, 0, sizeof(params));
2342
Johannes Berg04a773a2009-04-19 21:24:32 +02002343 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002344
Johannes Berg723b0382008-09-16 20:22:09 +02002345 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002346 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002347 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002348 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002349 if (ntype > NL80211_IFTYPE_MAX)
2350 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002351 }
2352
Johannes Berg92ffe052008-09-16 20:39:36 +02002353 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002354 struct wireless_dev *wdev = dev->ieee80211_ptr;
2355
Johannes Berg4c476992010-10-04 21:36:35 +02002356 if (ntype != NL80211_IFTYPE_MESH_POINT)
2357 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002358 if (netif_running(dev))
2359 return -EBUSY;
2360
2361 wdev_lock(wdev);
2362 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2363 IEEE80211_MAX_MESH_ID_LEN);
2364 wdev->mesh_id_up_len =
2365 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2366 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2367 wdev->mesh_id_up_len);
2368 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002369 }
2370
Felix Fietkau8b787642009-11-10 18:53:10 +01002371 if (info->attrs[NL80211_ATTR_4ADDR]) {
2372 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2373 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002374 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002375 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002376 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002377 } else {
2378 params.use_4addr = -1;
2379 }
2380
Johannes Berg92ffe052008-09-16 20:39:36 +02002381 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002382 if (ntype != NL80211_IFTYPE_MONITOR)
2383 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002384 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2385 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002386 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002387 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002388
2389 flags = &_flags;
2390 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002391 }
Johannes Berg3b858752009-03-12 09:55:09 +01002392
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002393 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002394 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002395 else
2396 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002397
Johannes Berg9bc383d2009-11-19 11:55:19 +01002398 if (!err && params.use_4addr != -1)
2399 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2400
Johannes Berg55682962007-09-20 13:09:35 -04002401 return err;
2402}
2403
2404static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2405{
Johannes Berg4c476992010-10-04 21:36:35 +02002406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002407 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002408 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002409 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002410 int err;
2411 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002412 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002413
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002414 memset(&params, 0, sizeof(params));
2415
Johannes Berg55682962007-09-20 13:09:35 -04002416 if (!info->attrs[NL80211_ATTR_IFNAME])
2417 return -EINVAL;
2418
2419 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2420 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2421 if (type > NL80211_IFTYPE_MAX)
2422 return -EINVAL;
2423 }
2424
Johannes Berg79c97e92009-07-07 03:56:12 +02002425 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002426 !(rdev->wiphy.interface_modes & (1 << type)))
2427 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002428
Arend van Spriel1c18f142013-01-08 10:17:27 +01002429 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2430 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2431 ETH_ALEN);
2432 if (!is_valid_ether_addr(params.macaddr))
2433 return -EADDRNOTAVAIL;
2434 }
2435
Johannes Berg9bc383d2009-11-19 11:55:19 +01002436 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002437 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002438 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002439 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002440 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002441 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002442
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2444 if (!msg)
2445 return -ENOMEM;
2446
Michael Wu66f7ac52008-01-31 19:48:22 +01002447 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2448 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2449 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002450 wdev = rdev_add_virtual_intf(rdev,
2451 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2452 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002453 if (IS_ERR(wdev)) {
2454 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002455 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002456 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002457
Johannes Berg98104fde2012-06-16 00:19:54 +02002458 switch (type) {
2459 case NL80211_IFTYPE_MESH_POINT:
2460 if (!info->attrs[NL80211_ATTR_MESH_ID])
2461 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002462 wdev_lock(wdev);
2463 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2464 IEEE80211_MAX_MESH_ID_LEN);
2465 wdev->mesh_id_up_len =
2466 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2467 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2468 wdev->mesh_id_up_len);
2469 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002470 break;
2471 case NL80211_IFTYPE_P2P_DEVICE:
2472 /*
2473 * P2P Device doesn't have a netdev, so doesn't go
2474 * through the netdev notifier and must be added here
2475 */
2476 mutex_init(&wdev->mtx);
2477 INIT_LIST_HEAD(&wdev->event_list);
2478 spin_lock_init(&wdev->event_lock);
2479 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2480 spin_lock_init(&wdev->mgmt_registrations_lock);
2481
2482 mutex_lock(&rdev->devlist_mtx);
2483 wdev->identifier = ++rdev->wdev_id;
2484 list_add_rcu(&wdev->list, &rdev->wdev_list);
2485 rdev->devlist_generation++;
2486 mutex_unlock(&rdev->devlist_mtx);
2487 break;
2488 default:
2489 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002490 }
2491
Eric W. Biederman15e47302012-09-07 20:12:54 +00002492 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002493 rdev, wdev) < 0) {
2494 nlmsg_free(msg);
2495 return -ENOBUFS;
2496 }
2497
2498 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002499}
2500
2501static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2502{
Johannes Berg4c476992010-10-04 21:36:35 +02002503 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002504 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002505
Johannes Berg4c476992010-10-04 21:36:35 +02002506 if (!rdev->ops->del_virtual_intf)
2507 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002508
Johannes Berg84efbb82012-06-16 00:00:26 +02002509 /*
2510 * If we remove a wireless device without a netdev then clear
2511 * user_ptr[1] so that nl80211_post_doit won't dereference it
2512 * to check if it needs to do dev_put(). Otherwise it crashes
2513 * since the wdev has been freed, unlike with a netdev where
2514 * we need the dev_put() for the netdev to really be freed.
2515 */
2516 if (!wdev->netdev)
2517 info->user_ptr[1] = NULL;
2518
Hila Gonene35e4d22012-06-27 17:19:42 +03002519 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002520}
2521
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002522static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2523{
2524 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2525 struct net_device *dev = info->user_ptr[1];
2526 u16 noack_map;
2527
2528 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2529 return -EINVAL;
2530
2531 if (!rdev->ops->set_noack_map)
2532 return -EOPNOTSUPP;
2533
2534 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2535
Hila Gonene35e4d22012-06-27 17:19:42 +03002536 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002537}
2538
Johannes Berg41ade002007-12-19 02:03:29 +01002539struct get_key_cookie {
2540 struct sk_buff *msg;
2541 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002542 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002543};
2544
2545static void get_key_callback(void *c, struct key_params *params)
2546{
Johannes Bergb9454e82009-07-08 13:29:08 +02002547 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002548 struct get_key_cookie *cookie = c;
2549
David S. Miller9360ffd2012-03-29 04:41:26 -04002550 if ((params->key &&
2551 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2552 params->key_len, params->key)) ||
2553 (params->seq &&
2554 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2555 params->seq_len, params->seq)) ||
2556 (params->cipher &&
2557 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2558 params->cipher)))
2559 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002560
Johannes Bergb9454e82009-07-08 13:29:08 +02002561 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2562 if (!key)
2563 goto nla_put_failure;
2564
David S. Miller9360ffd2012-03-29 04:41:26 -04002565 if ((params->key &&
2566 nla_put(cookie->msg, NL80211_KEY_DATA,
2567 params->key_len, params->key)) ||
2568 (params->seq &&
2569 nla_put(cookie->msg, NL80211_KEY_SEQ,
2570 params->seq_len, params->seq)) ||
2571 (params->cipher &&
2572 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2573 params->cipher)))
2574 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002575
David S. Miller9360ffd2012-03-29 04:41:26 -04002576 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2577 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002578
2579 nla_nest_end(cookie->msg, key);
2580
Johannes Berg41ade002007-12-19 02:03:29 +01002581 return;
2582 nla_put_failure:
2583 cookie->error = 1;
2584}
2585
2586static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2587{
Johannes Berg4c476992010-10-04 21:36:35 +02002588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002589 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002590 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002591 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002592 const u8 *mac_addr = NULL;
2593 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002594 struct get_key_cookie cookie = {
2595 .error = 0,
2596 };
2597 void *hdr;
2598 struct sk_buff *msg;
2599
2600 if (info->attrs[NL80211_ATTR_KEY_IDX])
2601 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2602
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002603 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002604 return -EINVAL;
2605
2606 if (info->attrs[NL80211_ATTR_MAC])
2607 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2608
Johannes Berge31b8212010-10-05 19:39:30 +02002609 pairwise = !!mac_addr;
2610 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2611 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2612 if (kt >= NUM_NL80211_KEYTYPES)
2613 return -EINVAL;
2614 if (kt != NL80211_KEYTYPE_GROUP &&
2615 kt != NL80211_KEYTYPE_PAIRWISE)
2616 return -EINVAL;
2617 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2618 }
2619
Johannes Berg4c476992010-10-04 21:36:35 +02002620 if (!rdev->ops->get_key)
2621 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002622
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002623 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002624 if (!msg)
2625 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002626
Eric W. Biederman15e47302012-09-07 20:12:54 +00002627 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002628 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002629 if (IS_ERR(hdr))
2630 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002631
2632 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002633 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002634
David S. Miller9360ffd2012-03-29 04:41:26 -04002635 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2636 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2637 goto nla_put_failure;
2638 if (mac_addr &&
2639 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2640 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002641
Johannes Berge31b8212010-10-05 19:39:30 +02002642 if (pairwise && mac_addr &&
2643 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2644 return -ENOENT;
2645
Hila Gonene35e4d22012-06-27 17:19:42 +03002646 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2647 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002648
2649 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002650 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002651
2652 if (cookie.error)
2653 goto nla_put_failure;
2654
2655 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002656 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002657
2658 nla_put_failure:
2659 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002660 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002661 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002662 return err;
2663}
2664
2665static int nl80211_set_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 Bergb9454e82009-07-08 13:29:08 +02002668 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002669 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002670 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002671
Johannes Bergb9454e82009-07-08 13:29:08 +02002672 err = nl80211_parse_key(info, &key);
2673 if (err)
2674 return err;
2675
2676 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002677 return -EINVAL;
2678
Johannes Bergb9454e82009-07-08 13:29:08 +02002679 /* only support setting default key */
2680 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002681 return -EINVAL;
2682
Johannes Bergfffd0932009-07-08 14:22:54 +02002683 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002684
2685 if (key.def) {
2686 if (!rdev->ops->set_default_key) {
2687 err = -EOPNOTSUPP;
2688 goto out;
2689 }
2690
2691 err = nl80211_key_allowed(dev->ieee80211_ptr);
2692 if (err)
2693 goto out;
2694
Hila Gonene35e4d22012-06-27 17:19:42 +03002695 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002696 key.def_uni, key.def_multi);
2697
2698 if (err)
2699 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002700
Johannes Berg3d23e342009-09-29 23:27:28 +02002701#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002702 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002703#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002704 } else {
2705 if (key.def_uni || !key.def_multi) {
2706 err = -EINVAL;
2707 goto out;
2708 }
2709
2710 if (!rdev->ops->set_default_mgmt_key) {
2711 err = -EOPNOTSUPP;
2712 goto out;
2713 }
2714
2715 err = nl80211_key_allowed(dev->ieee80211_ptr);
2716 if (err)
2717 goto out;
2718
Hila Gonene35e4d22012-06-27 17:19:42 +03002719 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002720 if (err)
2721 goto out;
2722
2723#ifdef CONFIG_CFG80211_WEXT
2724 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2725#endif
2726 }
2727
2728 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002729 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002730
Johannes Berg41ade002007-12-19 02:03:29 +01002731 return err;
2732}
2733
2734static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2735{
Johannes Berg4c476992010-10-04 21:36:35 +02002736 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002737 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002738 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002739 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002740 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002741
Johannes Bergb9454e82009-07-08 13:29:08 +02002742 err = nl80211_parse_key(info, &key);
2743 if (err)
2744 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002745
Johannes Bergb9454e82009-07-08 13:29:08 +02002746 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002747 return -EINVAL;
2748
Johannes Berg41ade002007-12-19 02:03:29 +01002749 if (info->attrs[NL80211_ATTR_MAC])
2750 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2751
Johannes Berge31b8212010-10-05 19:39:30 +02002752 if (key.type == -1) {
2753 if (mac_addr)
2754 key.type = NL80211_KEYTYPE_PAIRWISE;
2755 else
2756 key.type = NL80211_KEYTYPE_GROUP;
2757 }
2758
2759 /* for now */
2760 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2761 key.type != NL80211_KEYTYPE_GROUP)
2762 return -EINVAL;
2763
Johannes Berg4c476992010-10-04 21:36:35 +02002764 if (!rdev->ops->add_key)
2765 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002766
Johannes Berge31b8212010-10-05 19:39:30 +02002767 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2768 key.type == NL80211_KEYTYPE_PAIRWISE,
2769 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002770 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002771
2772 wdev_lock(dev->ieee80211_ptr);
2773 err = nl80211_key_allowed(dev->ieee80211_ptr);
2774 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002775 err = rdev_add_key(rdev, dev, key.idx,
2776 key.type == NL80211_KEYTYPE_PAIRWISE,
2777 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002778 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002779
Johannes Berg41ade002007-12-19 02:03:29 +01002780 return err;
2781}
2782
2783static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2784{
Johannes Berg4c476992010-10-04 21:36:35 +02002785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002786 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002787 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002788 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002789 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002790
Johannes Bergb9454e82009-07-08 13:29:08 +02002791 err = nl80211_parse_key(info, &key);
2792 if (err)
2793 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002794
2795 if (info->attrs[NL80211_ATTR_MAC])
2796 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2797
Johannes Berge31b8212010-10-05 19:39:30 +02002798 if (key.type == -1) {
2799 if (mac_addr)
2800 key.type = NL80211_KEYTYPE_PAIRWISE;
2801 else
2802 key.type = NL80211_KEYTYPE_GROUP;
2803 }
2804
2805 /* for now */
2806 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2807 key.type != NL80211_KEYTYPE_GROUP)
2808 return -EINVAL;
2809
Johannes Berg4c476992010-10-04 21:36:35 +02002810 if (!rdev->ops->del_key)
2811 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002812
Johannes Bergfffd0932009-07-08 14:22:54 +02002813 wdev_lock(dev->ieee80211_ptr);
2814 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002815
2816 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2817 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2818 err = -ENOENT;
2819
Johannes Bergfffd0932009-07-08 14:22:54 +02002820 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002821 err = rdev_del_key(rdev, dev, key.idx,
2822 key.type == NL80211_KEYTYPE_PAIRWISE,
2823 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002824
Johannes Berg3d23e342009-09-29 23:27:28 +02002825#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002826 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002827 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002828 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002829 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002830 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2831 }
2832#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002833 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002834
Johannes Berg41ade002007-12-19 02:03:29 +01002835 return err;
2836}
2837
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302838/* This function returns an error or the number of nested attributes */
2839static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2840{
2841 struct nlattr *attr;
2842 int n_entries = 0, tmp;
2843
2844 nla_for_each_nested(attr, nl_attr, tmp) {
2845 if (nla_len(attr) != ETH_ALEN)
2846 return -EINVAL;
2847
2848 n_entries++;
2849 }
2850
2851 return n_entries;
2852}
2853
2854/*
2855 * This function parses ACL information and allocates memory for ACL data.
2856 * On successful return, the calling function is responsible to free the
2857 * ACL buffer returned by this function.
2858 */
2859static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2860 struct genl_info *info)
2861{
2862 enum nl80211_acl_policy acl_policy;
2863 struct nlattr *attr;
2864 struct cfg80211_acl_data *acl;
2865 int i = 0, n_entries, tmp;
2866
2867 if (!wiphy->max_acl_mac_addrs)
2868 return ERR_PTR(-EOPNOTSUPP);
2869
2870 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2871 return ERR_PTR(-EINVAL);
2872
2873 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2874 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2875 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2876 return ERR_PTR(-EINVAL);
2877
2878 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2879 return ERR_PTR(-EINVAL);
2880
2881 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2882 if (n_entries < 0)
2883 return ERR_PTR(n_entries);
2884
2885 if (n_entries > wiphy->max_acl_mac_addrs)
2886 return ERR_PTR(-ENOTSUPP);
2887
2888 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2889 GFP_KERNEL);
2890 if (!acl)
2891 return ERR_PTR(-ENOMEM);
2892
2893 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2894 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2895 i++;
2896 }
2897
2898 acl->n_acl_entries = n_entries;
2899 acl->acl_policy = acl_policy;
2900
2901 return acl;
2902}
2903
2904static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2905{
2906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2907 struct net_device *dev = info->user_ptr[1];
2908 struct cfg80211_acl_data *acl;
2909 int err;
2910
2911 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2912 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2913 return -EOPNOTSUPP;
2914
2915 if (!dev->ieee80211_ptr->beacon_interval)
2916 return -EINVAL;
2917
2918 acl = parse_acl_data(&rdev->wiphy, info);
2919 if (IS_ERR(acl))
2920 return PTR_ERR(acl);
2921
2922 err = rdev_set_mac_acl(rdev, dev, acl);
2923
2924 kfree(acl);
2925
2926 return err;
2927}
2928
Johannes Berg88600202012-02-13 15:17:18 +01002929static int nl80211_parse_beacon(struct genl_info *info,
2930 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002931{
Johannes Berg88600202012-02-13 15:17:18 +01002932 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002933
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002934 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2935 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2936 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2937 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002938 return -EINVAL;
2939
Johannes Berg88600202012-02-13 15:17:18 +01002940 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002941
Johannes Berged1b6cc2007-12-19 02:03:32 +01002942 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002943 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2944 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2945 if (!bcn->head_len)
2946 return -EINVAL;
2947 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002948 }
2949
2950 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002951 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2952 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002953 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002954 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002955 }
2956
Johannes Berg4c476992010-10-04 21:36:35 +02002957 if (!haveinfo)
2958 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002959
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002960 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002961 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2962 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002963 }
2964
2965 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002966 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002967 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002968 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002969 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2970 }
2971
2972 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002973 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002974 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002975 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002976 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2977 }
2978
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002979 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002980 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002981 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002982 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002983 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2984 }
2985
Johannes Berg88600202012-02-13 15:17:18 +01002986 return 0;
2987}
2988
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002989static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2990 struct cfg80211_ap_settings *params)
2991{
2992 struct wireless_dev *wdev;
2993 bool ret = false;
2994
2995 mutex_lock(&rdev->devlist_mtx);
2996
Johannes Berg89a54e42012-06-15 14:33:17 +02002997 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002998 if (wdev->iftype != NL80211_IFTYPE_AP &&
2999 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3000 continue;
3001
Johannes Berg683b6d32012-11-08 21:25:48 +01003002 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003003 continue;
3004
Johannes Berg683b6d32012-11-08 21:25:48 +01003005 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003006 ret = true;
3007 break;
3008 }
3009
3010 mutex_unlock(&rdev->devlist_mtx);
3011
3012 return ret;
3013}
3014
Jouni Malinene39e5b52012-09-30 19:29:39 +03003015static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3016 enum nl80211_auth_type auth_type,
3017 enum nl80211_commands cmd)
3018{
3019 if (auth_type > NL80211_AUTHTYPE_MAX)
3020 return false;
3021
3022 switch (cmd) {
3023 case NL80211_CMD_AUTHENTICATE:
3024 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3025 auth_type == NL80211_AUTHTYPE_SAE)
3026 return false;
3027 return true;
3028 case NL80211_CMD_CONNECT:
3029 case NL80211_CMD_START_AP:
3030 /* SAE not supported yet */
3031 if (auth_type == NL80211_AUTHTYPE_SAE)
3032 return false;
3033 return true;
3034 default:
3035 return false;
3036 }
3037}
3038
Johannes Berg88600202012-02-13 15:17:18 +01003039static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3040{
3041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3042 struct net_device *dev = info->user_ptr[1];
3043 struct wireless_dev *wdev = dev->ieee80211_ptr;
3044 struct cfg80211_ap_settings params;
3045 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003046 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003047
3048 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3049 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3050 return -EOPNOTSUPP;
3051
3052 if (!rdev->ops->start_ap)
3053 return -EOPNOTSUPP;
3054
3055 if (wdev->beacon_interval)
3056 return -EALREADY;
3057
3058 memset(&params, 0, sizeof(params));
3059
3060 /* these are required for START_AP */
3061 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3062 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3063 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3064 return -EINVAL;
3065
3066 err = nl80211_parse_beacon(info, &params.beacon);
3067 if (err)
3068 return err;
3069
3070 params.beacon_interval =
3071 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3072 params.dtim_period =
3073 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3074
3075 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3076 if (err)
3077 return err;
3078
3079 /*
3080 * In theory, some of these attributes should be required here
3081 * but since they were not used when the command was originally
3082 * added, keep them optional for old user space programs to let
3083 * them continue to work with drivers that do not need the
3084 * additional information -- drivers must check!
3085 */
3086 if (info->attrs[NL80211_ATTR_SSID]) {
3087 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3088 params.ssid_len =
3089 nla_len(info->attrs[NL80211_ATTR_SSID]);
3090 if (params.ssid_len == 0 ||
3091 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3092 return -EINVAL;
3093 }
3094
3095 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3096 params.hidden_ssid = nla_get_u32(
3097 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3098 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3099 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3100 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3101 return -EINVAL;
3102 }
3103
3104 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3105
3106 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3107 params.auth_type = nla_get_u32(
3108 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003109 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3110 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003111 return -EINVAL;
3112 } else
3113 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3114
3115 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3116 NL80211_MAX_NR_CIPHER_SUITES);
3117 if (err)
3118 return err;
3119
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303120 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3121 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3122 return -EOPNOTSUPP;
3123 params.inactivity_timeout = nla_get_u16(
3124 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3125 }
3126
Johannes Berg53cabad2012-11-14 15:17:28 +01003127 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3128 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3129 return -EINVAL;
3130 params.p2p_ctwindow =
3131 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3132 if (params.p2p_ctwindow > 127)
3133 return -EINVAL;
3134 if (params.p2p_ctwindow != 0 &&
3135 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3136 return -EINVAL;
3137 }
3138
3139 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3140 u8 tmp;
3141
3142 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3143 return -EINVAL;
3144 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3145 if (tmp > 1)
3146 return -EINVAL;
3147 params.p2p_opp_ps = tmp;
3148 if (params.p2p_opp_ps != 0 &&
3149 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3150 return -EINVAL;
3151 }
3152
Johannes Bergaa430da2012-05-16 23:50:18 +02003153 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003154 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3155 if (err)
3156 return err;
3157 } else if (wdev->preset_chandef.chan) {
3158 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003159 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003160 return -EINVAL;
3161
Johannes Berg683b6d32012-11-08 21:25:48 +01003162 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003163 return -EINVAL;
3164
Simon Wunderlich04f39042013-02-08 18:16:19 +01003165 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3166 if (err < 0)
3167 return err;
3168 if (err) {
3169 radar_detect_width = BIT(params.chandef.width);
3170 params.radar_required = true;
3171 }
3172
Michal Kaziore4e32452012-06-29 12:47:08 +02003173 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003174 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3175 params.chandef.chan,
3176 CHAN_MODE_SHARED,
3177 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003178 mutex_unlock(&rdev->devlist_mtx);
3179
3180 if (err)
3181 return err;
3182
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303183 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3184 params.acl = parse_acl_data(&rdev->wiphy, info);
3185 if (IS_ERR(params.acl))
3186 return PTR_ERR(params.acl);
3187 }
3188
Hila Gonene35e4d22012-06-27 17:19:42 +03003189 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003190 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003191 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003192 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003193 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003194 wdev->ssid_len = params.ssid_len;
3195 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003196 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303197
3198 kfree(params.acl);
3199
Johannes Berg56d18932011-05-09 18:41:15 +02003200 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003201}
3202
Johannes Berg88600202012-02-13 15:17:18 +01003203static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3204{
3205 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3206 struct net_device *dev = info->user_ptr[1];
3207 struct wireless_dev *wdev = dev->ieee80211_ptr;
3208 struct cfg80211_beacon_data params;
3209 int err;
3210
3211 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3212 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3213 return -EOPNOTSUPP;
3214
3215 if (!rdev->ops->change_beacon)
3216 return -EOPNOTSUPP;
3217
3218 if (!wdev->beacon_interval)
3219 return -EINVAL;
3220
3221 err = nl80211_parse_beacon(info, &params);
3222 if (err)
3223 return err;
3224
Hila Gonene35e4d22012-06-27 17:19:42 +03003225 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003226}
3227
3228static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003229{
Johannes Berg4c476992010-10-04 21:36:35 +02003230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3231 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003232
Michal Kazior60771782012-06-29 12:46:56 +02003233 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003234}
3235
Johannes Berg5727ef12007-12-19 02:03:34 +01003236static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3237 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3238 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3239 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003240 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003241 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003242 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003243};
3244
Johannes Bergeccb8e82009-05-11 21:57:56 +03003245static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003246 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003247 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003248{
3249 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003250 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003251 int flag;
3252
Johannes Bergeccb8e82009-05-11 21:57:56 +03003253 /*
3254 * Try parsing the new attribute first so userspace
3255 * can specify both for older kernels.
3256 */
3257 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3258 if (nla) {
3259 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003260
Johannes Bergeccb8e82009-05-11 21:57:56 +03003261 sta_flags = nla_data(nla);
3262 params->sta_flags_mask = sta_flags->mask;
3263 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003264 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003265 if ((params->sta_flags_mask |
3266 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3267 return -EINVAL;
3268 return 0;
3269 }
3270
3271 /* if present, parse the old attribute */
3272
3273 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003274 if (!nla)
3275 return 0;
3276
3277 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3278 nla, sta_flags_policy))
3279 return -EINVAL;
3280
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003281 /*
3282 * Only allow certain flags for interface types so that
3283 * other attributes are silently ignored. Remember that
3284 * this is backward compatibility code with old userspace
3285 * and shouldn't be hit in other cases anyway.
3286 */
3287 switch (iftype) {
3288 case NL80211_IFTYPE_AP:
3289 case NL80211_IFTYPE_AP_VLAN:
3290 case NL80211_IFTYPE_P2P_GO:
3291 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3292 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3293 BIT(NL80211_STA_FLAG_WME) |
3294 BIT(NL80211_STA_FLAG_MFP);
3295 break;
3296 case NL80211_IFTYPE_P2P_CLIENT:
3297 case NL80211_IFTYPE_STATION:
3298 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3299 BIT(NL80211_STA_FLAG_TDLS_PEER);
3300 break;
3301 case NL80211_IFTYPE_MESH_POINT:
3302 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3303 BIT(NL80211_STA_FLAG_MFP) |
3304 BIT(NL80211_STA_FLAG_AUTHORIZED);
3305 default:
3306 return -EINVAL;
3307 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003308
Johannes Berg3383b5a2012-05-10 20:14:43 +02003309 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3310 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003311 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003312
Johannes Berg3383b5a2012-05-10 20:14:43 +02003313 /* no longer support new API additions in old API */
3314 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3315 return -EINVAL;
3316 }
3317 }
3318
Johannes Berg5727ef12007-12-19 02:03:34 +01003319 return 0;
3320}
3321
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003322static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3323 int attr)
3324{
3325 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003326 u32 bitrate;
3327 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003328
3329 rate = nla_nest_start(msg, attr);
3330 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003331 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003332
3333 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3334 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003335 /* report 16-bit bitrate only if we can */
3336 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003337 if (bitrate > 0 &&
3338 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3339 return false;
3340 if (bitrate_compat > 0 &&
3341 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3342 return false;
3343
3344 if (info->flags & RATE_INFO_FLAGS_MCS) {
3345 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3346 return false;
3347 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3348 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3349 return false;
3350 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3351 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3352 return false;
3353 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3354 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3355 return false;
3356 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3357 return false;
3358 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3359 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3360 return false;
3361 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3362 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3363 return false;
3364 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3365 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3366 return false;
3367 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3368 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3369 return false;
3370 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3371 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3372 return false;
3373 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003374
3375 nla_nest_end(msg, rate);
3376 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003377}
3378
Felix Fietkau119363c2013-04-22 16:29:30 +02003379static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3380 int id)
3381{
3382 void *attr;
3383 int i = 0;
3384
3385 if (!mask)
3386 return true;
3387
3388 attr = nla_nest_start(msg, id);
3389 if (!attr)
3390 return false;
3391
3392 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3393 if (!(mask & BIT(i)))
3394 continue;
3395
3396 if (nla_put_u8(msg, i, signal[i]))
3397 return false;
3398 }
3399
3400 nla_nest_end(msg, attr);
3401
3402 return true;
3403}
3404
Eric W. Biederman15e47302012-09-07 20:12:54 +00003405static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003406 int flags,
3407 struct cfg80211_registered_device *rdev,
3408 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003409 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003410{
3411 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003412 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003413
Eric W. Biederman15e47302012-09-07 20:12:54 +00003414 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003415 if (!hdr)
3416 return -1;
3417
David S. Miller9360ffd2012-03-29 04:41:26 -04003418 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3419 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3420 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3421 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003422
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003423 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3424 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003425 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003426 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3427 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3428 sinfo->connected_time))
3429 goto nla_put_failure;
3430 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3431 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3432 sinfo->inactive_time))
3433 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003434 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3435 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003436 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003437 (u32)sinfo->rx_bytes))
3438 goto nla_put_failure;
3439 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3440 NL80211_STA_INFO_TX_BYTES64)) &&
3441 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3442 (u32)sinfo->tx_bytes))
3443 goto nla_put_failure;
3444 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3445 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003446 sinfo->rx_bytes))
3447 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003448 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3449 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003450 sinfo->tx_bytes))
3451 goto nla_put_failure;
3452 if ((sinfo->filled & STATION_INFO_LLID) &&
3453 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3454 goto nla_put_failure;
3455 if ((sinfo->filled & STATION_INFO_PLID) &&
3456 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3457 goto nla_put_failure;
3458 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3459 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3460 sinfo->plink_state))
3461 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003462 switch (rdev->wiphy.signal_type) {
3463 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003464 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3465 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3466 sinfo->signal))
3467 goto nla_put_failure;
3468 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3469 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3470 sinfo->signal_avg))
3471 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003472 break;
3473 default:
3474 break;
3475 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003476 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3477 if (!nl80211_put_signal(msg, sinfo->chains,
3478 sinfo->chain_signal,
3479 NL80211_STA_INFO_CHAIN_SIGNAL))
3480 goto nla_put_failure;
3481 }
3482 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3483 if (!nl80211_put_signal(msg, sinfo->chains,
3484 sinfo->chain_signal_avg,
3485 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3486 goto nla_put_failure;
3487 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003488 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003489 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3490 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003491 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003492 }
3493 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3494 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3495 NL80211_STA_INFO_RX_BITRATE))
3496 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003497 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003498 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3499 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3500 sinfo->rx_packets))
3501 goto nla_put_failure;
3502 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3503 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3504 sinfo->tx_packets))
3505 goto nla_put_failure;
3506 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3507 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3508 sinfo->tx_retries))
3509 goto nla_put_failure;
3510 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3511 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3512 sinfo->tx_failed))
3513 goto nla_put_failure;
3514 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3515 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3516 sinfo->beacon_loss_count))
3517 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003518 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3519 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3520 sinfo->local_pm))
3521 goto nla_put_failure;
3522 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3523 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3524 sinfo->peer_pm))
3525 goto nla_put_failure;
3526 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3527 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3528 sinfo->nonpeer_pm))
3529 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003530 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3531 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3532 if (!bss_param)
3533 goto nla_put_failure;
3534
David S. Miller9360ffd2012-03-29 04:41:26 -04003535 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3536 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3537 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3538 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3539 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3540 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3541 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3542 sinfo->bss_param.dtim_period) ||
3543 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3544 sinfo->bss_param.beacon_interval))
3545 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003546
3547 nla_nest_end(msg, bss_param);
3548 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003549 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3550 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3551 sizeof(struct nl80211_sta_flag_update),
3552 &sinfo->sta_flags))
3553 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003554 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3555 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3556 sinfo->t_offset))
3557 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003558 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003559
David S. Miller9360ffd2012-03-29 04:41:26 -04003560 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3561 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3562 sinfo->assoc_req_ies))
3563 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003564
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003565 return genlmsg_end(msg, hdr);
3566
3567 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003568 genlmsg_cancel(msg, hdr);
3569 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003570}
3571
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003572static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003573 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003574{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003575 struct station_info sinfo;
3576 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003577 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003578 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003579 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003580 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003581
Johannes Berg97990a02013-04-19 01:02:55 +02003582 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003583 if (err)
3584 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003585
Johannes Berg97990a02013-04-19 01:02:55 +02003586 if (!wdev->netdev) {
3587 err = -EINVAL;
3588 goto out_err;
3589 }
3590
Johannes Bergbba95fe2008-07-29 13:22:51 +02003591 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003592 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003593 goto out_err;
3594 }
3595
Johannes Bergbba95fe2008-07-29 13:22:51 +02003596 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003597 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003598 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003599 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003600 if (err == -ENOENT)
3601 break;
3602 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003603 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003604
3605 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003606 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003607 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003608 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003609 &sinfo) < 0)
3610 goto out;
3611
3612 sta_idx++;
3613 }
3614
3615
3616 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003617 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003618 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003619 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003620 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003621
3622 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003623}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003624
Johannes Berg5727ef12007-12-19 02:03:34 +01003625static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3626{
Johannes Berg4c476992010-10-04 21:36:35 +02003627 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3628 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003629 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003630 struct sk_buff *msg;
3631 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003632 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003633
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003634 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003635
3636 if (!info->attrs[NL80211_ATTR_MAC])
3637 return -EINVAL;
3638
3639 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3640
Johannes Berg4c476992010-10-04 21:36:35 +02003641 if (!rdev->ops->get_station)
3642 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003643
Hila Gonene35e4d22012-06-27 17:19:42 +03003644 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003645 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003646 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003647
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003648 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003649 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003650 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003651
Eric W. Biederman15e47302012-09-07 20:12:54 +00003652 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003653 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003654 nlmsg_free(msg);
3655 return -ENOBUFS;
3656 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003657
Johannes Berg4c476992010-10-04 21:36:35 +02003658 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003659}
3660
Johannes Berg77ee7c82013-02-15 00:48:33 +01003661int cfg80211_check_station_change(struct wiphy *wiphy,
3662 struct station_parameters *params,
3663 enum cfg80211_station_type statype)
3664{
3665 if (params->listen_interval != -1)
3666 return -EINVAL;
3667 if (params->aid)
3668 return -EINVAL;
3669
3670 /* When you run into this, adjust the code below for the new flag */
3671 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3672
3673 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003674 case CFG80211_STA_MESH_PEER_KERNEL:
3675 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003676 /*
3677 * No ignoring the TDLS flag here -- the userspace mesh
3678 * code doesn't have the bug of including TDLS in the
3679 * mask everywhere.
3680 */
3681 if (params->sta_flags_mask &
3682 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3683 BIT(NL80211_STA_FLAG_MFP) |
3684 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3685 return -EINVAL;
3686 break;
3687 case CFG80211_STA_TDLS_PEER_SETUP:
3688 case CFG80211_STA_TDLS_PEER_ACTIVE:
3689 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3690 return -EINVAL;
3691 /* ignore since it can't change */
3692 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3693 break;
3694 default:
3695 /* disallow mesh-specific things */
3696 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3697 return -EINVAL;
3698 if (params->local_pm)
3699 return -EINVAL;
3700 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3701 return -EINVAL;
3702 }
3703
3704 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3705 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3706 /* TDLS can't be set, ... */
3707 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3708 return -EINVAL;
3709 /*
3710 * ... but don't bother the driver with it. This works around
3711 * a hostapd/wpa_supplicant issue -- it always includes the
3712 * TLDS_PEER flag in the mask even for AP mode.
3713 */
3714 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3715 }
3716
3717 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3718 /* reject other things that can't change */
3719 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3720 return -EINVAL;
3721 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3722 return -EINVAL;
3723 if (params->supported_rates)
3724 return -EINVAL;
3725 if (params->ext_capab || params->ht_capa || params->vht_capa)
3726 return -EINVAL;
3727 }
3728
3729 if (statype != CFG80211_STA_AP_CLIENT) {
3730 if (params->vlan)
3731 return -EINVAL;
3732 }
3733
3734 switch (statype) {
3735 case CFG80211_STA_AP_MLME_CLIENT:
3736 /* Use this only for authorizing/unauthorizing a station */
3737 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3738 return -EOPNOTSUPP;
3739 break;
3740 case CFG80211_STA_AP_CLIENT:
3741 /* accept only the listed bits */
3742 if (params->sta_flags_mask &
3743 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3744 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3745 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3746 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3747 BIT(NL80211_STA_FLAG_WME) |
3748 BIT(NL80211_STA_FLAG_MFP)))
3749 return -EINVAL;
3750
3751 /* but authenticated/associated only if driver handles it */
3752 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3753 params->sta_flags_mask &
3754 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3755 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3756 return -EINVAL;
3757 break;
3758 case CFG80211_STA_IBSS:
3759 case CFG80211_STA_AP_STA:
3760 /* reject any changes other than AUTHORIZED */
3761 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3762 return -EINVAL;
3763 break;
3764 case CFG80211_STA_TDLS_PEER_SETUP:
3765 /* reject any changes other than AUTHORIZED or WME */
3766 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3767 BIT(NL80211_STA_FLAG_WME)))
3768 return -EINVAL;
3769 /* force (at least) rates when authorizing */
3770 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3771 !params->supported_rates)
3772 return -EINVAL;
3773 break;
3774 case CFG80211_STA_TDLS_PEER_ACTIVE:
3775 /* reject any changes */
3776 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003777 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003778 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3779 return -EINVAL;
3780 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003781 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003782 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3783 return -EINVAL;
3784 break;
3785 }
3786
3787 return 0;
3788}
3789EXPORT_SYMBOL(cfg80211_check_station_change);
3790
Johannes Berg5727ef12007-12-19 02:03:34 +01003791/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003792 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003793 */
Johannes Berg80b99892011-11-18 16:23:01 +01003794static struct net_device *get_vlan(struct genl_info *info,
3795 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003796{
Johannes Berg463d0182009-07-14 00:33:35 +02003797 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003798 struct net_device *v;
3799 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003800
Johannes Berg80b99892011-11-18 16:23:01 +01003801 if (!vlanattr)
3802 return NULL;
3803
3804 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3805 if (!v)
3806 return ERR_PTR(-ENODEV);
3807
3808 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3809 ret = -EINVAL;
3810 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003811 }
Johannes Berg80b99892011-11-18 16:23:01 +01003812
Johannes Berg77ee7c82013-02-15 00:48:33 +01003813 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3814 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3815 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3816 ret = -EINVAL;
3817 goto error;
3818 }
3819
Johannes Berg80b99892011-11-18 16:23:01 +01003820 if (!netif_running(v)) {
3821 ret = -ENETDOWN;
3822 goto error;
3823 }
3824
3825 return v;
3826 error:
3827 dev_put(v);
3828 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003829}
3830
Jouni Malinendf881292013-02-14 21:10:54 +02003831static struct nla_policy
3832nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3833 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3834 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3835};
3836
Johannes Bergff276692013-02-15 00:09:01 +01003837static int nl80211_parse_sta_wme(struct genl_info *info,
3838 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003839{
Jouni Malinendf881292013-02-14 21:10:54 +02003840 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3841 struct nlattr *nla;
3842 int err;
3843
Jouni Malinendf881292013-02-14 21:10:54 +02003844 /* parse WME attributes if present */
3845 if (!info->attrs[NL80211_ATTR_STA_WME])
3846 return 0;
3847
3848 nla = info->attrs[NL80211_ATTR_STA_WME];
3849 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3850 nl80211_sta_wme_policy);
3851 if (err)
3852 return err;
3853
3854 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3855 params->uapsd_queues = nla_get_u8(
3856 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3857 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3858 return -EINVAL;
3859
3860 if (tb[NL80211_STA_WME_MAX_SP])
3861 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3862
3863 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3864 return -EINVAL;
3865
3866 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3867
3868 return 0;
3869}
3870
Johannes Bergff276692013-02-15 00:09:01 +01003871static int nl80211_set_station_tdls(struct genl_info *info,
3872 struct station_parameters *params)
3873{
3874 /* Dummy STA entry gets updated once the peer capabilities are known */
3875 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3876 params->ht_capa =
3877 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3878 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3879 params->vht_capa =
3880 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3881
3882 return nl80211_parse_sta_wme(info, params);
3883}
3884
Johannes Berg5727ef12007-12-19 02:03:34 +01003885static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3886{
Johannes Berg4c476992010-10-04 21:36:35 +02003887 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003888 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003889 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003890 u8 *mac_addr;
3891 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003892
3893 memset(&params, 0, sizeof(params));
3894
3895 params.listen_interval = -1;
3896
Johannes Berg77ee7c82013-02-15 00:48:33 +01003897 if (!rdev->ops->change_station)
3898 return -EOPNOTSUPP;
3899
Johannes Berg5727ef12007-12-19 02:03:34 +01003900 if (info->attrs[NL80211_ATTR_STA_AID])
3901 return -EINVAL;
3902
3903 if (!info->attrs[NL80211_ATTR_MAC])
3904 return -EINVAL;
3905
3906 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3907
3908 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3909 params.supported_rates =
3910 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3911 params.supported_rates_len =
3912 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3913 }
3914
Jouni Malinen9d62a982013-02-14 21:10:13 +02003915 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3916 params.capability =
3917 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3918 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3919 }
3920
3921 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3922 params.ext_capab =
3923 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3924 params.ext_capab_len =
3925 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3926 }
3927
Jouni Malinendf881292013-02-14 21:10:54 +02003928 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003929 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003930
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003931 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003932 return -EINVAL;
3933
Johannes Bergf8bacc22013-02-14 23:27:01 +01003934 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003935 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003936 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3937 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3938 return -EINVAL;
3939 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003940
Johannes Bergf8bacc22013-02-14 23:27:01 +01003941 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003942 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003943 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3944 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3945 return -EINVAL;
3946 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3947 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003948
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003949 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3950 enum nl80211_mesh_power_mode pm = nla_get_u32(
3951 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3952
3953 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3954 pm > NL80211_MESH_POWER_MAX)
3955 return -EINVAL;
3956
3957 params.local_pm = pm;
3958 }
3959
Johannes Berg77ee7c82013-02-15 00:48:33 +01003960 /* Include parameters for TDLS peer (will check later) */
3961 err = nl80211_set_station_tdls(info, &params);
3962 if (err)
3963 return err;
3964
3965 params.vlan = get_vlan(info, rdev);
3966 if (IS_ERR(params.vlan))
3967 return PTR_ERR(params.vlan);
3968
Johannes Berga97f4422009-06-18 17:23:43 +02003969 switch (dev->ieee80211_ptr->iftype) {
3970 case NL80211_IFTYPE_AP:
3971 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003972 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003973 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003974 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003975 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003976 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003977 break;
3978 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003979 err = -EOPNOTSUPP;
3980 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003981 }
3982
Johannes Berg77ee7c82013-02-15 00:48:33 +01003983 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003984 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003985
Johannes Berg77ee7c82013-02-15 00:48:33 +01003986 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003987 if (params.vlan)
3988 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003989
Johannes Berg5727ef12007-12-19 02:03:34 +01003990 return err;
3991}
3992
3993static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3994{
Johannes Berg4c476992010-10-04 21:36:35 +02003995 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003996 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003997 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003998 struct station_parameters params;
3999 u8 *mac_addr = NULL;
4000
4001 memset(&params, 0, sizeof(params));
4002
Johannes Berg984c3112013-02-14 23:43:25 +01004003 if (!rdev->ops->add_station)
4004 return -EOPNOTSUPP;
4005
Johannes Berg5727ef12007-12-19 02:03:34 +01004006 if (!info->attrs[NL80211_ATTR_MAC])
4007 return -EINVAL;
4008
Johannes Berg5727ef12007-12-19 02:03:34 +01004009 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4010 return -EINVAL;
4011
4012 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4013 return -EINVAL;
4014
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004015 if (!info->attrs[NL80211_ATTR_STA_AID])
4016 return -EINVAL;
4017
Johannes Berg5727ef12007-12-19 02:03:34 +01004018 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4019 params.supported_rates =
4020 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4021 params.supported_rates_len =
4022 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4023 params.listen_interval =
4024 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004025
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004026 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4027 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4028 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004029
Jouni Malinen9d62a982013-02-14 21:10:13 +02004030 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4031 params.capability =
4032 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4033 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4034 }
4035
4036 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4037 params.ext_capab =
4038 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4039 params.ext_capab_len =
4040 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4041 }
4042
Jouni Malinen36aedc92008-08-25 11:58:58 +03004043 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4044 params.ht_capa =
4045 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004046
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004047 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4048 params.vht_capa =
4049 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4050
Johannes Bergf8bacc22013-02-14 23:27:01 +01004051 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004052 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004053 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4054 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4055 return -EINVAL;
4056 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004057
Johannes Bergff276692013-02-15 00:09:01 +01004058 err = nl80211_parse_sta_wme(info, &params);
4059 if (err)
4060 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004061
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004062 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004063 return -EINVAL;
4064
Johannes Berg77ee7c82013-02-15 00:48:33 +01004065 /* When you run into this, adjust the code below for the new flag */
4066 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4067
Johannes Bergbdd90d52011-12-14 12:20:27 +01004068 switch (dev->ieee80211_ptr->iftype) {
4069 case NL80211_IFTYPE_AP:
4070 case NL80211_IFTYPE_AP_VLAN:
4071 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004072 /* ignore WME attributes if iface/sta is not capable */
4073 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4074 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4075 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004076
Johannes Bergbdd90d52011-12-14 12:20:27 +01004077 /* TDLS peers cannot be added */
4078 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004079 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004080 /* but don't bother the driver with it */
4081 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004082
Johannes Bergd582cff2012-10-26 17:53:44 +02004083 /* allow authenticated/associated only if driver handles it */
4084 if (!(rdev->wiphy.features &
4085 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4086 params.sta_flags_mask &
4087 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4088 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4089 return -EINVAL;
4090
Johannes Bergbdd90d52011-12-14 12:20:27 +01004091 /* must be last in here for error handling */
4092 params.vlan = get_vlan(info, rdev);
4093 if (IS_ERR(params.vlan))
4094 return PTR_ERR(params.vlan);
4095 break;
4096 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004097 /* ignore uAPSD data */
4098 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4099
Johannes Bergd582cff2012-10-26 17:53:44 +02004100 /* associated is disallowed */
4101 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4102 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004103 /* TDLS peers cannot be added */
4104 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004105 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004106 break;
4107 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004108 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004109 /* ignore uAPSD data */
4110 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4111
Johannes Berg77ee7c82013-02-15 00:48:33 +01004112 /* these are disallowed */
4113 if (params.sta_flags_mask &
4114 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4115 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004116 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004117 /* Only TDLS peers can be added */
4118 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4119 return -EINVAL;
4120 /* Can only add if TDLS ... */
4121 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4122 return -EOPNOTSUPP;
4123 /* ... with external setup is supported */
4124 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4125 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004126 /*
4127 * Older wpa_supplicant versions always mark the TDLS peer
4128 * as authorized, but it shouldn't yet be.
4129 */
4130 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004131 break;
4132 default:
4133 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004134 }
4135
Johannes Bergbdd90d52011-12-14 12:20:27 +01004136 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004137
Hila Gonene35e4d22012-06-27 17:19:42 +03004138 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004139
Johannes Berg5727ef12007-12-19 02:03:34 +01004140 if (params.vlan)
4141 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004142 return err;
4143}
4144
4145static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4146{
Johannes Berg4c476992010-10-04 21:36:35 +02004147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4148 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004149 u8 *mac_addr = NULL;
4150
4151 if (info->attrs[NL80211_ATTR_MAC])
4152 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4153
Johannes Berge80cf852009-05-11 14:43:13 +02004154 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004155 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004156 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004157 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4158 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004159
Johannes Berg4c476992010-10-04 21:36:35 +02004160 if (!rdev->ops->del_station)
4161 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004162
Hila Gonene35e4d22012-06-27 17:19:42 +03004163 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004164}
4165
Eric W. Biederman15e47302012-09-07 20:12:54 +00004166static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004167 int flags, struct net_device *dev,
4168 u8 *dst, u8 *next_hop,
4169 struct mpath_info *pinfo)
4170{
4171 void *hdr;
4172 struct nlattr *pinfoattr;
4173
Eric W. Biederman15e47302012-09-07 20:12:54 +00004174 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004175 if (!hdr)
4176 return -1;
4177
David S. Miller9360ffd2012-03-29 04:41:26 -04004178 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4179 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4180 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4181 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4182 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004183
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004184 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4185 if (!pinfoattr)
4186 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004187 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4188 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4189 pinfo->frame_qlen))
4190 goto nla_put_failure;
4191 if (((pinfo->filled & MPATH_INFO_SN) &&
4192 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4193 ((pinfo->filled & MPATH_INFO_METRIC) &&
4194 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4195 pinfo->metric)) ||
4196 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4197 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4198 pinfo->exptime)) ||
4199 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4200 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4201 pinfo->flags)) ||
4202 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4203 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4204 pinfo->discovery_timeout)) ||
4205 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4206 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4207 pinfo->discovery_retries)))
4208 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004209
4210 nla_nest_end(msg, pinfoattr);
4211
4212 return genlmsg_end(msg, hdr);
4213
4214 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004215 genlmsg_cancel(msg, hdr);
4216 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004217}
4218
4219static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004220 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004221{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004222 struct mpath_info pinfo;
4223 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004224 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004225 u8 dst[ETH_ALEN];
4226 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004227 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004228 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004229
Johannes Berg97990a02013-04-19 01:02:55 +02004230 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004231 if (err)
4232 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004233
4234 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004235 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004236 goto out_err;
4237 }
4238
Johannes Berg97990a02013-04-19 01:02:55 +02004239 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004240 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004241 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004242 }
4243
Johannes Bergbba95fe2008-07-29 13:22:51 +02004244 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004245 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4246 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004247 if (err == -ENOENT)
4248 break;
4249 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004250 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004251
Eric W. Biederman15e47302012-09-07 20:12:54 +00004252 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004253 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004254 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004255 &pinfo) < 0)
4256 goto out;
4257
4258 path_idx++;
4259 }
4260
4261
4262 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004263 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004264 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004265 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004266 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004267 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004268}
4269
4270static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4271{
Johannes Berg4c476992010-10-04 21:36:35 +02004272 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004273 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004274 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004275 struct mpath_info pinfo;
4276 struct sk_buff *msg;
4277 u8 *dst = NULL;
4278 u8 next_hop[ETH_ALEN];
4279
4280 memset(&pinfo, 0, sizeof(pinfo));
4281
4282 if (!info->attrs[NL80211_ATTR_MAC])
4283 return -EINVAL;
4284
4285 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4286
Johannes Berg4c476992010-10-04 21:36:35 +02004287 if (!rdev->ops->get_mpath)
4288 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004289
Johannes Berg4c476992010-10-04 21:36:35 +02004290 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4291 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004292
Hila Gonene35e4d22012-06-27 17:19:42 +03004293 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004294 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004295 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004296
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004297 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004298 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004299 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004300
Eric W. Biederman15e47302012-09-07 20:12:54 +00004301 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004302 dev, dst, next_hop, &pinfo) < 0) {
4303 nlmsg_free(msg);
4304 return -ENOBUFS;
4305 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004306
Johannes Berg4c476992010-10-04 21:36:35 +02004307 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004308}
4309
4310static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4311{
Johannes Berg4c476992010-10-04 21:36:35 +02004312 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4313 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004314 u8 *dst = NULL;
4315 u8 *next_hop = NULL;
4316
4317 if (!info->attrs[NL80211_ATTR_MAC])
4318 return -EINVAL;
4319
4320 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4321 return -EINVAL;
4322
4323 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4324 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4325
Johannes Berg4c476992010-10-04 21:36:35 +02004326 if (!rdev->ops->change_mpath)
4327 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004328
Johannes Berg4c476992010-10-04 21:36:35 +02004329 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4330 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004331
Hila Gonene35e4d22012-06-27 17:19:42 +03004332 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004333}
Johannes Berg4c476992010-10-04 21:36:35 +02004334
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004335static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4336{
Johannes Berg4c476992010-10-04 21:36:35 +02004337 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4338 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004339 u8 *dst = NULL;
4340 u8 *next_hop = NULL;
4341
4342 if (!info->attrs[NL80211_ATTR_MAC])
4343 return -EINVAL;
4344
4345 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4346 return -EINVAL;
4347
4348 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4349 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4350
Johannes Berg4c476992010-10-04 21:36:35 +02004351 if (!rdev->ops->add_mpath)
4352 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004353
Johannes Berg4c476992010-10-04 21:36:35 +02004354 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4355 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004356
Hila Gonene35e4d22012-06-27 17:19:42 +03004357 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004358}
4359
4360static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4361{
Johannes Berg4c476992010-10-04 21:36:35 +02004362 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4363 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004364 u8 *dst = NULL;
4365
4366 if (info->attrs[NL80211_ATTR_MAC])
4367 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4368
Johannes Berg4c476992010-10-04 21:36:35 +02004369 if (!rdev->ops->del_mpath)
4370 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004371
Hila Gonene35e4d22012-06-27 17:19:42 +03004372 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004373}
4374
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004375static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4376{
Johannes Berg4c476992010-10-04 21:36:35 +02004377 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4378 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004379 struct bss_parameters params;
4380
4381 memset(&params, 0, sizeof(params));
4382 /* default to not changing parameters */
4383 params.use_cts_prot = -1;
4384 params.use_short_preamble = -1;
4385 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004386 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004387 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004388 params.p2p_ctwindow = -1;
4389 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004390
4391 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4392 params.use_cts_prot =
4393 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4394 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4395 params.use_short_preamble =
4396 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4397 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4398 params.use_short_slot_time =
4399 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004400 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4401 params.basic_rates =
4402 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4403 params.basic_rates_len =
4404 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4405 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004406 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4407 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004408 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4409 params.ht_opmode =
4410 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004411
Johannes Berg53cabad2012-11-14 15:17:28 +01004412 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4413 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4414 return -EINVAL;
4415 params.p2p_ctwindow =
4416 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4417 if (params.p2p_ctwindow < 0)
4418 return -EINVAL;
4419 if (params.p2p_ctwindow != 0 &&
4420 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4421 return -EINVAL;
4422 }
4423
4424 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4425 u8 tmp;
4426
4427 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4428 return -EINVAL;
4429 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4430 if (tmp > 1)
4431 return -EINVAL;
4432 params.p2p_opp_ps = tmp;
4433 if (params.p2p_opp_ps &&
4434 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4435 return -EINVAL;
4436 }
4437
Johannes Berg4c476992010-10-04 21:36:35 +02004438 if (!rdev->ops->change_bss)
4439 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004440
Johannes Berg074ac8d2010-09-16 14:58:22 +02004441 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004442 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4443 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004444
Hila Gonene35e4d22012-06-27 17:19:42 +03004445 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004446}
4447
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004448static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004449 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4450 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4451 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4452 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4453 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4454 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4455};
4456
4457static int parse_reg_rule(struct nlattr *tb[],
4458 struct ieee80211_reg_rule *reg_rule)
4459{
4460 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4461 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4462
4463 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4464 return -EINVAL;
4465 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4466 return -EINVAL;
4467 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4468 return -EINVAL;
4469 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4470 return -EINVAL;
4471 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4472 return -EINVAL;
4473
4474 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4475
4476 freq_range->start_freq_khz =
4477 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4478 freq_range->end_freq_khz =
4479 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4480 freq_range->max_bandwidth_khz =
4481 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4482
4483 power_rule->max_eirp =
4484 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4485
4486 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4487 power_rule->max_antenna_gain =
4488 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4489
4490 return 0;
4491}
4492
4493static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4494{
4495 int r;
4496 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004497 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004498
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004499 /*
4500 * You should only get this when cfg80211 hasn't yet initialized
4501 * completely when built-in to the kernel right between the time
4502 * window between nl80211_init() and regulatory_init(), if that is
4503 * even possible.
4504 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004505 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004506 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004507
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004508 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4509 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004510
4511 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4512
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004513 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4514 user_reg_hint_type =
4515 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4516 else
4517 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4518
4519 switch (user_reg_hint_type) {
4520 case NL80211_USER_REG_HINT_USER:
4521 case NL80211_USER_REG_HINT_CELL_BASE:
4522 break;
4523 default:
4524 return -EINVAL;
4525 }
4526
4527 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004528
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004529 return r;
4530}
4531
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004532static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004533 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004534{
Johannes Berg4c476992010-10-04 21:36:35 +02004535 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004536 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004537 struct wireless_dev *wdev = dev->ieee80211_ptr;
4538 struct mesh_config cur_params;
4539 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004540 void *hdr;
4541 struct nlattr *pinfoattr;
4542 struct sk_buff *msg;
4543
Johannes Berg29cbe682010-12-03 09:20:44 +01004544 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4545 return -EOPNOTSUPP;
4546
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004547 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004548 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004549
Johannes Berg29cbe682010-12-03 09:20:44 +01004550 wdev_lock(wdev);
4551 /* If not connected, get default parameters */
4552 if (!wdev->mesh_id_len)
4553 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4554 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004555 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004556 wdev_unlock(wdev);
4557
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004558 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004559 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004560
4561 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004562 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004563 if (!msg)
4564 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004565 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004566 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004567 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004568 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004569 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004570 if (!pinfoattr)
4571 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004572 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4573 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4574 cur_params.dot11MeshRetryTimeout) ||
4575 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4576 cur_params.dot11MeshConfirmTimeout) ||
4577 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4578 cur_params.dot11MeshHoldingTimeout) ||
4579 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4580 cur_params.dot11MeshMaxPeerLinks) ||
4581 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4582 cur_params.dot11MeshMaxRetries) ||
4583 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4584 cur_params.dot11MeshTTL) ||
4585 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4586 cur_params.element_ttl) ||
4587 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4588 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004589 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4590 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004591 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4592 cur_params.dot11MeshHWMPmaxPREQretries) ||
4593 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4594 cur_params.path_refresh_time) ||
4595 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4596 cur_params.min_discovery_timeout) ||
4597 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4598 cur_params.dot11MeshHWMPactivePathTimeout) ||
4599 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4600 cur_params.dot11MeshHWMPpreqMinInterval) ||
4601 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4602 cur_params.dot11MeshHWMPperrMinInterval) ||
4603 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4604 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4605 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4606 cur_params.dot11MeshHWMPRootMode) ||
4607 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4608 cur_params.dot11MeshHWMPRannInterval) ||
4609 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4610 cur_params.dot11MeshGateAnnouncementProtocol) ||
4611 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4612 cur_params.dot11MeshForwarding) ||
4613 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004614 cur_params.rssi_threshold) ||
4615 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004616 cur_params.ht_opmode) ||
4617 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4618 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4619 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004620 cur_params.dot11MeshHWMProotInterval) ||
4621 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004622 cur_params.dot11MeshHWMPconfirmationInterval) ||
4623 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4624 cur_params.power_mode) ||
4625 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4626 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004627 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004628 nla_nest_end(msg, pinfoattr);
4629 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004630 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004631
Johannes Berg3b858752009-03-12 09:55:09 +01004632 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004633 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004634 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004635 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004636 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004637}
4638
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004639static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004640 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4641 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4642 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4643 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4644 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4645 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004646 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004647 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004648 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004649 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4650 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4651 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4652 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4653 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004654 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004655 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004656 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004657 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004658 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004659 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004660 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4661 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004662 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4663 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004664 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004665 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4666 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004667};
4668
Javier Cardonac80d5452010-12-16 17:37:49 -08004669static const struct nla_policy
4670 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004671 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004672 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4673 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004674 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004675 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004676 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004677 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004678 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004679};
4680
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004681static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004682 struct mesh_config *cfg,
4683 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004684{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004685 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004686 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004687
Marco Porschea54fba2013-01-07 16:04:48 +01004688#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4689do { \
4690 if (tb[attr]) { \
4691 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4692 return -EINVAL; \
4693 cfg->param = fn(tb[attr]); \
4694 mask |= (1 << (attr - 1)); \
4695 } \
4696} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004697
4698
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004699 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004700 return -EINVAL;
4701 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004702 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004703 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004704 return -EINVAL;
4705
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004706 /* This makes sure that there aren't more than 32 mesh config
4707 * parameters (otherwise our bitfield scheme would not work.) */
4708 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4709
4710 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004712 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4713 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004714 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004715 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4716 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004717 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004718 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4719 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004720 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004721 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4722 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004723 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004724 mask, NL80211_MESHCONF_MAX_RETRIES,
4725 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004726 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004727 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004728 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004729 mask, NL80211_MESHCONF_ELEMENT_TTL,
4730 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004731 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004732 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4733 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004734 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4735 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004736 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4737 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004738 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004739 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4740 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004741 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004742 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4743 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004744 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004745 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4746 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004747 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4748 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004749 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4750 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004751 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004752 1, 65535, mask,
4753 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004754 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004755 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004756 1, 65535, mask,
4757 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004758 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004759 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004760 dot11MeshHWMPnetDiameterTraversalTime,
4761 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004762 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4763 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004764 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4765 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4766 nla_get_u8);
4767 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4768 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004769 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004770 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004771 dot11MeshGateAnnouncementProtocol, 0, 1,
4772 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004773 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004774 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004775 mask, NL80211_MESHCONF_FORWARDING,
4776 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004777 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004778 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4779 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004780 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004781 mask, NL80211_MESHCONF_HT_OPMODE,
4782 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004783 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004784 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004785 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4786 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004787 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004788 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4789 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004790 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004791 dot11MeshHWMPconfirmationInterval,
4792 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004793 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4794 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004795 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4796 NL80211_MESH_POWER_ACTIVE,
4797 NL80211_MESH_POWER_MAX,
4798 mask, NL80211_MESHCONF_POWER_MODE,
4799 nla_get_u32);
4800 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4801 0, 65535, mask,
4802 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004803 if (mask_out)
4804 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004805
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004806 return 0;
4807
4808#undef FILL_IN_MESH_PARAM_IF_SET
4809}
4810
Javier Cardonac80d5452010-12-16 17:37:49 -08004811static int nl80211_parse_mesh_setup(struct genl_info *info,
4812 struct mesh_setup *setup)
4813{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004815 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4816
4817 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4818 return -EINVAL;
4819 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4820 info->attrs[NL80211_ATTR_MESH_SETUP],
4821 nl80211_mesh_setup_params_policy))
4822 return -EINVAL;
4823
Javier Cardonad299a1f2012-03-31 11:31:33 -07004824 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4825 setup->sync_method =
4826 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4827 IEEE80211_SYNC_METHOD_VENDOR :
4828 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4829
Javier Cardonac80d5452010-12-16 17:37:49 -08004830 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4831 setup->path_sel_proto =
4832 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4833 IEEE80211_PATH_PROTOCOL_VENDOR :
4834 IEEE80211_PATH_PROTOCOL_HWMP;
4835
4836 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4837 setup->path_metric =
4838 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4839 IEEE80211_PATH_METRIC_VENDOR :
4840 IEEE80211_PATH_METRIC_AIRTIME;
4841
Javier Cardona581a8b02011-04-07 15:08:27 -07004842
4843 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004844 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004845 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004846 if (!is_valid_ie_attr(ieattr))
4847 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004848 setup->ie = nla_data(ieattr);
4849 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004850 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004851 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4852 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4853 return -EINVAL;
4854 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004855 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4856 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004857 if (setup->is_secure)
4858 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004859
4860 return 0;
4861}
4862
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004863static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004864 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004865{
4866 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4867 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004868 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004869 struct mesh_config cfg;
4870 u32 mask;
4871 int err;
4872
Johannes Berg29cbe682010-12-03 09:20:44 +01004873 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4874 return -EOPNOTSUPP;
4875
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004876 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004877 return -EOPNOTSUPP;
4878
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004879 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004880 if (err)
4881 return err;
4882
Johannes Berg29cbe682010-12-03 09:20:44 +01004883 wdev_lock(wdev);
4884 if (!wdev->mesh_id_len)
4885 err = -ENOLINK;
4886
4887 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004888 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004889
4890 wdev_unlock(wdev);
4891
4892 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004893}
4894
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004895static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4896{
Johannes Berg458f4f92012-12-06 15:47:38 +01004897 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004898 struct sk_buff *msg;
4899 void *hdr = NULL;
4900 struct nlattr *nl_reg_rules;
4901 unsigned int i;
4902 int err = -EINVAL;
4903
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004904 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004905
4906 if (!cfg80211_regdomain)
4907 goto out;
4908
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004909 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004910 if (!msg) {
4911 err = -ENOBUFS;
4912 goto out;
4913 }
4914
Eric W. Biederman15e47302012-09-07 20:12:54 +00004915 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004916 NL80211_CMD_GET_REG);
4917 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004918 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004919
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004920 if (reg_last_request_cell_base() &&
4921 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4922 NL80211_USER_REG_HINT_CELL_BASE))
4923 goto nla_put_failure;
4924
Johannes Berg458f4f92012-12-06 15:47:38 +01004925 rcu_read_lock();
4926 regdom = rcu_dereference(cfg80211_regdomain);
4927
4928 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4929 (regdom->dfs_region &&
4930 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4931 goto nla_put_failure_rcu;
4932
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004933 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4934 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004935 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004936
Johannes Berg458f4f92012-12-06 15:47:38 +01004937 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004938 struct nlattr *nl_reg_rule;
4939 const struct ieee80211_reg_rule *reg_rule;
4940 const struct ieee80211_freq_range *freq_range;
4941 const struct ieee80211_power_rule *power_rule;
4942
Johannes Berg458f4f92012-12-06 15:47:38 +01004943 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004944 freq_range = &reg_rule->freq_range;
4945 power_rule = &reg_rule->power_rule;
4946
4947 nl_reg_rule = nla_nest_start(msg, i);
4948 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004949 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004950
David S. Miller9360ffd2012-03-29 04:41:26 -04004951 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4952 reg_rule->flags) ||
4953 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4954 freq_range->start_freq_khz) ||
4955 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4956 freq_range->end_freq_khz) ||
4957 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4958 freq_range->max_bandwidth_khz) ||
4959 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4960 power_rule->max_antenna_gain) ||
4961 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4962 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004963 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004964
4965 nla_nest_end(msg, nl_reg_rule);
4966 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004967 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004968
4969 nla_nest_end(msg, nl_reg_rules);
4970
4971 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004972 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004973 goto out;
4974
Johannes Berg458f4f92012-12-06 15:47:38 +01004975nla_put_failure_rcu:
4976 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004977nla_put_failure:
4978 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004979put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004980 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004981 err = -EMSGSIZE;
4982out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004983 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004984 return err;
4985}
4986
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004987static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4988{
4989 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4990 struct nlattr *nl_reg_rule;
4991 char *alpha2 = NULL;
4992 int rem_reg_rules = 0, r = 0;
4993 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004994 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004995 struct ieee80211_regdomain *rd = NULL;
4996
4997 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4998 return -EINVAL;
4999
5000 if (!info->attrs[NL80211_ATTR_REG_RULES])
5001 return -EINVAL;
5002
5003 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5004
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005005 if (info->attrs[NL80211_ATTR_DFS_REGION])
5006 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5007
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005008 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005009 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005010 num_rules++;
5011 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005012 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005013 }
5014
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005015 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005016 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005017
5018 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005019 if (!rd)
5020 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005021
5022 rd->n_reg_rules = num_rules;
5023 rd->alpha2[0] = alpha2[0];
5024 rd->alpha2[1] = alpha2[1];
5025
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005026 /*
5027 * Disable DFS master mode if the DFS region was
5028 * not supported or known on this kernel.
5029 */
5030 if (reg_supported_dfs_region(dfs_region))
5031 rd->dfs_region = dfs_region;
5032
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005033 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005034 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005035 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01005036 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5037 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005038 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5039 if (r)
5040 goto bad_reg;
5041
5042 rule_idx++;
5043
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005044 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5045 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005046 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005047 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005048 }
5049
Johannes Berg6913b492012-12-04 00:48:59 +01005050 mutex_lock(&cfg80211_mutex);
5051
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005052 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005053 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005054 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01005055 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005056
Johannes Bergd2372b32008-10-24 20:32:20 +02005057 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005058 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005059 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005060}
5061
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005062static int validate_scan_freqs(struct nlattr *freqs)
5063{
5064 struct nlattr *attr1, *attr2;
5065 int n_channels = 0, tmp1, tmp2;
5066
5067 nla_for_each_nested(attr1, freqs, tmp1) {
5068 n_channels++;
5069 /*
5070 * Some hardware has a limited channel list for
5071 * scanning, and it is pretty much nonsensical
5072 * to scan for a channel twice, so disallow that
5073 * and don't require drivers to check that the
5074 * channel list they get isn't longer than what
5075 * they can scan, as long as they can scan all
5076 * the channels they registered at once.
5077 */
5078 nla_for_each_nested(attr2, freqs, tmp2)
5079 if (attr1 != attr2 &&
5080 nla_get_u32(attr1) == nla_get_u32(attr2))
5081 return 0;
5082 }
5083
5084 return n_channels;
5085}
5086
Johannes Berg2a519312009-02-10 21:25:55 +01005087static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5088{
Johannes Berg4c476992010-10-04 21:36:35 +02005089 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005090 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005091 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005092 struct nlattr *attr;
5093 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005094 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005095 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005096
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005097 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5098 return -EINVAL;
5099
Johannes Berg79c97e92009-07-07 03:56:12 +02005100 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005101
Johannes Berg4c476992010-10-04 21:36:35 +02005102 if (!rdev->ops->scan)
5103 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005104
Johannes Bergf9f47522013-03-19 15:04:07 +01005105 mutex_lock(&rdev->sched_scan_mtx);
5106 if (rdev->scan_req) {
5107 err = -EBUSY;
5108 goto unlock;
5109 }
Johannes Berg2a519312009-02-10 21:25:55 +01005110
5111 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005112 n_channels = validate_scan_freqs(
5113 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005114 if (!n_channels) {
5115 err = -EINVAL;
5116 goto unlock;
5117 }
Johannes Berg2a519312009-02-10 21:25:55 +01005118 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005119 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005120 n_channels = 0;
5121
Johannes Berg2a519312009-02-10 21:25:55 +01005122 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5123 if (wiphy->bands[band])
5124 n_channels += wiphy->bands[band]->n_channels;
5125 }
5126
5127 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5128 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5129 n_ssids++;
5130
Johannes Bergf9f47522013-03-19 15:04:07 +01005131 if (n_ssids > wiphy->max_scan_ssids) {
5132 err = -EINVAL;
5133 goto unlock;
5134 }
Johannes Berg2a519312009-02-10 21:25:55 +01005135
Jouni Malinen70692ad2009-02-16 19:39:13 +02005136 if (info->attrs[NL80211_ATTR_IE])
5137 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5138 else
5139 ie_len = 0;
5140
Johannes Bergf9f47522013-03-19 15:04:07 +01005141 if (ie_len > wiphy->max_scan_ie_len) {
5142 err = -EINVAL;
5143 goto unlock;
5144 }
Johannes Berg18a83652009-03-31 12:12:05 +02005145
Johannes Berg2a519312009-02-10 21:25:55 +01005146 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005147 + sizeof(*request->ssids) * n_ssids
5148 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005149 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005150 if (!request) {
5151 err = -ENOMEM;
5152 goto unlock;
5153 }
Johannes Berg2a519312009-02-10 21:25:55 +01005154
Johannes Berg2a519312009-02-10 21:25:55 +01005155 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005156 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005157 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005158 if (ie_len) {
5159 if (request->ssids)
5160 request->ie = (void *)(request->ssids + n_ssids);
5161 else
5162 request->ie = (void *)(request->channels + n_channels);
5163 }
Johannes Berg2a519312009-02-10 21:25:55 +01005164
Johannes Berg584991d2009-11-02 13:32:03 +01005165 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005166 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5167 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005168 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005169 struct ieee80211_channel *chan;
5170
5171 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5172
5173 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005174 err = -EINVAL;
5175 goto out_free;
5176 }
Johannes Berg584991d2009-11-02 13:32:03 +01005177
5178 /* ignore disabled channels */
5179 if (chan->flags & IEEE80211_CHAN_DISABLED)
5180 continue;
5181
5182 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005183 i++;
5184 }
5185 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005186 enum ieee80211_band band;
5187
Johannes Berg2a519312009-02-10 21:25:55 +01005188 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005189 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5190 int j;
5191 if (!wiphy->bands[band])
5192 continue;
5193 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005194 struct ieee80211_channel *chan;
5195
5196 chan = &wiphy->bands[band]->channels[j];
5197
5198 if (chan->flags & IEEE80211_CHAN_DISABLED)
5199 continue;
5200
5201 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005202 i++;
5203 }
5204 }
5205 }
5206
Johannes Berg584991d2009-11-02 13:32:03 +01005207 if (!i) {
5208 err = -EINVAL;
5209 goto out_free;
5210 }
5211
5212 request->n_channels = i;
5213
Johannes Berg2a519312009-02-10 21:25:55 +01005214 i = 0;
5215 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5216 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005217 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005218 err = -EINVAL;
5219 goto out_free;
5220 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005221 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005222 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005223 i++;
5224 }
5225 }
5226
Jouni Malinen70692ad2009-02-16 19:39:13 +02005227 if (info->attrs[NL80211_ATTR_IE]) {
5228 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005229 memcpy((void *)request->ie,
5230 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005231 request->ie_len);
5232 }
5233
Johannes Berg34850ab2011-07-18 18:08:35 +02005234 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005235 if (wiphy->bands[i])
5236 request->rates[i] =
5237 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005238
5239 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5240 nla_for_each_nested(attr,
5241 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5242 tmp) {
5243 enum ieee80211_band band = nla_type(attr);
5244
Dan Carpenter84404622011-07-29 11:52:18 +03005245 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005246 err = -EINVAL;
5247 goto out_free;
5248 }
5249 err = ieee80211_get_ratemask(wiphy->bands[band],
5250 nla_data(attr),
5251 nla_len(attr),
5252 &request->rates[band]);
5253 if (err)
5254 goto out_free;
5255 }
5256 }
5257
Sam Leffler46856bb2012-10-11 21:03:32 -07005258 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005259 request->flags = nla_get_u32(
5260 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005261 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5262 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5263 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5264 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005265 err = -EOPNOTSUPP;
5266 goto out_free;
5267 }
5268 }
Sam Lefflered4737712012-10-11 21:03:31 -07005269
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305270 request->no_cck =
5271 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5272
Johannes Bergfd014282012-06-18 19:17:03 +02005273 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005274 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005275 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005276
Johannes Berg79c97e92009-07-07 03:56:12 +02005277 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005278 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005279
Johannes Berg463d0182009-07-14 00:33:35 +02005280 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005281 nl80211_send_scan_start(rdev, wdev);
5282 if (wdev->netdev)
5283 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005284 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005285 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005286 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005287 kfree(request);
5288 }
Johannes Berg3b858752009-03-12 09:55:09 +01005289
Johannes Bergf9f47522013-03-19 15:04:07 +01005290 unlock:
5291 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01005292 return err;
5293}
5294
Luciano Coelho807f8a82011-05-11 17:09:35 +03005295static int nl80211_start_sched_scan(struct sk_buff *skb,
5296 struct genl_info *info)
5297{
5298 struct cfg80211_sched_scan_request *request;
5299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5300 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005301 struct nlattr *attr;
5302 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005303 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005304 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005305 enum ieee80211_band band;
5306 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005307 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005308
5309 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5310 !rdev->ops->sched_scan_start)
5311 return -EOPNOTSUPP;
5312
5313 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5314 return -EINVAL;
5315
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005316 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5317 return -EINVAL;
5318
5319 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5320 if (interval == 0)
5321 return -EINVAL;
5322
Luciano Coelho807f8a82011-05-11 17:09:35 +03005323 wiphy = &rdev->wiphy;
5324
5325 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5326 n_channels = validate_scan_freqs(
5327 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5328 if (!n_channels)
5329 return -EINVAL;
5330 } else {
5331 n_channels = 0;
5332
5333 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5334 if (wiphy->bands[band])
5335 n_channels += wiphy->bands[band]->n_channels;
5336 }
5337
5338 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5339 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5340 tmp)
5341 n_ssids++;
5342
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005343 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005344 return -EINVAL;
5345
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005346 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5347 nla_for_each_nested(attr,
5348 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5349 tmp)
5350 n_match_sets++;
5351
5352 if (n_match_sets > wiphy->max_match_sets)
5353 return -EINVAL;
5354
Luciano Coelho807f8a82011-05-11 17:09:35 +03005355 if (info->attrs[NL80211_ATTR_IE])
5356 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5357 else
5358 ie_len = 0;
5359
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005360 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005361 return -EINVAL;
5362
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005363 mutex_lock(&rdev->sched_scan_mtx);
5364
5365 if (rdev->sched_scan_req) {
5366 err = -EINPROGRESS;
5367 goto out;
5368 }
5369
Luciano Coelho807f8a82011-05-11 17:09:35 +03005370 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005371 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005372 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005373 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005374 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005375 if (!request) {
5376 err = -ENOMEM;
5377 goto out;
5378 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005379
5380 if (n_ssids)
5381 request->ssids = (void *)&request->channels[n_channels];
5382 request->n_ssids = n_ssids;
5383 if (ie_len) {
5384 if (request->ssids)
5385 request->ie = (void *)(request->ssids + n_ssids);
5386 else
5387 request->ie = (void *)(request->channels + n_channels);
5388 }
5389
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005390 if (n_match_sets) {
5391 if (request->ie)
5392 request->match_sets = (void *)(request->ie + ie_len);
5393 else if (request->ssids)
5394 request->match_sets =
5395 (void *)(request->ssids + n_ssids);
5396 else
5397 request->match_sets =
5398 (void *)(request->channels + n_channels);
5399 }
5400 request->n_match_sets = n_match_sets;
5401
Luciano Coelho807f8a82011-05-11 17:09:35 +03005402 i = 0;
5403 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5404 /* user specified, bail out if channel not found */
5405 nla_for_each_nested(attr,
5406 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5407 tmp) {
5408 struct ieee80211_channel *chan;
5409
5410 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5411
5412 if (!chan) {
5413 err = -EINVAL;
5414 goto out_free;
5415 }
5416
5417 /* ignore disabled channels */
5418 if (chan->flags & IEEE80211_CHAN_DISABLED)
5419 continue;
5420
5421 request->channels[i] = chan;
5422 i++;
5423 }
5424 } else {
5425 /* all channels */
5426 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5427 int j;
5428 if (!wiphy->bands[band])
5429 continue;
5430 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5431 struct ieee80211_channel *chan;
5432
5433 chan = &wiphy->bands[band]->channels[j];
5434
5435 if (chan->flags & IEEE80211_CHAN_DISABLED)
5436 continue;
5437
5438 request->channels[i] = chan;
5439 i++;
5440 }
5441 }
5442 }
5443
5444 if (!i) {
5445 err = -EINVAL;
5446 goto out_free;
5447 }
5448
5449 request->n_channels = i;
5450
5451 i = 0;
5452 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5453 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5454 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005455 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005456 err = -EINVAL;
5457 goto out_free;
5458 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005459 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005460 memcpy(request->ssids[i].ssid, nla_data(attr),
5461 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005462 i++;
5463 }
5464 }
5465
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005466 i = 0;
5467 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5468 nla_for_each_nested(attr,
5469 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5470 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005471 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005472
5473 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5474 nla_data(attr), nla_len(attr),
5475 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005476 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005477 if (ssid) {
5478 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5479 err = -EINVAL;
5480 goto out_free;
5481 }
5482 memcpy(request->match_sets[i].ssid.ssid,
5483 nla_data(ssid), nla_len(ssid));
5484 request->match_sets[i].ssid.ssid_len =
5485 nla_len(ssid);
5486 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005487 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5488 if (rssi)
5489 request->rssi_thold = nla_get_u32(rssi);
5490 else
5491 request->rssi_thold =
5492 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005493 i++;
5494 }
5495 }
5496
Luciano Coelho807f8a82011-05-11 17:09:35 +03005497 if (info->attrs[NL80211_ATTR_IE]) {
5498 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5499 memcpy((void *)request->ie,
5500 nla_data(info->attrs[NL80211_ATTR_IE]),
5501 request->ie_len);
5502 }
5503
Sam Leffler46856bb2012-10-11 21:03:32 -07005504 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005505 request->flags = nla_get_u32(
5506 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005507 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5508 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5509 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5510 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005511 err = -EOPNOTSUPP;
5512 goto out_free;
5513 }
5514 }
Sam Lefflered4737712012-10-11 21:03:31 -07005515
Luciano Coelho807f8a82011-05-11 17:09:35 +03005516 request->dev = dev;
5517 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005518 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005519 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005520
Hila Gonene35e4d22012-06-27 17:19:42 +03005521 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005522 if (!err) {
5523 rdev->sched_scan_req = request;
5524 nl80211_send_sched_scan(rdev, dev,
5525 NL80211_CMD_START_SCHED_SCAN);
5526 goto out;
5527 }
5528
5529out_free:
5530 kfree(request);
5531out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005532 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005533 return err;
5534}
5535
5536static int nl80211_stop_sched_scan(struct sk_buff *skb,
5537 struct genl_info *info)
5538{
5539 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005540 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005541
5542 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5543 !rdev->ops->sched_scan_stop)
5544 return -EOPNOTSUPP;
5545
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005546 mutex_lock(&rdev->sched_scan_mtx);
5547 err = __cfg80211_stop_sched_scan(rdev, false);
5548 mutex_unlock(&rdev->sched_scan_mtx);
5549
5550 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005551}
5552
Simon Wunderlich04f39042013-02-08 18:16:19 +01005553static int nl80211_start_radar_detection(struct sk_buff *skb,
5554 struct genl_info *info)
5555{
5556 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5557 struct net_device *dev = info->user_ptr[1];
5558 struct wireless_dev *wdev = dev->ieee80211_ptr;
5559 struct cfg80211_chan_def chandef;
5560 int err;
5561
5562 err = nl80211_parse_chandef(rdev, info, &chandef);
5563 if (err)
5564 return err;
5565
5566 if (wdev->cac_started)
5567 return -EBUSY;
5568
5569 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5570 if (err < 0)
5571 return err;
5572
5573 if (err == 0)
5574 return -EINVAL;
5575
5576 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5577 return -EINVAL;
5578
5579 if (!rdev->ops->start_radar_detection)
5580 return -EOPNOTSUPP;
5581
5582 mutex_lock(&rdev->devlist_mtx);
5583 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5584 chandef.chan, CHAN_MODE_SHARED,
5585 BIT(chandef.width));
5586 if (err)
5587 goto err_locked;
5588
5589 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5590 if (!err) {
5591 wdev->channel = chandef.chan;
5592 wdev->cac_started = true;
5593 wdev->cac_start_time = jiffies;
5594 }
5595err_locked:
5596 mutex_unlock(&rdev->devlist_mtx);
5597
5598 return err;
5599}
5600
Johannes Berg9720bb32011-06-21 09:45:33 +02005601static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5602 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005603 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005604 struct wireless_dev *wdev,
5605 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005606{
Johannes Berg48ab9052009-07-10 18:42:31 +02005607 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005608 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005609 void *hdr;
5610 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005611 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005612
5613 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005614
Eric W. Biederman15e47302012-09-07 20:12:54 +00005615 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005616 NL80211_CMD_NEW_SCAN_RESULTS);
5617 if (!hdr)
5618 return -1;
5619
Johannes Berg9720bb32011-06-21 09:45:33 +02005620 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5621
Johannes Berg97990a02013-04-19 01:02:55 +02005622 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5623 goto nla_put_failure;
5624 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005625 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5626 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005627 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5628 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005629
5630 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5631 if (!bss)
5632 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005633 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005634 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005635 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005636
5637 rcu_read_lock();
5638 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005639 if (ies) {
5640 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5641 goto fail_unlock_rcu;
5642 tsf = true;
5643 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5644 ies->len, ies->data))
5645 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005646 }
5647 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005648 if (ies) {
5649 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5650 goto fail_unlock_rcu;
5651 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5652 ies->len, ies->data))
5653 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005654 }
5655 rcu_read_unlock();
5656
David S. Miller9360ffd2012-03-29 04:41:26 -04005657 if (res->beacon_interval &&
5658 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5659 goto nla_put_failure;
5660 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5661 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5662 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5663 jiffies_to_msecs(jiffies - intbss->ts)))
5664 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005665
Johannes Berg77965c92009-02-18 18:45:06 +01005666 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005667 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005668 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5669 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005670 break;
5671 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005672 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5673 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005674 break;
5675 default:
5676 break;
5677 }
5678
Johannes Berg48ab9052009-07-10 18:42:31 +02005679 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005680 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005681 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005682 if (intbss == wdev->current_bss &&
5683 nla_put_u32(msg, NL80211_BSS_STATUS,
5684 NL80211_BSS_STATUS_ASSOCIATED))
5685 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005686 break;
5687 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005688 if (intbss == wdev->current_bss &&
5689 nla_put_u32(msg, NL80211_BSS_STATUS,
5690 NL80211_BSS_STATUS_IBSS_JOINED))
5691 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005692 break;
5693 default:
5694 break;
5695 }
5696
Johannes Berg2a519312009-02-10 21:25:55 +01005697 nla_nest_end(msg, bss);
5698
5699 return genlmsg_end(msg, hdr);
5700
Johannes Berg8cef2c92013-02-05 16:54:31 +01005701 fail_unlock_rcu:
5702 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005703 nla_put_failure:
5704 genlmsg_cancel(msg, hdr);
5705 return -EMSGSIZE;
5706}
5707
Johannes Berg97990a02013-04-19 01:02:55 +02005708static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005709{
Johannes Berg48ab9052009-07-10 18:42:31 +02005710 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005711 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005712 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005713 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005714 int err;
5715
Johannes Berg97990a02013-04-19 01:02:55 +02005716 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005717 if (err)
5718 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005719
Johannes Berg48ab9052009-07-10 18:42:31 +02005720 wdev_lock(wdev);
5721 spin_lock_bh(&rdev->bss_lock);
5722 cfg80211_bss_expire(rdev);
5723
Johannes Berg9720bb32011-06-21 09:45:33 +02005724 cb->seq = rdev->bss_generation;
5725
Johannes Berg48ab9052009-07-10 18:42:31 +02005726 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005727 if (++idx <= start)
5728 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005729 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005730 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005731 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005732 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005733 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005734 }
5735 }
5736
Johannes Berg48ab9052009-07-10 18:42:31 +02005737 spin_unlock_bh(&rdev->bss_lock);
5738 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005739
Johannes Berg97990a02013-04-19 01:02:55 +02005740 cb->args[2] = idx;
5741 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005742
Johannes Berg67748892010-10-04 21:14:06 +02005743 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005744}
5745
Eric W. Biederman15e47302012-09-07 20:12:54 +00005746static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005747 int flags, struct net_device *dev,
5748 struct survey_info *survey)
5749{
5750 void *hdr;
5751 struct nlattr *infoattr;
5752
Eric W. Biederman15e47302012-09-07 20:12:54 +00005753 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005754 NL80211_CMD_NEW_SURVEY_RESULTS);
5755 if (!hdr)
5756 return -ENOMEM;
5757
David S. Miller9360ffd2012-03-29 04:41:26 -04005758 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5759 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005760
5761 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5762 if (!infoattr)
5763 goto nla_put_failure;
5764
David S. Miller9360ffd2012-03-29 04:41:26 -04005765 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5766 survey->channel->center_freq))
5767 goto nla_put_failure;
5768
5769 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5770 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5771 goto nla_put_failure;
5772 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5773 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5774 goto nla_put_failure;
5775 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5776 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5777 survey->channel_time))
5778 goto nla_put_failure;
5779 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5780 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5781 survey->channel_time_busy))
5782 goto nla_put_failure;
5783 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5784 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5785 survey->channel_time_ext_busy))
5786 goto nla_put_failure;
5787 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5788 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5789 survey->channel_time_rx))
5790 goto nla_put_failure;
5791 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5792 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5793 survey->channel_time_tx))
5794 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005795
5796 nla_nest_end(msg, infoattr);
5797
5798 return genlmsg_end(msg, hdr);
5799
5800 nla_put_failure:
5801 genlmsg_cancel(msg, hdr);
5802 return -EMSGSIZE;
5803}
5804
5805static int nl80211_dump_survey(struct sk_buff *skb,
5806 struct netlink_callback *cb)
5807{
5808 struct survey_info survey;
5809 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02005810 struct wireless_dev *wdev;
5811 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01005812 int res;
5813
Johannes Berg97990a02013-04-19 01:02:55 +02005814 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005815 if (res)
5816 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005817
Johannes Berg97990a02013-04-19 01:02:55 +02005818 if (!wdev->netdev) {
5819 res = -EINVAL;
5820 goto out_err;
5821 }
5822
Holger Schurig61fa7132009-11-11 12:25:40 +01005823 if (!dev->ops->dump_survey) {
5824 res = -EOPNOTSUPP;
5825 goto out_err;
5826 }
5827
5828 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005829 struct ieee80211_channel *chan;
5830
Johannes Berg97990a02013-04-19 01:02:55 +02005831 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005832 if (res == -ENOENT)
5833 break;
5834 if (res)
5835 goto out_err;
5836
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005837 /* Survey without a channel doesn't make sense */
5838 if (!survey.channel) {
5839 res = -EINVAL;
5840 goto out;
5841 }
5842
5843 chan = ieee80211_get_channel(&dev->wiphy,
5844 survey.channel->center_freq);
5845 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5846 survey_idx++;
5847 continue;
5848 }
5849
Holger Schurig61fa7132009-11-11 12:25:40 +01005850 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005851 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005852 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02005853 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01005854 goto out;
5855 survey_idx++;
5856 }
5857
5858 out:
Johannes Berg97990a02013-04-19 01:02:55 +02005859 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01005860 res = skb->len;
5861 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02005862 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005863 return res;
5864}
5865
Samuel Ortizb23aa672009-07-01 21:26:54 +02005866static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5867{
5868 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5869 NL80211_WPA_VERSION_2));
5870}
5871
Jouni Malinen636a5d32009-03-19 13:39:22 +02005872static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5873{
Johannes Berg4c476992010-10-04 21:36:35 +02005874 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5875 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005876 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005877 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5878 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005879 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005880 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005881 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005882
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005883 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5884 return -EINVAL;
5885
5886 if (!info->attrs[NL80211_ATTR_MAC])
5887 return -EINVAL;
5888
Jouni Malinen17780922009-03-27 20:52:47 +02005889 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5890 return -EINVAL;
5891
Johannes Berg19957bb2009-07-02 17:20:43 +02005892 if (!info->attrs[NL80211_ATTR_SSID])
5893 return -EINVAL;
5894
5895 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5896 return -EINVAL;
5897
Johannes Bergfffd0932009-07-08 14:22:54 +02005898 err = nl80211_parse_key(info, &key);
5899 if (err)
5900 return err;
5901
5902 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005903 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5904 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005905 if (!key.p.key || !key.p.key_len)
5906 return -EINVAL;
5907 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5908 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5909 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5910 key.p.key_len != WLAN_KEY_LEN_WEP104))
5911 return -EINVAL;
5912 if (key.idx > 4)
5913 return -EINVAL;
5914 } else {
5915 key.p.key_len = 0;
5916 key.p.key = NULL;
5917 }
5918
Johannes Bergafea0b72010-08-10 09:46:42 +02005919 if (key.idx >= 0) {
5920 int i;
5921 bool ok = false;
5922 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5923 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5924 ok = true;
5925 break;
5926 }
5927 }
Johannes Berg4c476992010-10-04 21:36:35 +02005928 if (!ok)
5929 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005930 }
5931
Johannes Berg4c476992010-10-04 21:36:35 +02005932 if (!rdev->ops->auth)
5933 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005934
Johannes Berg074ac8d2010-09-16 14:58:22 +02005935 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005936 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5937 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005938
Johannes Berg19957bb2009-07-02 17:20:43 +02005939 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005940 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005941 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005942 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5943 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005944
Johannes Berg19957bb2009-07-02 17:20:43 +02005945 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5946 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5947
5948 if (info->attrs[NL80211_ATTR_IE]) {
5949 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5950 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5951 }
5952
5953 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005954 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005955 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005956
Jouni Malinene39e5b52012-09-30 19:29:39 +03005957 if (auth_type == NL80211_AUTHTYPE_SAE &&
5958 !info->attrs[NL80211_ATTR_SAE_DATA])
5959 return -EINVAL;
5960
5961 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5962 if (auth_type != NL80211_AUTHTYPE_SAE)
5963 return -EINVAL;
5964 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5965 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5966 /* need to include at least Auth Transaction and Status Code */
5967 if (sae_data_len < 4)
5968 return -EINVAL;
5969 }
5970
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005971 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5972
Johannes Berg95de8172012-01-20 13:55:25 +01005973 /*
5974 * Since we no longer track auth state, ignore
5975 * requests to only change local state.
5976 */
5977 if (local_state_change)
5978 return 0;
5979
Johannes Berg4c476992010-10-04 21:36:35 +02005980 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5981 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005982 key.p.key, key.p.key_len, key.idx,
5983 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005984}
5985
Johannes Bergc0692b82010-08-27 14:26:53 +03005986static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5987 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005988 struct cfg80211_crypto_settings *settings,
5989 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005990{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005991 memset(settings, 0, sizeof(*settings));
5992
Samuel Ortizb23aa672009-07-01 21:26:54 +02005993 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5994
Johannes Bergc0692b82010-08-27 14:26:53 +03005995 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5996 u16 proto;
5997 proto = nla_get_u16(
5998 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5999 settings->control_port_ethertype = cpu_to_be16(proto);
6000 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6001 proto != ETH_P_PAE)
6002 return -EINVAL;
6003 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6004 settings->control_port_no_encrypt = true;
6005 } else
6006 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6007
Samuel Ortizb23aa672009-07-01 21:26:54 +02006008 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6009 void *data;
6010 int len, i;
6011
6012 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6013 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6014 settings->n_ciphers_pairwise = len / sizeof(u32);
6015
6016 if (len % sizeof(u32))
6017 return -EINVAL;
6018
Johannes Berg3dc27d22009-07-02 21:36:37 +02006019 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006020 return -EINVAL;
6021
6022 memcpy(settings->ciphers_pairwise, data, len);
6023
6024 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006025 if (!cfg80211_supported_cipher_suite(
6026 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006027 settings->ciphers_pairwise[i]))
6028 return -EINVAL;
6029 }
6030
6031 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6032 settings->cipher_group =
6033 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006034 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6035 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006036 return -EINVAL;
6037 }
6038
6039 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6040 settings->wpa_versions =
6041 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6042 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6043 return -EINVAL;
6044 }
6045
6046 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6047 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006048 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006049
6050 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6051 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6052 settings->n_akm_suites = len / sizeof(u32);
6053
6054 if (len % sizeof(u32))
6055 return -EINVAL;
6056
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006057 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6058 return -EINVAL;
6059
Samuel Ortizb23aa672009-07-01 21:26:54 +02006060 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006061 }
6062
6063 return 0;
6064}
6065
Jouni Malinen636a5d32009-03-19 13:39:22 +02006066static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6067{
Johannes Berg4c476992010-10-04 21:36:35 +02006068 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6069 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006070 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006071 struct cfg80211_assoc_request req = {};
6072 const u8 *bssid, *ssid;
6073 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006074
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006075 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6076 return -EINVAL;
6077
6078 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006079 !info->attrs[NL80211_ATTR_SSID] ||
6080 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006081 return -EINVAL;
6082
Johannes Berg4c476992010-10-04 21:36:35 +02006083 if (!rdev->ops->assoc)
6084 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006085
Johannes Berg074ac8d2010-09-16 14:58:22 +02006086 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006087 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6088 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006089
Johannes Berg19957bb2009-07-02 17:20:43 +02006090 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006091
Johannes Berg19957bb2009-07-02 17:20:43 +02006092 chan = ieee80211_get_channel(&rdev->wiphy,
6093 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006094 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6095 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006096
Johannes Berg19957bb2009-07-02 17:20:43 +02006097 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6098 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006099
6100 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006101 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6102 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006103 }
6104
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006105 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006106 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006107 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006108 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006109 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006110 else if (mfp != NL80211_MFP_NO)
6111 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006112 }
6113
Johannes Berg3e5d7642009-07-07 14:37:26 +02006114 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006115 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006116
Ben Greear7e7c8922011-11-18 11:31:59 -08006117 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006118 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006119
6120 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006121 memcpy(&req.ht_capa_mask,
6122 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6123 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006124
6125 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006126 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006127 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006128 memcpy(&req.ht_capa,
6129 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6130 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006131 }
6132
Johannes Bergee2aca32013-02-21 17:36:01 +01006133 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006134 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006135
6136 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006137 memcpy(&req.vht_capa_mask,
6138 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6139 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006140
6141 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006142 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006143 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006144 memcpy(&req.vht_capa,
6145 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6146 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006147 }
6148
Johannes Bergf62fab72013-02-21 20:09:09 +01006149 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006150 if (!err)
Johannes Bergf62fab72013-02-21 20:09:09 +01006151 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6152 ssid, ssid_len, &req);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006153
Jouni Malinen636a5d32009-03-19 13:39:22 +02006154 return err;
6155}
6156
6157static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6158{
Johannes Berg4c476992010-10-04 21:36:35 +02006159 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6160 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006161 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006162 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006163 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006164 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006165
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006166 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6167 return -EINVAL;
6168
6169 if (!info->attrs[NL80211_ATTR_MAC])
6170 return -EINVAL;
6171
6172 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6173 return -EINVAL;
6174
Johannes Berg4c476992010-10-04 21:36:35 +02006175 if (!rdev->ops->deauth)
6176 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006177
Johannes Berg074ac8d2010-09-16 14:58:22 +02006178 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006179 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6180 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006181
Johannes Berg19957bb2009-07-02 17:20:43 +02006182 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006183
Johannes Berg19957bb2009-07-02 17:20:43 +02006184 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6185 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006186 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006187 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006188 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006189
6190 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006191 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6192 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006193 }
6194
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006195 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6196
Johannes Berg4c476992010-10-04 21:36:35 +02006197 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6198 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006199}
6200
6201static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6202{
Johannes Berg4c476992010-10-04 21:36:35 +02006203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6204 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006205 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006206 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006207 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006208 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006209
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006210 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6211 return -EINVAL;
6212
6213 if (!info->attrs[NL80211_ATTR_MAC])
6214 return -EINVAL;
6215
6216 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6217 return -EINVAL;
6218
Johannes Berg4c476992010-10-04 21:36:35 +02006219 if (!rdev->ops->disassoc)
6220 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006221
Johannes Berg074ac8d2010-09-16 14:58:22 +02006222 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006223 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6224 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006225
Johannes Berg19957bb2009-07-02 17:20:43 +02006226 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006227
Johannes Berg19957bb2009-07-02 17:20:43 +02006228 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6229 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006230 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006231 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006232 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006233
6234 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006235 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6236 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006237 }
6238
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006239 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6240
Johannes Berg4c476992010-10-04 21:36:35 +02006241 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6242 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006243}
6244
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006245static bool
6246nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6247 int mcast_rate[IEEE80211_NUM_BANDS],
6248 int rateval)
6249{
6250 struct wiphy *wiphy = &rdev->wiphy;
6251 bool found = false;
6252 int band, i;
6253
6254 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6255 struct ieee80211_supported_band *sband;
6256
6257 sband = wiphy->bands[band];
6258 if (!sband)
6259 continue;
6260
6261 for (i = 0; i < sband->n_bitrates; i++) {
6262 if (sband->bitrates[i].bitrate == rateval) {
6263 mcast_rate[band] = i + 1;
6264 found = true;
6265 break;
6266 }
6267 }
6268 }
6269
6270 return found;
6271}
6272
Johannes Berg04a773a2009-04-19 21:24:32 +02006273static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6274{
Johannes Berg4c476992010-10-04 21:36:35 +02006275 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6276 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006277 struct cfg80211_ibss_params ibss;
6278 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006279 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006280 int err;
6281
Johannes Berg8e30bc52009-04-22 17:45:38 +02006282 memset(&ibss, 0, sizeof(ibss));
6283
Johannes Berg04a773a2009-04-19 21:24:32 +02006284 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6285 return -EINVAL;
6286
Johannes Berg683b6d32012-11-08 21:25:48 +01006287 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006288 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6289 return -EINVAL;
6290
Johannes Berg8e30bc52009-04-22 17:45:38 +02006291 ibss.beacon_interval = 100;
6292
6293 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6294 ibss.beacon_interval =
6295 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6296 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6297 return -EINVAL;
6298 }
6299
Johannes Berg4c476992010-10-04 21:36:35 +02006300 if (!rdev->ops->join_ibss)
6301 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006302
Johannes Berg4c476992010-10-04 21:36:35 +02006303 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6304 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006305
Johannes Berg79c97e92009-07-07 03:56:12 +02006306 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006307
Johannes Berg39193492011-09-16 13:45:25 +02006308 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006309 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006310
6311 if (!is_valid_ether_addr(ibss.bssid))
6312 return -EINVAL;
6313 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006314 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6315 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6316
6317 if (info->attrs[NL80211_ATTR_IE]) {
6318 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6319 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6320 }
6321
Johannes Berg683b6d32012-11-08 21:25:48 +01006322 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6323 if (err)
6324 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006325
Johannes Berg683b6d32012-11-08 21:25:48 +01006326 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006327 return -EINVAL;
6328
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006329 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6330 return -EINVAL;
6331 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6332 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006333 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006334
Johannes Berg04a773a2009-04-19 21:24:32 +02006335 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006336 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006337
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006338 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6339 u8 *rates =
6340 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6341 int n_rates =
6342 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6343 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006344 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006345
Johannes Berg34850ab2011-07-18 18:08:35 +02006346 err = ieee80211_get_ratemask(sband, rates, n_rates,
6347 &ibss.basic_rates);
6348 if (err)
6349 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006350 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006351
6352 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6353 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6354 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6355 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006356
Johannes Berg4c476992010-10-04 21:36:35 +02006357 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306358 bool no_ht = false;
6359
Johannes Berg4c476992010-10-04 21:36:35 +02006360 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306361 info->attrs[NL80211_ATTR_KEYS],
6362 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006363 if (IS_ERR(connkeys))
6364 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306365
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006366 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6367 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306368 kfree(connkeys);
6369 return -EINVAL;
6370 }
Johannes Berg4c476992010-10-04 21:36:35 +02006371 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006372
Antonio Quartulli267335d2012-01-31 20:25:47 +01006373 ibss.control_port =
6374 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6375
Johannes Berg4c476992010-10-04 21:36:35 +02006376 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006377 if (err)
6378 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006379 return err;
6380}
6381
6382static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6383{
Johannes Berg4c476992010-10-04 21:36:35 +02006384 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6385 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006386
Johannes Berg4c476992010-10-04 21:36:35 +02006387 if (!rdev->ops->leave_ibss)
6388 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006389
Johannes Berg4c476992010-10-04 21:36:35 +02006390 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6391 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006392
Johannes Berg4c476992010-10-04 21:36:35 +02006393 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006394}
6395
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006396static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6397{
6398 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6399 struct net_device *dev = info->user_ptr[1];
6400 int mcast_rate[IEEE80211_NUM_BANDS];
6401 u32 nla_rate;
6402 int err;
6403
6404 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6405 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6406 return -EOPNOTSUPP;
6407
6408 if (!rdev->ops->set_mcast_rate)
6409 return -EOPNOTSUPP;
6410
6411 memset(mcast_rate, 0, sizeof(mcast_rate));
6412
6413 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6414 return -EINVAL;
6415
6416 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6417 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6418 return -EINVAL;
6419
6420 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6421
6422 return err;
6423}
6424
6425
Johannes Bergaff89a92009-07-01 21:26:51 +02006426#ifdef CONFIG_NL80211_TESTMODE
6427static struct genl_multicast_group nl80211_testmode_mcgrp = {
6428 .name = "testmode",
6429};
6430
6431static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6432{
Johannes Berg4c476992010-10-04 21:36:35 +02006433 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006434 int err;
6435
6436 if (!info->attrs[NL80211_ATTR_TESTDATA])
6437 return -EINVAL;
6438
Johannes Bergaff89a92009-07-01 21:26:51 +02006439 err = -EOPNOTSUPP;
6440 if (rdev->ops->testmode_cmd) {
6441 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006442 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006443 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6444 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6445 rdev->testmode_info = NULL;
6446 }
6447
Johannes Bergaff89a92009-07-01 21:26:51 +02006448 return err;
6449}
6450
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006451static int nl80211_testmode_dump(struct sk_buff *skb,
6452 struct netlink_callback *cb)
6453{
Johannes Berg00918d32011-12-13 17:22:05 +01006454 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006455 int err;
6456 long phy_idx;
6457 void *data = NULL;
6458 int data_len = 0;
6459
6460 if (cb->args[0]) {
6461 /*
6462 * 0 is a valid index, but not valid for args[0],
6463 * so we need to offset by 1.
6464 */
6465 phy_idx = cb->args[0] - 1;
6466 } else {
6467 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6468 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6469 nl80211_policy);
6470 if (err)
6471 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006472
Johannes Berg2bd7e352012-06-15 14:23:16 +02006473 mutex_lock(&cfg80211_mutex);
6474 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6475 nl80211_fam.attrbuf);
6476 if (IS_ERR(rdev)) {
6477 mutex_unlock(&cfg80211_mutex);
6478 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006479 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006480 phy_idx = rdev->wiphy_idx;
6481 rdev = NULL;
6482 mutex_unlock(&cfg80211_mutex);
6483
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006484 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6485 cb->args[1] =
6486 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6487 }
6488
6489 if (cb->args[1]) {
6490 data = nla_data((void *)cb->args[1]);
6491 data_len = nla_len((void *)cb->args[1]);
6492 }
6493
6494 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006495 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6496 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006497 mutex_unlock(&cfg80211_mutex);
6498 return -ENOENT;
6499 }
Johannes Berg00918d32011-12-13 17:22:05 +01006500 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006501 mutex_unlock(&cfg80211_mutex);
6502
Johannes Berg00918d32011-12-13 17:22:05 +01006503 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006504 err = -EOPNOTSUPP;
6505 goto out_err;
6506 }
6507
6508 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006509 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006510 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6511 NL80211_CMD_TESTMODE);
6512 struct nlattr *tmdata;
6513
David S. Miller9360ffd2012-03-29 04:41:26 -04006514 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006515 genlmsg_cancel(skb, hdr);
6516 break;
6517 }
6518
6519 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6520 if (!tmdata) {
6521 genlmsg_cancel(skb, hdr);
6522 break;
6523 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006524 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006525 nla_nest_end(skb, tmdata);
6526
6527 if (err == -ENOBUFS || err == -ENOENT) {
6528 genlmsg_cancel(skb, hdr);
6529 break;
6530 } else if (err) {
6531 genlmsg_cancel(skb, hdr);
6532 goto out_err;
6533 }
6534
6535 genlmsg_end(skb, hdr);
6536 }
6537
6538 err = skb->len;
6539 /* see above */
6540 cb->args[0] = phy_idx + 1;
6541 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006542 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006543 return err;
6544}
6545
Johannes Bergaff89a92009-07-01 21:26:51 +02006546static struct sk_buff *
6547__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006548 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006549{
6550 struct sk_buff *skb;
6551 void *hdr;
6552 struct nlattr *data;
6553
6554 skb = nlmsg_new(approxlen + 100, gfp);
6555 if (!skb)
6556 return NULL;
6557
Eric W. Biederman15e47302012-09-07 20:12:54 +00006558 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006559 if (!hdr) {
6560 kfree_skb(skb);
6561 return NULL;
6562 }
6563
David S. Miller9360ffd2012-03-29 04:41:26 -04006564 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6565 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006566 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6567
6568 ((void **)skb->cb)[0] = rdev;
6569 ((void **)skb->cb)[1] = hdr;
6570 ((void **)skb->cb)[2] = data;
6571
6572 return skb;
6573
6574 nla_put_failure:
6575 kfree_skb(skb);
6576 return NULL;
6577}
6578
6579struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6580 int approxlen)
6581{
6582 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6583
6584 if (WARN_ON(!rdev->testmode_info))
6585 return NULL;
6586
6587 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006588 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006589 rdev->testmode_info->snd_seq,
6590 GFP_KERNEL);
6591}
6592EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6593
6594int cfg80211_testmode_reply(struct sk_buff *skb)
6595{
6596 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6597 void *hdr = ((void **)skb->cb)[1];
6598 struct nlattr *data = ((void **)skb->cb)[2];
6599
6600 if (WARN_ON(!rdev->testmode_info)) {
6601 kfree_skb(skb);
6602 return -EINVAL;
6603 }
6604
6605 nla_nest_end(skb, data);
6606 genlmsg_end(skb, hdr);
6607 return genlmsg_reply(skb, rdev->testmode_info);
6608}
6609EXPORT_SYMBOL(cfg80211_testmode_reply);
6610
6611struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6612 int approxlen, gfp_t gfp)
6613{
6614 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6615
6616 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6617}
6618EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6619
6620void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6621{
6622 void *hdr = ((void **)skb->cb)[1];
6623 struct nlattr *data = ((void **)skb->cb)[2];
6624
6625 nla_nest_end(skb, data);
6626 genlmsg_end(skb, hdr);
6627 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6628}
6629EXPORT_SYMBOL(cfg80211_testmode_event);
6630#endif
6631
Samuel Ortizb23aa672009-07-01 21:26:54 +02006632static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6633{
Johannes Berg4c476992010-10-04 21:36:35 +02006634 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6635 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006636 struct cfg80211_connect_params connect;
6637 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006638 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006639 int err;
6640
6641 memset(&connect, 0, sizeof(connect));
6642
6643 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6644 return -EINVAL;
6645
6646 if (!info->attrs[NL80211_ATTR_SSID] ||
6647 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6648 return -EINVAL;
6649
6650 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6651 connect.auth_type =
6652 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006653 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6654 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006655 return -EINVAL;
6656 } else
6657 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6658
6659 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6660
Johannes Bergc0692b82010-08-27 14:26:53 +03006661 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006662 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006663 if (err)
6664 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006665
Johannes Berg074ac8d2010-09-16 14:58:22 +02006666 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006667 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6668 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006669
Johannes Berg79c97e92009-07-07 03:56:12 +02006670 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006671
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306672 connect.bg_scan_period = -1;
6673 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6674 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6675 connect.bg_scan_period =
6676 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6677 }
6678
Samuel Ortizb23aa672009-07-01 21:26:54 +02006679 if (info->attrs[NL80211_ATTR_MAC])
6680 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6681 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6682 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6683
6684 if (info->attrs[NL80211_ATTR_IE]) {
6685 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6686 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6687 }
6688
Jouni Malinencee00a92013-01-15 17:15:57 +02006689 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6690 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6691 if (connect.mfp != NL80211_MFP_REQUIRED &&
6692 connect.mfp != NL80211_MFP_NO)
6693 return -EINVAL;
6694 } else {
6695 connect.mfp = NL80211_MFP_NO;
6696 }
6697
Samuel Ortizb23aa672009-07-01 21:26:54 +02006698 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6699 connect.channel =
6700 ieee80211_get_channel(wiphy,
6701 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6702 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006703 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6704 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006705 }
6706
Johannes Bergfffd0932009-07-08 14:22:54 +02006707 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6708 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306709 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006710 if (IS_ERR(connkeys))
6711 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006712 }
6713
Ben Greear7e7c8922011-11-18 11:31:59 -08006714 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6715 connect.flags |= ASSOC_REQ_DISABLE_HT;
6716
6717 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6718 memcpy(&connect.ht_capa_mask,
6719 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6720 sizeof(connect.ht_capa_mask));
6721
6722 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006723 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6724 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006725 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006726 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006727 memcpy(&connect.ht_capa,
6728 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6729 sizeof(connect.ht_capa));
6730 }
6731
Johannes Bergee2aca32013-02-21 17:36:01 +01006732 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6733 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6734
6735 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6736 memcpy(&connect.vht_capa_mask,
6737 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6738 sizeof(connect.vht_capa_mask));
6739
6740 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6741 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6742 kfree(connkeys);
6743 return -EINVAL;
6744 }
6745 memcpy(&connect.vht_capa,
6746 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6747 sizeof(connect.vht_capa));
6748 }
6749
Johannes Bergfffd0932009-07-08 14:22:54 +02006750 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006751 if (err)
6752 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006753 return err;
6754}
6755
6756static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6757{
Johannes Berg4c476992010-10-04 21:36:35 +02006758 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6759 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006760 u16 reason;
6761
6762 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6763 reason = WLAN_REASON_DEAUTH_LEAVING;
6764 else
6765 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6766
6767 if (reason == 0)
6768 return -EINVAL;
6769
Johannes Berg074ac8d2010-09-16 14:58:22 +02006770 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006771 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6772 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006773
Johannes Berg4c476992010-10-04 21:36:35 +02006774 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006775}
6776
Johannes Berg463d0182009-07-14 00:33:35 +02006777static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6778{
Johannes Berg4c476992010-10-04 21:36:35 +02006779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006780 struct net *net;
6781 int err;
6782 u32 pid;
6783
6784 if (!info->attrs[NL80211_ATTR_PID])
6785 return -EINVAL;
6786
6787 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6788
Johannes Berg463d0182009-07-14 00:33:35 +02006789 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006790 if (IS_ERR(net))
6791 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006792
6793 err = 0;
6794
6795 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006796 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6797 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006798
Johannes Berg463d0182009-07-14 00:33:35 +02006799 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006800 return err;
6801}
6802
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006803static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6804{
Johannes Berg4c476992010-10-04 21:36:35 +02006805 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006806 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6807 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006808 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006809 struct cfg80211_pmksa pmksa;
6810
6811 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6812
6813 if (!info->attrs[NL80211_ATTR_MAC])
6814 return -EINVAL;
6815
6816 if (!info->attrs[NL80211_ATTR_PMKID])
6817 return -EINVAL;
6818
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006819 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6820 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6821
Johannes Berg074ac8d2010-09-16 14:58:22 +02006822 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006823 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6824 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006825
6826 switch (info->genlhdr->cmd) {
6827 case NL80211_CMD_SET_PMKSA:
6828 rdev_ops = rdev->ops->set_pmksa;
6829 break;
6830 case NL80211_CMD_DEL_PMKSA:
6831 rdev_ops = rdev->ops->del_pmksa;
6832 break;
6833 default:
6834 WARN_ON(1);
6835 break;
6836 }
6837
Johannes Berg4c476992010-10-04 21:36:35 +02006838 if (!rdev_ops)
6839 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006840
Johannes Berg4c476992010-10-04 21:36:35 +02006841 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006842}
6843
6844static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6845{
Johannes Berg4c476992010-10-04 21:36:35 +02006846 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6847 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006848
Johannes Berg074ac8d2010-09-16 14:58:22 +02006849 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006850 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6851 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006852
Johannes Berg4c476992010-10-04 21:36:35 +02006853 if (!rdev->ops->flush_pmksa)
6854 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006855
Hila Gonene35e4d22012-06-27 17:19:42 +03006856 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006857}
6858
Arik Nemtsov109086c2011-09-28 14:12:50 +03006859static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6860{
6861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6862 struct net_device *dev = info->user_ptr[1];
6863 u8 action_code, dialog_token;
6864 u16 status_code;
6865 u8 *peer;
6866
6867 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6868 !rdev->ops->tdls_mgmt)
6869 return -EOPNOTSUPP;
6870
6871 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6872 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6873 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6874 !info->attrs[NL80211_ATTR_IE] ||
6875 !info->attrs[NL80211_ATTR_MAC])
6876 return -EINVAL;
6877
6878 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6879 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6880 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6881 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6882
Hila Gonene35e4d22012-06-27 17:19:42 +03006883 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6884 dialog_token, status_code,
6885 nla_data(info->attrs[NL80211_ATTR_IE]),
6886 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006887}
6888
6889static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6890{
6891 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6892 struct net_device *dev = info->user_ptr[1];
6893 enum nl80211_tdls_operation operation;
6894 u8 *peer;
6895
6896 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6897 !rdev->ops->tdls_oper)
6898 return -EOPNOTSUPP;
6899
6900 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6901 !info->attrs[NL80211_ATTR_MAC])
6902 return -EINVAL;
6903
6904 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6905 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6906
Hila Gonene35e4d22012-06-27 17:19:42 +03006907 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006908}
6909
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006910static int nl80211_remain_on_channel(struct sk_buff *skb,
6911 struct genl_info *info)
6912{
Johannes Berg4c476992010-10-04 21:36:35 +02006913 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006914 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006915 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006916 struct sk_buff *msg;
6917 void *hdr;
6918 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006919 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006920 int err;
6921
6922 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6923 !info->attrs[NL80211_ATTR_DURATION])
6924 return -EINVAL;
6925
6926 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6927
Johannes Berg7c4ef712011-11-18 15:33:48 +01006928 if (!rdev->ops->remain_on_channel ||
6929 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006930 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006931
Johannes Bergebf348f2012-06-01 12:50:54 +02006932 /*
6933 * We should be on that channel for at least a minimum amount of
6934 * time (10ms) but no longer than the driver supports.
6935 */
6936 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6937 duration > rdev->wiphy.max_remain_on_channel_duration)
6938 return -EINVAL;
6939
Johannes Berg683b6d32012-11-08 21:25:48 +01006940 err = nl80211_parse_chandef(rdev, info, &chandef);
6941 if (err)
6942 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006943
6944 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006945 if (!msg)
6946 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006947
Eric W. Biederman15e47302012-09-07 20:12:54 +00006948 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006949 NL80211_CMD_REMAIN_ON_CHANNEL);
6950
6951 if (IS_ERR(hdr)) {
6952 err = PTR_ERR(hdr);
6953 goto free_msg;
6954 }
6955
Johannes Berg683b6d32012-11-08 21:25:48 +01006956 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6957 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006958
6959 if (err)
6960 goto free_msg;
6961
David S. Miller9360ffd2012-03-29 04:41:26 -04006962 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6963 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006964
6965 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006966
6967 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006968
6969 nla_put_failure:
6970 err = -ENOBUFS;
6971 free_msg:
6972 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006973 return err;
6974}
6975
6976static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6977 struct genl_info *info)
6978{
Johannes Berg4c476992010-10-04 21:36:35 +02006979 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006980 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006981 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006982
6983 if (!info->attrs[NL80211_ATTR_COOKIE])
6984 return -EINVAL;
6985
Johannes Berg4c476992010-10-04 21:36:35 +02006986 if (!rdev->ops->cancel_remain_on_channel)
6987 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006988
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006989 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6990
Hila Gonene35e4d22012-06-27 17:19:42 +03006991 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006992}
6993
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006994static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6995 u8 *rates, u8 rates_len)
6996{
6997 u8 i;
6998 u32 mask = 0;
6999
7000 for (i = 0; i < rates_len; i++) {
7001 int rate = (rates[i] & 0x7f) * 5;
7002 int ridx;
7003 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7004 struct ieee80211_rate *srate =
7005 &sband->bitrates[ridx];
7006 if (rate == srate->bitrate) {
7007 mask |= 1 << ridx;
7008 break;
7009 }
7010 }
7011 if (ridx == sband->n_bitrates)
7012 return 0; /* rate not found */
7013 }
7014
7015 return mask;
7016}
7017
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007018static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7019 u8 *rates, u8 rates_len,
7020 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7021{
7022 u8 i;
7023
7024 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7025
7026 for (i = 0; i < rates_len; i++) {
7027 int ridx, rbit;
7028
7029 ridx = rates[i] / 8;
7030 rbit = BIT(rates[i] % 8);
7031
7032 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007033 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007034 return false;
7035
7036 /* check availability */
7037 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7038 mcs[ridx] |= rbit;
7039 else
7040 return false;
7041 }
7042
7043 return true;
7044}
7045
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007046static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007047 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7048 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007049 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7050 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007051};
7052
7053static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7054 struct genl_info *info)
7055{
7056 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007057 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007058 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007059 int rem, i;
7060 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007061 struct nlattr *tx_rates;
7062 struct ieee80211_supported_band *sband;
7063
7064 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7065 return -EINVAL;
7066
Johannes Berg4c476992010-10-04 21:36:35 +02007067 if (!rdev->ops->set_bitrate_mask)
7068 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007069
7070 memset(&mask, 0, sizeof(mask));
7071 /* Default to all rates enabled */
7072 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7073 sband = rdev->wiphy.bands[i];
7074 mask.control[i].legacy =
7075 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007076 if (sband)
7077 memcpy(mask.control[i].mcs,
7078 sband->ht_cap.mcs.rx_mask,
7079 sizeof(mask.control[i].mcs));
7080 else
7081 memset(mask.control[i].mcs, 0,
7082 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007083 }
7084
7085 /*
7086 * The nested attribute uses enum nl80211_band as the index. This maps
7087 * directly to the enum ieee80211_band values used in cfg80211.
7088 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007089 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007090 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7091 {
7092 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007093 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7094 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007095 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007096 if (sband == NULL)
7097 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007098 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7099 nla_len(tx_rates), nl80211_txattr_policy);
7100 if (tb[NL80211_TXRATE_LEGACY]) {
7101 mask.control[band].legacy = rateset_to_mask(
7102 sband,
7103 nla_data(tb[NL80211_TXRATE_LEGACY]),
7104 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307105 if ((mask.control[band].legacy == 0) &&
7106 nla_len(tb[NL80211_TXRATE_LEGACY]))
7107 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007108 }
7109 if (tb[NL80211_TXRATE_MCS]) {
7110 if (!ht_rateset_to_mask(
7111 sband,
7112 nla_data(tb[NL80211_TXRATE_MCS]),
7113 nla_len(tb[NL80211_TXRATE_MCS]),
7114 mask.control[band].mcs))
7115 return -EINVAL;
7116 }
7117
7118 if (mask.control[band].legacy == 0) {
7119 /* don't allow empty legacy rates if HT
7120 * is not even supported. */
7121 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7122 return -EINVAL;
7123
7124 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7125 if (mask.control[band].mcs[i])
7126 break;
7127
7128 /* legacy and mcs rates may not be both empty */
7129 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007130 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007131 }
7132 }
7133
Hila Gonene35e4d22012-06-27 17:19:42 +03007134 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007135}
7136
Johannes Berg2e161f72010-08-12 15:38:38 +02007137static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007138{
Johannes Berg4c476992010-10-04 21:36:35 +02007139 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007140 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007141 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007142
7143 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7144 return -EINVAL;
7145
Johannes Berg2e161f72010-08-12 15:38:38 +02007146 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7147 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007148
Johannes Berg71bbc992012-06-15 15:30:18 +02007149 switch (wdev->iftype) {
7150 case NL80211_IFTYPE_STATION:
7151 case NL80211_IFTYPE_ADHOC:
7152 case NL80211_IFTYPE_P2P_CLIENT:
7153 case NL80211_IFTYPE_AP:
7154 case NL80211_IFTYPE_AP_VLAN:
7155 case NL80211_IFTYPE_MESH_POINT:
7156 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007157 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007158 break;
7159 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007160 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007161 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007162
7163 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007164 if (!rdev->ops->mgmt_tx)
7165 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007166
Eric W. Biederman15e47302012-09-07 20:12:54 +00007167 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007168 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7169 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007170}
7171
Johannes Berg2e161f72010-08-12 15:38:38 +02007172static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007173{
Johannes Berg4c476992010-10-04 21:36:35 +02007174 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007175 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007176 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007177 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007178 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007179 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007180 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007181 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01007182 bool offchan, no_cck, dont_wait_for_ack;
7183
7184 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02007185
Johannes Berg683b6d32012-11-08 21:25:48 +01007186 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007187 return -EINVAL;
7188
Johannes Berg4c476992010-10-04 21:36:35 +02007189 if (!rdev->ops->mgmt_tx)
7190 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007191
Johannes Berg71bbc992012-06-15 15:30:18 +02007192 switch (wdev->iftype) {
7193 case NL80211_IFTYPE_STATION:
7194 case NL80211_IFTYPE_ADHOC:
7195 case NL80211_IFTYPE_P2P_CLIENT:
7196 case NL80211_IFTYPE_AP:
7197 case NL80211_IFTYPE_AP_VLAN:
7198 case NL80211_IFTYPE_MESH_POINT:
7199 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007200 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007201 break;
7202 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007203 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007204 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007205
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007206 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007207 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007208 return -EINVAL;
7209 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007210
7211 /*
7212 * We should wait on the channel for at least a minimum amount
7213 * of time (10ms) but no longer than the driver supports.
7214 */
7215 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7216 wait > rdev->wiphy.max_remain_on_channel_duration)
7217 return -EINVAL;
7218
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007219 }
7220
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007221 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7222
Johannes Berg7c4ef712011-11-18 15:33:48 +01007223 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7224 return -EINVAL;
7225
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307226 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7227
Johannes Berg683b6d32012-11-08 21:25:48 +01007228 err = nl80211_parse_chandef(rdev, info, &chandef);
7229 if (err)
7230 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007231
Johannes Berge247bd902011-11-04 11:18:21 +01007232 if (!dont_wait_for_ack) {
7233 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7234 if (!msg)
7235 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007236
Eric W. Biederman15e47302012-09-07 20:12:54 +00007237 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007238 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007239
Johannes Berge247bd902011-11-04 11:18:21 +01007240 if (IS_ERR(hdr)) {
7241 err = PTR_ERR(hdr);
7242 goto free_msg;
7243 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007244 }
Johannes Berge247bd902011-11-04 11:18:21 +01007245
Johannes Berg683b6d32012-11-08 21:25:48 +01007246 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007247 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7248 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007249 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007250 if (err)
7251 goto free_msg;
7252
Johannes Berge247bd902011-11-04 11:18:21 +01007253 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007254 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7255 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007256
Johannes Berge247bd902011-11-04 11:18:21 +01007257 genlmsg_end(msg, hdr);
7258 return genlmsg_reply(msg, info);
7259 }
7260
7261 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007262
7263 nla_put_failure:
7264 err = -ENOBUFS;
7265 free_msg:
7266 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007267 return err;
7268}
7269
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007270static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7271{
7272 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007273 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007274 u64 cookie;
7275
7276 if (!info->attrs[NL80211_ATTR_COOKIE])
7277 return -EINVAL;
7278
7279 if (!rdev->ops->mgmt_tx_cancel_wait)
7280 return -EOPNOTSUPP;
7281
Johannes Berg71bbc992012-06-15 15:30:18 +02007282 switch (wdev->iftype) {
7283 case NL80211_IFTYPE_STATION:
7284 case NL80211_IFTYPE_ADHOC:
7285 case NL80211_IFTYPE_P2P_CLIENT:
7286 case NL80211_IFTYPE_AP:
7287 case NL80211_IFTYPE_AP_VLAN:
7288 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007289 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007290 break;
7291 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007292 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007293 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007294
7295 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7296
Hila Gonene35e4d22012-06-27 17:19:42 +03007297 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007298}
7299
Kalle Valoffb9eb32010-02-17 17:58:10 +02007300static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7301{
Johannes Berg4c476992010-10-04 21:36:35 +02007302 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007303 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007304 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007305 u8 ps_state;
7306 bool state;
7307 int err;
7308
Johannes Berg4c476992010-10-04 21:36:35 +02007309 if (!info->attrs[NL80211_ATTR_PS_STATE])
7310 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007311
7312 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7313
Johannes Berg4c476992010-10-04 21:36:35 +02007314 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7315 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007316
7317 wdev = dev->ieee80211_ptr;
7318
Johannes Berg4c476992010-10-04 21:36:35 +02007319 if (!rdev->ops->set_power_mgmt)
7320 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007321
7322 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7323
7324 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007325 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007326
Hila Gonene35e4d22012-06-27 17:19:42 +03007327 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007328 if (!err)
7329 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007330 return err;
7331}
7332
7333static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7334{
Johannes Berg4c476992010-10-04 21:36:35 +02007335 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007336 enum nl80211_ps_state ps_state;
7337 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007338 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007339 struct sk_buff *msg;
7340 void *hdr;
7341 int err;
7342
Kalle Valoffb9eb32010-02-17 17:58:10 +02007343 wdev = dev->ieee80211_ptr;
7344
Johannes Berg4c476992010-10-04 21:36:35 +02007345 if (!rdev->ops->set_power_mgmt)
7346 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007347
7348 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007349 if (!msg)
7350 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007351
Eric W. Biederman15e47302012-09-07 20:12:54 +00007352 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007353 NL80211_CMD_GET_POWER_SAVE);
7354 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007355 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007356 goto free_msg;
7357 }
7358
7359 if (wdev->ps)
7360 ps_state = NL80211_PS_ENABLED;
7361 else
7362 ps_state = NL80211_PS_DISABLED;
7363
David S. Miller9360ffd2012-03-29 04:41:26 -04007364 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7365 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007366
7367 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007368 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007369
Johannes Berg4c476992010-10-04 21:36:35 +02007370 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007371 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007372 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007373 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007374 return err;
7375}
7376
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007377static struct nla_policy
7378nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7379 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7380 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7381 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007382 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7383 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7384 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007385};
7386
Thomas Pedersen84f10702012-07-12 16:17:33 -07007387static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007388 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007389{
7390 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7391 struct wireless_dev *wdev;
7392 struct net_device *dev = info->user_ptr[1];
7393
Johannes Bergd9d8b012012-11-26 12:51:52 +01007394 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007395 return -EINVAL;
7396
7397 wdev = dev->ieee80211_ptr;
7398
7399 if (!rdev->ops->set_cqm_txe_config)
7400 return -EOPNOTSUPP;
7401
7402 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7403 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7404 return -EOPNOTSUPP;
7405
Hila Gonene35e4d22012-06-27 17:19:42 +03007406 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007407}
7408
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007409static int nl80211_set_cqm_rssi(struct genl_info *info,
7410 s32 threshold, u32 hysteresis)
7411{
Johannes Berg4c476992010-10-04 21:36:35 +02007412 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007413 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007414 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007415
7416 if (threshold > 0)
7417 return -EINVAL;
7418
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007419 wdev = dev->ieee80211_ptr;
7420
Johannes Berg4c476992010-10-04 21:36:35 +02007421 if (!rdev->ops->set_cqm_rssi_config)
7422 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007423
Johannes Berg074ac8d2010-09-16 14:58:22 +02007424 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007425 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7426 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007427
Hila Gonene35e4d22012-06-27 17:19:42 +03007428 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007429}
7430
7431static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7432{
7433 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7434 struct nlattr *cqm;
7435 int err;
7436
7437 cqm = info->attrs[NL80211_ATTR_CQM];
7438 if (!cqm) {
7439 err = -EINVAL;
7440 goto out;
7441 }
7442
7443 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7444 nl80211_attr_cqm_policy);
7445 if (err)
7446 goto out;
7447
7448 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7449 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7450 s32 threshold;
7451 u32 hysteresis;
7452 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7453 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7454 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007455 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7456 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7457 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7458 u32 rate, pkts, intvl;
7459 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7460 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7461 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7462 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007463 } else
7464 err = -EINVAL;
7465
7466out:
7467 return err;
7468}
7469
Johannes Berg29cbe682010-12-03 09:20:44 +01007470static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7471{
7472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7473 struct net_device *dev = info->user_ptr[1];
7474 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007475 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007476 int err;
7477
7478 /* start with default */
7479 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007480 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007481
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007482 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007483 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007484 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007485 if (err)
7486 return err;
7487 }
7488
7489 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7490 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7491 return -EINVAL;
7492
Javier Cardonac80d5452010-12-16 17:37:49 -08007493 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7494 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7495
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007496 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7497 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7498 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7499 return -EINVAL;
7500
Marco Porsch9bdbf042013-01-07 16:04:51 +01007501 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7502 setup.beacon_interval =
7503 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7504 if (setup.beacon_interval < 10 ||
7505 setup.beacon_interval > 10000)
7506 return -EINVAL;
7507 }
7508
7509 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7510 setup.dtim_period =
7511 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7512 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7513 return -EINVAL;
7514 }
7515
Javier Cardonac80d5452010-12-16 17:37:49 -08007516 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7517 /* parse additional setup parameters if given */
7518 err = nl80211_parse_mesh_setup(info, &setup);
7519 if (err)
7520 return err;
7521 }
7522
Thomas Pedersend37bb182013-03-04 13:06:13 -08007523 if (setup.user_mpm)
7524 cfg.auto_open_plinks = false;
7525
Johannes Bergcc1d2802012-05-16 23:50:20 +02007526 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007527 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7528 if (err)
7529 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007530 } else {
7531 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007532 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007533 }
7534
Javier Cardonac80d5452010-12-16 17:37:49 -08007535 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007536}
7537
7538static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7539{
7540 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7541 struct net_device *dev = info->user_ptr[1];
7542
7543 return cfg80211_leave_mesh(rdev, dev);
7544}
7545
Johannes Bergdfb89c52012-06-27 09:23:48 +02007546#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007547static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7548 struct cfg80211_registered_device *rdev)
7549{
7550 struct nlattr *nl_pats, *nl_pat;
7551 int i, pat_len;
7552
7553 if (!rdev->wowlan->n_patterns)
7554 return 0;
7555
7556 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7557 if (!nl_pats)
7558 return -ENOBUFS;
7559
7560 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7561 nl_pat = nla_nest_start(msg, i + 1);
7562 if (!nl_pat)
7563 return -ENOBUFS;
7564 pat_len = rdev->wowlan->patterns[i].pattern_len;
7565 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7566 DIV_ROUND_UP(pat_len, 8),
7567 rdev->wowlan->patterns[i].mask) ||
7568 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7569 pat_len, rdev->wowlan->patterns[i].pattern) ||
7570 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7571 rdev->wowlan->patterns[i].pkt_offset))
7572 return -ENOBUFS;
7573 nla_nest_end(msg, nl_pat);
7574 }
7575 nla_nest_end(msg, nl_pats);
7576
7577 return 0;
7578}
7579
Johannes Berg2a0e0472013-01-23 22:57:40 +01007580static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7581 struct cfg80211_wowlan_tcp *tcp)
7582{
7583 struct nlattr *nl_tcp;
7584
7585 if (!tcp)
7586 return 0;
7587
7588 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7589 if (!nl_tcp)
7590 return -ENOBUFS;
7591
7592 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7593 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7594 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7595 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7596 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7597 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7598 tcp->payload_len, tcp->payload) ||
7599 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7600 tcp->data_interval) ||
7601 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7602 tcp->wake_len, tcp->wake_data) ||
7603 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7604 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7605 return -ENOBUFS;
7606
7607 if (tcp->payload_seq.len &&
7608 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7609 sizeof(tcp->payload_seq), &tcp->payload_seq))
7610 return -ENOBUFS;
7611
7612 if (tcp->payload_tok.len &&
7613 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7614 sizeof(tcp->payload_tok) + tcp->tokens_size,
7615 &tcp->payload_tok))
7616 return -ENOBUFS;
7617
7618 return 0;
7619}
7620
Johannes Bergff1b6e62011-05-04 15:37:28 +02007621static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7622{
7623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7624 struct sk_buff *msg;
7625 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007626 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007627
Johannes Berg2a0e0472013-01-23 22:57:40 +01007628 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7629 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007630 return -EOPNOTSUPP;
7631
Johannes Berg2a0e0472013-01-23 22:57:40 +01007632 if (rdev->wowlan && rdev->wowlan->tcp) {
7633 /* adjust size to have room for all the data */
7634 size += rdev->wowlan->tcp->tokens_size +
7635 rdev->wowlan->tcp->payload_len +
7636 rdev->wowlan->tcp->wake_len +
7637 rdev->wowlan->tcp->wake_len / 8;
7638 }
7639
7640 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007641 if (!msg)
7642 return -ENOMEM;
7643
Eric W. Biederman15e47302012-09-07 20:12:54 +00007644 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007645 NL80211_CMD_GET_WOWLAN);
7646 if (!hdr)
7647 goto nla_put_failure;
7648
7649 if (rdev->wowlan) {
7650 struct nlattr *nl_wowlan;
7651
7652 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7653 if (!nl_wowlan)
7654 goto nla_put_failure;
7655
David S. Miller9360ffd2012-03-29 04:41:26 -04007656 if ((rdev->wowlan->any &&
7657 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7658 (rdev->wowlan->disconnect &&
7659 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7660 (rdev->wowlan->magic_pkt &&
7661 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7662 (rdev->wowlan->gtk_rekey_failure &&
7663 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7664 (rdev->wowlan->eap_identity_req &&
7665 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7666 (rdev->wowlan->four_way_handshake &&
7667 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7668 (rdev->wowlan->rfkill_release &&
7669 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7670 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007671
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007672 if (nl80211_send_wowlan_patterns(msg, rdev))
7673 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007674
7675 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7676 goto nla_put_failure;
7677
Johannes Bergff1b6e62011-05-04 15:37:28 +02007678 nla_nest_end(msg, nl_wowlan);
7679 }
7680
7681 genlmsg_end(msg, hdr);
7682 return genlmsg_reply(msg, info);
7683
7684nla_put_failure:
7685 nlmsg_free(msg);
7686 return -ENOBUFS;
7687}
7688
Johannes Berg2a0e0472013-01-23 22:57:40 +01007689static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7690 struct nlattr *attr,
7691 struct cfg80211_wowlan *trig)
7692{
7693 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7694 struct cfg80211_wowlan_tcp *cfg;
7695 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7696 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7697 u32 size;
7698 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7699 int err, port;
7700
7701 if (!rdev->wiphy.wowlan.tcp)
7702 return -EINVAL;
7703
7704 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7705 nla_data(attr), nla_len(attr),
7706 nl80211_wowlan_tcp_policy);
7707 if (err)
7708 return err;
7709
7710 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7711 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7712 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7713 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7714 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7715 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7716 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7717 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7718 return -EINVAL;
7719
7720 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7721 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7722 return -EINVAL;
7723
7724 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg723d5682013-02-26 13:56:40 +01007725 rdev->wiphy.wowlan.tcp->data_interval_max ||
7726 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007727 return -EINVAL;
7728
7729 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7730 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7731 return -EINVAL;
7732
7733 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7734 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7735 return -EINVAL;
7736
7737 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7738 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7739
7740 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7741 tokens_size = tokln - sizeof(*tok);
7742
7743 if (!tok->len || tokens_size % tok->len)
7744 return -EINVAL;
7745 if (!rdev->wiphy.wowlan.tcp->tok)
7746 return -EINVAL;
7747 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7748 return -EINVAL;
7749 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7750 return -EINVAL;
7751 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7752 return -EINVAL;
7753 if (tok->offset + tok->len > data_size)
7754 return -EINVAL;
7755 }
7756
7757 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7758 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7759 if (!rdev->wiphy.wowlan.tcp->seq)
7760 return -EINVAL;
7761 if (seq->len == 0 || seq->len > 4)
7762 return -EINVAL;
7763 if (seq->len + seq->offset > data_size)
7764 return -EINVAL;
7765 }
7766
7767 size = sizeof(*cfg);
7768 size += data_size;
7769 size += wake_size + wake_mask_size;
7770 size += tokens_size;
7771
7772 cfg = kzalloc(size, GFP_KERNEL);
7773 if (!cfg)
7774 return -ENOMEM;
7775 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7776 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7777 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7778 ETH_ALEN);
7779 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7780 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7781 else
7782 port = 0;
7783#ifdef CONFIG_INET
7784 /* allocate a socket and port for it and use it */
7785 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7786 IPPROTO_TCP, &cfg->sock, 1);
7787 if (err) {
7788 kfree(cfg);
7789 return err;
7790 }
7791 if (inet_csk_get_port(cfg->sock->sk, port)) {
7792 sock_release(cfg->sock);
7793 kfree(cfg);
7794 return -EADDRINUSE;
7795 }
7796 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7797#else
7798 if (!port) {
7799 kfree(cfg);
7800 return -EINVAL;
7801 }
7802 cfg->src_port = port;
7803#endif
7804
7805 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7806 cfg->payload_len = data_size;
7807 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7808 memcpy((void *)cfg->payload,
7809 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7810 data_size);
7811 if (seq)
7812 cfg->payload_seq = *seq;
7813 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7814 cfg->wake_len = wake_size;
7815 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7816 memcpy((void *)cfg->wake_data,
7817 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7818 wake_size);
7819 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7820 data_size + wake_size;
7821 memcpy((void *)cfg->wake_mask,
7822 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7823 wake_mask_size);
7824 if (tok) {
7825 cfg->tokens_size = tokens_size;
7826 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7827 }
7828
7829 trig->tcp = cfg;
7830
7831 return 0;
7832}
7833
Johannes Bergff1b6e62011-05-04 15:37:28 +02007834static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7835{
7836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7837 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007838 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007839 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007840 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7841 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007842 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007843
Johannes Berg2a0e0472013-01-23 22:57:40 +01007844 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7845 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007846 return -EOPNOTSUPP;
7847
Johannes Bergae33bd82012-07-12 16:25:02 +02007848 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7849 cfg80211_rdev_free_wowlan(rdev);
7850 rdev->wowlan = NULL;
7851 goto set_wakeup;
7852 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007853
7854 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7855 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7856 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7857 nl80211_wowlan_policy);
7858 if (err)
7859 return err;
7860
7861 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7862 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7863 return -EINVAL;
7864 new_triggers.any = true;
7865 }
7866
7867 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7868 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7869 return -EINVAL;
7870 new_triggers.disconnect = true;
7871 }
7872
7873 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7874 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7875 return -EINVAL;
7876 new_triggers.magic_pkt = true;
7877 }
7878
Johannes Berg77dbbb12011-07-13 10:48:55 +02007879 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7880 return -EINVAL;
7881
7882 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7883 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7884 return -EINVAL;
7885 new_triggers.gtk_rekey_failure = true;
7886 }
7887
7888 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7889 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7890 return -EINVAL;
7891 new_triggers.eap_identity_req = true;
7892 }
7893
7894 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7895 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7896 return -EINVAL;
7897 new_triggers.four_way_handshake = true;
7898 }
7899
7900 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7901 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7902 return -EINVAL;
7903 new_triggers.rfkill_release = true;
7904 }
7905
Johannes Bergff1b6e62011-05-04 15:37:28 +02007906 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7907 struct nlattr *pat;
7908 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007909 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007910 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7911
7912 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7913 rem)
7914 n_patterns++;
7915 if (n_patterns > wowlan->n_patterns)
7916 return -EINVAL;
7917
7918 new_triggers.patterns = kcalloc(n_patterns,
7919 sizeof(new_triggers.patterns[0]),
7920 GFP_KERNEL);
7921 if (!new_triggers.patterns)
7922 return -ENOMEM;
7923
7924 new_triggers.n_patterns = n_patterns;
7925 i = 0;
7926
7927 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7928 rem) {
7929 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7930 nla_data(pat), nla_len(pat), NULL);
7931 err = -EINVAL;
7932 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7933 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7934 goto error;
7935 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7936 mask_len = DIV_ROUND_UP(pat_len, 8);
7937 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7938 mask_len)
7939 goto error;
7940 if (pat_len > wowlan->pattern_max_len ||
7941 pat_len < wowlan->pattern_min_len)
7942 goto error;
7943
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007944 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7945 pkt_offset = 0;
7946 else
7947 pkt_offset = nla_get_u32(
7948 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7949 if (pkt_offset > wowlan->max_pkt_offset)
7950 goto error;
7951 new_triggers.patterns[i].pkt_offset = pkt_offset;
7952
Johannes Bergff1b6e62011-05-04 15:37:28 +02007953 new_triggers.patterns[i].mask =
7954 kmalloc(mask_len + pat_len, GFP_KERNEL);
7955 if (!new_triggers.patterns[i].mask) {
7956 err = -ENOMEM;
7957 goto error;
7958 }
7959 new_triggers.patterns[i].pattern =
7960 new_triggers.patterns[i].mask + mask_len;
7961 memcpy(new_triggers.patterns[i].mask,
7962 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7963 mask_len);
7964 new_triggers.patterns[i].pattern_len = pat_len;
7965 memcpy(new_triggers.patterns[i].pattern,
7966 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7967 pat_len);
7968 i++;
7969 }
7970 }
7971
Johannes Berg2a0e0472013-01-23 22:57:40 +01007972 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7973 err = nl80211_parse_wowlan_tcp(
7974 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7975 &new_triggers);
7976 if (err)
7977 goto error;
7978 }
7979
Johannes Bergae33bd82012-07-12 16:25:02 +02007980 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7981 if (!ntrig) {
7982 err = -ENOMEM;
7983 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007984 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007985 cfg80211_rdev_free_wowlan(rdev);
7986 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007987
Johannes Bergae33bd82012-07-12 16:25:02 +02007988 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007989 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007990 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007991
Johannes Bergff1b6e62011-05-04 15:37:28 +02007992 return 0;
7993 error:
7994 for (i = 0; i < new_triggers.n_patterns; i++)
7995 kfree(new_triggers.patterns[i].mask);
7996 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007997 if (new_triggers.tcp && new_triggers.tcp->sock)
7998 sock_release(new_triggers.tcp->sock);
7999 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008000 return err;
8001}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008002#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008003
Johannes Berge5497d72011-07-05 16:35:40 +02008004static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8005{
8006 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8007 struct net_device *dev = info->user_ptr[1];
8008 struct wireless_dev *wdev = dev->ieee80211_ptr;
8009 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8010 struct cfg80211_gtk_rekey_data rekey_data;
8011 int err;
8012
8013 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8014 return -EINVAL;
8015
8016 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8017 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8018 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8019 nl80211_rekey_policy);
8020 if (err)
8021 return err;
8022
8023 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8024 return -ERANGE;
8025 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8026 return -ERANGE;
8027 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8028 return -ERANGE;
8029
8030 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8031 NL80211_KEK_LEN);
8032 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8033 NL80211_KCK_LEN);
8034 memcpy(rekey_data.replay_ctr,
8035 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8036 NL80211_REPLAY_CTR_LEN);
8037
8038 wdev_lock(wdev);
8039 if (!wdev->current_bss) {
8040 err = -ENOTCONN;
8041 goto out;
8042 }
8043
8044 if (!rdev->ops->set_rekey_data) {
8045 err = -EOPNOTSUPP;
8046 goto out;
8047 }
8048
Hila Gonene35e4d22012-06-27 17:19:42 +03008049 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008050 out:
8051 wdev_unlock(wdev);
8052 return err;
8053}
8054
Johannes Berg28946da2011-11-04 11:18:12 +01008055static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8056 struct genl_info *info)
8057{
8058 struct net_device *dev = info->user_ptr[1];
8059 struct wireless_dev *wdev = dev->ieee80211_ptr;
8060
8061 if (wdev->iftype != NL80211_IFTYPE_AP &&
8062 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8063 return -EINVAL;
8064
Eric W. Biederman15e47302012-09-07 20:12:54 +00008065 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008066 return -EBUSY;
8067
Eric W. Biederman15e47302012-09-07 20:12:54 +00008068 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008069 return 0;
8070}
8071
Johannes Berg7f6cf312011-11-04 11:18:15 +01008072static int nl80211_probe_client(struct sk_buff *skb,
8073 struct genl_info *info)
8074{
8075 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8076 struct net_device *dev = info->user_ptr[1];
8077 struct wireless_dev *wdev = dev->ieee80211_ptr;
8078 struct sk_buff *msg;
8079 void *hdr;
8080 const u8 *addr;
8081 u64 cookie;
8082 int err;
8083
8084 if (wdev->iftype != NL80211_IFTYPE_AP &&
8085 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8086 return -EOPNOTSUPP;
8087
8088 if (!info->attrs[NL80211_ATTR_MAC])
8089 return -EINVAL;
8090
8091 if (!rdev->ops->probe_client)
8092 return -EOPNOTSUPP;
8093
8094 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8095 if (!msg)
8096 return -ENOMEM;
8097
Eric W. Biederman15e47302012-09-07 20:12:54 +00008098 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008099 NL80211_CMD_PROBE_CLIENT);
8100
8101 if (IS_ERR(hdr)) {
8102 err = PTR_ERR(hdr);
8103 goto free_msg;
8104 }
8105
8106 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8107
Hila Gonene35e4d22012-06-27 17:19:42 +03008108 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008109 if (err)
8110 goto free_msg;
8111
David S. Miller9360ffd2012-03-29 04:41:26 -04008112 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8113 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008114
8115 genlmsg_end(msg, hdr);
8116
8117 return genlmsg_reply(msg, info);
8118
8119 nla_put_failure:
8120 err = -ENOBUFS;
8121 free_msg:
8122 nlmsg_free(msg);
8123 return err;
8124}
8125
Johannes Berg5e760232011-11-04 11:18:17 +01008126static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8127{
8128 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008129 struct cfg80211_beacon_registration *reg, *nreg;
8130 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008131
8132 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8133 return -EOPNOTSUPP;
8134
Ben Greear37c73b52012-10-26 14:49:25 -07008135 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8136 if (!nreg)
8137 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008138
Ben Greear37c73b52012-10-26 14:49:25 -07008139 /* First, check if already registered. */
8140 spin_lock_bh(&rdev->beacon_registrations_lock);
8141 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8142 if (reg->nlportid == info->snd_portid) {
8143 rv = -EALREADY;
8144 goto out_err;
8145 }
8146 }
8147 /* Add it to the list */
8148 nreg->nlportid = info->snd_portid;
8149 list_add(&nreg->list, &rdev->beacon_registrations);
8150
8151 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008152
8153 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008154out_err:
8155 spin_unlock_bh(&rdev->beacon_registrations_lock);
8156 kfree(nreg);
8157 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008158}
8159
Johannes Berg98104fde2012-06-16 00:19:54 +02008160static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8161{
8162 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8163 struct wireless_dev *wdev = info->user_ptr[1];
8164 int err;
8165
8166 if (!rdev->ops->start_p2p_device)
8167 return -EOPNOTSUPP;
8168
8169 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8170 return -EOPNOTSUPP;
8171
8172 if (wdev->p2p_started)
8173 return 0;
8174
8175 mutex_lock(&rdev->devlist_mtx);
8176 err = cfg80211_can_add_interface(rdev, wdev->iftype);
8177 mutex_unlock(&rdev->devlist_mtx);
8178 if (err)
8179 return err;
8180
Johannes Bergeeb126e2012-10-23 15:16:50 +02008181 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008182 if (err)
8183 return err;
8184
8185 wdev->p2p_started = true;
8186 mutex_lock(&rdev->devlist_mtx);
8187 rdev->opencount++;
8188 mutex_unlock(&rdev->devlist_mtx);
8189
8190 return 0;
8191}
8192
8193static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8194{
8195 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8196 struct wireless_dev *wdev = info->user_ptr[1];
8197
8198 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8199 return -EOPNOTSUPP;
8200
8201 if (!rdev->ops->stop_p2p_device)
8202 return -EOPNOTSUPP;
8203
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008204 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +01008205 mutex_lock(&rdev->sched_scan_mtx);
8206 cfg80211_stop_p2p_device(rdev, wdev);
8207 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008208 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +02008209
8210 return 0;
8211}
8212
Johannes Berg3713b4e2013-02-14 16:19:38 +01008213static int nl80211_get_protocol_features(struct sk_buff *skb,
8214 struct genl_info *info)
8215{
8216 void *hdr;
8217 struct sk_buff *msg;
8218
8219 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8220 if (!msg)
8221 return -ENOMEM;
8222
8223 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8224 NL80211_CMD_GET_PROTOCOL_FEATURES);
8225 if (!hdr)
8226 goto nla_put_failure;
8227
8228 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8229 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8230 goto nla_put_failure;
8231
8232 genlmsg_end(msg, hdr);
8233 return genlmsg_reply(msg, info);
8234
8235 nla_put_failure:
8236 kfree_skb(msg);
8237 return -ENOBUFS;
8238}
8239
Jouni Malinen355199e2013-02-27 17:14:27 +02008240static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8241{
8242 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8243 struct cfg80211_update_ft_ies_params ft_params;
8244 struct net_device *dev = info->user_ptr[1];
8245
8246 if (!rdev->ops->update_ft_ies)
8247 return -EOPNOTSUPP;
8248
8249 if (!info->attrs[NL80211_ATTR_MDID] ||
8250 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8251 return -EINVAL;
8252
8253 memset(&ft_params, 0, sizeof(ft_params));
8254 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8255 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8256 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8257
8258 return rdev_update_ft_ies(rdev, dev, &ft_params);
8259}
8260
Arend van Spriel5de17982013-04-18 15:49:00 +02008261static int nl80211_crit_protocol_start(struct sk_buff *skb,
8262 struct genl_info *info)
8263{
8264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8265 struct wireless_dev *wdev = info->user_ptr[1];
8266 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8267 u16 duration;
8268 int ret;
8269
8270 if (!rdev->ops->crit_proto_start)
8271 return -EOPNOTSUPP;
8272
8273 if (WARN_ON(!rdev->ops->crit_proto_stop))
8274 return -EINVAL;
8275
8276 if (rdev->crit_proto_nlportid)
8277 return -EBUSY;
8278
8279 /* determine protocol if provided */
8280 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8281 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8282
8283 if (proto >= NUM_NL80211_CRIT_PROTO)
8284 return -EINVAL;
8285
8286 /* timeout must be provided */
8287 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8288 return -EINVAL;
8289
8290 duration =
8291 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8292
8293 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8294 return -ERANGE;
8295
8296 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8297 if (!ret)
8298 rdev->crit_proto_nlportid = info->snd_portid;
8299
8300 return ret;
8301}
8302
8303static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8304 struct genl_info *info)
8305{
8306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8307 struct wireless_dev *wdev = info->user_ptr[1];
8308
8309 if (!rdev->ops->crit_proto_stop)
8310 return -EOPNOTSUPP;
8311
8312 if (rdev->crit_proto_nlportid) {
8313 rdev->crit_proto_nlportid = 0;
8314 rdev_crit_proto_stop(rdev, wdev);
8315 }
8316 return 0;
8317}
8318
Johannes Berg4c476992010-10-04 21:36:35 +02008319#define NL80211_FLAG_NEED_WIPHY 0x01
8320#define NL80211_FLAG_NEED_NETDEV 0x02
8321#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008322#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8323#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8324 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008325#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008326/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008327#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8328 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008329
8330static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8331 struct genl_info *info)
8332{
8333 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008334 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008335 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008336 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8337
8338 if (rtnl)
8339 rtnl_lock();
8340
8341 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008342 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008343 if (IS_ERR(rdev)) {
8344 if (rtnl)
8345 rtnl_unlock();
8346 return PTR_ERR(rdev);
8347 }
8348 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008349 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8350 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008351 mutex_lock(&cfg80211_mutex);
8352 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8353 info->attrs);
8354 if (IS_ERR(wdev)) {
8355 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02008356 if (rtnl)
8357 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008358 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008359 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008360
Johannes Berg89a54e42012-06-15 14:33:17 +02008361 dev = wdev->netdev;
8362 rdev = wiphy_to_dev(wdev->wiphy);
8363
Johannes Berg1bf614e2012-06-15 15:23:36 +02008364 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8365 if (!dev) {
8366 mutex_unlock(&cfg80211_mutex);
8367 if (rtnl)
8368 rtnl_unlock();
8369 return -EINVAL;
8370 }
8371
8372 info->user_ptr[1] = dev;
8373 } else {
8374 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008375 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008376
Johannes Berg1bf614e2012-06-15 15:23:36 +02008377 if (dev) {
8378 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8379 !netif_running(dev)) {
8380 mutex_unlock(&cfg80211_mutex);
8381 if (rtnl)
8382 rtnl_unlock();
8383 return -ENETDOWN;
8384 }
8385
8386 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008387 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8388 if (!wdev->p2p_started) {
8389 mutex_unlock(&cfg80211_mutex);
8390 if (rtnl)
8391 rtnl_unlock();
8392 return -ENETDOWN;
8393 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008394 }
8395
Johannes Berg89a54e42012-06-15 14:33:17 +02008396 cfg80211_lock_rdev(rdev);
8397
8398 mutex_unlock(&cfg80211_mutex);
8399
Johannes Berg4c476992010-10-04 21:36:35 +02008400 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008401 }
8402
8403 return 0;
8404}
8405
8406static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8407 struct genl_info *info)
8408{
8409 if (info->user_ptr[0])
8410 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02008411 if (info->user_ptr[1]) {
8412 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8413 struct wireless_dev *wdev = info->user_ptr[1];
8414
8415 if (wdev->netdev)
8416 dev_put(wdev->netdev);
8417 } else {
8418 dev_put(info->user_ptr[1]);
8419 }
8420 }
Johannes Berg4c476992010-10-04 21:36:35 +02008421 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8422 rtnl_unlock();
8423}
8424
Johannes Berg55682962007-09-20 13:09:35 -04008425static struct genl_ops nl80211_ops[] = {
8426 {
8427 .cmd = NL80211_CMD_GET_WIPHY,
8428 .doit = nl80211_get_wiphy,
8429 .dumpit = nl80211_dump_wiphy,
8430 .policy = nl80211_policy,
8431 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008432 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04008433 },
8434 {
8435 .cmd = NL80211_CMD_SET_WIPHY,
8436 .doit = nl80211_set_wiphy,
8437 .policy = nl80211_policy,
8438 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008439 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008440 },
8441 {
8442 .cmd = NL80211_CMD_GET_INTERFACE,
8443 .doit = nl80211_get_interface,
8444 .dumpit = nl80211_dump_interface,
8445 .policy = nl80211_policy,
8446 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02008447 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04008448 },
8449 {
8450 .cmd = NL80211_CMD_SET_INTERFACE,
8451 .doit = nl80211_set_interface,
8452 .policy = nl80211_policy,
8453 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008454 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8455 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008456 },
8457 {
8458 .cmd = NL80211_CMD_NEW_INTERFACE,
8459 .doit = nl80211_new_interface,
8460 .policy = nl80211_policy,
8461 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008462 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8463 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008464 },
8465 {
8466 .cmd = NL80211_CMD_DEL_INTERFACE,
8467 .doit = nl80211_del_interface,
8468 .policy = nl80211_policy,
8469 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008470 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008471 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008472 },
Johannes Berg41ade002007-12-19 02:03:29 +01008473 {
8474 .cmd = NL80211_CMD_GET_KEY,
8475 .doit = nl80211_get_key,
8476 .policy = nl80211_policy,
8477 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008478 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008479 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008480 },
8481 {
8482 .cmd = NL80211_CMD_SET_KEY,
8483 .doit = nl80211_set_key,
8484 .policy = nl80211_policy,
8485 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008486 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008487 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008488 },
8489 {
8490 .cmd = NL80211_CMD_NEW_KEY,
8491 .doit = nl80211_new_key,
8492 .policy = nl80211_policy,
8493 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008494 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008495 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008496 },
8497 {
8498 .cmd = NL80211_CMD_DEL_KEY,
8499 .doit = nl80211_del_key,
8500 .policy = nl80211_policy,
8501 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008502 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008503 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008504 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008505 {
8506 .cmd = NL80211_CMD_SET_BEACON,
8507 .policy = nl80211_policy,
8508 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008509 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008510 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008511 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008512 },
8513 {
Johannes Berg88600202012-02-13 15:17:18 +01008514 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008515 .policy = nl80211_policy,
8516 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008517 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008518 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008519 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008520 },
8521 {
Johannes Berg88600202012-02-13 15:17:18 +01008522 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008523 .policy = nl80211_policy,
8524 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008525 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008526 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008527 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008528 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008529 {
8530 .cmd = NL80211_CMD_GET_STATION,
8531 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008532 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008533 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008534 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8535 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008536 },
8537 {
8538 .cmd = NL80211_CMD_SET_STATION,
8539 .doit = nl80211_set_station,
8540 .policy = nl80211_policy,
8541 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008543 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008544 },
8545 {
8546 .cmd = NL80211_CMD_NEW_STATION,
8547 .doit = nl80211_new_station,
8548 .policy = nl80211_policy,
8549 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008551 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008552 },
8553 {
8554 .cmd = NL80211_CMD_DEL_STATION,
8555 .doit = nl80211_del_station,
8556 .policy = nl80211_policy,
8557 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008558 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008559 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008560 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008561 {
8562 .cmd = NL80211_CMD_GET_MPATH,
8563 .doit = nl80211_get_mpath,
8564 .dumpit = nl80211_dump_mpath,
8565 .policy = nl80211_policy,
8566 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008567 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008568 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008569 },
8570 {
8571 .cmd = NL80211_CMD_SET_MPATH,
8572 .doit = nl80211_set_mpath,
8573 .policy = nl80211_policy,
8574 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008575 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008576 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008577 },
8578 {
8579 .cmd = NL80211_CMD_NEW_MPATH,
8580 .doit = nl80211_new_mpath,
8581 .policy = nl80211_policy,
8582 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008583 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008584 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008585 },
8586 {
8587 .cmd = NL80211_CMD_DEL_MPATH,
8588 .doit = nl80211_del_mpath,
8589 .policy = nl80211_policy,
8590 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008591 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008592 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008593 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008594 {
8595 .cmd = NL80211_CMD_SET_BSS,
8596 .doit = nl80211_set_bss,
8597 .policy = nl80211_policy,
8598 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008599 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008600 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008601 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008602 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008603 .cmd = NL80211_CMD_GET_REG,
8604 .doit = nl80211_get_reg,
8605 .policy = nl80211_policy,
8606 /* can be retrieved by unprivileged users */
8607 },
8608 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008609 .cmd = NL80211_CMD_SET_REG,
8610 .doit = nl80211_set_reg,
8611 .policy = nl80211_policy,
8612 .flags = GENL_ADMIN_PERM,
8613 },
8614 {
8615 .cmd = NL80211_CMD_REQ_SET_REG,
8616 .doit = nl80211_req_set_reg,
8617 .policy = nl80211_policy,
8618 .flags = GENL_ADMIN_PERM,
8619 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008620 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008621 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8622 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008623 .policy = nl80211_policy,
8624 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008625 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008626 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008627 },
8628 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008629 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8630 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008631 .policy = nl80211_policy,
8632 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008634 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008635 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008636 {
Johannes Berg2a519312009-02-10 21:25:55 +01008637 .cmd = NL80211_CMD_TRIGGER_SCAN,
8638 .doit = nl80211_trigger_scan,
8639 .policy = nl80211_policy,
8640 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008641 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008642 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008643 },
8644 {
8645 .cmd = NL80211_CMD_GET_SCAN,
8646 .policy = nl80211_policy,
8647 .dumpit = nl80211_dump_scan,
8648 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008649 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008650 .cmd = NL80211_CMD_START_SCHED_SCAN,
8651 .doit = nl80211_start_sched_scan,
8652 .policy = nl80211_policy,
8653 .flags = GENL_ADMIN_PERM,
8654 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8655 NL80211_FLAG_NEED_RTNL,
8656 },
8657 {
8658 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8659 .doit = nl80211_stop_sched_scan,
8660 .policy = nl80211_policy,
8661 .flags = GENL_ADMIN_PERM,
8662 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8663 NL80211_FLAG_NEED_RTNL,
8664 },
8665 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008666 .cmd = NL80211_CMD_AUTHENTICATE,
8667 .doit = nl80211_authenticate,
8668 .policy = nl80211_policy,
8669 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008670 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008671 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008672 },
8673 {
8674 .cmd = NL80211_CMD_ASSOCIATE,
8675 .doit = nl80211_associate,
8676 .policy = nl80211_policy,
8677 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008678 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008679 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008680 },
8681 {
8682 .cmd = NL80211_CMD_DEAUTHENTICATE,
8683 .doit = nl80211_deauthenticate,
8684 .policy = nl80211_policy,
8685 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008686 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008687 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008688 },
8689 {
8690 .cmd = NL80211_CMD_DISASSOCIATE,
8691 .doit = nl80211_disassociate,
8692 .policy = nl80211_policy,
8693 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008695 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008696 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008697 {
8698 .cmd = NL80211_CMD_JOIN_IBSS,
8699 .doit = nl80211_join_ibss,
8700 .policy = nl80211_policy,
8701 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008702 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008703 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008704 },
8705 {
8706 .cmd = NL80211_CMD_LEAVE_IBSS,
8707 .doit = nl80211_leave_ibss,
8708 .policy = nl80211_policy,
8709 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008710 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008711 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008712 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008713#ifdef CONFIG_NL80211_TESTMODE
8714 {
8715 .cmd = NL80211_CMD_TESTMODE,
8716 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008717 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008718 .policy = nl80211_policy,
8719 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008720 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8721 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008722 },
8723#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008724 {
8725 .cmd = NL80211_CMD_CONNECT,
8726 .doit = nl80211_connect,
8727 .policy = nl80211_policy,
8728 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008729 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008730 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008731 },
8732 {
8733 .cmd = NL80211_CMD_DISCONNECT,
8734 .doit = nl80211_disconnect,
8735 .policy = nl80211_policy,
8736 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008737 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008738 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008739 },
Johannes Berg463d0182009-07-14 00:33:35 +02008740 {
8741 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8742 .doit = nl80211_wiphy_netns,
8743 .policy = nl80211_policy,
8744 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008745 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8746 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008747 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008748 {
8749 .cmd = NL80211_CMD_GET_SURVEY,
8750 .policy = nl80211_policy,
8751 .dumpit = nl80211_dump_survey,
8752 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008753 {
8754 .cmd = NL80211_CMD_SET_PMKSA,
8755 .doit = nl80211_setdel_pmksa,
8756 .policy = nl80211_policy,
8757 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008758 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008759 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008760 },
8761 {
8762 .cmd = NL80211_CMD_DEL_PMKSA,
8763 .doit = nl80211_setdel_pmksa,
8764 .policy = nl80211_policy,
8765 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008766 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008767 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008768 },
8769 {
8770 .cmd = NL80211_CMD_FLUSH_PMKSA,
8771 .doit = nl80211_flush_pmksa,
8772 .policy = nl80211_policy,
8773 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008774 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008775 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008776 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008777 {
8778 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8779 .doit = nl80211_remain_on_channel,
8780 .policy = nl80211_policy,
8781 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008782 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008783 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008784 },
8785 {
8786 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8787 .doit = nl80211_cancel_remain_on_channel,
8788 .policy = nl80211_policy,
8789 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008790 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008791 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008792 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008793 {
8794 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8795 .doit = nl80211_set_tx_bitrate_mask,
8796 .policy = nl80211_policy,
8797 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008798 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8799 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008800 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008801 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008802 .cmd = NL80211_CMD_REGISTER_FRAME,
8803 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008804 .policy = nl80211_policy,
8805 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008806 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008807 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008808 },
8809 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008810 .cmd = NL80211_CMD_FRAME,
8811 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008812 .policy = nl80211_policy,
8813 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008814 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008815 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008816 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008817 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008818 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8819 .doit = nl80211_tx_mgmt_cancel_wait,
8820 .policy = nl80211_policy,
8821 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008822 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008823 NL80211_FLAG_NEED_RTNL,
8824 },
8825 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008826 .cmd = NL80211_CMD_SET_POWER_SAVE,
8827 .doit = nl80211_set_power_save,
8828 .policy = nl80211_policy,
8829 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008830 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8831 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008832 },
8833 {
8834 .cmd = NL80211_CMD_GET_POWER_SAVE,
8835 .doit = nl80211_get_power_save,
8836 .policy = nl80211_policy,
8837 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008838 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8839 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008840 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008841 {
8842 .cmd = NL80211_CMD_SET_CQM,
8843 .doit = nl80211_set_cqm,
8844 .policy = nl80211_policy,
8845 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008846 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8847 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008848 },
Johannes Bergf444de02010-05-05 15:25:02 +02008849 {
8850 .cmd = NL80211_CMD_SET_CHANNEL,
8851 .doit = nl80211_set_channel,
8852 .policy = nl80211_policy,
8853 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008854 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8855 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008856 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008857 {
8858 .cmd = NL80211_CMD_SET_WDS_PEER,
8859 .doit = nl80211_set_wds_peer,
8860 .policy = nl80211_policy,
8861 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008862 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8863 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008864 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008865 {
8866 .cmd = NL80211_CMD_JOIN_MESH,
8867 .doit = nl80211_join_mesh,
8868 .policy = nl80211_policy,
8869 .flags = GENL_ADMIN_PERM,
8870 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8871 NL80211_FLAG_NEED_RTNL,
8872 },
8873 {
8874 .cmd = NL80211_CMD_LEAVE_MESH,
8875 .doit = nl80211_leave_mesh,
8876 .policy = nl80211_policy,
8877 .flags = GENL_ADMIN_PERM,
8878 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8879 NL80211_FLAG_NEED_RTNL,
8880 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008881#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008882 {
8883 .cmd = NL80211_CMD_GET_WOWLAN,
8884 .doit = nl80211_get_wowlan,
8885 .policy = nl80211_policy,
8886 /* can be retrieved by unprivileged users */
8887 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8888 NL80211_FLAG_NEED_RTNL,
8889 },
8890 {
8891 .cmd = NL80211_CMD_SET_WOWLAN,
8892 .doit = nl80211_set_wowlan,
8893 .policy = nl80211_policy,
8894 .flags = GENL_ADMIN_PERM,
8895 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8896 NL80211_FLAG_NEED_RTNL,
8897 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008898#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008899 {
8900 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8901 .doit = nl80211_set_rekey_data,
8902 .policy = nl80211_policy,
8903 .flags = GENL_ADMIN_PERM,
8904 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8905 NL80211_FLAG_NEED_RTNL,
8906 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008907 {
8908 .cmd = NL80211_CMD_TDLS_MGMT,
8909 .doit = nl80211_tdls_mgmt,
8910 .policy = nl80211_policy,
8911 .flags = GENL_ADMIN_PERM,
8912 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8913 NL80211_FLAG_NEED_RTNL,
8914 },
8915 {
8916 .cmd = NL80211_CMD_TDLS_OPER,
8917 .doit = nl80211_tdls_oper,
8918 .policy = nl80211_policy,
8919 .flags = GENL_ADMIN_PERM,
8920 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8921 NL80211_FLAG_NEED_RTNL,
8922 },
Johannes Berg28946da2011-11-04 11:18:12 +01008923 {
8924 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8925 .doit = nl80211_register_unexpected_frame,
8926 .policy = nl80211_policy,
8927 .flags = GENL_ADMIN_PERM,
8928 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8929 NL80211_FLAG_NEED_RTNL,
8930 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008931 {
8932 .cmd = NL80211_CMD_PROBE_CLIENT,
8933 .doit = nl80211_probe_client,
8934 .policy = nl80211_policy,
8935 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008936 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008937 NL80211_FLAG_NEED_RTNL,
8938 },
Johannes Berg5e760232011-11-04 11:18:17 +01008939 {
8940 .cmd = NL80211_CMD_REGISTER_BEACONS,
8941 .doit = nl80211_register_beacons,
8942 .policy = nl80211_policy,
8943 .flags = GENL_ADMIN_PERM,
8944 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8945 NL80211_FLAG_NEED_RTNL,
8946 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008947 {
8948 .cmd = NL80211_CMD_SET_NOACK_MAP,
8949 .doit = nl80211_set_noack_map,
8950 .policy = nl80211_policy,
8951 .flags = GENL_ADMIN_PERM,
8952 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8953 NL80211_FLAG_NEED_RTNL,
8954 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008955 {
8956 .cmd = NL80211_CMD_START_P2P_DEVICE,
8957 .doit = nl80211_start_p2p_device,
8958 .policy = nl80211_policy,
8959 .flags = GENL_ADMIN_PERM,
8960 .internal_flags = NL80211_FLAG_NEED_WDEV |
8961 NL80211_FLAG_NEED_RTNL,
8962 },
8963 {
8964 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8965 .doit = nl80211_stop_p2p_device,
8966 .policy = nl80211_policy,
8967 .flags = GENL_ADMIN_PERM,
8968 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8969 NL80211_FLAG_NEED_RTNL,
8970 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008971 {
8972 .cmd = NL80211_CMD_SET_MCAST_RATE,
8973 .doit = nl80211_set_mcast_rate,
8974 .policy = nl80211_policy,
8975 .flags = GENL_ADMIN_PERM,
8976 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8977 NL80211_FLAG_NEED_RTNL,
8978 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308979 {
8980 .cmd = NL80211_CMD_SET_MAC_ACL,
8981 .doit = nl80211_set_mac_acl,
8982 .policy = nl80211_policy,
8983 .flags = GENL_ADMIN_PERM,
8984 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8985 NL80211_FLAG_NEED_RTNL,
8986 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008987 {
8988 .cmd = NL80211_CMD_RADAR_DETECT,
8989 .doit = nl80211_start_radar_detection,
8990 .policy = nl80211_policy,
8991 .flags = GENL_ADMIN_PERM,
8992 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8993 NL80211_FLAG_NEED_RTNL,
8994 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01008995 {
8996 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8997 .doit = nl80211_get_protocol_features,
8998 .policy = nl80211_policy,
8999 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009000 {
9001 .cmd = NL80211_CMD_UPDATE_FT_IES,
9002 .doit = nl80211_update_ft_ies,
9003 .policy = nl80211_policy,
9004 .flags = GENL_ADMIN_PERM,
9005 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9006 NL80211_FLAG_NEED_RTNL,
9007 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009008 {
9009 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9010 .doit = nl80211_crit_protocol_start,
9011 .policy = nl80211_policy,
9012 .flags = GENL_ADMIN_PERM,
9013 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9014 NL80211_FLAG_NEED_RTNL,
9015 },
9016 {
9017 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9018 .doit = nl80211_crit_protocol_stop,
9019 .policy = nl80211_policy,
9020 .flags = GENL_ADMIN_PERM,
9021 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9022 NL80211_FLAG_NEED_RTNL,
9023 }
Johannes Berg55682962007-09-20 13:09:35 -04009024};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009025
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009026static struct genl_multicast_group nl80211_mlme_mcgrp = {
9027 .name = "mlme",
9028};
Johannes Berg55682962007-09-20 13:09:35 -04009029
9030/* multicast groups */
9031static struct genl_multicast_group nl80211_config_mcgrp = {
9032 .name = "config",
9033};
Johannes Berg2a519312009-02-10 21:25:55 +01009034static struct genl_multicast_group nl80211_scan_mcgrp = {
9035 .name = "scan",
9036};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009037static struct genl_multicast_group nl80211_regulatory_mcgrp = {
9038 .name = "regulatory",
9039};
Johannes Berg55682962007-09-20 13:09:35 -04009040
9041/* notification functions */
9042
9043void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9044{
9045 struct sk_buff *msg;
9046
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009047 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009048 if (!msg)
9049 return;
9050
Johannes Berg3713b4e2013-02-14 16:19:38 +01009051 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
9052 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04009053 nlmsg_free(msg);
9054 return;
9055 }
9056
Johannes Berg463d0182009-07-14 00:33:35 +02009057 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9058 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009059}
9060
Johannes Berg362a4152009-05-24 16:43:15 +02009061static int nl80211_add_scan_req(struct sk_buff *msg,
9062 struct cfg80211_registered_device *rdev)
9063{
9064 struct cfg80211_scan_request *req = rdev->scan_req;
9065 struct nlattr *nest;
9066 int i;
9067
Johannes Bergf9f47522013-03-19 15:04:07 +01009068 lockdep_assert_held(&rdev->sched_scan_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +02009069
Johannes Berg362a4152009-05-24 16:43:15 +02009070 if (WARN_ON(!req))
9071 return 0;
9072
9073 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9074 if (!nest)
9075 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009076 for (i = 0; i < req->n_ssids; i++) {
9077 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9078 goto nla_put_failure;
9079 }
Johannes Berg362a4152009-05-24 16:43:15 +02009080 nla_nest_end(msg, nest);
9081
9082 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9083 if (!nest)
9084 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009085 for (i = 0; i < req->n_channels; i++) {
9086 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9087 goto nla_put_failure;
9088 }
Johannes Berg362a4152009-05-24 16:43:15 +02009089 nla_nest_end(msg, nest);
9090
David S. Miller9360ffd2012-03-29 04:41:26 -04009091 if (req->ie &&
9092 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9093 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009094
Sam Lefflered4737712012-10-11 21:03:31 -07009095 if (req->flags)
9096 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9097
Johannes Berg362a4152009-05-24 16:43:15 +02009098 return 0;
9099 nla_put_failure:
9100 return -ENOBUFS;
9101}
9102
Johannes Berga538e2d2009-06-16 19:56:42 +02009103static int nl80211_send_scan_msg(struct sk_buff *msg,
9104 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009105 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009106 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009107 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009108{
9109 void *hdr;
9110
Eric W. Biederman15e47302012-09-07 20:12:54 +00009111 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009112 if (!hdr)
9113 return -1;
9114
David S. Miller9360ffd2012-03-29 04:41:26 -04009115 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009116 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9117 wdev->netdev->ifindex)) ||
9118 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009119 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009120
Johannes Berg362a4152009-05-24 16:43:15 +02009121 /* ignore errors and send incomplete event anyway */
9122 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009123
9124 return genlmsg_end(msg, hdr);
9125
9126 nla_put_failure:
9127 genlmsg_cancel(msg, hdr);
9128 return -EMSGSIZE;
9129}
9130
Luciano Coelho807f8a82011-05-11 17:09:35 +03009131static int
9132nl80211_send_sched_scan_msg(struct sk_buff *msg,
9133 struct cfg80211_registered_device *rdev,
9134 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009135 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009136{
9137 void *hdr;
9138
Eric W. Biederman15e47302012-09-07 20:12:54 +00009139 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009140 if (!hdr)
9141 return -1;
9142
David S. Miller9360ffd2012-03-29 04:41:26 -04009143 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9144 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9145 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009146
9147 return genlmsg_end(msg, hdr);
9148
9149 nla_put_failure:
9150 genlmsg_cancel(msg, hdr);
9151 return -EMSGSIZE;
9152}
9153
Johannes Berga538e2d2009-06-16 19:56:42 +02009154void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009155 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009156{
9157 struct sk_buff *msg;
9158
Thomas Graf58050fc2012-06-28 03:57:45 +00009159 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009160 if (!msg)
9161 return;
9162
Johannes Bergfd014282012-06-18 19:17:03 +02009163 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009164 NL80211_CMD_TRIGGER_SCAN) < 0) {
9165 nlmsg_free(msg);
9166 return;
9167 }
9168
Johannes Berg463d0182009-07-14 00:33:35 +02009169 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9170 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009171}
9172
Johannes Berg2a519312009-02-10 21:25:55 +01009173void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009174 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009175{
9176 struct sk_buff *msg;
9177
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009178 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009179 if (!msg)
9180 return;
9181
Johannes Bergfd014282012-06-18 19:17:03 +02009182 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009183 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009184 nlmsg_free(msg);
9185 return;
9186 }
9187
Johannes Berg463d0182009-07-14 00:33:35 +02009188 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9189 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009190}
9191
9192void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009193 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009194{
9195 struct sk_buff *msg;
9196
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009197 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009198 if (!msg)
9199 return;
9200
Johannes Bergfd014282012-06-18 19:17:03 +02009201 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009202 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009203 nlmsg_free(msg);
9204 return;
9205 }
9206
Johannes Berg463d0182009-07-14 00:33:35 +02009207 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9208 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009209}
9210
Luciano Coelho807f8a82011-05-11 17:09:35 +03009211void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9212 struct net_device *netdev)
9213{
9214 struct sk_buff *msg;
9215
9216 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9217 if (!msg)
9218 return;
9219
9220 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9221 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9222 nlmsg_free(msg);
9223 return;
9224 }
9225
9226 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9227 nl80211_scan_mcgrp.id, GFP_KERNEL);
9228}
9229
9230void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9231 struct net_device *netdev, u32 cmd)
9232{
9233 struct sk_buff *msg;
9234
Thomas Graf58050fc2012-06-28 03:57:45 +00009235 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009236 if (!msg)
9237 return;
9238
9239 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9240 nlmsg_free(msg);
9241 return;
9242 }
9243
9244 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9245 nl80211_scan_mcgrp.id, GFP_KERNEL);
9246}
9247
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009248/*
9249 * This can happen on global regulatory changes or device specific settings
9250 * based on custom world regulatory domains.
9251 */
9252void nl80211_send_reg_change_event(struct regulatory_request *request)
9253{
9254 struct sk_buff *msg;
9255 void *hdr;
9256
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009257 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009258 if (!msg)
9259 return;
9260
9261 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9262 if (!hdr) {
9263 nlmsg_free(msg);
9264 return;
9265 }
9266
9267 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009268 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9269 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009270
David S. Miller9360ffd2012-03-29 04:41:26 -04009271 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9272 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9273 NL80211_REGDOM_TYPE_WORLD))
9274 goto nla_put_failure;
9275 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9276 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9277 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9278 goto nla_put_failure;
9279 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9280 request->intersect) {
9281 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9282 NL80211_REGDOM_TYPE_INTERSECTION))
9283 goto nla_put_failure;
9284 } else {
9285 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9286 NL80211_REGDOM_TYPE_COUNTRY) ||
9287 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9288 request->alpha2))
9289 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009290 }
9291
Johannes Bergf4173762012-12-03 18:23:37 +01009292 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009293 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9294 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009295
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009296 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009297
Johannes Bergbc43b282009-07-25 10:54:13 +02009298 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009299 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009300 GFP_ATOMIC);
9301 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009302
9303 return;
9304
9305nla_put_failure:
9306 genlmsg_cancel(msg, hdr);
9307 nlmsg_free(msg);
9308}
9309
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009310static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9311 struct net_device *netdev,
9312 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009313 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009314{
9315 struct sk_buff *msg;
9316 void *hdr;
9317
Johannes Berge6d6e342009-07-01 21:26:47 +02009318 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009319 if (!msg)
9320 return;
9321
9322 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9323 if (!hdr) {
9324 nlmsg_free(msg);
9325 return;
9326 }
9327
David S. Miller9360ffd2012-03-29 04:41:26 -04009328 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9329 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9330 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9331 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009332
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009333 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009334
Johannes Berg463d0182009-07-14 00:33:35 +02009335 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9336 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009337 return;
9338
9339 nla_put_failure:
9340 genlmsg_cancel(msg, hdr);
9341 nlmsg_free(msg);
9342}
9343
9344void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009345 struct net_device *netdev, const u8 *buf,
9346 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009347{
9348 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009349 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009350}
9351
9352void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9353 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009354 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009355{
Johannes Berge6d6e342009-07-01 21:26:47 +02009356 nl80211_send_mlme_event(rdev, netdev, buf, len,
9357 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009358}
9359
Jouni Malinen53b46b82009-03-27 20:53:56 +02009360void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009361 struct net_device *netdev, const u8 *buf,
9362 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009363{
9364 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009365 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009366}
9367
Jouni Malinen53b46b82009-03-27 20:53:56 +02009368void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9369 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009370 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009371{
9372 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009373 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009374}
9375
Johannes Berg947add32013-02-22 22:05:20 +01009376void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
9377 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +02009378{
Johannes Berg947add32013-02-22 22:05:20 +01009379 struct wireless_dev *wdev = dev->ieee80211_ptr;
9380 struct wiphy *wiphy = wdev->wiphy;
9381 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009382
Johannes Berg947add32013-02-22 22:05:20 +01009383 trace_cfg80211_send_unprot_deauth(dev);
9384 nl80211_send_mlme_event(rdev, dev, buf, len,
9385 NL80211_CMD_UNPROT_DEAUTHENTICATE, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009386}
Johannes Berg947add32013-02-22 22:05:20 +01009387EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
9388
9389void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
9390 size_t len)
9391{
9392 struct wireless_dev *wdev = dev->ieee80211_ptr;
9393 struct wiphy *wiphy = wdev->wiphy;
9394 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9395
9396 trace_cfg80211_send_unprot_disassoc(dev);
9397 nl80211_send_mlme_event(rdev, dev, buf, len,
9398 NL80211_CMD_UNPROT_DISASSOCIATE, GFP_ATOMIC);
9399}
9400EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009401
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009402static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9403 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009404 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009405{
9406 struct sk_buff *msg;
9407 void *hdr;
9408
Johannes Berge6d6e342009-07-01 21:26:47 +02009409 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009410 if (!msg)
9411 return;
9412
9413 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9414 if (!hdr) {
9415 nlmsg_free(msg);
9416 return;
9417 }
9418
David S. Miller9360ffd2012-03-29 04:41:26 -04009419 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9420 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9421 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9422 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9423 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009424
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009425 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009426
Johannes Berg463d0182009-07-14 00:33:35 +02009427 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9428 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009429 return;
9430
9431 nla_put_failure:
9432 genlmsg_cancel(msg, hdr);
9433 nlmsg_free(msg);
9434}
9435
9436void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009437 struct net_device *netdev, const u8 *addr,
9438 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009439{
9440 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009441 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009442}
9443
9444void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009445 struct net_device *netdev, const u8 *addr,
9446 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009447{
Johannes Berge6d6e342009-07-01 21:26:47 +02009448 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9449 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009450}
9451
Samuel Ortizb23aa672009-07-01 21:26:54 +02009452void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9453 struct net_device *netdev, const u8 *bssid,
9454 const u8 *req_ie, size_t req_ie_len,
9455 const u8 *resp_ie, size_t resp_ie_len,
9456 u16 status, gfp_t gfp)
9457{
9458 struct sk_buff *msg;
9459 void *hdr;
9460
Thomas Graf58050fc2012-06-28 03:57:45 +00009461 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009462 if (!msg)
9463 return;
9464
9465 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9466 if (!hdr) {
9467 nlmsg_free(msg);
9468 return;
9469 }
9470
David S. Miller9360ffd2012-03-29 04:41:26 -04009471 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9472 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9473 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9474 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9475 (req_ie &&
9476 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9477 (resp_ie &&
9478 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9479 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009480
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009481 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009482
Johannes Berg463d0182009-07-14 00:33:35 +02009483 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9484 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009485 return;
9486
9487 nla_put_failure:
9488 genlmsg_cancel(msg, hdr);
9489 nlmsg_free(msg);
9490
9491}
9492
9493void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9494 struct net_device *netdev, const u8 *bssid,
9495 const u8 *req_ie, size_t req_ie_len,
9496 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9497{
9498 struct sk_buff *msg;
9499 void *hdr;
9500
Thomas Graf58050fc2012-06-28 03:57:45 +00009501 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009502 if (!msg)
9503 return;
9504
9505 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9506 if (!hdr) {
9507 nlmsg_free(msg);
9508 return;
9509 }
9510
David S. Miller9360ffd2012-03-29 04:41:26 -04009511 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9512 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9513 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9514 (req_ie &&
9515 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9516 (resp_ie &&
9517 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9518 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009519
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009520 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009521
Johannes Berg463d0182009-07-14 00:33:35 +02009522 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9523 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009524 return;
9525
9526 nla_put_failure:
9527 genlmsg_cancel(msg, hdr);
9528 nlmsg_free(msg);
9529
9530}
9531
9532void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9533 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02009534 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009535{
9536 struct sk_buff *msg;
9537 void *hdr;
9538
Thomas Graf58050fc2012-06-28 03:57:45 +00009539 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009540 if (!msg)
9541 return;
9542
9543 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9544 if (!hdr) {
9545 nlmsg_free(msg);
9546 return;
9547 }
9548
David S. Miller9360ffd2012-03-29 04:41:26 -04009549 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9550 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9551 (from_ap && reason &&
9552 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9553 (from_ap &&
9554 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9555 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9556 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009557
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009558 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009559
Johannes Berg463d0182009-07-14 00:33:35 +02009560 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9561 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009562 return;
9563
9564 nla_put_failure:
9565 genlmsg_cancel(msg, hdr);
9566 nlmsg_free(msg);
9567
9568}
9569
Johannes Berg04a773a2009-04-19 21:24:32 +02009570void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9571 struct net_device *netdev, const u8 *bssid,
9572 gfp_t gfp)
9573{
9574 struct sk_buff *msg;
9575 void *hdr;
9576
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009577 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009578 if (!msg)
9579 return;
9580
9581 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9582 if (!hdr) {
9583 nlmsg_free(msg);
9584 return;
9585 }
9586
David S. Miller9360ffd2012-03-29 04:41:26 -04009587 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9588 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9589 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9590 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009591
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009592 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009593
Johannes Berg463d0182009-07-14 00:33:35 +02009594 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9595 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009596 return;
9597
9598 nla_put_failure:
9599 genlmsg_cancel(msg, hdr);
9600 nlmsg_free(msg);
9601}
9602
Johannes Berg947add32013-02-22 22:05:20 +01009603void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9604 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -07009605{
Johannes Berg947add32013-02-22 22:05:20 +01009606 struct wireless_dev *wdev = dev->ieee80211_ptr;
9607 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009608 struct sk_buff *msg;
9609 void *hdr;
9610
Johannes Berg947add32013-02-22 22:05:20 +01009611 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9612 return;
9613
9614 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9615
Javier Cardonac93b5e72011-04-07 15:08:34 -07009616 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9617 if (!msg)
9618 return;
9619
9620 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9621 if (!hdr) {
9622 nlmsg_free(msg);
9623 return;
9624 }
9625
David S. Miller9360ffd2012-03-29 04:41:26 -04009626 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009627 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9628 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009629 (ie_len && ie &&
9630 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9631 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009632
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009633 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009634
9635 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9636 nl80211_mlme_mcgrp.id, gfp);
9637 return;
9638
9639 nla_put_failure:
9640 genlmsg_cancel(msg, hdr);
9641 nlmsg_free(msg);
9642}
Johannes Berg947add32013-02-22 22:05:20 +01009643EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009644
Jouni Malinena3b8b052009-03-27 21:59:49 +02009645void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9646 struct net_device *netdev, const u8 *addr,
9647 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009648 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009649{
9650 struct sk_buff *msg;
9651 void *hdr;
9652
Johannes Berge6d6e342009-07-01 21:26:47 +02009653 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009654 if (!msg)
9655 return;
9656
9657 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9658 if (!hdr) {
9659 nlmsg_free(msg);
9660 return;
9661 }
9662
David S. Miller9360ffd2012-03-29 04:41:26 -04009663 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9664 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9665 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9666 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9667 (key_id != -1 &&
9668 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9669 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9670 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009671
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009672 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009673
Johannes Berg463d0182009-07-14 00:33:35 +02009674 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9675 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009676 return;
9677
9678 nla_put_failure:
9679 genlmsg_cancel(msg, hdr);
9680 nlmsg_free(msg);
9681}
9682
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009683void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9684 struct ieee80211_channel *channel_before,
9685 struct ieee80211_channel *channel_after)
9686{
9687 struct sk_buff *msg;
9688 void *hdr;
9689 struct nlattr *nl_freq;
9690
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009691 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009692 if (!msg)
9693 return;
9694
9695 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9696 if (!hdr) {
9697 nlmsg_free(msg);
9698 return;
9699 }
9700
9701 /*
9702 * Since we are applying the beacon hint to a wiphy we know its
9703 * wiphy_idx is valid
9704 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009705 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9706 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009707
9708 /* Before */
9709 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9710 if (!nl_freq)
9711 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009712 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009713 goto nla_put_failure;
9714 nla_nest_end(msg, nl_freq);
9715
9716 /* After */
9717 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9718 if (!nl_freq)
9719 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009720 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009721 goto nla_put_failure;
9722 nla_nest_end(msg, nl_freq);
9723
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009724 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009725
Johannes Berg463d0182009-07-14 00:33:35 +02009726 rcu_read_lock();
9727 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9728 GFP_ATOMIC);
9729 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009730
9731 return;
9732
9733nla_put_failure:
9734 genlmsg_cancel(msg, hdr);
9735 nlmsg_free(msg);
9736}
9737
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009738static void nl80211_send_remain_on_chan_event(
9739 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009740 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009741 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009742 unsigned int duration, gfp_t gfp)
9743{
9744 struct sk_buff *msg;
9745 void *hdr;
9746
9747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9748 if (!msg)
9749 return;
9750
9751 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9752 if (!hdr) {
9753 nlmsg_free(msg);
9754 return;
9755 }
9756
David S. Miller9360ffd2012-03-29 04:41:26 -04009757 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009758 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9759 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009760 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009761 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009762 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9763 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009764 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9765 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009766
David S. Miller9360ffd2012-03-29 04:41:26 -04009767 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9768 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9769 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009770
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009771 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009772
9773 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9774 nl80211_mlme_mcgrp.id, gfp);
9775 return;
9776
9777 nla_put_failure:
9778 genlmsg_cancel(msg, hdr);
9779 nlmsg_free(msg);
9780}
9781
Johannes Berg947add32013-02-22 22:05:20 +01009782void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9783 struct ieee80211_channel *chan,
9784 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009785{
Johannes Berg947add32013-02-22 22:05:20 +01009786 struct wiphy *wiphy = wdev->wiphy;
9787 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9788
9789 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009790 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009791 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009792 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009793}
Johannes Berg947add32013-02-22 22:05:20 +01009794EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009795
Johannes Berg947add32013-02-22 22:05:20 +01009796void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9797 struct ieee80211_channel *chan,
9798 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009799{
Johannes Berg947add32013-02-22 22:05:20 +01009800 struct wiphy *wiphy = wdev->wiphy;
9801 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9802
9803 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009804 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009805 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009806}
Johannes Berg947add32013-02-22 22:05:20 +01009807EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009808
Johannes Berg947add32013-02-22 22:05:20 +01009809void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9810 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +01009811{
Johannes Berg947add32013-02-22 22:05:20 +01009812 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9813 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +01009814 struct sk_buff *msg;
9815
Johannes Berg947add32013-02-22 22:05:20 +01009816 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9817
Thomas Graf58050fc2012-06-28 03:57:45 +00009818 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009819 if (!msg)
9820 return;
9821
John W. Linville66266b32012-03-15 13:25:41 -04009822 if (nl80211_send_station(msg, 0, 0, 0,
9823 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009824 nlmsg_free(msg);
9825 return;
9826 }
9827
9828 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9829 nl80211_mlme_mcgrp.id, gfp);
9830}
Johannes Berg947add32013-02-22 22:05:20 +01009831EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +01009832
Johannes Berg947add32013-02-22 22:05:20 +01009833void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +02009834{
Johannes Berg947add32013-02-22 22:05:20 +01009835 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9836 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +02009837 struct sk_buff *msg;
9838 void *hdr;
9839
Johannes Berg947add32013-02-22 22:05:20 +01009840 trace_cfg80211_del_sta(dev, mac_addr);
9841
Thomas Graf58050fc2012-06-28 03:57:45 +00009842 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009843 if (!msg)
9844 return;
9845
9846 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9847 if (!hdr) {
9848 nlmsg_free(msg);
9849 return;
9850 }
9851
David S. Miller9360ffd2012-03-29 04:41:26 -04009852 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9853 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9854 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009855
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009856 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009857
9858 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9859 nl80211_mlme_mcgrp.id, gfp);
9860 return;
9861
9862 nla_put_failure:
9863 genlmsg_cancel(msg, hdr);
9864 nlmsg_free(msg);
9865}
Johannes Berg947add32013-02-22 22:05:20 +01009866EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +02009867
Johannes Berg947add32013-02-22 22:05:20 +01009868void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9869 enum nl80211_connect_failed_reason reason,
9870 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309871{
Johannes Berg947add32013-02-22 22:05:20 +01009872 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9873 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309874 struct sk_buff *msg;
9875 void *hdr;
9876
9877 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9878 if (!msg)
9879 return;
9880
9881 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9882 if (!hdr) {
9883 nlmsg_free(msg);
9884 return;
9885 }
9886
9887 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9888 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9889 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9890 goto nla_put_failure;
9891
9892 genlmsg_end(msg, hdr);
9893
9894 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9895 nl80211_mlme_mcgrp.id, gfp);
9896 return;
9897
9898 nla_put_failure:
9899 genlmsg_cancel(msg, hdr);
9900 nlmsg_free(msg);
9901}
Johannes Berg947add32013-02-22 22:05:20 +01009902EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309903
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009904static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9905 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009906{
9907 struct wireless_dev *wdev = dev->ieee80211_ptr;
9908 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9909 struct sk_buff *msg;
9910 void *hdr;
9911 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009912 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009913
Eric W. Biederman15e47302012-09-07 20:12:54 +00009914 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009915 return false;
9916
9917 msg = nlmsg_new(100, gfp);
9918 if (!msg)
9919 return true;
9920
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009921 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009922 if (!hdr) {
9923 nlmsg_free(msg);
9924 return true;
9925 }
9926
David S. Miller9360ffd2012-03-29 04:41:26 -04009927 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9928 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9929 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9930 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009931
9932 err = genlmsg_end(msg, hdr);
9933 if (err < 0) {
9934 nlmsg_free(msg);
9935 return true;
9936 }
9937
Eric W. Biederman15e47302012-09-07 20:12:54 +00009938 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009939 return true;
9940
9941 nla_put_failure:
9942 genlmsg_cancel(msg, hdr);
9943 nlmsg_free(msg);
9944 return true;
9945}
9946
Johannes Berg947add32013-02-22 22:05:20 +01009947bool cfg80211_rx_spurious_frame(struct net_device *dev,
9948 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009949{
Johannes Berg947add32013-02-22 22:05:20 +01009950 struct wireless_dev *wdev = dev->ieee80211_ptr;
9951 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009952
Johannes Berg947add32013-02-22 22:05:20 +01009953 trace_cfg80211_rx_spurious_frame(dev, addr);
9954
9955 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9956 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9957 trace_cfg80211_return_bool(false);
9958 return false;
9959 }
9960 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9961 addr, gfp);
9962 trace_cfg80211_return_bool(ret);
9963 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009964}
Johannes Berg947add32013-02-22 22:05:20 +01009965EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
9966
9967bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9968 const u8 *addr, gfp_t gfp)
9969{
9970 struct wireless_dev *wdev = dev->ieee80211_ptr;
9971 bool ret;
9972
9973 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9974
9975 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9976 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9977 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9978 trace_cfg80211_return_bool(false);
9979 return false;
9980 }
9981 ret = __nl80211_unexpected_frame(dev,
9982 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9983 addr, gfp);
9984 trace_cfg80211_return_bool(ret);
9985 return ret;
9986}
9987EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009988
Johannes Berg2e161f72010-08-12 15:38:38 +02009989int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009990 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009991 int freq, int sig_dbm,
9992 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009993{
Johannes Berg71bbc992012-06-15 15:30:18 +02009994 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009995 struct sk_buff *msg;
9996 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009997
9998 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9999 if (!msg)
10000 return -ENOMEM;
10001
Johannes Berg2e161f72010-08-12 15:38:38 +020010002 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010003 if (!hdr) {
10004 nlmsg_free(msg);
10005 return -ENOMEM;
10006 }
10007
David S. Miller9360ffd2012-03-29 04:41:26 -040010008 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010009 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10010 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010011 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10012 (sig_dbm &&
10013 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10014 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10015 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010016
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010017 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010018
Eric W. Biederman15e47302012-09-07 20:12:54 +000010019 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010020
10021 nla_put_failure:
10022 genlmsg_cancel(msg, hdr);
10023 nlmsg_free(msg);
10024 return -ENOBUFS;
10025}
10026
Johannes Berg947add32013-02-22 22:05:20 +010010027void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10028 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010029{
Johannes Berg947add32013-02-22 22:05:20 +010010030 struct wiphy *wiphy = wdev->wiphy;
10031 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010032 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010033 struct sk_buff *msg;
10034 void *hdr;
10035
Johannes Berg947add32013-02-22 22:05:20 +010010036 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10037
Jouni Malinen026331c2010-02-15 12:53:10 +020010038 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10039 if (!msg)
10040 return;
10041
Johannes Berg2e161f72010-08-12 15:38:38 +020010042 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010043 if (!hdr) {
10044 nlmsg_free(msg);
10045 return;
10046 }
10047
David S. Miller9360ffd2012-03-29 04:41:26 -040010048 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010049 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10050 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010051 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10052 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10053 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10054 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010055
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010056 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010057
10058 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
10059 return;
10060
10061 nla_put_failure:
10062 genlmsg_cancel(msg, hdr);
10063 nlmsg_free(msg);
10064}
Johannes Berg947add32013-02-22 22:05:20 +010010065EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020010066
Johannes Berg947add32013-02-22 22:05:20 +010010067void cfg80211_cqm_rssi_notify(struct net_device *dev,
10068 enum nl80211_cqm_rssi_threshold_event rssi_event,
10069 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010070{
Johannes Berg947add32013-02-22 22:05:20 +010010071 struct wireless_dev *wdev = dev->ieee80211_ptr;
10072 struct wiphy *wiphy = wdev->wiphy;
10073 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010074 struct sk_buff *msg;
10075 struct nlattr *pinfoattr;
10076 void *hdr;
10077
Johannes Berg947add32013-02-22 22:05:20 +010010078 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10079
Thomas Graf58050fc2012-06-28 03:57:45 +000010080 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010081 if (!msg)
10082 return;
10083
10084 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10085 if (!hdr) {
10086 nlmsg_free(msg);
10087 return;
10088 }
10089
David S. Miller9360ffd2012-03-29 04:41:26 -040010090 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010091 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010092 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010093
10094 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10095 if (!pinfoattr)
10096 goto nla_put_failure;
10097
David S. Miller9360ffd2012-03-29 04:41:26 -040010098 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10099 rssi_event))
10100 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010101
10102 nla_nest_end(msg, pinfoattr);
10103
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010104 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010105
10106 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10107 nl80211_mlme_mcgrp.id, gfp);
10108 return;
10109
10110 nla_put_failure:
10111 genlmsg_cancel(msg, hdr);
10112 nlmsg_free(msg);
10113}
Johannes Berg947add32013-02-22 22:05:20 +010010114EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010115
Johannes Berg947add32013-02-22 22:05:20 +010010116static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10117 struct net_device *netdev, const u8 *bssid,
10118 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010119{
10120 struct sk_buff *msg;
10121 struct nlattr *rekey_attr;
10122 void *hdr;
10123
Thomas Graf58050fc2012-06-28 03:57:45 +000010124 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010125 if (!msg)
10126 return;
10127
10128 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10129 if (!hdr) {
10130 nlmsg_free(msg);
10131 return;
10132 }
10133
David S. Miller9360ffd2012-03-29 04:41:26 -040010134 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10135 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10136 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10137 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010138
10139 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10140 if (!rekey_attr)
10141 goto nla_put_failure;
10142
David S. Miller9360ffd2012-03-29 04:41:26 -040010143 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10144 NL80211_REPLAY_CTR_LEN, replay_ctr))
10145 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010146
10147 nla_nest_end(msg, rekey_attr);
10148
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010149 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010150
10151 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10152 nl80211_mlme_mcgrp.id, gfp);
10153 return;
10154
10155 nla_put_failure:
10156 genlmsg_cancel(msg, hdr);
10157 nlmsg_free(msg);
10158}
10159
Johannes Berg947add32013-02-22 22:05:20 +010010160void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10161 const u8 *replay_ctr, gfp_t gfp)
10162{
10163 struct wireless_dev *wdev = dev->ieee80211_ptr;
10164 struct wiphy *wiphy = wdev->wiphy;
10165 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10166
10167 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10168 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10169}
10170EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10171
10172static void
10173nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10174 struct net_device *netdev, int index,
10175 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010176{
10177 struct sk_buff *msg;
10178 struct nlattr *attr;
10179 void *hdr;
10180
Thomas Graf58050fc2012-06-28 03:57:45 +000010181 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010182 if (!msg)
10183 return;
10184
10185 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10186 if (!hdr) {
10187 nlmsg_free(msg);
10188 return;
10189 }
10190
David S. Miller9360ffd2012-03-29 04:41:26 -040010191 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10192 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10193 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010194
10195 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10196 if (!attr)
10197 goto nla_put_failure;
10198
David S. Miller9360ffd2012-03-29 04:41:26 -040010199 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10200 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10201 (preauth &&
10202 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10203 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010204
10205 nla_nest_end(msg, attr);
10206
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010207 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010208
10209 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10210 nl80211_mlme_mcgrp.id, gfp);
10211 return;
10212
10213 nla_put_failure:
10214 genlmsg_cancel(msg, hdr);
10215 nlmsg_free(msg);
10216}
10217
Johannes Berg947add32013-02-22 22:05:20 +010010218void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10219 const u8 *bssid, bool preauth, gfp_t gfp)
10220{
10221 struct wireless_dev *wdev = dev->ieee80211_ptr;
10222 struct wiphy *wiphy = wdev->wiphy;
10223 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10224
10225 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10226 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10227}
10228EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10229
10230static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10231 struct net_device *netdev,
10232 struct cfg80211_chan_def *chandef,
10233 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010234{
10235 struct sk_buff *msg;
10236 void *hdr;
10237
Thomas Graf58050fc2012-06-28 03:57:45 +000010238 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010239 if (!msg)
10240 return;
10241
10242 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10243 if (!hdr) {
10244 nlmsg_free(msg);
10245 return;
10246 }
10247
Johannes Berg683b6d32012-11-08 21:25:48 +010010248 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10249 goto nla_put_failure;
10250
10251 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010252 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010253
10254 genlmsg_end(msg, hdr);
10255
10256 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10257 nl80211_mlme_mcgrp.id, gfp);
10258 return;
10259
10260 nla_put_failure:
10261 genlmsg_cancel(msg, hdr);
10262 nlmsg_free(msg);
10263}
10264
Johannes Berg947add32013-02-22 22:05:20 +010010265void cfg80211_ch_switch_notify(struct net_device *dev,
10266 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010267{
Johannes Berg947add32013-02-22 22:05:20 +010010268 struct wireless_dev *wdev = dev->ieee80211_ptr;
10269 struct wiphy *wiphy = wdev->wiphy;
10270 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10271
10272 trace_cfg80211_ch_switch_notify(dev, chandef);
10273
10274 wdev_lock(wdev);
10275
10276 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10277 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10278 goto out;
10279
10280 wdev->channel = chandef->chan;
10281 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10282out:
10283 wdev_unlock(wdev);
10284 return;
10285}
10286EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10287
10288void cfg80211_cqm_txe_notify(struct net_device *dev,
10289 const u8 *peer, u32 num_packets,
10290 u32 rate, u32 intvl, gfp_t gfp)
10291{
10292 struct wireless_dev *wdev = dev->ieee80211_ptr;
10293 struct wiphy *wiphy = wdev->wiphy;
10294 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010295 struct sk_buff *msg;
10296 struct nlattr *pinfoattr;
10297 void *hdr;
10298
10299 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10300 if (!msg)
10301 return;
10302
10303 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10304 if (!hdr) {
10305 nlmsg_free(msg);
10306 return;
10307 }
10308
10309 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010310 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010311 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10312 goto nla_put_failure;
10313
10314 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10315 if (!pinfoattr)
10316 goto nla_put_failure;
10317
10318 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10319 goto nla_put_failure;
10320
10321 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10322 goto nla_put_failure;
10323
10324 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10325 goto nla_put_failure;
10326
10327 nla_nest_end(msg, pinfoattr);
10328
10329 genlmsg_end(msg, hdr);
10330
10331 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10332 nl80211_mlme_mcgrp.id, gfp);
10333 return;
10334
10335 nla_put_failure:
10336 genlmsg_cancel(msg, hdr);
10337 nlmsg_free(msg);
10338}
Johannes Berg947add32013-02-22 22:05:20 +010010339EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010340
10341void
Simon Wunderlich04f39042013-02-08 18:16:19 +010010342nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10343 struct cfg80211_chan_def *chandef,
10344 enum nl80211_radar_event event,
10345 struct net_device *netdev, gfp_t gfp)
10346{
10347 struct sk_buff *msg;
10348 void *hdr;
10349
10350 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10351 if (!msg)
10352 return;
10353
10354 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10355 if (!hdr) {
10356 nlmsg_free(msg);
10357 return;
10358 }
10359
10360 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10361 goto nla_put_failure;
10362
10363 /* NOP and radar events don't need a netdev parameter */
10364 if (netdev) {
10365 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10366
10367 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10368 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10369 goto nla_put_failure;
10370 }
10371
10372 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10373 goto nla_put_failure;
10374
10375 if (nl80211_send_chandef(msg, chandef))
10376 goto nla_put_failure;
10377
10378 if (genlmsg_end(msg, hdr) < 0) {
10379 nlmsg_free(msg);
10380 return;
10381 }
10382
10383 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10384 nl80211_mlme_mcgrp.id, gfp);
10385 return;
10386
10387 nla_put_failure:
10388 genlmsg_cancel(msg, hdr);
10389 nlmsg_free(msg);
10390}
10391
Johannes Berg947add32013-02-22 22:05:20 +010010392void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10393 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010010394{
Johannes Berg947add32013-02-22 22:05:20 +010010395 struct wireless_dev *wdev = dev->ieee80211_ptr;
10396 struct wiphy *wiphy = wdev->wiphy;
10397 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010398 struct sk_buff *msg;
10399 struct nlattr *pinfoattr;
10400 void *hdr;
10401
Johannes Berg947add32013-02-22 22:05:20 +010010402 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10403
Thomas Graf58050fc2012-06-28 03:57:45 +000010404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010405 if (!msg)
10406 return;
10407
10408 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10409 if (!hdr) {
10410 nlmsg_free(msg);
10411 return;
10412 }
10413
David S. Miller9360ffd2012-03-29 04:41:26 -040010414 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010415 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010416 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10417 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010418
10419 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10420 if (!pinfoattr)
10421 goto nla_put_failure;
10422
David S. Miller9360ffd2012-03-29 04:41:26 -040010423 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10424 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010425
10426 nla_nest_end(msg, pinfoattr);
10427
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010428 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010429
10430 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10431 nl80211_mlme_mcgrp.id, gfp);
10432 return;
10433
10434 nla_put_failure:
10435 genlmsg_cancel(msg, hdr);
10436 nlmsg_free(msg);
10437}
Johannes Berg947add32013-02-22 22:05:20 +010010438EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010439
Johannes Berg7f6cf312011-11-04 11:18:15 +010010440void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10441 u64 cookie, bool acked, gfp_t gfp)
10442{
10443 struct wireless_dev *wdev = dev->ieee80211_ptr;
10444 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10445 struct sk_buff *msg;
10446 void *hdr;
10447 int err;
10448
Beni Lev4ee3e062012-08-27 12:49:39 +030010449 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10450
Thomas Graf58050fc2012-06-28 03:57:45 +000010451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010452
Johannes Berg7f6cf312011-11-04 11:18:15 +010010453 if (!msg)
10454 return;
10455
10456 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10457 if (!hdr) {
10458 nlmsg_free(msg);
10459 return;
10460 }
10461
David S. Miller9360ffd2012-03-29 04:41:26 -040010462 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10463 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10464 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10465 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10466 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10467 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010468
10469 err = genlmsg_end(msg, hdr);
10470 if (err < 0) {
10471 nlmsg_free(msg);
10472 return;
10473 }
10474
10475 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10476 nl80211_mlme_mcgrp.id, gfp);
10477 return;
10478
10479 nla_put_failure:
10480 genlmsg_cancel(msg, hdr);
10481 nlmsg_free(msg);
10482}
10483EXPORT_SYMBOL(cfg80211_probe_status);
10484
Johannes Berg5e760232011-11-04 11:18:17 +010010485void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10486 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010487 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010010488{
10489 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10490 struct sk_buff *msg;
10491 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010492 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010010493
Beni Lev4ee3e062012-08-27 12:49:39 +030010494 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10495
Ben Greear37c73b52012-10-26 14:49:25 -070010496 spin_lock_bh(&rdev->beacon_registrations_lock);
10497 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10498 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10499 if (!msg) {
10500 spin_unlock_bh(&rdev->beacon_registrations_lock);
10501 return;
10502 }
Johannes Berg5e760232011-11-04 11:18:17 +010010503
Ben Greear37c73b52012-10-26 14:49:25 -070010504 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10505 if (!hdr)
10506 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010010507
Ben Greear37c73b52012-10-26 14:49:25 -070010508 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10509 (freq &&
10510 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10511 (sig_dbm &&
10512 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10513 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10514 goto nla_put_failure;
10515
10516 genlmsg_end(msg, hdr);
10517
10518 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010010519 }
Ben Greear37c73b52012-10-26 14:49:25 -070010520 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010521 return;
10522
10523 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010524 spin_unlock_bh(&rdev->beacon_registrations_lock);
10525 if (hdr)
10526 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010010527 nlmsg_free(msg);
10528}
10529EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10530
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010531#ifdef CONFIG_PM
10532void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10533 struct cfg80211_wowlan_wakeup *wakeup,
10534 gfp_t gfp)
10535{
10536 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10537 struct sk_buff *msg;
10538 void *hdr;
10539 int err, size = 200;
10540
10541 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10542
10543 if (wakeup)
10544 size += wakeup->packet_present_len;
10545
10546 msg = nlmsg_new(size, gfp);
10547 if (!msg)
10548 return;
10549
10550 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10551 if (!hdr)
10552 goto free_msg;
10553
10554 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10555 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10556 goto free_msg;
10557
10558 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10559 wdev->netdev->ifindex))
10560 goto free_msg;
10561
10562 if (wakeup) {
10563 struct nlattr *reasons;
10564
10565 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10566
10567 if (wakeup->disconnect &&
10568 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10569 goto free_msg;
10570 if (wakeup->magic_pkt &&
10571 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10572 goto free_msg;
10573 if (wakeup->gtk_rekey_failure &&
10574 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10575 goto free_msg;
10576 if (wakeup->eap_identity_req &&
10577 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10578 goto free_msg;
10579 if (wakeup->four_way_handshake &&
10580 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10581 goto free_msg;
10582 if (wakeup->rfkill_release &&
10583 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10584 goto free_msg;
10585
10586 if (wakeup->pattern_idx >= 0 &&
10587 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10588 wakeup->pattern_idx))
10589 goto free_msg;
10590
Johannes Berg2a0e0472013-01-23 22:57:40 +010010591 if (wakeup->tcp_match)
10592 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10593
10594 if (wakeup->tcp_connlost)
10595 nla_put_flag(msg,
10596 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10597
10598 if (wakeup->tcp_nomoretokens)
10599 nla_put_flag(msg,
10600 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10601
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010602 if (wakeup->packet) {
10603 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10604 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10605
10606 if (!wakeup->packet_80211) {
10607 pkt_attr =
10608 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10609 len_attr =
10610 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10611 }
10612
10613 if (wakeup->packet_len &&
10614 nla_put_u32(msg, len_attr, wakeup->packet_len))
10615 goto free_msg;
10616
10617 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10618 wakeup->packet))
10619 goto free_msg;
10620 }
10621
10622 nla_nest_end(msg, reasons);
10623 }
10624
10625 err = genlmsg_end(msg, hdr);
10626 if (err < 0)
10627 goto free_msg;
10628
10629 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10630 nl80211_mlme_mcgrp.id, gfp);
10631 return;
10632
10633 free_msg:
10634 nlmsg_free(msg);
10635}
10636EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10637#endif
10638
Jouni Malinen3475b092012-11-16 22:49:57 +020010639void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10640 enum nl80211_tdls_operation oper,
10641 u16 reason_code, gfp_t gfp)
10642{
10643 struct wireless_dev *wdev = dev->ieee80211_ptr;
10644 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10645 struct sk_buff *msg;
10646 void *hdr;
10647 int err;
10648
10649 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10650 reason_code);
10651
10652 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10653 if (!msg)
10654 return;
10655
10656 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10657 if (!hdr) {
10658 nlmsg_free(msg);
10659 return;
10660 }
10661
10662 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10663 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10664 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10665 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10666 (reason_code > 0 &&
10667 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10668 goto nla_put_failure;
10669
10670 err = genlmsg_end(msg, hdr);
10671 if (err < 0) {
10672 nlmsg_free(msg);
10673 return;
10674 }
10675
10676 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10677 nl80211_mlme_mcgrp.id, gfp);
10678 return;
10679
10680 nla_put_failure:
10681 genlmsg_cancel(msg, hdr);
10682 nlmsg_free(msg);
10683}
10684EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10685
Jouni Malinen026331c2010-02-15 12:53:10 +020010686static int nl80211_netlink_notify(struct notifier_block * nb,
10687 unsigned long state,
10688 void *_notify)
10689{
10690 struct netlink_notify *notify = _notify;
10691 struct cfg80211_registered_device *rdev;
10692 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010693 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010694
10695 if (state != NETLINK_URELEASE)
10696 return NOTIFY_DONE;
10697
10698 rcu_read_lock();
10699
Johannes Berg5e760232011-11-04 11:18:17 +010010700 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010701 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010702 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010703
10704 spin_lock_bh(&rdev->beacon_registrations_lock);
10705 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10706 list) {
10707 if (reg->nlportid == notify->portid) {
10708 list_del(&reg->list);
10709 kfree(reg);
10710 break;
10711 }
10712 }
10713 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010714 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010715
10716 rcu_read_unlock();
10717
10718 return NOTIFY_DONE;
10719}
10720
10721static struct notifier_block nl80211_netlink_notifier = {
10722 .notifier_call = nl80211_netlink_notify,
10723};
10724
Jouni Malinen355199e2013-02-27 17:14:27 +020010725void cfg80211_ft_event(struct net_device *netdev,
10726 struct cfg80211_ft_event_params *ft_event)
10727{
10728 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10729 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10730 struct sk_buff *msg;
10731 void *hdr;
10732 int err;
10733
10734 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10735
10736 if (!ft_event->target_ap)
10737 return;
10738
10739 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10740 if (!msg)
10741 return;
10742
10743 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10744 if (!hdr) {
10745 nlmsg_free(msg);
10746 return;
10747 }
10748
10749 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10750 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10751 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10752 if (ft_event->ies)
10753 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10754 if (ft_event->ric_ies)
10755 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10756 ft_event->ric_ies);
10757
10758 err = genlmsg_end(msg, hdr);
10759 if (err < 0) {
10760 nlmsg_free(msg);
10761 return;
10762 }
10763
10764 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10765 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10766}
10767EXPORT_SYMBOL(cfg80211_ft_event);
10768
Arend van Spriel5de17982013-04-18 15:49:00 +020010769void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
10770{
10771 struct cfg80211_registered_device *rdev;
10772 struct sk_buff *msg;
10773 void *hdr;
10774 u32 nlportid;
10775
10776 rdev = wiphy_to_dev(wdev->wiphy);
10777 if (!rdev->crit_proto_nlportid)
10778 return;
10779
10780 nlportid = rdev->crit_proto_nlportid;
10781 rdev->crit_proto_nlportid = 0;
10782
10783 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10784 if (!msg)
10785 return;
10786
10787 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
10788 if (!hdr)
10789 goto nla_put_failure;
10790
10791 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10792 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10793 goto nla_put_failure;
10794
10795 genlmsg_end(msg, hdr);
10796
10797 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
10798 return;
10799
10800 nla_put_failure:
10801 if (hdr)
10802 genlmsg_cancel(msg, hdr);
10803 nlmsg_free(msg);
10804
10805}
10806EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
10807
Johannes Berg55682962007-09-20 13:09:35 -040010808/* initialisation/exit functions */
10809
10810int nl80211_init(void)
10811{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010812 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010813
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010814 err = genl_register_family_with_ops(&nl80211_fam,
10815 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010816 if (err)
10817 return err;
10818
Johannes Berg55682962007-09-20 13:09:35 -040010819 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10820 if (err)
10821 goto err_out;
10822
Johannes Berg2a519312009-02-10 21:25:55 +010010823 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10824 if (err)
10825 goto err_out;
10826
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010827 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10828 if (err)
10829 goto err_out;
10830
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010831 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10832 if (err)
10833 goto err_out;
10834
Johannes Bergaff89a92009-07-01 21:26:51 +020010835#ifdef CONFIG_NL80211_TESTMODE
10836 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10837 if (err)
10838 goto err_out;
10839#endif
10840
Jouni Malinen026331c2010-02-15 12:53:10 +020010841 err = netlink_register_notifier(&nl80211_netlink_notifier);
10842 if (err)
10843 goto err_out;
10844
Johannes Berg55682962007-09-20 13:09:35 -040010845 return 0;
10846 err_out:
10847 genl_unregister_family(&nl80211_fam);
10848 return err;
10849}
10850
10851void nl80211_exit(void)
10852{
Jouni Malinen026331c2010-02-15 12:53:10 +020010853 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010854 genl_unregister_family(&nl80211_fam);
10855}