blob: 35545ccc30fd0079acb1e848c4ecaff0cdc8b371 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg55682962007-09-20 13:09:35 -0400373};
374
Johannes Berge31b8212010-10-05 19:39:30 +0200375/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000376static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200377 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200378 [NL80211_KEY_IDX] = { .type = NLA_U8 },
379 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200380 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200381 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
382 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200383 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100384 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
385};
386
387/* policy for the key default flags */
388static const struct nla_policy
389nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
390 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
391 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200392};
393
Johannes Bergff1b6e62011-05-04 15:37:28 +0200394/* policy for WoWLAN attributes */
395static const struct nla_policy
396nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
397 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
398 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
400 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb132011-07-13 10:48:55 +0200401 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
402 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
403 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
404 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100405 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
406};
407
408static const struct nla_policy
409nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
410 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
411 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
412 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
413 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
414 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
415 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
416 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
417 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
418 },
419 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
420 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
421 },
422 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
423 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200425};
426
Johannes Berge5497d72011-07-05 16:35:40 +0200427/* policy for GTK rekey offload attributes */
428static const struct nla_policy
429nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
430 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
431 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
432 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
433};
434
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300435static const struct nla_policy
436nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200437 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300438 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700439 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300440};
441
Holger Schuriga0438972009-11-11 11:30:02 +0100442/* ifidx get helper */
443static int nl80211_get_ifidx(struct netlink_callback *cb)
444{
445 int res;
446
447 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
448 nl80211_fam.attrbuf, nl80211_fam.maxattr,
449 nl80211_policy);
450 if (res)
451 return res;
452
453 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
454 return -EINVAL;
455
456 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
457 if (!res)
458 return -EINVAL;
459 return res;
460}
461
Johannes Berg67748892010-10-04 21:14:06 +0200462static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
463 struct netlink_callback *cb,
464 struct cfg80211_registered_device **rdev,
465 struct net_device **dev)
466{
467 int ifidx = cb->args[0];
468 int err;
469
470 if (!ifidx)
471 ifidx = nl80211_get_ifidx(cb);
472 if (ifidx < 0)
473 return ifidx;
474
475 cb->args[0] = ifidx;
476
477 rtnl_lock();
478
479 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
480 if (!*dev) {
481 err = -ENODEV;
482 goto out_rtnl;
483 }
484
485 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100486 if (IS_ERR(*rdev)) {
487 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200488 goto out_rtnl;
489 }
490
491 return 0;
492 out_rtnl:
493 rtnl_unlock();
494 return err;
495}
496
497static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
498{
499 cfg80211_unlock_rdev(rdev);
500 rtnl_unlock();
501}
502
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100503/* IE validation */
504static bool is_valid_ie_attr(const struct nlattr *attr)
505{
506 const u8 *pos;
507 int len;
508
509 if (!attr)
510 return true;
511
512 pos = nla_data(attr);
513 len = nla_len(attr);
514
515 while (len) {
516 u8 elemlen;
517
518 if (len < 2)
519 return false;
520 len -= 2;
521
522 elemlen = pos[1];
523 if (elemlen > len)
524 return false;
525
526 len -= elemlen;
527 pos += 2 + elemlen;
528 }
529
530 return true;
531}
532
Johannes Berg55682962007-09-20 13:09:35 -0400533/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000534static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400535 int flags, u8 cmd)
536{
537 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000538 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400539}
540
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400541static int nl80211_msg_put_channel(struct sk_buff *msg,
542 struct ieee80211_channel *chan)
543{
David S. Miller9360ffd2012-03-29 04:41:26 -0400544 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
545 chan->center_freq))
546 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400547
David S. Miller9360ffd2012-03-29 04:41:26 -0400548 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
549 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
550 goto nla_put_failure;
551 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
552 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
553 goto nla_put_failure;
554 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
555 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
556 goto nla_put_failure;
Simon Wunderlich04f39042013-02-08 18:16:19 +0100557 if (chan->flags & IEEE80211_CHAN_RADAR) {
558 u32 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
559 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
560 goto nla_put_failure;
561 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
562 chan->dfs_state))
563 goto nla_put_failure;
564 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME, time))
565 goto nla_put_failure;
566 }
Johannes Berg50640f12012-12-12 17:59:39 +0100567 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
568 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
569 goto nla_put_failure;
570 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
571 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
572 goto nla_put_failure;
573 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
574 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
575 goto nla_put_failure;
576 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
577 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
578 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400579
David S. Miller9360ffd2012-03-29 04:41:26 -0400580 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
581 DBM_TO_MBM(chan->max_power)))
582 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400583
584 return 0;
585
586 nla_put_failure:
587 return -ENOBUFS;
588}
589
Johannes Berg55682962007-09-20 13:09:35 -0400590/* netlink command implementations */
591
Johannes Bergb9454e82009-07-08 13:29:08 +0200592struct key_parse {
593 struct key_params p;
594 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200595 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200596 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100597 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200598};
599
600static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
601{
602 struct nlattr *tb[NL80211_KEY_MAX + 1];
603 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
604 nl80211_key_policy);
605 if (err)
606 return err;
607
608 k->def = !!tb[NL80211_KEY_DEFAULT];
609 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
610
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100611 if (k->def) {
612 k->def_uni = true;
613 k->def_multi = true;
614 }
615 if (k->defmgmt)
616 k->def_multi = true;
617
Johannes Bergb9454e82009-07-08 13:29:08 +0200618 if (tb[NL80211_KEY_IDX])
619 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
620
621 if (tb[NL80211_KEY_DATA]) {
622 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
623 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
624 }
625
626 if (tb[NL80211_KEY_SEQ]) {
627 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
628 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
629 }
630
631 if (tb[NL80211_KEY_CIPHER])
632 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
633
Johannes Berge31b8212010-10-05 19:39:30 +0200634 if (tb[NL80211_KEY_TYPE]) {
635 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
636 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
637 return -EINVAL;
638 }
639
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100640 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
641 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100642 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
643 tb[NL80211_KEY_DEFAULT_TYPES],
644 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100645 if (err)
646 return err;
647
648 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
649 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
650 }
651
Johannes Bergb9454e82009-07-08 13:29:08 +0200652 return 0;
653}
654
655static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
656{
657 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
658 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
659 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
660 }
661
662 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
663 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
664 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
665 }
666
667 if (info->attrs[NL80211_ATTR_KEY_IDX])
668 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
669
670 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
671 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
672
673 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
674 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
675
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100676 if (k->def) {
677 k->def_uni = true;
678 k->def_multi = true;
679 }
680 if (k->defmgmt)
681 k->def_multi = true;
682
Johannes Berge31b8212010-10-05 19:39:30 +0200683 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
684 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
685 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
686 return -EINVAL;
687 }
688
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100689 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
690 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
691 int err = nla_parse_nested(
692 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
693 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
694 nl80211_key_default_policy);
695 if (err)
696 return err;
697
698 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
699 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
700 }
701
Johannes Bergb9454e82009-07-08 13:29:08 +0200702 return 0;
703}
704
705static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
706{
707 int err;
708
709 memset(k, 0, sizeof(*k));
710 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200711 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200712
713 if (info->attrs[NL80211_ATTR_KEY])
714 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
715 else
716 err = nl80211_parse_key_old(info, k);
717
718 if (err)
719 return err;
720
721 if (k->def && k->defmgmt)
722 return -EINVAL;
723
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100724 if (k->defmgmt) {
725 if (k->def_uni || !k->def_multi)
726 return -EINVAL;
727 }
728
Johannes Bergb9454e82009-07-08 13:29:08 +0200729 if (k->idx != -1) {
730 if (k->defmgmt) {
731 if (k->idx < 4 || k->idx > 5)
732 return -EINVAL;
733 } else if (k->def) {
734 if (k->idx < 0 || k->idx > 3)
735 return -EINVAL;
736 } else {
737 if (k->idx < 0 || k->idx > 5)
738 return -EINVAL;
739 }
740 }
741
742 return 0;
743}
744
Johannes Bergfffd0932009-07-08 14:22:54 +0200745static struct cfg80211_cached_keys *
746nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530747 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200748{
749 struct key_parse parse;
750 struct nlattr *key;
751 struct cfg80211_cached_keys *result;
752 int rem, err, def = 0;
753
754 result = kzalloc(sizeof(*result), GFP_KERNEL);
755 if (!result)
756 return ERR_PTR(-ENOMEM);
757
758 result->def = -1;
759 result->defmgmt = -1;
760
761 nla_for_each_nested(key, keys, rem) {
762 memset(&parse, 0, sizeof(parse));
763 parse.idx = -1;
764
765 err = nl80211_parse_key_new(key, &parse);
766 if (err)
767 goto error;
768 err = -EINVAL;
769 if (!parse.p.key)
770 goto error;
771 if (parse.idx < 0 || parse.idx > 4)
772 goto error;
773 if (parse.def) {
774 if (def)
775 goto error;
776 def = 1;
777 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100778 if (!parse.def_uni || !parse.def_multi)
779 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200780 } else if (parse.defmgmt)
781 goto error;
782 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200783 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200784 if (err)
785 goto error;
786 result->params[parse.idx].cipher = parse.p.cipher;
787 result->params[parse.idx].key_len = parse.p.key_len;
788 result->params[parse.idx].key = result->data[parse.idx];
789 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530790
791 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
792 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
793 if (no_ht)
794 *no_ht = true;
795 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200796 }
797
798 return result;
799 error:
800 kfree(result);
801 return ERR_PTR(err);
802}
803
804static int nl80211_key_allowed(struct wireless_dev *wdev)
805{
806 ASSERT_WDEV_LOCK(wdev);
807
Johannes Bergfffd0932009-07-08 14:22:54 +0200808 switch (wdev->iftype) {
809 case NL80211_IFTYPE_AP:
810 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200811 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700812 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200813 break;
814 case NL80211_IFTYPE_ADHOC:
815 if (!wdev->current_bss)
816 return -ENOLINK;
817 break;
818 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200819 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200820 if (wdev->sme_state != CFG80211_SME_CONNECTED)
821 return -ENOLINK;
822 break;
823 default:
824 return -EINVAL;
825 }
826
827 return 0;
828}
829
Johannes Berg7527a782011-05-13 10:58:57 +0200830static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
831{
832 struct nlattr *nl_modes = nla_nest_start(msg, attr);
833 int i;
834
835 if (!nl_modes)
836 goto nla_put_failure;
837
838 i = 0;
839 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400840 if ((ifmodes & 1) && nla_put_flag(msg, i))
841 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200842 ifmodes >>= 1;
843 i++;
844 }
845
846 nla_nest_end(msg, nl_modes);
847 return 0;
848
849nla_put_failure:
850 return -ENOBUFS;
851}
852
853static int nl80211_put_iface_combinations(struct wiphy *wiphy,
854 struct sk_buff *msg)
855{
856 struct nlattr *nl_combis;
857 int i, j;
858
859 nl_combis = nla_nest_start(msg,
860 NL80211_ATTR_INTERFACE_COMBINATIONS);
861 if (!nl_combis)
862 goto nla_put_failure;
863
864 for (i = 0; i < wiphy->n_iface_combinations; i++) {
865 const struct ieee80211_iface_combination *c;
866 struct nlattr *nl_combi, *nl_limits;
867
868 c = &wiphy->iface_combinations[i];
869
870 nl_combi = nla_nest_start(msg, i + 1);
871 if (!nl_combi)
872 goto nla_put_failure;
873
874 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
875 if (!nl_limits)
876 goto nla_put_failure;
877
878 for (j = 0; j < c->n_limits; j++) {
879 struct nlattr *nl_limit;
880
881 nl_limit = nla_nest_start(msg, j + 1);
882 if (!nl_limit)
883 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400884 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
885 c->limits[j].max))
886 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200887 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
888 c->limits[j].types))
889 goto nla_put_failure;
890 nla_nest_end(msg, nl_limit);
891 }
892
893 nla_nest_end(msg, nl_limits);
894
David S. Miller9360ffd2012-03-29 04:41:26 -0400895 if (c->beacon_int_infra_match &&
896 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
897 goto nla_put_failure;
898 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
899 c->num_different_channels) ||
900 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
901 c->max_interfaces))
902 goto nla_put_failure;
Simon Wunderlich11c4a072013-01-08 14:04:07 +0100903 if (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
904 c->radar_detect_widths))
905 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200906
907 nla_nest_end(msg, nl_combi);
908 }
909
910 nla_nest_end(msg, nl_combis);
911
912 return 0;
913nla_put_failure:
914 return -ENOBUFS;
915}
916
Johannes Berg2a0e0472013-01-23 22:57:40 +0100917#ifdef CONFIG_PM
918static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
919 struct sk_buff *msg)
920{
921 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
922 struct nlattr *nl_tcp;
923
924 if (!tcp)
925 return 0;
926
927 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
928 if (!nl_tcp)
929 return -ENOBUFS;
930
931 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
932 tcp->data_payload_max))
933 return -ENOBUFS;
934
935 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
936 tcp->data_payload_max))
937 return -ENOBUFS;
938
939 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
940 return -ENOBUFS;
941
942 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
943 sizeof(*tcp->tok), tcp->tok))
944 return -ENOBUFS;
945
946 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
947 tcp->data_interval_max))
948 return -ENOBUFS;
949
950 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
951 tcp->wake_payload_max))
952 return -ENOBUFS;
953
954 nla_nest_end(msg, nl_tcp);
955 return 0;
956}
957#endif
958
Eric W. Biederman15e47302012-09-07 20:12:54 +0000959static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400960 struct cfg80211_registered_device *dev)
961{
962 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100963 struct nlattr *nl_bands, *nl_band;
964 struct nlattr *nl_freqs, *nl_freq;
965 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100966 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100967 enum ieee80211_band band;
968 struct ieee80211_channel *chan;
969 struct ieee80211_rate *rate;
970 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200971 const struct ieee80211_txrx_stypes *mgmt_stypes =
972 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400973
Eric W. Biederman15e47302012-09-07 20:12:54 +0000974 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400975 if (!hdr)
976 return -1;
977
David S. Miller9360ffd2012-03-29 04:41:26 -0400978 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
979 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
980 nla_put_u32(msg, NL80211_ATTR_GENERATION,
981 cfg80211_rdev_list_generation) ||
982 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
983 dev->wiphy.retry_short) ||
984 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
985 dev->wiphy.retry_long) ||
986 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
987 dev->wiphy.frag_threshold) ||
988 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
989 dev->wiphy.rts_threshold) ||
990 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
991 dev->wiphy.coverage_class) ||
992 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
993 dev->wiphy.max_scan_ssids) ||
994 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
995 dev->wiphy.max_sched_scan_ssids) ||
996 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
997 dev->wiphy.max_scan_ie_len) ||
998 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
999 dev->wiphy.max_sched_scan_ie_len) ||
1000 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1001 dev->wiphy.max_match_sets))
1002 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001003
David S. Miller9360ffd2012-03-29 04:41:26 -04001004 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1005 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1006 goto nla_put_failure;
1007 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1008 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1009 goto nla_put_failure;
1010 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1011 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1012 goto nla_put_failure;
1013 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1014 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1015 goto nla_put_failure;
1016 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1017 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1018 goto nla_put_failure;
1019 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1020 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1021 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001022
David S. Miller9360ffd2012-03-29 04:41:26 -04001023 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1024 sizeof(u32) * dev->wiphy.n_cipher_suites,
1025 dev->wiphy.cipher_suites))
1026 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001027
David S. Miller9360ffd2012-03-29 04:41:26 -04001028 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1029 dev->wiphy.max_num_pmkids))
1030 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +05301031
David S. Miller9360ffd2012-03-29 04:41:26 -04001032 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1033 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1034 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +02001035
David S. Miller9360ffd2012-03-29 04:41:26 -04001036 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1037 dev->wiphy.available_antennas_tx) ||
1038 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1039 dev->wiphy.available_antennas_rx))
1040 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001041
David S. Miller9360ffd2012-03-29 04:41:26 -04001042 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1043 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1044 dev->wiphy.probe_resp_offload))
1045 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +02001046
Bruno Randolf7f531e02010-12-16 11:30:22 +09001047 if ((dev->wiphy.available_antennas_tx ||
1048 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001049 u32 tx_ant = 0, rx_ant = 0;
1050 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +03001051 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001052 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001053 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
1054 tx_ant) ||
1055 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
1056 rx_ant))
1057 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001058 }
1059 }
1060
Johannes Berg7527a782011-05-13 10:58:57 +02001061 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1062 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001063 goto nla_put_failure;
1064
Johannes Bergee688b002008-01-24 19:38:39 +01001065 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1066 if (!nl_bands)
1067 goto nla_put_failure;
1068
1069 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1070 if (!dev->wiphy.bands[band])
1071 continue;
1072
1073 nl_band = nla_nest_start(msg, band);
1074 if (!nl_band)
1075 goto nla_put_failure;
1076
Johannes Bergd51626d2008-10-09 12:20:13 +02001077 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -04001078 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
1079 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1080 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
1081 &dev->wiphy.bands[band]->ht_cap.mcs) ||
1082 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1083 dev->wiphy.bands[band]->ht_cap.cap) ||
1084 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1085 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
1086 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1087 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
1088 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001089
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001090 /* add VHT info */
1091 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1092 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1093 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1094 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1095 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1096 dev->wiphy.bands[band]->vht_cap.cap)))
1097 goto nla_put_failure;
1098
Johannes Bergee688b002008-01-24 19:38:39 +01001099 /* add frequencies */
1100 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1101 if (!nl_freqs)
1102 goto nla_put_failure;
1103
1104 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1105 nl_freq = nla_nest_start(msg, i);
1106 if (!nl_freq)
1107 goto nla_put_failure;
1108
1109 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001110
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001111 if (nl80211_msg_put_channel(msg, chan))
1112 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001113
Johannes Bergee688b002008-01-24 19:38:39 +01001114 nla_nest_end(msg, nl_freq);
1115 }
1116
1117 nla_nest_end(msg, nl_freqs);
1118
1119 /* add bitrates */
1120 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1121 if (!nl_rates)
1122 goto nla_put_failure;
1123
1124 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1125 nl_rate = nla_nest_start(msg, i);
1126 if (!nl_rate)
1127 goto nla_put_failure;
1128
1129 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001130 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1131 rate->bitrate))
1132 goto nla_put_failure;
1133 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1134 nla_put_flag(msg,
1135 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1136 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001137
1138 nla_nest_end(msg, nl_rate);
1139 }
1140
1141 nla_nest_end(msg, nl_rates);
1142
1143 nla_nest_end(msg, nl_band);
1144 }
1145 nla_nest_end(msg, nl_bands);
1146
Johannes Berg8fdc6212009-03-14 09:34:01 +01001147 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1148 if (!nl_cmds)
1149 goto nla_put_failure;
1150
1151 i = 0;
1152#define CMD(op, n) \
1153 do { \
1154 if (dev->ops->op) { \
1155 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001156 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1157 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001158 } \
1159 } while (0)
1160
1161 CMD(add_virtual_intf, NEW_INTERFACE);
1162 CMD(change_virtual_intf, SET_INTERFACE);
1163 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001164 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001165 CMD(add_station, NEW_STATION);
1166 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001167 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001168 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001169 CMD(auth, AUTHENTICATE);
1170 CMD(assoc, ASSOCIATE);
1171 CMD(deauth, DEAUTHENTICATE);
1172 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001173 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001174 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001175 CMD(set_pmksa, SET_PMKSA);
1176 CMD(del_pmksa, DEL_PMKSA);
1177 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001178 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1179 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001180 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001181 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001182 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001183 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001184 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001185 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1186 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001187 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001188 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001189 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001190 i++;
1191 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1192 goto nla_put_failure;
1193 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001194 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001195 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1196 CMD(tdls_mgmt, TDLS_MGMT);
1197 CMD(tdls_oper, TDLS_OPER);
1198 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001199 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1200 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001201 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001202 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001203 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1204 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001205 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1206 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001207 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001208 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001209 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001210
Kalle Valo4745fc02011-11-17 19:06:10 +02001211#ifdef CONFIG_NL80211_TESTMODE
1212 CMD(testmode_cmd, TESTMODE);
1213#endif
1214
Johannes Berg8fdc6212009-03-14 09:34:01 +01001215#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001216
Johannes Berg6829c872009-07-02 09:13:27 +02001217 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001218 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001219 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1220 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001221 }
1222
Johannes Berg6829c872009-07-02 09:13:27 +02001223 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001224 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001225 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1226 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001227 }
1228
Johannes Berg8fdc6212009-03-14 09:34:01 +01001229 nla_nest_end(msg, nl_cmds);
1230
Johannes Berg7c4ef712011-11-18 15:33:48 +01001231 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001232 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1233 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1234 dev->wiphy.max_remain_on_channel_duration))
1235 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001236
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1238 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1239 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001240
Johannes Berg2e161f72010-08-12 15:38:38 +02001241 if (mgmt_stypes) {
1242 u16 stypes;
1243 struct nlattr *nl_ftypes, *nl_ifs;
1244 enum nl80211_iftype ift;
1245
1246 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1247 if (!nl_ifs)
1248 goto nla_put_failure;
1249
1250 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1251 nl_ftypes = nla_nest_start(msg, ift);
1252 if (!nl_ftypes)
1253 goto nla_put_failure;
1254 i = 0;
1255 stypes = mgmt_stypes[ift].tx;
1256 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001257 if ((stypes & 1) &&
1258 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1259 (i << 4) | IEEE80211_FTYPE_MGMT))
1260 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001261 stypes >>= 1;
1262 i++;
1263 }
1264 nla_nest_end(msg, nl_ftypes);
1265 }
1266
Johannes Berg74b70a42010-08-24 12:15:53 +02001267 nla_nest_end(msg, nl_ifs);
1268
Johannes Berg2e161f72010-08-12 15:38:38 +02001269 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1270 if (!nl_ifs)
1271 goto nla_put_failure;
1272
1273 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1274 nl_ftypes = nla_nest_start(msg, ift);
1275 if (!nl_ftypes)
1276 goto nla_put_failure;
1277 i = 0;
1278 stypes = mgmt_stypes[ift].rx;
1279 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001280 if ((stypes & 1) &&
1281 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1282 (i << 4) | IEEE80211_FTYPE_MGMT))
1283 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001284 stypes >>= 1;
1285 i++;
1286 }
1287 nla_nest_end(msg, nl_ftypes);
1288 }
1289 nla_nest_end(msg, nl_ifs);
1290 }
1291
Johannes Bergdfb89c52012-06-27 09:23:48 +02001292#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001293 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1294 struct nlattr *nl_wowlan;
1295
1296 nl_wowlan = nla_nest_start(msg,
1297 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1298 if (!nl_wowlan)
1299 goto nla_put_failure;
1300
David S. Miller9360ffd2012-03-29 04:41:26 -04001301 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1302 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1303 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1304 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1305 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1306 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1307 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1308 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1309 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1310 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1311 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1312 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1313 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1314 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1315 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1316 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1317 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001318 if (dev->wiphy.wowlan.n_patterns) {
1319 struct nl80211_wowlan_pattern_support pat = {
1320 .max_patterns = dev->wiphy.wowlan.n_patterns,
1321 .min_pattern_len =
1322 dev->wiphy.wowlan.pattern_min_len,
1323 .max_pattern_len =
1324 dev->wiphy.wowlan.pattern_max_len,
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08001325 .max_pkt_offset =
1326 dev->wiphy.wowlan.max_pkt_offset,
Johannes Bergff1b6e62011-05-04 15:37:28 +02001327 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001328 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1329 sizeof(pat), &pat))
1330 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001331 }
1332
Johannes Berg2a0e0472013-01-23 22:57:40 +01001333 if (nl80211_send_wowlan_tcp_caps(dev, msg))
1334 goto nla_put_failure;
1335
Johannes Bergff1b6e62011-05-04 15:37:28 +02001336 nla_nest_end(msg, nl_wowlan);
1337 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001338#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001339
Johannes Berg7527a782011-05-13 10:58:57 +02001340 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1341 dev->wiphy.software_iftypes))
1342 goto nla_put_failure;
1343
1344 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1345 goto nla_put_failure;
1346
David S. Miller9360ffd2012-03-29 04:41:26 -04001347 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1348 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1349 dev->wiphy.ap_sme_capa))
1350 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001351
David S. Miller9360ffd2012-03-29 04:41:26 -04001352 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1353 dev->wiphy.features))
1354 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001355
David S. Miller9360ffd2012-03-29 04:41:26 -04001356 if (dev->wiphy.ht_capa_mod_mask &&
1357 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1358 sizeof(*dev->wiphy.ht_capa_mod_mask),
1359 dev->wiphy.ht_capa_mod_mask))
1360 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001361
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05301362 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1363 dev->wiphy.max_acl_mac_addrs &&
1364 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1365 dev->wiphy.max_acl_mac_addrs))
1366 goto nla_put_failure;
1367
Johannes Berga50df0c42013-02-11 14:20:05 +01001368 if (dev->wiphy.extended_capabilities &&
1369 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1370 dev->wiphy.extended_capabilities_len,
1371 dev->wiphy.extended_capabilities) ||
1372 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1373 dev->wiphy.extended_capabilities_len,
1374 dev->wiphy.extended_capabilities_mask)))
1375 goto nla_put_failure;
1376
Johannes Berg55682962007-09-20 13:09:35 -04001377 return genlmsg_end(msg, hdr);
1378
1379 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001380 genlmsg_cancel(msg, hdr);
1381 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001382}
1383
1384static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1385{
1386 int idx = 0;
1387 int start = cb->args[0];
1388 struct cfg80211_registered_device *dev;
1389
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001390 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001391 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001392 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1393 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001394 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001395 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001396 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001397 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001398 dev) < 0) {
1399 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001400 break;
Julius Volzb4637272008-07-08 14:02:19 +02001401 }
Johannes Berg55682962007-09-20 13:09:35 -04001402 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001403 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001404
1405 cb->args[0] = idx;
1406
1407 return skb->len;
1408}
1409
1410static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1411{
1412 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001413 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001414
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001415 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001416 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001417 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001418
Eric W. Biederman15e47302012-09-07 20:12:54 +00001419 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001420 nlmsg_free(msg);
1421 return -ENOBUFS;
1422 }
Johannes Berg55682962007-09-20 13:09:35 -04001423
Johannes Berg134e6372009-07-10 09:51:34 +00001424 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001425}
1426
Jouni Malinen31888482008-10-30 16:59:24 +02001427static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1428 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1429 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1430 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1431 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1432 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1433};
1434
1435static int parse_txq_params(struct nlattr *tb[],
1436 struct ieee80211_txq_params *txq_params)
1437{
Johannes Berga3304b02012-03-28 11:04:24 +02001438 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001439 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1440 !tb[NL80211_TXQ_ATTR_AIFS])
1441 return -EINVAL;
1442
Johannes Berga3304b02012-03-28 11:04:24 +02001443 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001444 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1445 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1446 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1447 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1448
Johannes Berga3304b02012-03-28 11:04:24 +02001449 if (txq_params->ac >= NL80211_NUM_ACS)
1450 return -EINVAL;
1451
Jouni Malinen31888482008-10-30 16:59:24 +02001452 return 0;
1453}
1454
Johannes Bergf444de02010-05-05 15:25:02 +02001455static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1456{
1457 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001458 * You can only set the channel explicitly for WDS interfaces,
1459 * all others have their channel managed via their respective
1460 * "establish a connection" command (connect, join, ...)
1461 *
1462 * For AP/GO and mesh mode, the channel can be set with the
1463 * channel userspace API, but is only stored and passed to the
1464 * low-level driver when the AP starts or the mesh is joined.
1465 * This is for backward compatibility, userspace can also give
1466 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001467 *
1468 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001469 * whatever else is going on, so they have their own special
1470 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001471 */
1472 return !wdev ||
1473 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001474 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001475 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1476 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001477}
1478
Johannes Berg683b6d32012-11-08 21:25:48 +01001479static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1480 struct genl_info *info,
1481 struct cfg80211_chan_def *chandef)
1482{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301483 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001484
1485 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1486 return -EINVAL;
1487
1488 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1489
1490 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001491 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1492 chandef->center_freq1 = control_freq;
1493 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001494
1495 /* Primary channel not allowed */
1496 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1497 return -EINVAL;
1498
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001499 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1500 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001501
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001502 chantype = nla_get_u32(
1503 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1504
1505 switch (chantype) {
1506 case NL80211_CHAN_NO_HT:
1507 case NL80211_CHAN_HT20:
1508 case NL80211_CHAN_HT40PLUS:
1509 case NL80211_CHAN_HT40MINUS:
1510 cfg80211_chandef_create(chandef, chandef->chan,
1511 chantype);
1512 break;
1513 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001514 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001515 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001516 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1517 chandef->width =
1518 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1519 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1520 chandef->center_freq1 =
1521 nla_get_u32(
1522 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1523 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1524 chandef->center_freq2 =
1525 nla_get_u32(
1526 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1527 }
1528
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001529 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001530 return -EINVAL;
1531
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001532 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1533 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001534 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001535
Johannes Berg683b6d32012-11-08 21:25:48 +01001536 return 0;
1537}
1538
Johannes Bergf444de02010-05-05 15:25:02 +02001539static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1540 struct wireless_dev *wdev,
1541 struct genl_info *info)
1542{
Johannes Berg683b6d32012-11-08 21:25:48 +01001543 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001544 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001545 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1546
1547 if (wdev)
1548 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001549
Johannes Bergf444de02010-05-05 15:25:02 +02001550 if (!nl80211_can_set_dev_channel(wdev))
1551 return -EOPNOTSUPP;
1552
Johannes Berg683b6d32012-11-08 21:25:48 +01001553 result = nl80211_parse_chandef(rdev, info, &chandef);
1554 if (result)
1555 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001556
1557 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001558 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001559 case NL80211_IFTYPE_AP:
1560 case NL80211_IFTYPE_P2P_GO:
1561 if (wdev->beacon_interval) {
1562 result = -EBUSY;
1563 break;
1564 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001565 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001566 result = -EINVAL;
1567 break;
1568 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001569 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001570 result = 0;
1571 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001572 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001573 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001574 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001575 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001576 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001577 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001578 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001579 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001580 }
1581 mutex_unlock(&rdev->devlist_mtx);
1582
1583 return result;
1584}
1585
1586static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1587{
Johannes Berg4c476992010-10-04 21:36:35 +02001588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1589 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001590
Johannes Berg4c476992010-10-04 21:36:35 +02001591 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001592}
1593
Bill Jordane8347eb2010-10-01 13:54:28 -04001594static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1595{
Johannes Berg43b19952010-10-07 13:10:30 +02001596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1597 struct net_device *dev = info->user_ptr[1];
1598 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001599 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001600
1601 if (!info->attrs[NL80211_ATTR_MAC])
1602 return -EINVAL;
1603
Johannes Berg43b19952010-10-07 13:10:30 +02001604 if (netif_running(dev))
1605 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001606
Johannes Berg43b19952010-10-07 13:10:30 +02001607 if (!rdev->ops->set_wds_peer)
1608 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001609
Johannes Berg43b19952010-10-07 13:10:30 +02001610 if (wdev->iftype != NL80211_IFTYPE_WDS)
1611 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001612
1613 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001614 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001615}
1616
1617
Johannes Berg55682962007-09-20 13:09:35 -04001618static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1619{
1620 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001621 struct net_device *netdev = NULL;
1622 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001623 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001624 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001625 u32 changed;
1626 u8 retry_short = 0, retry_long = 0;
1627 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001628 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001629
Johannes Bergf444de02010-05-05 15:25:02 +02001630 /*
1631 * Try to find the wiphy and netdev. Normally this
1632 * function shouldn't need the netdev, but this is
1633 * done for backward compatibility -- previously
1634 * setting the channel was done per wiphy, but now
1635 * it is per netdev. Previous userland like hostapd
1636 * also passed a netdev to set_wiphy, so that it is
1637 * possible to let that go to the right netdev!
1638 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001639 mutex_lock(&cfg80211_mutex);
1640
Johannes Bergf444de02010-05-05 15:25:02 +02001641 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1642 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1643
1644 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1645 if (netdev && netdev->ieee80211_ptr) {
1646 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1647 mutex_lock(&rdev->mtx);
1648 } else
1649 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001650 }
1651
Johannes Bergf444de02010-05-05 15:25:02 +02001652 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001653 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1654 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001655 if (IS_ERR(rdev)) {
1656 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001657 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001658 }
1659 wdev = NULL;
1660 netdev = NULL;
1661 result = 0;
1662
1663 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001664 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001665 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001666
1667 /*
1668 * end workaround code, by now the rdev is available
1669 * and locked, and wdev may or may not be NULL.
1670 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001671
1672 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001673 result = cfg80211_dev_rename(
1674 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001675
1676 mutex_unlock(&cfg80211_mutex);
1677
1678 if (result)
1679 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001680
Jouni Malinen31888482008-10-30 16:59:24 +02001681 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1682 struct ieee80211_txq_params txq_params;
1683 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1684
1685 if (!rdev->ops->set_txq_params) {
1686 result = -EOPNOTSUPP;
1687 goto bad_res;
1688 }
1689
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001690 if (!netdev) {
1691 result = -EINVAL;
1692 goto bad_res;
1693 }
1694
Johannes Berg133a3ff2011-11-03 14:50:13 +01001695 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1696 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1697 result = -EINVAL;
1698 goto bad_res;
1699 }
1700
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001701 if (!netif_running(netdev)) {
1702 result = -ENETDOWN;
1703 goto bad_res;
1704 }
1705
Jouni Malinen31888482008-10-30 16:59:24 +02001706 nla_for_each_nested(nl_txq_params,
1707 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1708 rem_txq_params) {
1709 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1710 nla_data(nl_txq_params),
1711 nla_len(nl_txq_params),
1712 txq_params_policy);
1713 result = parse_txq_params(tb, &txq_params);
1714 if (result)
1715 goto bad_res;
1716
Hila Gonene35e4d22012-06-27 17:19:42 +03001717 result = rdev_set_txq_params(rdev, netdev,
1718 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001719 if (result)
1720 goto bad_res;
1721 }
1722 }
1723
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001724 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001725 result = __nl80211_set_channel(rdev,
1726 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1727 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001728 if (result)
1729 goto bad_res;
1730 }
1731
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001732 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001733 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001734 enum nl80211_tx_power_setting type;
1735 int idx, mbm = 0;
1736
Johannes Bergc8442112012-10-24 10:17:18 +02001737 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1738 txp_wdev = NULL;
1739
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001740 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001741 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001742 goto bad_res;
1743 }
1744
1745 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1746 type = nla_get_u32(info->attrs[idx]);
1747
1748 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1749 (type != NL80211_TX_POWER_AUTOMATIC)) {
1750 result = -EINVAL;
1751 goto bad_res;
1752 }
1753
1754 if (type != NL80211_TX_POWER_AUTOMATIC) {
1755 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1756 mbm = nla_get_u32(info->attrs[idx]);
1757 }
1758
Johannes Bergc8442112012-10-24 10:17:18 +02001759 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001760 if (result)
1761 goto bad_res;
1762 }
1763
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001764 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1765 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1766 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001767 if ((!rdev->wiphy.available_antennas_tx &&
1768 !rdev->wiphy.available_antennas_rx) ||
1769 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001770 result = -EOPNOTSUPP;
1771 goto bad_res;
1772 }
1773
1774 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1775 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1776
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001777 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001778 * available antenna masks, except for the "all" mask */
1779 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1780 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001781 result = -EINVAL;
1782 goto bad_res;
1783 }
1784
Bruno Randolf7f531e02010-12-16 11:30:22 +09001785 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1786 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001787
Hila Gonene35e4d22012-06-27 17:19:42 +03001788 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001789 if (result)
1790 goto bad_res;
1791 }
1792
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001793 changed = 0;
1794
1795 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1796 retry_short = nla_get_u8(
1797 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1798 if (retry_short == 0) {
1799 result = -EINVAL;
1800 goto bad_res;
1801 }
1802 changed |= WIPHY_PARAM_RETRY_SHORT;
1803 }
1804
1805 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1806 retry_long = nla_get_u8(
1807 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1808 if (retry_long == 0) {
1809 result = -EINVAL;
1810 goto bad_res;
1811 }
1812 changed |= WIPHY_PARAM_RETRY_LONG;
1813 }
1814
1815 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1816 frag_threshold = nla_get_u32(
1817 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1818 if (frag_threshold < 256) {
1819 result = -EINVAL;
1820 goto bad_res;
1821 }
1822 if (frag_threshold != (u32) -1) {
1823 /*
1824 * Fragments (apart from the last one) are required to
1825 * have even length. Make the fragmentation code
1826 * simpler by stripping LSB should someone try to use
1827 * odd threshold value.
1828 */
1829 frag_threshold &= ~0x1;
1830 }
1831 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1832 }
1833
1834 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1835 rts_threshold = nla_get_u32(
1836 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1837 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1838 }
1839
Lukáš Turek81077e82009-12-21 22:50:47 +01001840 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1841 coverage_class = nla_get_u8(
1842 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1843 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1844 }
1845
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001846 if (changed) {
1847 u8 old_retry_short, old_retry_long;
1848 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001849 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001850
1851 if (!rdev->ops->set_wiphy_params) {
1852 result = -EOPNOTSUPP;
1853 goto bad_res;
1854 }
1855
1856 old_retry_short = rdev->wiphy.retry_short;
1857 old_retry_long = rdev->wiphy.retry_long;
1858 old_frag_threshold = rdev->wiphy.frag_threshold;
1859 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001860 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001861
1862 if (changed & WIPHY_PARAM_RETRY_SHORT)
1863 rdev->wiphy.retry_short = retry_short;
1864 if (changed & WIPHY_PARAM_RETRY_LONG)
1865 rdev->wiphy.retry_long = retry_long;
1866 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1867 rdev->wiphy.frag_threshold = frag_threshold;
1868 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1869 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001870 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1871 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001872
Hila Gonene35e4d22012-06-27 17:19:42 +03001873 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001874 if (result) {
1875 rdev->wiphy.retry_short = old_retry_short;
1876 rdev->wiphy.retry_long = old_retry_long;
1877 rdev->wiphy.frag_threshold = old_frag_threshold;
1878 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001879 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001880 }
1881 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001882
Johannes Berg306d6112008-12-08 12:39:04 +01001883 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001884 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001885 if (netdev)
1886 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001887 return result;
1888}
1889
Johannes Berg71bbc992012-06-15 15:30:18 +02001890static inline u64 wdev_id(struct wireless_dev *wdev)
1891{
1892 return (u64)wdev->identifier |
1893 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1894}
Johannes Berg55682962007-09-20 13:09:35 -04001895
Johannes Berg683b6d32012-11-08 21:25:48 +01001896static int nl80211_send_chandef(struct sk_buff *msg,
1897 struct cfg80211_chan_def *chandef)
1898{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001899 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001900
Johannes Berg683b6d32012-11-08 21:25:48 +01001901 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1902 chandef->chan->center_freq))
1903 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001904 switch (chandef->width) {
1905 case NL80211_CHAN_WIDTH_20_NOHT:
1906 case NL80211_CHAN_WIDTH_20:
1907 case NL80211_CHAN_WIDTH_40:
1908 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1909 cfg80211_get_chandef_type(chandef)))
1910 return -ENOBUFS;
1911 break;
1912 default:
1913 break;
1914 }
1915 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1916 return -ENOBUFS;
1917 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1918 return -ENOBUFS;
1919 if (chandef->center_freq2 &&
1920 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001921 return -ENOBUFS;
1922 return 0;
1923}
1924
Eric W. Biederman15e47302012-09-07 20:12:54 +00001925static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001926 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001927 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001928{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001929 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001930 void *hdr;
1931
Eric W. Biederman15e47302012-09-07 20:12:54 +00001932 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001933 if (!hdr)
1934 return -1;
1935
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001936 if (dev &&
1937 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001938 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001939 goto nla_put_failure;
1940
1941 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1942 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001943 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001944 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001945 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1946 rdev->devlist_generation ^
1947 (cfg80211_rdev_list_generation << 2)))
1948 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001949
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001950 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001951 int ret;
1952 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001953
Johannes Berg683b6d32012-11-08 21:25:48 +01001954 ret = rdev_get_channel(rdev, wdev, &chandef);
1955 if (ret == 0) {
1956 if (nl80211_send_chandef(msg, &chandef))
1957 goto nla_put_failure;
1958 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001959 }
1960
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001961 if (wdev->ssid_len) {
1962 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1963 goto nla_put_failure;
1964 }
1965
Johannes Berg55682962007-09-20 13:09:35 -04001966 return genlmsg_end(msg, hdr);
1967
1968 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001969 genlmsg_cancel(msg, hdr);
1970 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001971}
1972
1973static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1974{
1975 int wp_idx = 0;
1976 int if_idx = 0;
1977 int wp_start = cb->args[0];
1978 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001979 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001980 struct wireless_dev *wdev;
1981
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001982 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001983 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1984 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001985 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001986 if (wp_idx < wp_start) {
1987 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001988 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001989 }
Johannes Berg55682962007-09-20 13:09:35 -04001990 if_idx = 0;
1991
Johannes Bergf5ea9122009-08-07 16:17:38 +02001992 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001993 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001994 if (if_idx < if_start) {
1995 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001996 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001997 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001998 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001999 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002000 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002001 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002002 goto out;
2003 }
2004 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002005 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002006 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002007
2008 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002009 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002010 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002011 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002012
2013 cb->args[0] = wp_idx;
2014 cb->args[1] = if_idx;
2015
2016 return skb->len;
2017}
2018
2019static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2020{
2021 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002022 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002023 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002024
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002025 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002026 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002027 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002028
Eric W. Biederman15e47302012-09-07 20:12:54 +00002029 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002030 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002031 nlmsg_free(msg);
2032 return -ENOBUFS;
2033 }
Johannes Berg55682962007-09-20 13:09:35 -04002034
Johannes Berg134e6372009-07-10 09:51:34 +00002035 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002036}
2037
Michael Wu66f7ac52008-01-31 19:48:22 +01002038static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2039 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2040 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2041 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2042 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2043 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2044};
2045
2046static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2047{
2048 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2049 int flag;
2050
2051 *mntrflags = 0;
2052
2053 if (!nla)
2054 return -EINVAL;
2055
2056 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2057 nla, mntr_flags_policy))
2058 return -EINVAL;
2059
2060 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2061 if (flags[flag])
2062 *mntrflags |= (1<<flag);
2063
2064 return 0;
2065}
2066
Johannes Berg9bc383d2009-11-19 11:55:19 +01002067static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002068 struct net_device *netdev, u8 use_4addr,
2069 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002070{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002071 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002072 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002073 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002074 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002075 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002076
2077 switch (iftype) {
2078 case NL80211_IFTYPE_AP_VLAN:
2079 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2080 return 0;
2081 break;
2082 case NL80211_IFTYPE_STATION:
2083 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2084 return 0;
2085 break;
2086 default:
2087 break;
2088 }
2089
2090 return -EOPNOTSUPP;
2091}
2092
Johannes Berg55682962007-09-20 13:09:35 -04002093static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2094{
Johannes Berg4c476992010-10-04 21:36:35 +02002095 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002096 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002097 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002098 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002099 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002100 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002101 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002102
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002103 memset(&params, 0, sizeof(params));
2104
Johannes Berg04a773a2009-04-19 21:24:32 +02002105 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002106
Johannes Berg723b0382008-09-16 20:22:09 +02002107 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002108 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002109 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002110 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002111 if (ntype > NL80211_IFTYPE_MAX)
2112 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002113 }
2114
Johannes Berg92ffe052008-09-16 20:39:36 +02002115 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002116 struct wireless_dev *wdev = dev->ieee80211_ptr;
2117
Johannes Berg4c476992010-10-04 21:36:35 +02002118 if (ntype != NL80211_IFTYPE_MESH_POINT)
2119 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002120 if (netif_running(dev))
2121 return -EBUSY;
2122
2123 wdev_lock(wdev);
2124 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2125 IEEE80211_MAX_MESH_ID_LEN);
2126 wdev->mesh_id_up_len =
2127 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2128 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2129 wdev->mesh_id_up_len);
2130 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002131 }
2132
Felix Fietkau8b787642009-11-10 18:53:10 +01002133 if (info->attrs[NL80211_ATTR_4ADDR]) {
2134 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2135 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002136 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002137 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002138 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002139 } else {
2140 params.use_4addr = -1;
2141 }
2142
Johannes Berg92ffe052008-09-16 20:39:36 +02002143 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002144 if (ntype != NL80211_IFTYPE_MONITOR)
2145 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002146 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2147 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002148 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002149 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002150
2151 flags = &_flags;
2152 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002153 }
Johannes Berg3b858752009-03-12 09:55:09 +01002154
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002155 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002156 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002157 else
2158 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002159
Johannes Berg9bc383d2009-11-19 11:55:19 +01002160 if (!err && params.use_4addr != -1)
2161 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2162
Johannes Berg55682962007-09-20 13:09:35 -04002163 return err;
2164}
2165
2166static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2167{
Johannes Berg4c476992010-10-04 21:36:35 +02002168 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002169 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002170 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002171 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002172 int err;
2173 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002174 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002175
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002176 memset(&params, 0, sizeof(params));
2177
Johannes Berg55682962007-09-20 13:09:35 -04002178 if (!info->attrs[NL80211_ATTR_IFNAME])
2179 return -EINVAL;
2180
2181 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2182 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2183 if (type > NL80211_IFTYPE_MAX)
2184 return -EINVAL;
2185 }
2186
Johannes Berg79c97e92009-07-07 03:56:12 +02002187 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002188 !(rdev->wiphy.interface_modes & (1 << type)))
2189 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002190
Arend van Spriel1c18f142013-01-08 10:17:27 +01002191 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2192 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2193 ETH_ALEN);
2194 if (!is_valid_ether_addr(params.macaddr))
2195 return -EADDRNOTAVAIL;
2196 }
2197
Johannes Berg9bc383d2009-11-19 11:55:19 +01002198 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002199 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002200 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002201 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002202 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002203 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002204
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2206 if (!msg)
2207 return -ENOMEM;
2208
Michael Wu66f7ac52008-01-31 19:48:22 +01002209 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2210 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2211 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002212 wdev = rdev_add_virtual_intf(rdev,
2213 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2214 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002215 if (IS_ERR(wdev)) {
2216 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002217 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002218 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002219
Johannes Berg98104fde2012-06-16 00:19:54 +02002220 switch (type) {
2221 case NL80211_IFTYPE_MESH_POINT:
2222 if (!info->attrs[NL80211_ATTR_MESH_ID])
2223 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002224 wdev_lock(wdev);
2225 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2226 IEEE80211_MAX_MESH_ID_LEN);
2227 wdev->mesh_id_up_len =
2228 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2229 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2230 wdev->mesh_id_up_len);
2231 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002232 break;
2233 case NL80211_IFTYPE_P2P_DEVICE:
2234 /*
2235 * P2P Device doesn't have a netdev, so doesn't go
2236 * through the netdev notifier and must be added here
2237 */
2238 mutex_init(&wdev->mtx);
2239 INIT_LIST_HEAD(&wdev->event_list);
2240 spin_lock_init(&wdev->event_lock);
2241 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2242 spin_lock_init(&wdev->mgmt_registrations_lock);
2243
2244 mutex_lock(&rdev->devlist_mtx);
2245 wdev->identifier = ++rdev->wdev_id;
2246 list_add_rcu(&wdev->list, &rdev->wdev_list);
2247 rdev->devlist_generation++;
2248 mutex_unlock(&rdev->devlist_mtx);
2249 break;
2250 default:
2251 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002252 }
2253
Eric W. Biederman15e47302012-09-07 20:12:54 +00002254 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002255 rdev, wdev) < 0) {
2256 nlmsg_free(msg);
2257 return -ENOBUFS;
2258 }
2259
2260 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002261}
2262
2263static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2264{
Johannes Berg4c476992010-10-04 21:36:35 +02002265 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002266 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002267
Johannes Berg4c476992010-10-04 21:36:35 +02002268 if (!rdev->ops->del_virtual_intf)
2269 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002270
Johannes Berg84efbb82012-06-16 00:00:26 +02002271 /*
2272 * If we remove a wireless device without a netdev then clear
2273 * user_ptr[1] so that nl80211_post_doit won't dereference it
2274 * to check if it needs to do dev_put(). Otherwise it crashes
2275 * since the wdev has been freed, unlike with a netdev where
2276 * we need the dev_put() for the netdev to really be freed.
2277 */
2278 if (!wdev->netdev)
2279 info->user_ptr[1] = NULL;
2280
Hila Gonene35e4d22012-06-27 17:19:42 +03002281 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002282}
2283
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002284static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2285{
2286 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2287 struct net_device *dev = info->user_ptr[1];
2288 u16 noack_map;
2289
2290 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2291 return -EINVAL;
2292
2293 if (!rdev->ops->set_noack_map)
2294 return -EOPNOTSUPP;
2295
2296 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2297
Hila Gonene35e4d22012-06-27 17:19:42 +03002298 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002299}
2300
Johannes Berg41ade002007-12-19 02:03:29 +01002301struct get_key_cookie {
2302 struct sk_buff *msg;
2303 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002304 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002305};
2306
2307static void get_key_callback(void *c, struct key_params *params)
2308{
Johannes Bergb9454e82009-07-08 13:29:08 +02002309 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002310 struct get_key_cookie *cookie = c;
2311
David S. Miller9360ffd2012-03-29 04:41:26 -04002312 if ((params->key &&
2313 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2314 params->key_len, params->key)) ||
2315 (params->seq &&
2316 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2317 params->seq_len, params->seq)) ||
2318 (params->cipher &&
2319 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2320 params->cipher)))
2321 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002322
Johannes Bergb9454e82009-07-08 13:29:08 +02002323 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2324 if (!key)
2325 goto nla_put_failure;
2326
David S. Miller9360ffd2012-03-29 04:41:26 -04002327 if ((params->key &&
2328 nla_put(cookie->msg, NL80211_KEY_DATA,
2329 params->key_len, params->key)) ||
2330 (params->seq &&
2331 nla_put(cookie->msg, NL80211_KEY_SEQ,
2332 params->seq_len, params->seq)) ||
2333 (params->cipher &&
2334 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2335 params->cipher)))
2336 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002337
David S. Miller9360ffd2012-03-29 04:41:26 -04002338 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2339 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002340
2341 nla_nest_end(cookie->msg, key);
2342
Johannes Berg41ade002007-12-19 02:03:29 +01002343 return;
2344 nla_put_failure:
2345 cookie->error = 1;
2346}
2347
2348static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2349{
Johannes Berg4c476992010-10-04 21:36:35 +02002350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002351 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002352 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002353 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002354 const u8 *mac_addr = NULL;
2355 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002356 struct get_key_cookie cookie = {
2357 .error = 0,
2358 };
2359 void *hdr;
2360 struct sk_buff *msg;
2361
2362 if (info->attrs[NL80211_ATTR_KEY_IDX])
2363 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2364
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002365 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002366 return -EINVAL;
2367
2368 if (info->attrs[NL80211_ATTR_MAC])
2369 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2370
Johannes Berge31b8212010-10-05 19:39:30 +02002371 pairwise = !!mac_addr;
2372 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2373 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2374 if (kt >= NUM_NL80211_KEYTYPES)
2375 return -EINVAL;
2376 if (kt != NL80211_KEYTYPE_GROUP &&
2377 kt != NL80211_KEYTYPE_PAIRWISE)
2378 return -EINVAL;
2379 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2380 }
2381
Johannes Berg4c476992010-10-04 21:36:35 +02002382 if (!rdev->ops->get_key)
2383 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002384
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002386 if (!msg)
2387 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002388
Eric W. Biederman15e47302012-09-07 20:12:54 +00002389 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002390 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002391 if (IS_ERR(hdr))
2392 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002393
2394 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002395 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002396
David S. Miller9360ffd2012-03-29 04:41:26 -04002397 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2398 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2399 goto nla_put_failure;
2400 if (mac_addr &&
2401 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2402 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002403
Johannes Berge31b8212010-10-05 19:39:30 +02002404 if (pairwise && mac_addr &&
2405 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2406 return -ENOENT;
2407
Hila Gonene35e4d22012-06-27 17:19:42 +03002408 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2409 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002410
2411 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002412 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002413
2414 if (cookie.error)
2415 goto nla_put_failure;
2416
2417 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002418 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002419
2420 nla_put_failure:
2421 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002422 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002423 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002424 return err;
2425}
2426
2427static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2428{
Johannes Berg4c476992010-10-04 21:36:35 +02002429 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002430 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002431 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002432 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002433
Johannes Bergb9454e82009-07-08 13:29:08 +02002434 err = nl80211_parse_key(info, &key);
2435 if (err)
2436 return err;
2437
2438 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002439 return -EINVAL;
2440
Johannes Bergb9454e82009-07-08 13:29:08 +02002441 /* only support setting default key */
2442 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002443 return -EINVAL;
2444
Johannes Bergfffd0932009-07-08 14:22:54 +02002445 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002446
2447 if (key.def) {
2448 if (!rdev->ops->set_default_key) {
2449 err = -EOPNOTSUPP;
2450 goto out;
2451 }
2452
2453 err = nl80211_key_allowed(dev->ieee80211_ptr);
2454 if (err)
2455 goto out;
2456
Hila Gonene35e4d22012-06-27 17:19:42 +03002457 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002458 key.def_uni, key.def_multi);
2459
2460 if (err)
2461 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002462
Johannes Berg3d23e342009-09-29 23:27:28 +02002463#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002464 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002465#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002466 } else {
2467 if (key.def_uni || !key.def_multi) {
2468 err = -EINVAL;
2469 goto out;
2470 }
2471
2472 if (!rdev->ops->set_default_mgmt_key) {
2473 err = -EOPNOTSUPP;
2474 goto out;
2475 }
2476
2477 err = nl80211_key_allowed(dev->ieee80211_ptr);
2478 if (err)
2479 goto out;
2480
Hila Gonene35e4d22012-06-27 17:19:42 +03002481 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002482 if (err)
2483 goto out;
2484
2485#ifdef CONFIG_CFG80211_WEXT
2486 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2487#endif
2488 }
2489
2490 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002491 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002492
Johannes Berg41ade002007-12-19 02:03:29 +01002493 return err;
2494}
2495
2496static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2497{
Johannes Berg4c476992010-10-04 21:36:35 +02002498 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002499 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002500 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002501 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002502 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002503
Johannes Bergb9454e82009-07-08 13:29:08 +02002504 err = nl80211_parse_key(info, &key);
2505 if (err)
2506 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002507
Johannes Bergb9454e82009-07-08 13:29:08 +02002508 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002509 return -EINVAL;
2510
Johannes Berg41ade002007-12-19 02:03:29 +01002511 if (info->attrs[NL80211_ATTR_MAC])
2512 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2513
Johannes Berge31b8212010-10-05 19:39:30 +02002514 if (key.type == -1) {
2515 if (mac_addr)
2516 key.type = NL80211_KEYTYPE_PAIRWISE;
2517 else
2518 key.type = NL80211_KEYTYPE_GROUP;
2519 }
2520
2521 /* for now */
2522 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2523 key.type != NL80211_KEYTYPE_GROUP)
2524 return -EINVAL;
2525
Johannes Berg4c476992010-10-04 21:36:35 +02002526 if (!rdev->ops->add_key)
2527 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002528
Johannes Berge31b8212010-10-05 19:39:30 +02002529 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2530 key.type == NL80211_KEYTYPE_PAIRWISE,
2531 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002532 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002533
2534 wdev_lock(dev->ieee80211_ptr);
2535 err = nl80211_key_allowed(dev->ieee80211_ptr);
2536 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002537 err = rdev_add_key(rdev, dev, key.idx,
2538 key.type == NL80211_KEYTYPE_PAIRWISE,
2539 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002540 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002541
Johannes Berg41ade002007-12-19 02:03:29 +01002542 return err;
2543}
2544
2545static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2546{
Johannes Berg4c476992010-10-04 21:36:35 +02002547 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002548 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002549 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002550 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002551 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002552
Johannes Bergb9454e82009-07-08 13:29:08 +02002553 err = nl80211_parse_key(info, &key);
2554 if (err)
2555 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002556
2557 if (info->attrs[NL80211_ATTR_MAC])
2558 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2559
Johannes Berge31b8212010-10-05 19:39:30 +02002560 if (key.type == -1) {
2561 if (mac_addr)
2562 key.type = NL80211_KEYTYPE_PAIRWISE;
2563 else
2564 key.type = NL80211_KEYTYPE_GROUP;
2565 }
2566
2567 /* for now */
2568 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2569 key.type != NL80211_KEYTYPE_GROUP)
2570 return -EINVAL;
2571
Johannes Berg4c476992010-10-04 21:36:35 +02002572 if (!rdev->ops->del_key)
2573 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002574
Johannes Bergfffd0932009-07-08 14:22:54 +02002575 wdev_lock(dev->ieee80211_ptr);
2576 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002577
2578 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2579 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2580 err = -ENOENT;
2581
Johannes Bergfffd0932009-07-08 14:22:54 +02002582 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002583 err = rdev_del_key(rdev, dev, key.idx,
2584 key.type == NL80211_KEYTYPE_PAIRWISE,
2585 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002586
Johannes Berg3d23e342009-09-29 23:27:28 +02002587#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002588 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002589 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002590 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002591 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002592 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2593 }
2594#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002595 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002596
Johannes Berg41ade002007-12-19 02:03:29 +01002597 return err;
2598}
2599
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302600/* This function returns an error or the number of nested attributes */
2601static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2602{
2603 struct nlattr *attr;
2604 int n_entries = 0, tmp;
2605
2606 nla_for_each_nested(attr, nl_attr, tmp) {
2607 if (nla_len(attr) != ETH_ALEN)
2608 return -EINVAL;
2609
2610 n_entries++;
2611 }
2612
2613 return n_entries;
2614}
2615
2616/*
2617 * This function parses ACL information and allocates memory for ACL data.
2618 * On successful return, the calling function is responsible to free the
2619 * ACL buffer returned by this function.
2620 */
2621static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2622 struct genl_info *info)
2623{
2624 enum nl80211_acl_policy acl_policy;
2625 struct nlattr *attr;
2626 struct cfg80211_acl_data *acl;
2627 int i = 0, n_entries, tmp;
2628
2629 if (!wiphy->max_acl_mac_addrs)
2630 return ERR_PTR(-EOPNOTSUPP);
2631
2632 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2633 return ERR_PTR(-EINVAL);
2634
2635 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2636 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2637 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2638 return ERR_PTR(-EINVAL);
2639
2640 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2641 return ERR_PTR(-EINVAL);
2642
2643 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2644 if (n_entries < 0)
2645 return ERR_PTR(n_entries);
2646
2647 if (n_entries > wiphy->max_acl_mac_addrs)
2648 return ERR_PTR(-ENOTSUPP);
2649
2650 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2651 GFP_KERNEL);
2652 if (!acl)
2653 return ERR_PTR(-ENOMEM);
2654
2655 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2656 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2657 i++;
2658 }
2659
2660 acl->n_acl_entries = n_entries;
2661 acl->acl_policy = acl_policy;
2662
2663 return acl;
2664}
2665
2666static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2667{
2668 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2669 struct net_device *dev = info->user_ptr[1];
2670 struct cfg80211_acl_data *acl;
2671 int err;
2672
2673 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2674 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2675 return -EOPNOTSUPP;
2676
2677 if (!dev->ieee80211_ptr->beacon_interval)
2678 return -EINVAL;
2679
2680 acl = parse_acl_data(&rdev->wiphy, info);
2681 if (IS_ERR(acl))
2682 return PTR_ERR(acl);
2683
2684 err = rdev_set_mac_acl(rdev, dev, acl);
2685
2686 kfree(acl);
2687
2688 return err;
2689}
2690
Johannes Berg88600202012-02-13 15:17:18 +01002691static int nl80211_parse_beacon(struct genl_info *info,
2692 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002693{
Johannes Berg88600202012-02-13 15:17:18 +01002694 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002695
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002696 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2697 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2698 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2699 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002700 return -EINVAL;
2701
Johannes Berg88600202012-02-13 15:17:18 +01002702 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002703
Johannes Berged1b6cc2007-12-19 02:03:32 +01002704 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002705 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2706 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2707 if (!bcn->head_len)
2708 return -EINVAL;
2709 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002710 }
2711
2712 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002713 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2714 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002715 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002716 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002717 }
2718
Johannes Berg4c476992010-10-04 21:36:35 +02002719 if (!haveinfo)
2720 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002721
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002722 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002723 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2724 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002725 }
2726
2727 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002728 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002729 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002730 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002731 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2732 }
2733
2734 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002735 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002736 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002737 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002738 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2739 }
2740
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002741 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002742 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002743 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002744 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002745 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2746 }
2747
Johannes Berg88600202012-02-13 15:17:18 +01002748 return 0;
2749}
2750
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002751static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2752 struct cfg80211_ap_settings *params)
2753{
2754 struct wireless_dev *wdev;
2755 bool ret = false;
2756
2757 mutex_lock(&rdev->devlist_mtx);
2758
Johannes Berg89a54e42012-06-15 14:33:17 +02002759 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002760 if (wdev->iftype != NL80211_IFTYPE_AP &&
2761 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2762 continue;
2763
Johannes Berg683b6d32012-11-08 21:25:48 +01002764 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002765 continue;
2766
Johannes Berg683b6d32012-11-08 21:25:48 +01002767 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002768 ret = true;
2769 break;
2770 }
2771
2772 mutex_unlock(&rdev->devlist_mtx);
2773
2774 return ret;
2775}
2776
Jouni Malinene39e5b52012-09-30 19:29:39 +03002777static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2778 enum nl80211_auth_type auth_type,
2779 enum nl80211_commands cmd)
2780{
2781 if (auth_type > NL80211_AUTHTYPE_MAX)
2782 return false;
2783
2784 switch (cmd) {
2785 case NL80211_CMD_AUTHENTICATE:
2786 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2787 auth_type == NL80211_AUTHTYPE_SAE)
2788 return false;
2789 return true;
2790 case NL80211_CMD_CONNECT:
2791 case NL80211_CMD_START_AP:
2792 /* SAE not supported yet */
2793 if (auth_type == NL80211_AUTHTYPE_SAE)
2794 return false;
2795 return true;
2796 default:
2797 return false;
2798 }
2799}
2800
Johannes Berg88600202012-02-13 15:17:18 +01002801static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2802{
2803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2804 struct net_device *dev = info->user_ptr[1];
2805 struct wireless_dev *wdev = dev->ieee80211_ptr;
2806 struct cfg80211_ap_settings params;
2807 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002808 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002809
2810 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2811 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2812 return -EOPNOTSUPP;
2813
2814 if (!rdev->ops->start_ap)
2815 return -EOPNOTSUPP;
2816
2817 if (wdev->beacon_interval)
2818 return -EALREADY;
2819
2820 memset(&params, 0, sizeof(params));
2821
2822 /* these are required for START_AP */
2823 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2824 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2825 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2826 return -EINVAL;
2827
2828 err = nl80211_parse_beacon(info, &params.beacon);
2829 if (err)
2830 return err;
2831
2832 params.beacon_interval =
2833 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2834 params.dtim_period =
2835 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2836
2837 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2838 if (err)
2839 return err;
2840
2841 /*
2842 * In theory, some of these attributes should be required here
2843 * but since they were not used when the command was originally
2844 * added, keep them optional for old user space programs to let
2845 * them continue to work with drivers that do not need the
2846 * additional information -- drivers must check!
2847 */
2848 if (info->attrs[NL80211_ATTR_SSID]) {
2849 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2850 params.ssid_len =
2851 nla_len(info->attrs[NL80211_ATTR_SSID]);
2852 if (params.ssid_len == 0 ||
2853 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2854 return -EINVAL;
2855 }
2856
2857 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2858 params.hidden_ssid = nla_get_u32(
2859 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2860 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2861 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2862 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2863 return -EINVAL;
2864 }
2865
2866 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2867
2868 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2869 params.auth_type = nla_get_u32(
2870 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002871 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2872 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002873 return -EINVAL;
2874 } else
2875 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2876
2877 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2878 NL80211_MAX_NR_CIPHER_SUITES);
2879 if (err)
2880 return err;
2881
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302882 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2883 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2884 return -EOPNOTSUPP;
2885 params.inactivity_timeout = nla_get_u16(
2886 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2887 }
2888
Johannes Berg53cabad2012-11-14 15:17:28 +01002889 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2890 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2891 return -EINVAL;
2892 params.p2p_ctwindow =
2893 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2894 if (params.p2p_ctwindow > 127)
2895 return -EINVAL;
2896 if (params.p2p_ctwindow != 0 &&
2897 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2898 return -EINVAL;
2899 }
2900
2901 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2902 u8 tmp;
2903
2904 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2905 return -EINVAL;
2906 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2907 if (tmp > 1)
2908 return -EINVAL;
2909 params.p2p_opp_ps = tmp;
2910 if (params.p2p_opp_ps != 0 &&
2911 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2912 return -EINVAL;
2913 }
2914
Johannes Bergaa430da2012-05-16 23:50:18 +02002915 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002916 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2917 if (err)
2918 return err;
2919 } else if (wdev->preset_chandef.chan) {
2920 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002921 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002922 return -EINVAL;
2923
Johannes Berg683b6d32012-11-08 21:25:48 +01002924 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002925 return -EINVAL;
2926
Simon Wunderlich04f39042013-02-08 18:16:19 +01002927 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
2928 if (err < 0)
2929 return err;
2930 if (err) {
2931 radar_detect_width = BIT(params.chandef.width);
2932 params.radar_required = true;
2933 }
2934
Michal Kaziore4e32452012-06-29 12:47:08 +02002935 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01002936 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
2937 params.chandef.chan,
2938 CHAN_MODE_SHARED,
2939 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02002940 mutex_unlock(&rdev->devlist_mtx);
2941
2942 if (err)
2943 return err;
2944
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302945 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
2946 params.acl = parse_acl_data(&rdev->wiphy, info);
2947 if (IS_ERR(params.acl))
2948 return PTR_ERR(params.acl);
2949 }
2950
Hila Gonene35e4d22012-06-27 17:19:42 +03002951 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002952 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002953 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002954 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002955 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002956 wdev->ssid_len = params.ssid_len;
2957 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002958 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302959
2960 kfree(params.acl);
2961
Johannes Berg56d18932011-05-09 18:41:15 +02002962 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002963}
2964
Johannes Berg88600202012-02-13 15:17:18 +01002965static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2966{
2967 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2968 struct net_device *dev = info->user_ptr[1];
2969 struct wireless_dev *wdev = dev->ieee80211_ptr;
2970 struct cfg80211_beacon_data params;
2971 int err;
2972
2973 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2974 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2975 return -EOPNOTSUPP;
2976
2977 if (!rdev->ops->change_beacon)
2978 return -EOPNOTSUPP;
2979
2980 if (!wdev->beacon_interval)
2981 return -EINVAL;
2982
2983 err = nl80211_parse_beacon(info, &params);
2984 if (err)
2985 return err;
2986
Hila Gonene35e4d22012-06-27 17:19:42 +03002987 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002988}
2989
2990static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002991{
Johannes Berg4c476992010-10-04 21:36:35 +02002992 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2993 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002994
Michal Kazior60771782012-06-29 12:46:56 +02002995 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002996}
2997
Johannes Berg5727ef12007-12-19 02:03:34 +01002998static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2999 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3000 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3001 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003002 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003003 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003004 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003005};
3006
Johannes Bergeccb8e82009-05-11 21:57:56 +03003007static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003008 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003009 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003010{
3011 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003012 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003013 int flag;
3014
Johannes Bergeccb8e82009-05-11 21:57:56 +03003015 /*
3016 * Try parsing the new attribute first so userspace
3017 * can specify both for older kernels.
3018 */
3019 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3020 if (nla) {
3021 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003022
Johannes Bergeccb8e82009-05-11 21:57:56 +03003023 sta_flags = nla_data(nla);
3024 params->sta_flags_mask = sta_flags->mask;
3025 params->sta_flags_set = sta_flags->set;
3026 if ((params->sta_flags_mask |
3027 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3028 return -EINVAL;
3029 return 0;
3030 }
3031
3032 /* if present, parse the old attribute */
3033
3034 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003035 if (!nla)
3036 return 0;
3037
3038 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3039 nla, sta_flags_policy))
3040 return -EINVAL;
3041
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003042 /*
3043 * Only allow certain flags for interface types so that
3044 * other attributes are silently ignored. Remember that
3045 * this is backward compatibility code with old userspace
3046 * and shouldn't be hit in other cases anyway.
3047 */
3048 switch (iftype) {
3049 case NL80211_IFTYPE_AP:
3050 case NL80211_IFTYPE_AP_VLAN:
3051 case NL80211_IFTYPE_P2P_GO:
3052 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3053 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3054 BIT(NL80211_STA_FLAG_WME) |
3055 BIT(NL80211_STA_FLAG_MFP);
3056 break;
3057 case NL80211_IFTYPE_P2P_CLIENT:
3058 case NL80211_IFTYPE_STATION:
3059 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3060 BIT(NL80211_STA_FLAG_TDLS_PEER);
3061 break;
3062 case NL80211_IFTYPE_MESH_POINT:
3063 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3064 BIT(NL80211_STA_FLAG_MFP) |
3065 BIT(NL80211_STA_FLAG_AUTHORIZED);
3066 default:
3067 return -EINVAL;
3068 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003069
Johannes Berg3383b5a2012-05-10 20:14:43 +02003070 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3071 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003072 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003073
Johannes Berg3383b5a2012-05-10 20:14:43 +02003074 /* no longer support new API additions in old API */
3075 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3076 return -EINVAL;
3077 }
3078 }
3079
Johannes Berg5727ef12007-12-19 02:03:34 +01003080 return 0;
3081}
3082
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003083static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3084 int attr)
3085{
3086 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003087 u32 bitrate;
3088 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003089
3090 rate = nla_nest_start(msg, attr);
3091 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003092 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003093
3094 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3095 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003096 /* report 16-bit bitrate only if we can */
3097 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003098 if (bitrate > 0 &&
3099 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3100 return false;
3101 if (bitrate_compat > 0 &&
3102 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3103 return false;
3104
3105 if (info->flags & RATE_INFO_FLAGS_MCS) {
3106 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3107 return false;
3108 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3109 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3110 return false;
3111 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3112 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3113 return false;
3114 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3115 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3116 return false;
3117 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3118 return false;
3119 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3120 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3121 return false;
3122 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3123 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3124 return false;
3125 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3126 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3127 return false;
3128 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3129 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3130 return false;
3131 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3132 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3133 return false;
3134 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003135
3136 nla_nest_end(msg, rate);
3137 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003138}
3139
Eric W. Biederman15e47302012-09-07 20:12:54 +00003140static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003141 int flags,
3142 struct cfg80211_registered_device *rdev,
3143 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003144 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003145{
3146 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003147 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003148
Eric W. Biederman15e47302012-09-07 20:12:54 +00003149 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003150 if (!hdr)
3151 return -1;
3152
David S. Miller9360ffd2012-03-29 04:41:26 -04003153 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3154 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3155 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3156 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003157
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003158 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3159 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003160 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003161 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3162 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3163 sinfo->connected_time))
3164 goto nla_put_failure;
3165 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3166 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3167 sinfo->inactive_time))
3168 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003169 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3170 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003171 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003172 (u32)sinfo->rx_bytes))
3173 goto nla_put_failure;
3174 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3175 NL80211_STA_INFO_TX_BYTES64)) &&
3176 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3177 (u32)sinfo->tx_bytes))
3178 goto nla_put_failure;
3179 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3180 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003181 sinfo->rx_bytes))
3182 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003183 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3184 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003185 sinfo->tx_bytes))
3186 goto nla_put_failure;
3187 if ((sinfo->filled & STATION_INFO_LLID) &&
3188 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3189 goto nla_put_failure;
3190 if ((sinfo->filled & STATION_INFO_PLID) &&
3191 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3192 goto nla_put_failure;
3193 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3194 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3195 sinfo->plink_state))
3196 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003197 switch (rdev->wiphy.signal_type) {
3198 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003199 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3200 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3201 sinfo->signal))
3202 goto nla_put_failure;
3203 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3204 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3205 sinfo->signal_avg))
3206 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003207 break;
3208 default:
3209 break;
3210 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003211 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003212 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3213 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003214 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003215 }
3216 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3217 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3218 NL80211_STA_INFO_RX_BITRATE))
3219 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003220 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003221 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3222 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3223 sinfo->rx_packets))
3224 goto nla_put_failure;
3225 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3226 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3227 sinfo->tx_packets))
3228 goto nla_put_failure;
3229 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3230 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3231 sinfo->tx_retries))
3232 goto nla_put_failure;
3233 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3234 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3235 sinfo->tx_failed))
3236 goto nla_put_failure;
3237 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3238 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3239 sinfo->beacon_loss_count))
3240 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003241 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3242 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3243 sinfo->local_pm))
3244 goto nla_put_failure;
3245 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3246 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3247 sinfo->peer_pm))
3248 goto nla_put_failure;
3249 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3250 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3251 sinfo->nonpeer_pm))
3252 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003253 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3254 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3255 if (!bss_param)
3256 goto nla_put_failure;
3257
David S. Miller9360ffd2012-03-29 04:41:26 -04003258 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3259 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3260 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3261 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3262 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3263 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3264 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3265 sinfo->bss_param.dtim_period) ||
3266 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3267 sinfo->bss_param.beacon_interval))
3268 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003269
3270 nla_nest_end(msg, bss_param);
3271 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003272 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3273 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3274 sizeof(struct nl80211_sta_flag_update),
3275 &sinfo->sta_flags))
3276 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003277 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3278 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3279 sinfo->t_offset))
3280 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003281 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003282
David S. Miller9360ffd2012-03-29 04:41:26 -04003283 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3284 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3285 sinfo->assoc_req_ies))
3286 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003287
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003288 return genlmsg_end(msg, hdr);
3289
3290 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003291 genlmsg_cancel(msg, hdr);
3292 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003293}
3294
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003295static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003296 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003297{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003298 struct station_info sinfo;
3299 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003300 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003301 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003302 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003303 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003304
Johannes Berg67748892010-10-04 21:14:06 +02003305 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3306 if (err)
3307 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003308
3309 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003310 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003311 goto out_err;
3312 }
3313
Johannes Bergbba95fe2008-07-29 13:22:51 +02003314 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003315 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003316 err = rdev_dump_station(dev, netdev, sta_idx,
3317 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003318 if (err == -ENOENT)
3319 break;
3320 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003321 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003322
3323 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003324 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003325 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003326 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003327 &sinfo) < 0)
3328 goto out;
3329
3330 sta_idx++;
3331 }
3332
3333
3334 out:
3335 cb->args[1] = sta_idx;
3336 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003337 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003338 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003339
3340 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003341}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003342
Johannes Berg5727ef12007-12-19 02:03:34 +01003343static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3344{
Johannes Berg4c476992010-10-04 21:36:35 +02003345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3346 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003347 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003348 struct sk_buff *msg;
3349 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003350 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003351
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003352 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003353
3354 if (!info->attrs[NL80211_ATTR_MAC])
3355 return -EINVAL;
3356
3357 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3358
Johannes Berg4c476992010-10-04 21:36:35 +02003359 if (!rdev->ops->get_station)
3360 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003361
Hila Gonene35e4d22012-06-27 17:19:42 +03003362 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003363 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003364 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003365
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003366 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003367 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003368 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003369
Eric W. Biederman15e47302012-09-07 20:12:54 +00003370 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003371 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003372 nlmsg_free(msg);
3373 return -ENOBUFS;
3374 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003375
Johannes Berg4c476992010-10-04 21:36:35 +02003376 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003377}
3378
3379/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003380 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003381 */
Johannes Berg80b99892011-11-18 16:23:01 +01003382static struct net_device *get_vlan(struct genl_info *info,
3383 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003384{
Johannes Berg463d0182009-07-14 00:33:35 +02003385 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003386 struct net_device *v;
3387 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003388
Johannes Berg80b99892011-11-18 16:23:01 +01003389 if (!vlanattr)
3390 return NULL;
3391
3392 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3393 if (!v)
3394 return ERR_PTR(-ENODEV);
3395
3396 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3397 ret = -EINVAL;
3398 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003399 }
Johannes Berg80b99892011-11-18 16:23:01 +01003400
3401 if (!netif_running(v)) {
3402 ret = -ENETDOWN;
3403 goto error;
3404 }
3405
3406 return v;
3407 error:
3408 dev_put(v);
3409 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003410}
3411
Jouni Malinendf881292013-02-14 21:10:54 +02003412static struct nla_policy
3413nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3414 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3415 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3416};
3417
3418static int nl80211_set_station_tdls(struct genl_info *info,
3419 struct station_parameters *params)
3420{
Jouni Malinendf881292013-02-14 21:10:54 +02003421 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3422 struct nlattr *nla;
3423 int err;
3424
Jouni Malinendf881292013-02-14 21:10:54 +02003425 /* Dummy STA entry gets updated once the peer capabilities are known */
3426 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3427 params->ht_capa =
3428 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3429 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3430 params->vht_capa =
3431 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3432
3433 /* parse WME attributes if present */
3434 if (!info->attrs[NL80211_ATTR_STA_WME])
3435 return 0;
3436
3437 nla = info->attrs[NL80211_ATTR_STA_WME];
3438 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3439 nl80211_sta_wme_policy);
3440 if (err)
3441 return err;
3442
3443 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3444 params->uapsd_queues = nla_get_u8(
3445 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3446 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3447 return -EINVAL;
3448
3449 if (tb[NL80211_STA_WME_MAX_SP])
3450 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3451
3452 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3453 return -EINVAL;
3454
3455 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3456
3457 return 0;
3458}
3459
Johannes Berg5727ef12007-12-19 02:03:34 +01003460static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3461{
Johannes Berg4c476992010-10-04 21:36:35 +02003462 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003463 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003464 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003465 struct station_parameters params;
3466 u8 *mac_addr = NULL;
3467
3468 memset(&params, 0, sizeof(params));
3469
3470 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003471 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003472
3473 if (info->attrs[NL80211_ATTR_STA_AID])
3474 return -EINVAL;
3475
3476 if (!info->attrs[NL80211_ATTR_MAC])
3477 return -EINVAL;
3478
3479 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3480
3481 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3482 params.supported_rates =
3483 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3484 params.supported_rates_len =
3485 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3486 }
3487
Jouni Malinen9d62a982013-02-14 21:10:13 +02003488 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3489 params.capability =
3490 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3491 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3492 }
3493
3494 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3495 params.ext_capab =
3496 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3497 params.ext_capab_len =
3498 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3499 }
3500
Jouni Malinendf881292013-02-14 21:10:54 +02003501 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003502 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003503
Johannes Bergbdd90d52011-12-14 12:20:27 +01003504 if (!rdev->ops->change_station)
3505 return -EOPNOTSUPP;
3506
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003507 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003508 return -EINVAL;
3509
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003510 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3511 params.plink_action =
3512 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3513
Javier Cardona9c3990a2011-05-03 16:57:11 -07003514 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3515 params.plink_state =
3516 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3517
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003518 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3519 enum nl80211_mesh_power_mode pm = nla_get_u32(
3520 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3521
3522 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3523 pm > NL80211_MESH_POWER_MAX)
3524 return -EINVAL;
3525
3526 params.local_pm = pm;
3527 }
3528
Johannes Berga97f4422009-06-18 17:23:43 +02003529 switch (dev->ieee80211_ptr->iftype) {
3530 case NL80211_IFTYPE_AP:
3531 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003532 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003533 /* disallow mesh-specific things */
3534 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003535 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003536 if (params.local_pm)
3537 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003538
3539 /* TDLS can't be set, ... */
3540 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3541 return -EINVAL;
3542 /*
3543 * ... but don't bother the driver with it. This works around
3544 * a hostapd/wpa_supplicant issue -- it always includes the
3545 * TLDS_PEER flag in the mask even for AP mode.
3546 */
3547 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3548
3549 /* accept only the listed bits */
3550 if (params.sta_flags_mask &
3551 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003552 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3553 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003554 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3555 BIT(NL80211_STA_FLAG_WME) |
3556 BIT(NL80211_STA_FLAG_MFP)))
3557 return -EINVAL;
3558
Johannes Bergd582cff2012-10-26 17:53:44 +02003559 /* but authenticated/associated only if driver handles it */
3560 if (!(rdev->wiphy.features &
3561 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3562 params.sta_flags_mask &
3563 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3564 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3565 return -EINVAL;
3566
Johannes Bergba23d202012-12-27 17:32:09 +01003567 /* reject other things that can't change */
3568 if (params.supported_rates)
3569 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003570 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3571 return -EINVAL;
3572 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3573 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003574 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3575 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3576 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003577
Johannes Bergbdd90d52011-12-14 12:20:27 +01003578 /* must be last in here for error handling */
3579 params.vlan = get_vlan(info, rdev);
3580 if (IS_ERR(params.vlan))
3581 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003582 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003583 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003584 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003585 /*
3586 * Don't allow userspace to change the TDLS_PEER flag,
3587 * but silently ignore attempts to change it since we
3588 * don't have state here to verify that it doesn't try
3589 * to change the flag.
3590 */
3591 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Jouni Malinendf881292013-02-14 21:10:54 +02003592 /* Include parameters for TDLS peer (driver will check) */
3593 err = nl80211_set_station_tdls(info, &params);
3594 if (err)
3595 return err;
3596 /* disallow things sta doesn't support */
3597 if (params.plink_action)
3598 return -EINVAL;
3599 if (params.local_pm)
3600 return -EINVAL;
3601 /* reject any changes other than AUTHORIZED or WME (for TDLS) */
3602 if (params.sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3603 BIT(NL80211_STA_FLAG_WME)))
3604 return -EINVAL;
3605 break;
Antonio Quartulli267335d2012-01-31 20:25:47 +01003606 case NL80211_IFTYPE_ADHOC:
3607 /* disallow things sta doesn't support */
3608 if (params.plink_action)
3609 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003610 if (params.local_pm)
3611 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003612 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3613 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3614 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003615 /* reject any changes other than AUTHORIZED */
3616 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3617 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003618 break;
3619 case NL80211_IFTYPE_MESH_POINT:
3620 /* disallow things mesh doesn't support */
3621 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003622 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003623 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003624 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003625 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3626 return -EINVAL;
3627 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3628 return -EINVAL;
Jouni Malinendf881292013-02-14 21:10:54 +02003629 if (info->attrs[NL80211_ATTR_HT_CAPABILITY] ||
3630 info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3631 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003632 /*
3633 * No special handling for TDLS here -- the userspace
3634 * mesh code doesn't have this bug.
3635 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003636 if (params.sta_flags_mask &
3637 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003638 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003639 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003640 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003641 break;
3642 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003643 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003644 }
3645
Johannes Bergbdd90d52011-12-14 12:20:27 +01003646 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003647
Hila Gonene35e4d22012-06-27 17:19:42 +03003648 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003649
Johannes Berg5727ef12007-12-19 02:03:34 +01003650 if (params.vlan)
3651 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003652
Johannes Berg5727ef12007-12-19 02:03:34 +01003653 return err;
3654}
3655
3656static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3657{
Johannes Berg4c476992010-10-04 21:36:35 +02003658 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003659 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003660 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003661 struct station_parameters params;
3662 u8 *mac_addr = NULL;
3663
3664 memset(&params, 0, sizeof(params));
3665
3666 if (!info->attrs[NL80211_ATTR_MAC])
3667 return -EINVAL;
3668
Johannes Berg5727ef12007-12-19 02:03:34 +01003669 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3670 return -EINVAL;
3671
3672 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3673 return -EINVAL;
3674
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003675 if (!info->attrs[NL80211_ATTR_STA_AID])
3676 return -EINVAL;
3677
Johannes Berg5727ef12007-12-19 02:03:34 +01003678 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3679 params.supported_rates =
3680 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3681 params.supported_rates_len =
3682 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3683 params.listen_interval =
3684 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003685
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003686 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3687 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3688 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003689
Jouni Malinen9d62a982013-02-14 21:10:13 +02003690 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3691 params.capability =
3692 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3693 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3694 }
3695
3696 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3697 params.ext_capab =
3698 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3699 params.ext_capab_len =
3700 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3701 }
3702
Jouni Malinen36aedc92008-08-25 11:58:58 +03003703 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3704 params.ht_capa =
3705 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003706
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003707 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3708 params.vht_capa =
3709 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3710
Javier Cardona96b78df2011-04-07 15:08:33 -07003711 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3712 params.plink_action =
3713 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3714
Johannes Bergbdd90d52011-12-14 12:20:27 +01003715 if (!rdev->ops->add_station)
3716 return -EOPNOTSUPP;
3717
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003718 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003719 return -EINVAL;
3720
Johannes Bergbdd90d52011-12-14 12:20:27 +01003721 switch (dev->ieee80211_ptr->iftype) {
3722 case NL80211_IFTYPE_AP:
3723 case NL80211_IFTYPE_AP_VLAN:
3724 case NL80211_IFTYPE_P2P_GO:
3725 /* parse WME attributes if sta is WME capable */
3726 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3727 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3728 info->attrs[NL80211_ATTR_STA_WME]) {
3729 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3730 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003731
Johannes Bergbdd90d52011-12-14 12:20:27 +01003732 nla = info->attrs[NL80211_ATTR_STA_WME];
3733 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3734 nl80211_sta_wme_policy);
3735 if (err)
3736 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003737
Johannes Bergbdd90d52011-12-14 12:20:27 +01003738 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3739 params.uapsd_queues =
3740 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3741 if (params.uapsd_queues &
3742 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3743 return -EINVAL;
3744
3745 if (tb[NL80211_STA_WME_MAX_SP])
3746 params.max_sp =
3747 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3748
3749 if (params.max_sp &
3750 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3751 return -EINVAL;
3752
3753 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3754 }
3755 /* TDLS peers cannot be added */
3756 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003757 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003758 /* but don't bother the driver with it */
3759 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003760
Johannes Bergd582cff2012-10-26 17:53:44 +02003761 /* allow authenticated/associated only if driver handles it */
3762 if (!(rdev->wiphy.features &
3763 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3764 params.sta_flags_mask &
3765 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3766 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3767 return -EINVAL;
3768
Johannes Bergbdd90d52011-12-14 12:20:27 +01003769 /* must be last in here for error handling */
3770 params.vlan = get_vlan(info, rdev);
3771 if (IS_ERR(params.vlan))
3772 return PTR_ERR(params.vlan);
3773 break;
3774 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergd582cff2012-10-26 17:53:44 +02003775 /* associated is disallowed */
3776 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3777 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003778 /* TDLS peers cannot be added */
3779 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003780 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003781 break;
3782 case NL80211_IFTYPE_STATION:
Johannes Bergd582cff2012-10-26 17:53:44 +02003783 /* associated is disallowed */
3784 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3785 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003786 /* Only TDLS peers can be added */
3787 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3788 return -EINVAL;
3789 /* Can only add if TDLS ... */
3790 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3791 return -EOPNOTSUPP;
3792 /* ... with external setup is supported */
3793 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3794 return -EOPNOTSUPP;
3795 break;
3796 default:
3797 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003798 }
3799
Johannes Bergbdd90d52011-12-14 12:20:27 +01003800 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003801
Hila Gonene35e4d22012-06-27 17:19:42 +03003802 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003803
Johannes Berg5727ef12007-12-19 02:03:34 +01003804 if (params.vlan)
3805 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003806 return err;
3807}
3808
3809static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3810{
Johannes Berg4c476992010-10-04 21:36:35 +02003811 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3812 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003813 u8 *mac_addr = NULL;
3814
3815 if (info->attrs[NL80211_ATTR_MAC])
3816 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3817
Johannes Berge80cf852009-05-11 14:43:13 +02003818 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003819 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003820 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003821 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3822 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003823
Johannes Berg4c476992010-10-04 21:36:35 +02003824 if (!rdev->ops->del_station)
3825 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003826
Hila Gonene35e4d22012-06-27 17:19:42 +03003827 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003828}
3829
Eric W. Biederman15e47302012-09-07 20:12:54 +00003830static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003831 int flags, struct net_device *dev,
3832 u8 *dst, u8 *next_hop,
3833 struct mpath_info *pinfo)
3834{
3835 void *hdr;
3836 struct nlattr *pinfoattr;
3837
Eric W. Biederman15e47302012-09-07 20:12:54 +00003838 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003839 if (!hdr)
3840 return -1;
3841
David S. Miller9360ffd2012-03-29 04:41:26 -04003842 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3843 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3844 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3845 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3846 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003847
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003848 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3849 if (!pinfoattr)
3850 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003851 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3852 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3853 pinfo->frame_qlen))
3854 goto nla_put_failure;
3855 if (((pinfo->filled & MPATH_INFO_SN) &&
3856 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3857 ((pinfo->filled & MPATH_INFO_METRIC) &&
3858 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3859 pinfo->metric)) ||
3860 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3861 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3862 pinfo->exptime)) ||
3863 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3864 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3865 pinfo->flags)) ||
3866 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3867 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3868 pinfo->discovery_timeout)) ||
3869 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3870 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3871 pinfo->discovery_retries)))
3872 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003873
3874 nla_nest_end(msg, pinfoattr);
3875
3876 return genlmsg_end(msg, hdr);
3877
3878 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003879 genlmsg_cancel(msg, hdr);
3880 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003881}
3882
3883static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003884 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003885{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003886 struct mpath_info pinfo;
3887 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003888 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003889 u8 dst[ETH_ALEN];
3890 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003891 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003892 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003893
Johannes Berg67748892010-10-04 21:14:06 +02003894 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3895 if (err)
3896 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003897
3898 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003899 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003900 goto out_err;
3901 }
3902
Jouni Malineneec60b02009-03-20 21:21:19 +02003903 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3904 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003905 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003906 }
3907
Johannes Bergbba95fe2008-07-29 13:22:51 +02003908 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003909 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3910 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003911 if (err == -ENOENT)
3912 break;
3913 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003914 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003915
Eric W. Biederman15e47302012-09-07 20:12:54 +00003916 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003917 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3918 netdev, dst, next_hop,
3919 &pinfo) < 0)
3920 goto out;
3921
3922 path_idx++;
3923 }
3924
3925
3926 out:
3927 cb->args[1] = path_idx;
3928 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003929 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003930 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003931 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003932}
3933
3934static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3935{
Johannes Berg4c476992010-10-04 21:36:35 +02003936 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003937 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003938 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003939 struct mpath_info pinfo;
3940 struct sk_buff *msg;
3941 u8 *dst = NULL;
3942 u8 next_hop[ETH_ALEN];
3943
3944 memset(&pinfo, 0, sizeof(pinfo));
3945
3946 if (!info->attrs[NL80211_ATTR_MAC])
3947 return -EINVAL;
3948
3949 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3950
Johannes Berg4c476992010-10-04 21:36:35 +02003951 if (!rdev->ops->get_mpath)
3952 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003953
Johannes Berg4c476992010-10-04 21:36:35 +02003954 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3955 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003956
Hila Gonene35e4d22012-06-27 17:19:42 +03003957 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003958 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003959 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003960
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003961 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003962 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003963 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003964
Eric W. Biederman15e47302012-09-07 20:12:54 +00003965 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003966 dev, dst, next_hop, &pinfo) < 0) {
3967 nlmsg_free(msg);
3968 return -ENOBUFS;
3969 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003970
Johannes Berg4c476992010-10-04 21:36:35 +02003971 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003972}
3973
3974static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3975{
Johannes Berg4c476992010-10-04 21:36:35 +02003976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3977 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003978 u8 *dst = NULL;
3979 u8 *next_hop = NULL;
3980
3981 if (!info->attrs[NL80211_ATTR_MAC])
3982 return -EINVAL;
3983
3984 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3985 return -EINVAL;
3986
3987 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3988 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3989
Johannes Berg4c476992010-10-04 21:36:35 +02003990 if (!rdev->ops->change_mpath)
3991 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003992
Johannes Berg4c476992010-10-04 21:36:35 +02003993 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3994 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003995
Hila Gonene35e4d22012-06-27 17:19:42 +03003996 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003997}
Johannes Berg4c476992010-10-04 21:36:35 +02003998
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003999static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4000{
Johannes Berg4c476992010-10-04 21:36:35 +02004001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4002 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004003 u8 *dst = NULL;
4004 u8 *next_hop = NULL;
4005
4006 if (!info->attrs[NL80211_ATTR_MAC])
4007 return -EINVAL;
4008
4009 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4010 return -EINVAL;
4011
4012 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4013 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4014
Johannes Berg4c476992010-10-04 21:36:35 +02004015 if (!rdev->ops->add_mpath)
4016 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004017
Johannes Berg4c476992010-10-04 21:36:35 +02004018 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4019 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004020
Hila Gonene35e4d22012-06-27 17:19:42 +03004021 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004022}
4023
4024static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4025{
Johannes Berg4c476992010-10-04 21:36:35 +02004026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4027 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004028 u8 *dst = NULL;
4029
4030 if (info->attrs[NL80211_ATTR_MAC])
4031 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4032
Johannes Berg4c476992010-10-04 21:36:35 +02004033 if (!rdev->ops->del_mpath)
4034 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004035
Hila Gonene35e4d22012-06-27 17:19:42 +03004036 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004037}
4038
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004039static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4040{
Johannes Berg4c476992010-10-04 21:36:35 +02004041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4042 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004043 struct bss_parameters params;
4044
4045 memset(&params, 0, sizeof(params));
4046 /* default to not changing parameters */
4047 params.use_cts_prot = -1;
4048 params.use_short_preamble = -1;
4049 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004050 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004051 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004052 params.p2p_ctwindow = -1;
4053 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004054
4055 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4056 params.use_cts_prot =
4057 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4058 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4059 params.use_short_preamble =
4060 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4061 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4062 params.use_short_slot_time =
4063 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004064 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4065 params.basic_rates =
4066 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4067 params.basic_rates_len =
4068 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4069 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004070 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4071 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004072 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4073 params.ht_opmode =
4074 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004075
Johannes Berg53cabad2012-11-14 15:17:28 +01004076 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4077 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4078 return -EINVAL;
4079 params.p2p_ctwindow =
4080 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4081 if (params.p2p_ctwindow < 0)
4082 return -EINVAL;
4083 if (params.p2p_ctwindow != 0 &&
4084 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4085 return -EINVAL;
4086 }
4087
4088 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4089 u8 tmp;
4090
4091 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4092 return -EINVAL;
4093 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4094 if (tmp > 1)
4095 return -EINVAL;
4096 params.p2p_opp_ps = tmp;
4097 if (params.p2p_opp_ps &&
4098 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4099 return -EINVAL;
4100 }
4101
Johannes Berg4c476992010-10-04 21:36:35 +02004102 if (!rdev->ops->change_bss)
4103 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004104
Johannes Berg074ac8d2010-09-16 14:58:22 +02004105 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004106 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4107 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004108
Hila Gonene35e4d22012-06-27 17:19:42 +03004109 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004110}
4111
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004112static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004113 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4114 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4115 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4116 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4117 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4118 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4119};
4120
4121static int parse_reg_rule(struct nlattr *tb[],
4122 struct ieee80211_reg_rule *reg_rule)
4123{
4124 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4125 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4126
4127 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4128 return -EINVAL;
4129 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4130 return -EINVAL;
4131 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4132 return -EINVAL;
4133 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4134 return -EINVAL;
4135 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4136 return -EINVAL;
4137
4138 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4139
4140 freq_range->start_freq_khz =
4141 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4142 freq_range->end_freq_khz =
4143 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4144 freq_range->max_bandwidth_khz =
4145 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4146
4147 power_rule->max_eirp =
4148 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4149
4150 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4151 power_rule->max_antenna_gain =
4152 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4153
4154 return 0;
4155}
4156
4157static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4158{
4159 int r;
4160 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004161 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004162
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004163 /*
4164 * You should only get this when cfg80211 hasn't yet initialized
4165 * completely when built-in to the kernel right between the time
4166 * window between nl80211_init() and regulatory_init(), if that is
4167 * even possible.
4168 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004169 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004170 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004171
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004172 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4173 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004174
4175 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4176
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004177 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4178 user_reg_hint_type =
4179 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4180 else
4181 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4182
4183 switch (user_reg_hint_type) {
4184 case NL80211_USER_REG_HINT_USER:
4185 case NL80211_USER_REG_HINT_CELL_BASE:
4186 break;
4187 default:
4188 return -EINVAL;
4189 }
4190
4191 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004192
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004193 return r;
4194}
4195
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004196static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004197 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004198{
Johannes Berg4c476992010-10-04 21:36:35 +02004199 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004200 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004201 struct wireless_dev *wdev = dev->ieee80211_ptr;
4202 struct mesh_config cur_params;
4203 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004204 void *hdr;
4205 struct nlattr *pinfoattr;
4206 struct sk_buff *msg;
4207
Johannes Berg29cbe682010-12-03 09:20:44 +01004208 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4209 return -EOPNOTSUPP;
4210
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004211 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004212 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004213
Johannes Berg29cbe682010-12-03 09:20:44 +01004214 wdev_lock(wdev);
4215 /* If not connected, get default parameters */
4216 if (!wdev->mesh_id_len)
4217 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4218 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004219 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004220 wdev_unlock(wdev);
4221
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004222 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004223 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004224
4225 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004226 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004227 if (!msg)
4228 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004229 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004230 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004231 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004232 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004233 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004234 if (!pinfoattr)
4235 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004236 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4237 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4238 cur_params.dot11MeshRetryTimeout) ||
4239 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4240 cur_params.dot11MeshConfirmTimeout) ||
4241 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4242 cur_params.dot11MeshHoldingTimeout) ||
4243 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4244 cur_params.dot11MeshMaxPeerLinks) ||
4245 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4246 cur_params.dot11MeshMaxRetries) ||
4247 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4248 cur_params.dot11MeshTTL) ||
4249 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4250 cur_params.element_ttl) ||
4251 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4252 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004253 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4254 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004255 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4256 cur_params.dot11MeshHWMPmaxPREQretries) ||
4257 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4258 cur_params.path_refresh_time) ||
4259 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4260 cur_params.min_discovery_timeout) ||
4261 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4262 cur_params.dot11MeshHWMPactivePathTimeout) ||
4263 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4264 cur_params.dot11MeshHWMPpreqMinInterval) ||
4265 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4266 cur_params.dot11MeshHWMPperrMinInterval) ||
4267 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4268 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4269 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4270 cur_params.dot11MeshHWMPRootMode) ||
4271 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4272 cur_params.dot11MeshHWMPRannInterval) ||
4273 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4274 cur_params.dot11MeshGateAnnouncementProtocol) ||
4275 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4276 cur_params.dot11MeshForwarding) ||
4277 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004278 cur_params.rssi_threshold) ||
4279 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004280 cur_params.ht_opmode) ||
4281 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4282 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4283 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004284 cur_params.dot11MeshHWMProotInterval) ||
4285 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004286 cur_params.dot11MeshHWMPconfirmationInterval) ||
4287 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4288 cur_params.power_mode) ||
4289 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4290 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004291 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004292 nla_nest_end(msg, pinfoattr);
4293 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004294 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004295
Johannes Berg3b858752009-03-12 09:55:09 +01004296 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004297 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004298 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004299 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004300 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004301}
4302
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004303static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004304 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4305 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4306 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4307 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4308 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4309 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004310 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004311 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004312 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004313 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4314 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4315 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4316 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4317 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004318 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004319 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004320 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004321 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004322 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004323 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004324 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4325 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004326 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4327 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004328 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004329 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4330 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004331};
4332
Javier Cardonac80d5452010-12-16 17:37:49 -08004333static const struct nla_policy
4334 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004335 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004336 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4337 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004338 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004339 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004340 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004341 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004342};
4343
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004344static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004345 struct mesh_config *cfg,
4346 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004347{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004348 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004349 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004350
Marco Porschea54fba2013-01-07 16:04:48 +01004351#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4352do { \
4353 if (tb[attr]) { \
4354 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4355 return -EINVAL; \
4356 cfg->param = fn(tb[attr]); \
4357 mask |= (1 << (attr - 1)); \
4358 } \
4359} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004360
4361
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004362 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004363 return -EINVAL;
4364 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004365 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004366 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004367 return -EINVAL;
4368
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004369 /* This makes sure that there aren't more than 32 mesh config
4370 * parameters (otherwise our bitfield scheme would not work.) */
4371 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4372
4373 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004374 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004375 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4376 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004377 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004378 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4379 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004380 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004381 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4382 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004383 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004384 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4385 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004386 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004387 mask, NL80211_MESHCONF_MAX_RETRIES,
4388 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004389 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004390 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004391 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004392 mask, NL80211_MESHCONF_ELEMENT_TTL,
4393 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004394 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004395 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4396 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004397 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4398 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004399 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4400 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004401 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004402 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4403 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004404 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004405 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4406 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004407 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004408 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4409 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004410 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4411 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004412 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4413 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004414 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004415 1, 65535, mask,
4416 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004417 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004418 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004419 1, 65535, mask,
4420 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004421 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004422 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004423 dot11MeshHWMPnetDiameterTraversalTime,
4424 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004425 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4426 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004427 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4428 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4429 nla_get_u8);
4430 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4431 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004432 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004433 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004434 dot11MeshGateAnnouncementProtocol, 0, 1,
4435 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004436 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004437 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004438 mask, NL80211_MESHCONF_FORWARDING,
4439 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004440 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004441 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4442 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004443 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004444 mask, NL80211_MESHCONF_HT_OPMODE,
4445 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004446 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004447 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004448 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4449 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004450 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004451 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4452 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004453 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004454 dot11MeshHWMPconfirmationInterval,
4455 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004456 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4457 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004458 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4459 NL80211_MESH_POWER_ACTIVE,
4460 NL80211_MESH_POWER_MAX,
4461 mask, NL80211_MESHCONF_POWER_MODE,
4462 nla_get_u32);
4463 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4464 0, 65535, mask,
4465 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004466 if (mask_out)
4467 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004468
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004469 return 0;
4470
4471#undef FILL_IN_MESH_PARAM_IF_SET
4472}
4473
Javier Cardonac80d5452010-12-16 17:37:49 -08004474static int nl80211_parse_mesh_setup(struct genl_info *info,
4475 struct mesh_setup *setup)
4476{
4477 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4478
4479 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4480 return -EINVAL;
4481 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4482 info->attrs[NL80211_ATTR_MESH_SETUP],
4483 nl80211_mesh_setup_params_policy))
4484 return -EINVAL;
4485
Javier Cardonad299a1f2012-03-31 11:31:33 -07004486 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4487 setup->sync_method =
4488 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4489 IEEE80211_SYNC_METHOD_VENDOR :
4490 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4491
Javier Cardonac80d5452010-12-16 17:37:49 -08004492 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4493 setup->path_sel_proto =
4494 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4495 IEEE80211_PATH_PROTOCOL_VENDOR :
4496 IEEE80211_PATH_PROTOCOL_HWMP;
4497
4498 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4499 setup->path_metric =
4500 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4501 IEEE80211_PATH_METRIC_VENDOR :
4502 IEEE80211_PATH_METRIC_AIRTIME;
4503
Javier Cardona581a8b02011-04-07 15:08:27 -07004504
4505 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004506 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004507 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004508 if (!is_valid_ie_attr(ieattr))
4509 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004510 setup->ie = nla_data(ieattr);
4511 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004512 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004513 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4514 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004515
4516 return 0;
4517}
4518
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004519static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004520 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004521{
4522 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4523 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004524 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004525 struct mesh_config cfg;
4526 u32 mask;
4527 int err;
4528
Johannes Berg29cbe682010-12-03 09:20:44 +01004529 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4530 return -EOPNOTSUPP;
4531
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004532 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004533 return -EOPNOTSUPP;
4534
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004535 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004536 if (err)
4537 return err;
4538
Johannes Berg29cbe682010-12-03 09:20:44 +01004539 wdev_lock(wdev);
4540 if (!wdev->mesh_id_len)
4541 err = -ENOLINK;
4542
4543 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004544 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004545
4546 wdev_unlock(wdev);
4547
4548 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004549}
4550
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004551static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4552{
Johannes Berg458f4f92012-12-06 15:47:38 +01004553 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004554 struct sk_buff *msg;
4555 void *hdr = NULL;
4556 struct nlattr *nl_reg_rules;
4557 unsigned int i;
4558 int err = -EINVAL;
4559
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004560 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004561
4562 if (!cfg80211_regdomain)
4563 goto out;
4564
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004565 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004566 if (!msg) {
4567 err = -ENOBUFS;
4568 goto out;
4569 }
4570
Eric W. Biederman15e47302012-09-07 20:12:54 +00004571 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004572 NL80211_CMD_GET_REG);
4573 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004574 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004575
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004576 if (reg_last_request_cell_base() &&
4577 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4578 NL80211_USER_REG_HINT_CELL_BASE))
4579 goto nla_put_failure;
4580
Johannes Berg458f4f92012-12-06 15:47:38 +01004581 rcu_read_lock();
4582 regdom = rcu_dereference(cfg80211_regdomain);
4583
4584 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4585 (regdom->dfs_region &&
4586 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4587 goto nla_put_failure_rcu;
4588
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004589 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4590 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004591 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004592
Johannes Berg458f4f92012-12-06 15:47:38 +01004593 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004594 struct nlattr *nl_reg_rule;
4595 const struct ieee80211_reg_rule *reg_rule;
4596 const struct ieee80211_freq_range *freq_range;
4597 const struct ieee80211_power_rule *power_rule;
4598
Johannes Berg458f4f92012-12-06 15:47:38 +01004599 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004600 freq_range = &reg_rule->freq_range;
4601 power_rule = &reg_rule->power_rule;
4602
4603 nl_reg_rule = nla_nest_start(msg, i);
4604 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004605 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004606
David S. Miller9360ffd2012-03-29 04:41:26 -04004607 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4608 reg_rule->flags) ||
4609 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4610 freq_range->start_freq_khz) ||
4611 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4612 freq_range->end_freq_khz) ||
4613 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4614 freq_range->max_bandwidth_khz) ||
4615 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4616 power_rule->max_antenna_gain) ||
4617 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4618 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004619 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004620
4621 nla_nest_end(msg, nl_reg_rule);
4622 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004623 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004624
4625 nla_nest_end(msg, nl_reg_rules);
4626
4627 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004628 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004629 goto out;
4630
Johannes Berg458f4f92012-12-06 15:47:38 +01004631nla_put_failure_rcu:
4632 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004633nla_put_failure:
4634 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004635put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004636 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004637 err = -EMSGSIZE;
4638out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004639 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004640 return err;
4641}
4642
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004643static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4644{
4645 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4646 struct nlattr *nl_reg_rule;
4647 char *alpha2 = NULL;
4648 int rem_reg_rules = 0, r = 0;
4649 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004650 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004651 struct ieee80211_regdomain *rd = NULL;
4652
4653 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4654 return -EINVAL;
4655
4656 if (!info->attrs[NL80211_ATTR_REG_RULES])
4657 return -EINVAL;
4658
4659 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4660
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004661 if (info->attrs[NL80211_ATTR_DFS_REGION])
4662 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4663
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004664 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004665 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004666 num_rules++;
4667 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004668 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004669 }
4670
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004671 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004672 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004673
4674 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004675 if (!rd)
4676 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004677
4678 rd->n_reg_rules = num_rules;
4679 rd->alpha2[0] = alpha2[0];
4680 rd->alpha2[1] = alpha2[1];
4681
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004682 /*
4683 * Disable DFS master mode if the DFS region was
4684 * not supported or known on this kernel.
4685 */
4686 if (reg_supported_dfs_region(dfs_region))
4687 rd->dfs_region = dfs_region;
4688
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004689 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004690 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004691 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004692 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4693 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004694 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4695 if (r)
4696 goto bad_reg;
4697
4698 rule_idx++;
4699
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004700 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4701 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004702 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004703 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004704 }
4705
Johannes Berg6913b492012-12-04 00:48:59 +01004706 mutex_lock(&cfg80211_mutex);
4707
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004708 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004709 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004710 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004711 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004712
Johannes Bergd2372b32008-10-24 20:32:20 +02004713 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004714 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004715 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004716}
4717
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004718static int validate_scan_freqs(struct nlattr *freqs)
4719{
4720 struct nlattr *attr1, *attr2;
4721 int n_channels = 0, tmp1, tmp2;
4722
4723 nla_for_each_nested(attr1, freqs, tmp1) {
4724 n_channels++;
4725 /*
4726 * Some hardware has a limited channel list for
4727 * scanning, and it is pretty much nonsensical
4728 * to scan for a channel twice, so disallow that
4729 * and don't require drivers to check that the
4730 * channel list they get isn't longer than what
4731 * they can scan, as long as they can scan all
4732 * the channels they registered at once.
4733 */
4734 nla_for_each_nested(attr2, freqs, tmp2)
4735 if (attr1 != attr2 &&
4736 nla_get_u32(attr1) == nla_get_u32(attr2))
4737 return 0;
4738 }
4739
4740 return n_channels;
4741}
4742
Johannes Berg2a519312009-02-10 21:25:55 +01004743static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4744{
Johannes Berg4c476992010-10-04 21:36:35 +02004745 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004746 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004747 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004748 struct nlattr *attr;
4749 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004750 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004751 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004752
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004753 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4754 return -EINVAL;
4755
Johannes Berg79c97e92009-07-07 03:56:12 +02004756 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004757
Johannes Berg4c476992010-10-04 21:36:35 +02004758 if (!rdev->ops->scan)
4759 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004760
Johannes Berg4c476992010-10-04 21:36:35 +02004761 if (rdev->scan_req)
4762 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004763
4764 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004765 n_channels = validate_scan_freqs(
4766 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004767 if (!n_channels)
4768 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004769 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004770 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004771 n_channels = 0;
4772
Johannes Berg2a519312009-02-10 21:25:55 +01004773 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4774 if (wiphy->bands[band])
4775 n_channels += wiphy->bands[band]->n_channels;
4776 }
4777
4778 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4779 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4780 n_ssids++;
4781
Johannes Berg4c476992010-10-04 21:36:35 +02004782 if (n_ssids > wiphy->max_scan_ssids)
4783 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004784
Jouni Malinen70692ad2009-02-16 19:39:13 +02004785 if (info->attrs[NL80211_ATTR_IE])
4786 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4787 else
4788 ie_len = 0;
4789
Johannes Berg4c476992010-10-04 21:36:35 +02004790 if (ie_len > wiphy->max_scan_ie_len)
4791 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004792
Johannes Berg2a519312009-02-10 21:25:55 +01004793 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004794 + sizeof(*request->ssids) * n_ssids
4795 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004796 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004797 if (!request)
4798 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004799
Johannes Berg2a519312009-02-10 21:25:55 +01004800 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004801 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004802 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004803 if (ie_len) {
4804 if (request->ssids)
4805 request->ie = (void *)(request->ssids + n_ssids);
4806 else
4807 request->ie = (void *)(request->channels + n_channels);
4808 }
Johannes Berg2a519312009-02-10 21:25:55 +01004809
Johannes Berg584991d2009-11-02 13:32:03 +01004810 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004811 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4812 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004813 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004814 struct ieee80211_channel *chan;
4815
4816 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4817
4818 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004819 err = -EINVAL;
4820 goto out_free;
4821 }
Johannes Berg584991d2009-11-02 13:32:03 +01004822
4823 /* ignore disabled channels */
4824 if (chan->flags & IEEE80211_CHAN_DISABLED)
4825 continue;
4826
4827 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004828 i++;
4829 }
4830 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004831 enum ieee80211_band band;
4832
Johannes Berg2a519312009-02-10 21:25:55 +01004833 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004834 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4835 int j;
4836 if (!wiphy->bands[band])
4837 continue;
4838 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004839 struct ieee80211_channel *chan;
4840
4841 chan = &wiphy->bands[band]->channels[j];
4842
4843 if (chan->flags & IEEE80211_CHAN_DISABLED)
4844 continue;
4845
4846 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004847 i++;
4848 }
4849 }
4850 }
4851
Johannes Berg584991d2009-11-02 13:32:03 +01004852 if (!i) {
4853 err = -EINVAL;
4854 goto out_free;
4855 }
4856
4857 request->n_channels = i;
4858
Johannes Berg2a519312009-02-10 21:25:55 +01004859 i = 0;
4860 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4861 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004862 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004863 err = -EINVAL;
4864 goto out_free;
4865 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004866 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004867 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004868 i++;
4869 }
4870 }
4871
Jouni Malinen70692ad2009-02-16 19:39:13 +02004872 if (info->attrs[NL80211_ATTR_IE]) {
4873 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004874 memcpy((void *)request->ie,
4875 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004876 request->ie_len);
4877 }
4878
Johannes Berg34850ab2011-07-18 18:08:35 +02004879 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004880 if (wiphy->bands[i])
4881 request->rates[i] =
4882 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004883
4884 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4885 nla_for_each_nested(attr,
4886 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4887 tmp) {
4888 enum ieee80211_band band = nla_type(attr);
4889
Dan Carpenter84404622011-07-29 11:52:18 +03004890 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004891 err = -EINVAL;
4892 goto out_free;
4893 }
4894 err = ieee80211_get_ratemask(wiphy->bands[band],
4895 nla_data(attr),
4896 nla_len(attr),
4897 &request->rates[band]);
4898 if (err)
4899 goto out_free;
4900 }
4901 }
4902
Sam Leffler46856bb2012-10-11 21:03:32 -07004903 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004904 request->flags = nla_get_u32(
4905 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004906 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4907 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4908 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4909 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004910 err = -EOPNOTSUPP;
4911 goto out_free;
4912 }
4913 }
Sam Lefflered4737712012-10-11 21:03:31 -07004914
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304915 request->no_cck =
4916 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4917
Johannes Bergfd014282012-06-18 19:17:03 +02004918 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004919 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004920 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004921
Johannes Berg79c97e92009-07-07 03:56:12 +02004922 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004923 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004924
Johannes Berg463d0182009-07-14 00:33:35 +02004925 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004926 nl80211_send_scan_start(rdev, wdev);
4927 if (wdev->netdev)
4928 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004929 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004930 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004931 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004932 kfree(request);
4933 }
Johannes Berg3b858752009-03-12 09:55:09 +01004934
Johannes Berg2a519312009-02-10 21:25:55 +01004935 return err;
4936}
4937
Luciano Coelho807f8a82011-05-11 17:09:35 +03004938static int nl80211_start_sched_scan(struct sk_buff *skb,
4939 struct genl_info *info)
4940{
4941 struct cfg80211_sched_scan_request *request;
4942 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4943 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004944 struct nlattr *attr;
4945 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004946 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004947 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004948 enum ieee80211_band band;
4949 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004950 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004951
4952 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4953 !rdev->ops->sched_scan_start)
4954 return -EOPNOTSUPP;
4955
4956 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4957 return -EINVAL;
4958
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004959 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4960 return -EINVAL;
4961
4962 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4963 if (interval == 0)
4964 return -EINVAL;
4965
Luciano Coelho807f8a82011-05-11 17:09:35 +03004966 wiphy = &rdev->wiphy;
4967
4968 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4969 n_channels = validate_scan_freqs(
4970 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4971 if (!n_channels)
4972 return -EINVAL;
4973 } else {
4974 n_channels = 0;
4975
4976 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4977 if (wiphy->bands[band])
4978 n_channels += wiphy->bands[band]->n_channels;
4979 }
4980
4981 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4982 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4983 tmp)
4984 n_ssids++;
4985
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004986 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004987 return -EINVAL;
4988
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004989 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4990 nla_for_each_nested(attr,
4991 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4992 tmp)
4993 n_match_sets++;
4994
4995 if (n_match_sets > wiphy->max_match_sets)
4996 return -EINVAL;
4997
Luciano Coelho807f8a82011-05-11 17:09:35 +03004998 if (info->attrs[NL80211_ATTR_IE])
4999 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5000 else
5001 ie_len = 0;
5002
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005003 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005004 return -EINVAL;
5005
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005006 mutex_lock(&rdev->sched_scan_mtx);
5007
5008 if (rdev->sched_scan_req) {
5009 err = -EINPROGRESS;
5010 goto out;
5011 }
5012
Luciano Coelho807f8a82011-05-11 17:09:35 +03005013 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005014 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005015 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005016 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005017 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005018 if (!request) {
5019 err = -ENOMEM;
5020 goto out;
5021 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005022
5023 if (n_ssids)
5024 request->ssids = (void *)&request->channels[n_channels];
5025 request->n_ssids = n_ssids;
5026 if (ie_len) {
5027 if (request->ssids)
5028 request->ie = (void *)(request->ssids + n_ssids);
5029 else
5030 request->ie = (void *)(request->channels + n_channels);
5031 }
5032
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005033 if (n_match_sets) {
5034 if (request->ie)
5035 request->match_sets = (void *)(request->ie + ie_len);
5036 else if (request->ssids)
5037 request->match_sets =
5038 (void *)(request->ssids + n_ssids);
5039 else
5040 request->match_sets =
5041 (void *)(request->channels + n_channels);
5042 }
5043 request->n_match_sets = n_match_sets;
5044
Luciano Coelho807f8a82011-05-11 17:09:35 +03005045 i = 0;
5046 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5047 /* user specified, bail out if channel not found */
5048 nla_for_each_nested(attr,
5049 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5050 tmp) {
5051 struct ieee80211_channel *chan;
5052
5053 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5054
5055 if (!chan) {
5056 err = -EINVAL;
5057 goto out_free;
5058 }
5059
5060 /* ignore disabled channels */
5061 if (chan->flags & IEEE80211_CHAN_DISABLED)
5062 continue;
5063
5064 request->channels[i] = chan;
5065 i++;
5066 }
5067 } else {
5068 /* all channels */
5069 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5070 int j;
5071 if (!wiphy->bands[band])
5072 continue;
5073 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5074 struct ieee80211_channel *chan;
5075
5076 chan = &wiphy->bands[band]->channels[j];
5077
5078 if (chan->flags & IEEE80211_CHAN_DISABLED)
5079 continue;
5080
5081 request->channels[i] = chan;
5082 i++;
5083 }
5084 }
5085 }
5086
5087 if (!i) {
5088 err = -EINVAL;
5089 goto out_free;
5090 }
5091
5092 request->n_channels = i;
5093
5094 i = 0;
5095 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5096 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5097 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005098 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005099 err = -EINVAL;
5100 goto out_free;
5101 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005102 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005103 memcpy(request->ssids[i].ssid, nla_data(attr),
5104 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005105 i++;
5106 }
5107 }
5108
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005109 i = 0;
5110 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5111 nla_for_each_nested(attr,
5112 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5113 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005114 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005115
5116 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5117 nla_data(attr), nla_len(attr),
5118 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005119 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005120 if (ssid) {
5121 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5122 err = -EINVAL;
5123 goto out_free;
5124 }
5125 memcpy(request->match_sets[i].ssid.ssid,
5126 nla_data(ssid), nla_len(ssid));
5127 request->match_sets[i].ssid.ssid_len =
5128 nla_len(ssid);
5129 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005130 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5131 if (rssi)
5132 request->rssi_thold = nla_get_u32(rssi);
5133 else
5134 request->rssi_thold =
5135 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005136 i++;
5137 }
5138 }
5139
Luciano Coelho807f8a82011-05-11 17:09:35 +03005140 if (info->attrs[NL80211_ATTR_IE]) {
5141 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5142 memcpy((void *)request->ie,
5143 nla_data(info->attrs[NL80211_ATTR_IE]),
5144 request->ie_len);
5145 }
5146
Sam Leffler46856bb2012-10-11 21:03:32 -07005147 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005148 request->flags = nla_get_u32(
5149 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005150 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5151 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5152 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5153 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005154 err = -EOPNOTSUPP;
5155 goto out_free;
5156 }
5157 }
Sam Lefflered4737712012-10-11 21:03:31 -07005158
Luciano Coelho807f8a82011-05-11 17:09:35 +03005159 request->dev = dev;
5160 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005161 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005162 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005163
Hila Gonene35e4d22012-06-27 17:19:42 +03005164 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005165 if (!err) {
5166 rdev->sched_scan_req = request;
5167 nl80211_send_sched_scan(rdev, dev,
5168 NL80211_CMD_START_SCHED_SCAN);
5169 goto out;
5170 }
5171
5172out_free:
5173 kfree(request);
5174out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005175 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005176 return err;
5177}
5178
5179static int nl80211_stop_sched_scan(struct sk_buff *skb,
5180 struct genl_info *info)
5181{
5182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005183 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005184
5185 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5186 !rdev->ops->sched_scan_stop)
5187 return -EOPNOTSUPP;
5188
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005189 mutex_lock(&rdev->sched_scan_mtx);
5190 err = __cfg80211_stop_sched_scan(rdev, false);
5191 mutex_unlock(&rdev->sched_scan_mtx);
5192
5193 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005194}
5195
Simon Wunderlich04f39042013-02-08 18:16:19 +01005196static int nl80211_start_radar_detection(struct sk_buff *skb,
5197 struct genl_info *info)
5198{
5199 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5200 struct net_device *dev = info->user_ptr[1];
5201 struct wireless_dev *wdev = dev->ieee80211_ptr;
5202 struct cfg80211_chan_def chandef;
5203 int err;
5204
5205 err = nl80211_parse_chandef(rdev, info, &chandef);
5206 if (err)
5207 return err;
5208
5209 if (wdev->cac_started)
5210 return -EBUSY;
5211
5212 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5213 if (err < 0)
5214 return err;
5215
5216 if (err == 0)
5217 return -EINVAL;
5218
5219 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5220 return -EINVAL;
5221
5222 if (!rdev->ops->start_radar_detection)
5223 return -EOPNOTSUPP;
5224
5225 mutex_lock(&rdev->devlist_mtx);
5226 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5227 chandef.chan, CHAN_MODE_SHARED,
5228 BIT(chandef.width));
5229 if (err)
5230 goto err_locked;
5231
5232 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5233 if (!err) {
5234 wdev->channel = chandef.chan;
5235 wdev->cac_started = true;
5236 wdev->cac_start_time = jiffies;
5237 }
5238err_locked:
5239 mutex_unlock(&rdev->devlist_mtx);
5240
5241 return err;
5242}
5243
Johannes Berg9720bb32011-06-21 09:45:33 +02005244static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5245 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005246 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005247 struct wireless_dev *wdev,
5248 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005249{
Johannes Berg48ab9052009-07-10 18:42:31 +02005250 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005251 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005252 void *hdr;
5253 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005254 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005255
5256 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005257
Eric W. Biederman15e47302012-09-07 20:12:54 +00005258 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005259 NL80211_CMD_NEW_SCAN_RESULTS);
5260 if (!hdr)
5261 return -1;
5262
Johannes Berg9720bb32011-06-21 09:45:33 +02005263 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5264
David S. Miller9360ffd2012-03-29 04:41:26 -04005265 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5266 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5267 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005268
5269 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5270 if (!bss)
5271 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005272 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005273 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005274 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005275
5276 rcu_read_lock();
5277 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005278 if (ies) {
5279 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5280 goto fail_unlock_rcu;
5281 tsf = true;
5282 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5283 ies->len, ies->data))
5284 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005285 }
5286 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005287 if (ies) {
5288 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5289 goto fail_unlock_rcu;
5290 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5291 ies->len, ies->data))
5292 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005293 }
5294 rcu_read_unlock();
5295
David S. Miller9360ffd2012-03-29 04:41:26 -04005296 if (res->beacon_interval &&
5297 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5298 goto nla_put_failure;
5299 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5300 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5301 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5302 jiffies_to_msecs(jiffies - intbss->ts)))
5303 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005304
Johannes Berg77965c92009-02-18 18:45:06 +01005305 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005306 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005307 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5308 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005309 break;
5310 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005311 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5312 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005313 break;
5314 default:
5315 break;
5316 }
5317
Johannes Berg48ab9052009-07-10 18:42:31 +02005318 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005319 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005320 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005321 if (intbss == wdev->current_bss &&
5322 nla_put_u32(msg, NL80211_BSS_STATUS,
5323 NL80211_BSS_STATUS_ASSOCIATED))
5324 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005325 break;
5326 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005327 if (intbss == wdev->current_bss &&
5328 nla_put_u32(msg, NL80211_BSS_STATUS,
5329 NL80211_BSS_STATUS_IBSS_JOINED))
5330 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005331 break;
5332 default:
5333 break;
5334 }
5335
Johannes Berg2a519312009-02-10 21:25:55 +01005336 nla_nest_end(msg, bss);
5337
5338 return genlmsg_end(msg, hdr);
5339
Johannes Berg8cef2c92013-02-05 16:54:31 +01005340 fail_unlock_rcu:
5341 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005342 nla_put_failure:
5343 genlmsg_cancel(msg, hdr);
5344 return -EMSGSIZE;
5345}
5346
5347static int nl80211_dump_scan(struct sk_buff *skb,
5348 struct netlink_callback *cb)
5349{
Johannes Berg48ab9052009-07-10 18:42:31 +02005350 struct cfg80211_registered_device *rdev;
5351 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005352 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005353 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005354 int start = cb->args[1], idx = 0;
5355 int err;
5356
Johannes Berg67748892010-10-04 21:14:06 +02005357 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5358 if (err)
5359 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005360
Johannes Berg48ab9052009-07-10 18:42:31 +02005361 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005362
Johannes Berg48ab9052009-07-10 18:42:31 +02005363 wdev_lock(wdev);
5364 spin_lock_bh(&rdev->bss_lock);
5365 cfg80211_bss_expire(rdev);
5366
Johannes Berg9720bb32011-06-21 09:45:33 +02005367 cb->seq = rdev->bss_generation;
5368
Johannes Berg48ab9052009-07-10 18:42:31 +02005369 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005370 if (++idx <= start)
5371 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005372 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005373 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005374 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005375 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005376 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005377 }
5378 }
5379
Johannes Berg48ab9052009-07-10 18:42:31 +02005380 spin_unlock_bh(&rdev->bss_lock);
5381 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005382
5383 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005384 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005385
Johannes Berg67748892010-10-04 21:14:06 +02005386 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005387}
5388
Eric W. Biederman15e47302012-09-07 20:12:54 +00005389static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005390 int flags, struct net_device *dev,
5391 struct survey_info *survey)
5392{
5393 void *hdr;
5394 struct nlattr *infoattr;
5395
Eric W. Biederman15e47302012-09-07 20:12:54 +00005396 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005397 NL80211_CMD_NEW_SURVEY_RESULTS);
5398 if (!hdr)
5399 return -ENOMEM;
5400
David S. Miller9360ffd2012-03-29 04:41:26 -04005401 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5402 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005403
5404 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5405 if (!infoattr)
5406 goto nla_put_failure;
5407
David S. Miller9360ffd2012-03-29 04:41:26 -04005408 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5409 survey->channel->center_freq))
5410 goto nla_put_failure;
5411
5412 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5413 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5414 goto nla_put_failure;
5415 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5416 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5417 goto nla_put_failure;
5418 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5419 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5420 survey->channel_time))
5421 goto nla_put_failure;
5422 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5423 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5424 survey->channel_time_busy))
5425 goto nla_put_failure;
5426 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5427 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5428 survey->channel_time_ext_busy))
5429 goto nla_put_failure;
5430 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5431 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5432 survey->channel_time_rx))
5433 goto nla_put_failure;
5434 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5435 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5436 survey->channel_time_tx))
5437 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005438
5439 nla_nest_end(msg, infoattr);
5440
5441 return genlmsg_end(msg, hdr);
5442
5443 nla_put_failure:
5444 genlmsg_cancel(msg, hdr);
5445 return -EMSGSIZE;
5446}
5447
5448static int nl80211_dump_survey(struct sk_buff *skb,
5449 struct netlink_callback *cb)
5450{
5451 struct survey_info survey;
5452 struct cfg80211_registered_device *dev;
5453 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005454 int survey_idx = cb->args[1];
5455 int res;
5456
Johannes Berg67748892010-10-04 21:14:06 +02005457 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5458 if (res)
5459 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005460
5461 if (!dev->ops->dump_survey) {
5462 res = -EOPNOTSUPP;
5463 goto out_err;
5464 }
5465
5466 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005467 struct ieee80211_channel *chan;
5468
Hila Gonene35e4d22012-06-27 17:19:42 +03005469 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005470 if (res == -ENOENT)
5471 break;
5472 if (res)
5473 goto out_err;
5474
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005475 /* Survey without a channel doesn't make sense */
5476 if (!survey.channel) {
5477 res = -EINVAL;
5478 goto out;
5479 }
5480
5481 chan = ieee80211_get_channel(&dev->wiphy,
5482 survey.channel->center_freq);
5483 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5484 survey_idx++;
5485 continue;
5486 }
5487
Holger Schurig61fa7132009-11-11 12:25:40 +01005488 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005489 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005490 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5491 netdev,
5492 &survey) < 0)
5493 goto out;
5494 survey_idx++;
5495 }
5496
5497 out:
5498 cb->args[1] = survey_idx;
5499 res = skb->len;
5500 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005501 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005502 return res;
5503}
5504
Samuel Ortizb23aa672009-07-01 21:26:54 +02005505static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5506{
5507 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5508 NL80211_WPA_VERSION_2));
5509}
5510
Jouni Malinen636a5d32009-03-19 13:39:22 +02005511static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5512{
Johannes Berg4c476992010-10-04 21:36:35 +02005513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5514 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005515 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005516 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5517 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005518 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005519 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005520 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005521
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005522 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5523 return -EINVAL;
5524
5525 if (!info->attrs[NL80211_ATTR_MAC])
5526 return -EINVAL;
5527
Jouni Malinen17780922009-03-27 20:52:47 +02005528 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5529 return -EINVAL;
5530
Johannes Berg19957bb2009-07-02 17:20:43 +02005531 if (!info->attrs[NL80211_ATTR_SSID])
5532 return -EINVAL;
5533
5534 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5535 return -EINVAL;
5536
Johannes Bergfffd0932009-07-08 14:22:54 +02005537 err = nl80211_parse_key(info, &key);
5538 if (err)
5539 return err;
5540
5541 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005542 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5543 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005544 if (!key.p.key || !key.p.key_len)
5545 return -EINVAL;
5546 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5547 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5548 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5549 key.p.key_len != WLAN_KEY_LEN_WEP104))
5550 return -EINVAL;
5551 if (key.idx > 4)
5552 return -EINVAL;
5553 } else {
5554 key.p.key_len = 0;
5555 key.p.key = NULL;
5556 }
5557
Johannes Bergafea0b72010-08-10 09:46:42 +02005558 if (key.idx >= 0) {
5559 int i;
5560 bool ok = false;
5561 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5562 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5563 ok = true;
5564 break;
5565 }
5566 }
Johannes Berg4c476992010-10-04 21:36:35 +02005567 if (!ok)
5568 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005569 }
5570
Johannes Berg4c476992010-10-04 21:36:35 +02005571 if (!rdev->ops->auth)
5572 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005573
Johannes Berg074ac8d2010-09-16 14:58:22 +02005574 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005575 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5576 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005577
Johannes Berg19957bb2009-07-02 17:20:43 +02005578 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005579 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005580 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005581 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5582 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005583
Johannes Berg19957bb2009-07-02 17:20:43 +02005584 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5585 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5586
5587 if (info->attrs[NL80211_ATTR_IE]) {
5588 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5589 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5590 }
5591
5592 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005593 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005594 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005595
Jouni Malinene39e5b52012-09-30 19:29:39 +03005596 if (auth_type == NL80211_AUTHTYPE_SAE &&
5597 !info->attrs[NL80211_ATTR_SAE_DATA])
5598 return -EINVAL;
5599
5600 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5601 if (auth_type != NL80211_AUTHTYPE_SAE)
5602 return -EINVAL;
5603 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5604 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5605 /* need to include at least Auth Transaction and Status Code */
5606 if (sae_data_len < 4)
5607 return -EINVAL;
5608 }
5609
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005610 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5611
Johannes Berg95de8172012-01-20 13:55:25 +01005612 /*
5613 * Since we no longer track auth state, ignore
5614 * requests to only change local state.
5615 */
5616 if (local_state_change)
5617 return 0;
5618
Johannes Berg4c476992010-10-04 21:36:35 +02005619 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5620 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005621 key.p.key, key.p.key_len, key.idx,
5622 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005623}
5624
Johannes Bergc0692b82010-08-27 14:26:53 +03005625static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5626 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005627 struct cfg80211_crypto_settings *settings,
5628 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005629{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005630 memset(settings, 0, sizeof(*settings));
5631
Samuel Ortizb23aa672009-07-01 21:26:54 +02005632 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5633
Johannes Bergc0692b82010-08-27 14:26:53 +03005634 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5635 u16 proto;
5636 proto = nla_get_u16(
5637 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5638 settings->control_port_ethertype = cpu_to_be16(proto);
5639 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5640 proto != ETH_P_PAE)
5641 return -EINVAL;
5642 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5643 settings->control_port_no_encrypt = true;
5644 } else
5645 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5646
Samuel Ortizb23aa672009-07-01 21:26:54 +02005647 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5648 void *data;
5649 int len, i;
5650
5651 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5652 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5653 settings->n_ciphers_pairwise = len / sizeof(u32);
5654
5655 if (len % sizeof(u32))
5656 return -EINVAL;
5657
Johannes Berg3dc27d22009-07-02 21:36:37 +02005658 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005659 return -EINVAL;
5660
5661 memcpy(settings->ciphers_pairwise, data, len);
5662
5663 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005664 if (!cfg80211_supported_cipher_suite(
5665 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005666 settings->ciphers_pairwise[i]))
5667 return -EINVAL;
5668 }
5669
5670 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5671 settings->cipher_group =
5672 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005673 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5674 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005675 return -EINVAL;
5676 }
5677
5678 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5679 settings->wpa_versions =
5680 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5681 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5682 return -EINVAL;
5683 }
5684
5685 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5686 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005687 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005688
5689 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5690 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5691 settings->n_akm_suites = len / sizeof(u32);
5692
5693 if (len % sizeof(u32))
5694 return -EINVAL;
5695
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005696 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5697 return -EINVAL;
5698
Samuel Ortizb23aa672009-07-01 21:26:54 +02005699 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005700 }
5701
5702 return 0;
5703}
5704
Jouni Malinen636a5d32009-03-19 13:39:22 +02005705static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5706{
Johannes Berg4c476992010-10-04 21:36:35 +02005707 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5708 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005709 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005710 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005711 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005712 int err, ssid_len, ie_len = 0;
5713 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005714 u32 flags = 0;
5715 struct ieee80211_ht_cap *ht_capa = NULL;
5716 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005717
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005718 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5719 return -EINVAL;
5720
5721 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005722 !info->attrs[NL80211_ATTR_SSID] ||
5723 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005724 return -EINVAL;
5725
Johannes Berg4c476992010-10-04 21:36:35 +02005726 if (!rdev->ops->assoc)
5727 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005728
Johannes Berg074ac8d2010-09-16 14:58:22 +02005729 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005730 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5731 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005732
Johannes Berg19957bb2009-07-02 17:20:43 +02005733 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005734
Johannes Berg19957bb2009-07-02 17:20:43 +02005735 chan = ieee80211_get_channel(&rdev->wiphy,
5736 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005737 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5738 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005739
Johannes Berg19957bb2009-07-02 17:20:43 +02005740 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5741 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005742
5743 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005744 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5745 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005746 }
5747
Jouni Malinendc6382c2009-05-06 22:09:37 +03005748 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005749 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005750 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005751 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005752 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005753 else if (mfp != NL80211_MFP_NO)
5754 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005755 }
5756
Johannes Berg3e5d7642009-07-07 14:37:26 +02005757 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5758 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5759
Ben Greear7e7c8922011-11-18 11:31:59 -08005760 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5761 flags |= ASSOC_REQ_DISABLE_HT;
5762
5763 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5764 ht_capa_mask =
5765 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5766
5767 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5768 if (!ht_capa_mask)
5769 return -EINVAL;
5770 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5771 }
5772
Johannes Bergc0692b82010-08-27 14:26:53 +03005773 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005774 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005775 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5776 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005777 &crypto, flags, ht_capa,
5778 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005779
Jouni Malinen636a5d32009-03-19 13:39:22 +02005780 return err;
5781}
5782
5783static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5784{
Johannes Berg4c476992010-10-04 21:36:35 +02005785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5786 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005787 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005788 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005789 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005790 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005791
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005792 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5793 return -EINVAL;
5794
5795 if (!info->attrs[NL80211_ATTR_MAC])
5796 return -EINVAL;
5797
5798 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5799 return -EINVAL;
5800
Johannes Berg4c476992010-10-04 21:36:35 +02005801 if (!rdev->ops->deauth)
5802 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005803
Johannes Berg074ac8d2010-09-16 14:58:22 +02005804 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005805 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5806 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005807
Johannes Berg19957bb2009-07-02 17:20:43 +02005808 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005809
Johannes Berg19957bb2009-07-02 17:20:43 +02005810 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5811 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005812 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005813 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005814 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005815
5816 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005817 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5818 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005819 }
5820
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005821 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5822
Johannes Berg4c476992010-10-04 21:36:35 +02005823 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5824 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005825}
5826
5827static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5828{
Johannes Berg4c476992010-10-04 21:36:35 +02005829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5830 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005831 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005832 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005833 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005834 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005835
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005836 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5837 return -EINVAL;
5838
5839 if (!info->attrs[NL80211_ATTR_MAC])
5840 return -EINVAL;
5841
5842 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5843 return -EINVAL;
5844
Johannes Berg4c476992010-10-04 21:36:35 +02005845 if (!rdev->ops->disassoc)
5846 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005847
Johannes Berg074ac8d2010-09-16 14:58:22 +02005848 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005849 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5850 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005851
Johannes Berg19957bb2009-07-02 17:20:43 +02005852 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005853
Johannes Berg19957bb2009-07-02 17:20:43 +02005854 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5855 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005856 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005857 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005858 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005859
5860 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005861 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5862 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005863 }
5864
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005865 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5866
Johannes Berg4c476992010-10-04 21:36:35 +02005867 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5868 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005869}
5870
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005871static bool
5872nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5873 int mcast_rate[IEEE80211_NUM_BANDS],
5874 int rateval)
5875{
5876 struct wiphy *wiphy = &rdev->wiphy;
5877 bool found = false;
5878 int band, i;
5879
5880 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5881 struct ieee80211_supported_band *sband;
5882
5883 sband = wiphy->bands[band];
5884 if (!sband)
5885 continue;
5886
5887 for (i = 0; i < sband->n_bitrates; i++) {
5888 if (sband->bitrates[i].bitrate == rateval) {
5889 mcast_rate[band] = i + 1;
5890 found = true;
5891 break;
5892 }
5893 }
5894 }
5895
5896 return found;
5897}
5898
Johannes Berg04a773a2009-04-19 21:24:32 +02005899static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5900{
Johannes Berg4c476992010-10-04 21:36:35 +02005901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5902 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005903 struct cfg80211_ibss_params ibss;
5904 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005905 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005906 int err;
5907
Johannes Berg8e30bc52009-04-22 17:45:38 +02005908 memset(&ibss, 0, sizeof(ibss));
5909
Johannes Berg04a773a2009-04-19 21:24:32 +02005910 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5911 return -EINVAL;
5912
Johannes Berg683b6d32012-11-08 21:25:48 +01005913 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005914 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5915 return -EINVAL;
5916
Johannes Berg8e30bc52009-04-22 17:45:38 +02005917 ibss.beacon_interval = 100;
5918
5919 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5920 ibss.beacon_interval =
5921 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5922 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5923 return -EINVAL;
5924 }
5925
Johannes Berg4c476992010-10-04 21:36:35 +02005926 if (!rdev->ops->join_ibss)
5927 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005928
Johannes Berg4c476992010-10-04 21:36:35 +02005929 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5930 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005931
Johannes Berg79c97e92009-07-07 03:56:12 +02005932 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005933
Johannes Berg39193492011-09-16 13:45:25 +02005934 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005935 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005936
5937 if (!is_valid_ether_addr(ibss.bssid))
5938 return -EINVAL;
5939 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005940 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5941 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5942
5943 if (info->attrs[NL80211_ATTR_IE]) {
5944 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5945 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5946 }
5947
Johannes Berg683b6d32012-11-08 21:25:48 +01005948 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5949 if (err)
5950 return err;
Alexander Simon54858ee2011-11-30 16:56:32 +01005951
Johannes Berg683b6d32012-11-08 21:25:48 +01005952 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee2011-11-30 16:56:32 +01005953 return -EINVAL;
5954
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005955 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5956 return -EINVAL;
5957 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5958 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005959 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005960
Johannes Berg04a773a2009-04-19 21:24:32 +02005961 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005962 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005963
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005964 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5965 u8 *rates =
5966 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5967 int n_rates =
5968 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5969 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005970 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005971
Johannes Berg34850ab2011-07-18 18:08:35 +02005972 err = ieee80211_get_ratemask(sband, rates, n_rates,
5973 &ibss.basic_rates);
5974 if (err)
5975 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005976 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005977
5978 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5979 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5980 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5981 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005982
Johannes Berg4c476992010-10-04 21:36:35 +02005983 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305984 bool no_ht = false;
5985
Johannes Berg4c476992010-10-04 21:36:35 +02005986 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305987 info->attrs[NL80211_ATTR_KEYS],
5988 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005989 if (IS_ERR(connkeys))
5990 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305991
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005992 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5993 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305994 kfree(connkeys);
5995 return -EINVAL;
5996 }
Johannes Berg4c476992010-10-04 21:36:35 +02005997 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005998
Antonio Quartulli267335d2012-01-31 20:25:47 +01005999 ibss.control_port =
6000 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6001
Johannes Berg4c476992010-10-04 21:36:35 +02006002 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006003 if (err)
6004 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006005 return err;
6006}
6007
6008static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6009{
Johannes Berg4c476992010-10-04 21:36:35 +02006010 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6011 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006012
Johannes Berg4c476992010-10-04 21:36:35 +02006013 if (!rdev->ops->leave_ibss)
6014 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006015
Johannes Berg4c476992010-10-04 21:36:35 +02006016 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6017 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006018
Johannes Berg4c476992010-10-04 21:36:35 +02006019 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006020}
6021
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006022static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6023{
6024 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6025 struct net_device *dev = info->user_ptr[1];
6026 int mcast_rate[IEEE80211_NUM_BANDS];
6027 u32 nla_rate;
6028 int err;
6029
6030 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6031 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6032 return -EOPNOTSUPP;
6033
6034 if (!rdev->ops->set_mcast_rate)
6035 return -EOPNOTSUPP;
6036
6037 memset(mcast_rate, 0, sizeof(mcast_rate));
6038
6039 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6040 return -EINVAL;
6041
6042 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6043 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6044 return -EINVAL;
6045
6046 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6047
6048 return err;
6049}
6050
6051
Johannes Bergaff89a92009-07-01 21:26:51 +02006052#ifdef CONFIG_NL80211_TESTMODE
6053static struct genl_multicast_group nl80211_testmode_mcgrp = {
6054 .name = "testmode",
6055};
6056
6057static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6058{
Johannes Berg4c476992010-10-04 21:36:35 +02006059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006060 int err;
6061
6062 if (!info->attrs[NL80211_ATTR_TESTDATA])
6063 return -EINVAL;
6064
Johannes Bergaff89a92009-07-01 21:26:51 +02006065 err = -EOPNOTSUPP;
6066 if (rdev->ops->testmode_cmd) {
6067 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006068 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006069 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6070 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6071 rdev->testmode_info = NULL;
6072 }
6073
Johannes Bergaff89a92009-07-01 21:26:51 +02006074 return err;
6075}
6076
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006077static int nl80211_testmode_dump(struct sk_buff *skb,
6078 struct netlink_callback *cb)
6079{
Johannes Berg00918d32011-12-13 17:22:05 +01006080 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006081 int err;
6082 long phy_idx;
6083 void *data = NULL;
6084 int data_len = 0;
6085
6086 if (cb->args[0]) {
6087 /*
6088 * 0 is a valid index, but not valid for args[0],
6089 * so we need to offset by 1.
6090 */
6091 phy_idx = cb->args[0] - 1;
6092 } else {
6093 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6094 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6095 nl80211_policy);
6096 if (err)
6097 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006098
Johannes Berg2bd7e352012-06-15 14:23:16 +02006099 mutex_lock(&cfg80211_mutex);
6100 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6101 nl80211_fam.attrbuf);
6102 if (IS_ERR(rdev)) {
6103 mutex_unlock(&cfg80211_mutex);
6104 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006105 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006106 phy_idx = rdev->wiphy_idx;
6107 rdev = NULL;
6108 mutex_unlock(&cfg80211_mutex);
6109
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006110 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6111 cb->args[1] =
6112 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6113 }
6114
6115 if (cb->args[1]) {
6116 data = nla_data((void *)cb->args[1]);
6117 data_len = nla_len((void *)cb->args[1]);
6118 }
6119
6120 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006121 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6122 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006123 mutex_unlock(&cfg80211_mutex);
6124 return -ENOENT;
6125 }
Johannes Berg00918d32011-12-13 17:22:05 +01006126 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006127 mutex_unlock(&cfg80211_mutex);
6128
Johannes Berg00918d32011-12-13 17:22:05 +01006129 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006130 err = -EOPNOTSUPP;
6131 goto out_err;
6132 }
6133
6134 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006135 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006136 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6137 NL80211_CMD_TESTMODE);
6138 struct nlattr *tmdata;
6139
David S. Miller9360ffd2012-03-29 04:41:26 -04006140 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006141 genlmsg_cancel(skb, hdr);
6142 break;
6143 }
6144
6145 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6146 if (!tmdata) {
6147 genlmsg_cancel(skb, hdr);
6148 break;
6149 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006150 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006151 nla_nest_end(skb, tmdata);
6152
6153 if (err == -ENOBUFS || err == -ENOENT) {
6154 genlmsg_cancel(skb, hdr);
6155 break;
6156 } else if (err) {
6157 genlmsg_cancel(skb, hdr);
6158 goto out_err;
6159 }
6160
6161 genlmsg_end(skb, hdr);
6162 }
6163
6164 err = skb->len;
6165 /* see above */
6166 cb->args[0] = phy_idx + 1;
6167 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006168 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006169 return err;
6170}
6171
Johannes Bergaff89a92009-07-01 21:26:51 +02006172static struct sk_buff *
6173__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006174 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006175{
6176 struct sk_buff *skb;
6177 void *hdr;
6178 struct nlattr *data;
6179
6180 skb = nlmsg_new(approxlen + 100, gfp);
6181 if (!skb)
6182 return NULL;
6183
Eric W. Biederman15e47302012-09-07 20:12:54 +00006184 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006185 if (!hdr) {
6186 kfree_skb(skb);
6187 return NULL;
6188 }
6189
David S. Miller9360ffd2012-03-29 04:41:26 -04006190 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6191 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006192 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6193
6194 ((void **)skb->cb)[0] = rdev;
6195 ((void **)skb->cb)[1] = hdr;
6196 ((void **)skb->cb)[2] = data;
6197
6198 return skb;
6199
6200 nla_put_failure:
6201 kfree_skb(skb);
6202 return NULL;
6203}
6204
6205struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6206 int approxlen)
6207{
6208 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6209
6210 if (WARN_ON(!rdev->testmode_info))
6211 return NULL;
6212
6213 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006214 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006215 rdev->testmode_info->snd_seq,
6216 GFP_KERNEL);
6217}
6218EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6219
6220int cfg80211_testmode_reply(struct sk_buff *skb)
6221{
6222 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6223 void *hdr = ((void **)skb->cb)[1];
6224 struct nlattr *data = ((void **)skb->cb)[2];
6225
6226 if (WARN_ON(!rdev->testmode_info)) {
6227 kfree_skb(skb);
6228 return -EINVAL;
6229 }
6230
6231 nla_nest_end(skb, data);
6232 genlmsg_end(skb, hdr);
6233 return genlmsg_reply(skb, rdev->testmode_info);
6234}
6235EXPORT_SYMBOL(cfg80211_testmode_reply);
6236
6237struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6238 int approxlen, gfp_t gfp)
6239{
6240 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6241
6242 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6243}
6244EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6245
6246void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6247{
6248 void *hdr = ((void **)skb->cb)[1];
6249 struct nlattr *data = ((void **)skb->cb)[2];
6250
6251 nla_nest_end(skb, data);
6252 genlmsg_end(skb, hdr);
6253 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6254}
6255EXPORT_SYMBOL(cfg80211_testmode_event);
6256#endif
6257
Samuel Ortizb23aa672009-07-01 21:26:54 +02006258static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6259{
Johannes Berg4c476992010-10-04 21:36:35 +02006260 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6261 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006262 struct cfg80211_connect_params connect;
6263 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006264 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006265 int err;
6266
6267 memset(&connect, 0, sizeof(connect));
6268
6269 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6270 return -EINVAL;
6271
6272 if (!info->attrs[NL80211_ATTR_SSID] ||
6273 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6274 return -EINVAL;
6275
6276 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6277 connect.auth_type =
6278 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006279 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6280 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006281 return -EINVAL;
6282 } else
6283 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6284
6285 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6286
Johannes Bergc0692b82010-08-27 14:26:53 +03006287 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006288 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006289 if (err)
6290 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006291
Johannes Berg074ac8d2010-09-16 14:58:22 +02006292 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006293 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6294 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006295
Johannes Berg79c97e92009-07-07 03:56:12 +02006296 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006297
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306298 connect.bg_scan_period = -1;
6299 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6300 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6301 connect.bg_scan_period =
6302 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6303 }
6304
Samuel Ortizb23aa672009-07-01 21:26:54 +02006305 if (info->attrs[NL80211_ATTR_MAC])
6306 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6307 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6308 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6309
6310 if (info->attrs[NL80211_ATTR_IE]) {
6311 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6312 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6313 }
6314
Jouni Malinencee00a92013-01-15 17:15:57 +02006315 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6316 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6317 if (connect.mfp != NL80211_MFP_REQUIRED &&
6318 connect.mfp != NL80211_MFP_NO)
6319 return -EINVAL;
6320 } else {
6321 connect.mfp = NL80211_MFP_NO;
6322 }
6323
Samuel Ortizb23aa672009-07-01 21:26:54 +02006324 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6325 connect.channel =
6326 ieee80211_get_channel(wiphy,
6327 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6328 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006329 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6330 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006331 }
6332
Johannes Bergfffd0932009-07-08 14:22:54 +02006333 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6334 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306335 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006336 if (IS_ERR(connkeys))
6337 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006338 }
6339
Ben Greear7e7c8922011-11-18 11:31:59 -08006340 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6341 connect.flags |= ASSOC_REQ_DISABLE_HT;
6342
6343 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6344 memcpy(&connect.ht_capa_mask,
6345 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6346 sizeof(connect.ht_capa_mask));
6347
6348 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006349 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6350 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006351 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006352 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006353 memcpy(&connect.ht_capa,
6354 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6355 sizeof(connect.ht_capa));
6356 }
6357
Johannes Bergfffd0932009-07-08 14:22:54 +02006358 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006359 if (err)
6360 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006361 return err;
6362}
6363
6364static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6365{
Johannes Berg4c476992010-10-04 21:36:35 +02006366 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6367 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006368 u16 reason;
6369
6370 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6371 reason = WLAN_REASON_DEAUTH_LEAVING;
6372 else
6373 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6374
6375 if (reason == 0)
6376 return -EINVAL;
6377
Johannes Berg074ac8d2010-09-16 14:58:22 +02006378 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006379 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6380 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006381
Johannes Berg4c476992010-10-04 21:36:35 +02006382 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006383}
6384
Johannes Berg463d0182009-07-14 00:33:35 +02006385static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6386{
Johannes Berg4c476992010-10-04 21:36:35 +02006387 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006388 struct net *net;
6389 int err;
6390 u32 pid;
6391
6392 if (!info->attrs[NL80211_ATTR_PID])
6393 return -EINVAL;
6394
6395 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6396
Johannes Berg463d0182009-07-14 00:33:35 +02006397 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006398 if (IS_ERR(net))
6399 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006400
6401 err = 0;
6402
6403 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006404 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6405 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006406
Johannes Berg463d0182009-07-14 00:33:35 +02006407 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006408 return err;
6409}
6410
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006411static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6412{
Johannes Berg4c476992010-10-04 21:36:35 +02006413 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006414 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6415 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006416 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006417 struct cfg80211_pmksa pmksa;
6418
6419 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6420
6421 if (!info->attrs[NL80211_ATTR_MAC])
6422 return -EINVAL;
6423
6424 if (!info->attrs[NL80211_ATTR_PMKID])
6425 return -EINVAL;
6426
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006427 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6428 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6429
Johannes Berg074ac8d2010-09-16 14:58:22 +02006430 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006431 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6432 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006433
6434 switch (info->genlhdr->cmd) {
6435 case NL80211_CMD_SET_PMKSA:
6436 rdev_ops = rdev->ops->set_pmksa;
6437 break;
6438 case NL80211_CMD_DEL_PMKSA:
6439 rdev_ops = rdev->ops->del_pmksa;
6440 break;
6441 default:
6442 WARN_ON(1);
6443 break;
6444 }
6445
Johannes Berg4c476992010-10-04 21:36:35 +02006446 if (!rdev_ops)
6447 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006448
Johannes Berg4c476992010-10-04 21:36:35 +02006449 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006450}
6451
6452static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6453{
Johannes Berg4c476992010-10-04 21:36:35 +02006454 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6455 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006456
Johannes Berg074ac8d2010-09-16 14:58:22 +02006457 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006458 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6459 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006460
Johannes Berg4c476992010-10-04 21:36:35 +02006461 if (!rdev->ops->flush_pmksa)
6462 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006463
Hila Gonene35e4d22012-06-27 17:19:42 +03006464 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006465}
6466
Arik Nemtsov109086c2011-09-28 14:12:50 +03006467static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6468{
6469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6470 struct net_device *dev = info->user_ptr[1];
6471 u8 action_code, dialog_token;
6472 u16 status_code;
6473 u8 *peer;
6474
6475 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6476 !rdev->ops->tdls_mgmt)
6477 return -EOPNOTSUPP;
6478
6479 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6480 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6481 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6482 !info->attrs[NL80211_ATTR_IE] ||
6483 !info->attrs[NL80211_ATTR_MAC])
6484 return -EINVAL;
6485
6486 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6487 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6488 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6489 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6490
Hila Gonene35e4d22012-06-27 17:19:42 +03006491 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6492 dialog_token, status_code,
6493 nla_data(info->attrs[NL80211_ATTR_IE]),
6494 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006495}
6496
6497static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6498{
6499 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6500 struct net_device *dev = info->user_ptr[1];
6501 enum nl80211_tdls_operation operation;
6502 u8 *peer;
6503
6504 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6505 !rdev->ops->tdls_oper)
6506 return -EOPNOTSUPP;
6507
6508 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6509 !info->attrs[NL80211_ATTR_MAC])
6510 return -EINVAL;
6511
6512 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6513 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6514
Hila Gonene35e4d22012-06-27 17:19:42 +03006515 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006516}
6517
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006518static int nl80211_remain_on_channel(struct sk_buff *skb,
6519 struct genl_info *info)
6520{
Johannes Berg4c476992010-10-04 21:36:35 +02006521 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006522 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006523 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006524 struct sk_buff *msg;
6525 void *hdr;
6526 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006527 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006528 int err;
6529
6530 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6531 !info->attrs[NL80211_ATTR_DURATION])
6532 return -EINVAL;
6533
6534 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6535
Johannes Berg7c4ef712011-11-18 15:33:48 +01006536 if (!rdev->ops->remain_on_channel ||
6537 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006538 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006539
Johannes Bergebf348f2012-06-01 12:50:54 +02006540 /*
6541 * We should be on that channel for at least a minimum amount of
6542 * time (10ms) but no longer than the driver supports.
6543 */
6544 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6545 duration > rdev->wiphy.max_remain_on_channel_duration)
6546 return -EINVAL;
6547
Johannes Berg683b6d32012-11-08 21:25:48 +01006548 err = nl80211_parse_chandef(rdev, info, &chandef);
6549 if (err)
6550 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006551
6552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006553 if (!msg)
6554 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006555
Eric W. Biederman15e47302012-09-07 20:12:54 +00006556 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006557 NL80211_CMD_REMAIN_ON_CHANNEL);
6558
6559 if (IS_ERR(hdr)) {
6560 err = PTR_ERR(hdr);
6561 goto free_msg;
6562 }
6563
Johannes Berg683b6d32012-11-08 21:25:48 +01006564 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6565 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006566
6567 if (err)
6568 goto free_msg;
6569
David S. Miller9360ffd2012-03-29 04:41:26 -04006570 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6571 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006572
6573 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006574
6575 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006576
6577 nla_put_failure:
6578 err = -ENOBUFS;
6579 free_msg:
6580 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006581 return err;
6582}
6583
6584static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6585 struct genl_info *info)
6586{
Johannes Berg4c476992010-10-04 21:36:35 +02006587 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006588 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006589 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006590
6591 if (!info->attrs[NL80211_ATTR_COOKIE])
6592 return -EINVAL;
6593
Johannes Berg4c476992010-10-04 21:36:35 +02006594 if (!rdev->ops->cancel_remain_on_channel)
6595 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006596
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006597 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6598
Hila Gonene35e4d22012-06-27 17:19:42 +03006599 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006600}
6601
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006602static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6603 u8 *rates, u8 rates_len)
6604{
6605 u8 i;
6606 u32 mask = 0;
6607
6608 for (i = 0; i < rates_len; i++) {
6609 int rate = (rates[i] & 0x7f) * 5;
6610 int ridx;
6611 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6612 struct ieee80211_rate *srate =
6613 &sband->bitrates[ridx];
6614 if (rate == srate->bitrate) {
6615 mask |= 1 << ridx;
6616 break;
6617 }
6618 }
6619 if (ridx == sband->n_bitrates)
6620 return 0; /* rate not found */
6621 }
6622
6623 return mask;
6624}
6625
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006626static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6627 u8 *rates, u8 rates_len,
6628 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6629{
6630 u8 i;
6631
6632 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6633
6634 for (i = 0; i < rates_len; i++) {
6635 int ridx, rbit;
6636
6637 ridx = rates[i] / 8;
6638 rbit = BIT(rates[i] % 8);
6639
6640 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006641 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006642 return false;
6643
6644 /* check availability */
6645 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6646 mcs[ridx] |= rbit;
6647 else
6648 return false;
6649 }
6650
6651 return true;
6652}
6653
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006654static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006655 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6656 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006657 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6658 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006659};
6660
6661static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6662 struct genl_info *info)
6663{
6664 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006665 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006666 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006667 int rem, i;
6668 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006669 struct nlattr *tx_rates;
6670 struct ieee80211_supported_band *sband;
6671
6672 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6673 return -EINVAL;
6674
Johannes Berg4c476992010-10-04 21:36:35 +02006675 if (!rdev->ops->set_bitrate_mask)
6676 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006677
6678 memset(&mask, 0, sizeof(mask));
6679 /* Default to all rates enabled */
6680 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6681 sband = rdev->wiphy.bands[i];
6682 mask.control[i].legacy =
6683 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006684 if (sband)
6685 memcpy(mask.control[i].mcs,
6686 sband->ht_cap.mcs.rx_mask,
6687 sizeof(mask.control[i].mcs));
6688 else
6689 memset(mask.control[i].mcs, 0,
6690 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006691 }
6692
6693 /*
6694 * The nested attribute uses enum nl80211_band as the index. This maps
6695 * directly to the enum ieee80211_band values used in cfg80211.
6696 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006697 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006698 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6699 {
6700 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006701 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6702 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006703 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006704 if (sband == NULL)
6705 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006706 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6707 nla_len(tx_rates), nl80211_txattr_policy);
6708 if (tb[NL80211_TXRATE_LEGACY]) {
6709 mask.control[band].legacy = rateset_to_mask(
6710 sband,
6711 nla_data(tb[NL80211_TXRATE_LEGACY]),
6712 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306713 if ((mask.control[band].legacy == 0) &&
6714 nla_len(tb[NL80211_TXRATE_LEGACY]))
6715 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006716 }
6717 if (tb[NL80211_TXRATE_MCS]) {
6718 if (!ht_rateset_to_mask(
6719 sband,
6720 nla_data(tb[NL80211_TXRATE_MCS]),
6721 nla_len(tb[NL80211_TXRATE_MCS]),
6722 mask.control[band].mcs))
6723 return -EINVAL;
6724 }
6725
6726 if (mask.control[band].legacy == 0) {
6727 /* don't allow empty legacy rates if HT
6728 * is not even supported. */
6729 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6730 return -EINVAL;
6731
6732 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6733 if (mask.control[band].mcs[i])
6734 break;
6735
6736 /* legacy and mcs rates may not be both empty */
6737 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006738 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006739 }
6740 }
6741
Hila Gonene35e4d22012-06-27 17:19:42 +03006742 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006743}
6744
Johannes Berg2e161f72010-08-12 15:38:38 +02006745static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006746{
Johannes Berg4c476992010-10-04 21:36:35 +02006747 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006748 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006749 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006750
6751 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6752 return -EINVAL;
6753
Johannes Berg2e161f72010-08-12 15:38:38 +02006754 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6755 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006756
Johannes Berg71bbc992012-06-15 15:30:18 +02006757 switch (wdev->iftype) {
6758 case NL80211_IFTYPE_STATION:
6759 case NL80211_IFTYPE_ADHOC:
6760 case NL80211_IFTYPE_P2P_CLIENT:
6761 case NL80211_IFTYPE_AP:
6762 case NL80211_IFTYPE_AP_VLAN:
6763 case NL80211_IFTYPE_MESH_POINT:
6764 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006765 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006766 break;
6767 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006768 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006769 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006770
6771 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006772 if (!rdev->ops->mgmt_tx)
6773 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006774
Eric W. Biederman15e47302012-09-07 20:12:54 +00006775 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006776 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6777 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006778}
6779
Johannes Berg2e161f72010-08-12 15:38:38 +02006780static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006781{
Johannes Berg4c476992010-10-04 21:36:35 +02006782 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006783 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006784 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006785 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006786 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006787 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006788 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006789 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006790 bool offchan, no_cck, dont_wait_for_ack;
6791
6792 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006793
Johannes Berg683b6d32012-11-08 21:25:48 +01006794 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006795 return -EINVAL;
6796
Johannes Berg4c476992010-10-04 21:36:35 +02006797 if (!rdev->ops->mgmt_tx)
6798 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006799
Johannes Berg71bbc992012-06-15 15:30:18 +02006800 switch (wdev->iftype) {
6801 case NL80211_IFTYPE_STATION:
6802 case NL80211_IFTYPE_ADHOC:
6803 case NL80211_IFTYPE_P2P_CLIENT:
6804 case NL80211_IFTYPE_AP:
6805 case NL80211_IFTYPE_AP_VLAN:
6806 case NL80211_IFTYPE_MESH_POINT:
6807 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006808 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006809 break;
6810 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006811 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006812 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006813
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006814 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006815 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006816 return -EINVAL;
6817 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006818
6819 /*
6820 * We should wait on the channel for at least a minimum amount
6821 * of time (10ms) but no longer than the driver supports.
6822 */
6823 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6824 wait > rdev->wiphy.max_remain_on_channel_duration)
6825 return -EINVAL;
6826
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006827 }
6828
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006829 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6830
Johannes Berg7c4ef712011-11-18 15:33:48 +01006831 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6832 return -EINVAL;
6833
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306834 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6835
Johannes Berg683b6d32012-11-08 21:25:48 +01006836 err = nl80211_parse_chandef(rdev, info, &chandef);
6837 if (err)
6838 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006839
Johannes Berge247bd902011-11-04 11:18:21 +01006840 if (!dont_wait_for_ack) {
6841 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6842 if (!msg)
6843 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006844
Eric W. Biederman15e47302012-09-07 20:12:54 +00006845 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006846 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006847
Johannes Berge247bd902011-11-04 11:18:21 +01006848 if (IS_ERR(hdr)) {
6849 err = PTR_ERR(hdr);
6850 goto free_msg;
6851 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006852 }
Johannes Berge247bd902011-11-04 11:18:21 +01006853
Johannes Berg683b6d32012-11-08 21:25:48 +01006854 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006855 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6856 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006857 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006858 if (err)
6859 goto free_msg;
6860
Johannes Berge247bd902011-11-04 11:18:21 +01006861 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006862 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6863 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006864
Johannes Berge247bd902011-11-04 11:18:21 +01006865 genlmsg_end(msg, hdr);
6866 return genlmsg_reply(msg, info);
6867 }
6868
6869 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006870
6871 nla_put_failure:
6872 err = -ENOBUFS;
6873 free_msg:
6874 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006875 return err;
6876}
6877
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006878static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6879{
6880 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006881 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006882 u64 cookie;
6883
6884 if (!info->attrs[NL80211_ATTR_COOKIE])
6885 return -EINVAL;
6886
6887 if (!rdev->ops->mgmt_tx_cancel_wait)
6888 return -EOPNOTSUPP;
6889
Johannes Berg71bbc992012-06-15 15:30:18 +02006890 switch (wdev->iftype) {
6891 case NL80211_IFTYPE_STATION:
6892 case NL80211_IFTYPE_ADHOC:
6893 case NL80211_IFTYPE_P2P_CLIENT:
6894 case NL80211_IFTYPE_AP:
6895 case NL80211_IFTYPE_AP_VLAN:
6896 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006897 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006898 break;
6899 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006900 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006901 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006902
6903 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6904
Hila Gonene35e4d22012-06-27 17:19:42 +03006905 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006906}
6907
Kalle Valoffb9eb32010-02-17 17:58:10 +02006908static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6909{
Johannes Berg4c476992010-10-04 21:36:35 +02006910 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006911 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006912 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006913 u8 ps_state;
6914 bool state;
6915 int err;
6916
Johannes Berg4c476992010-10-04 21:36:35 +02006917 if (!info->attrs[NL80211_ATTR_PS_STATE])
6918 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006919
6920 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6921
Johannes Berg4c476992010-10-04 21:36:35 +02006922 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6923 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006924
6925 wdev = dev->ieee80211_ptr;
6926
Johannes Berg4c476992010-10-04 21:36:35 +02006927 if (!rdev->ops->set_power_mgmt)
6928 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006929
6930 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6931
6932 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006933 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006934
Hila Gonene35e4d22012-06-27 17:19:42 +03006935 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006936 if (!err)
6937 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006938 return err;
6939}
6940
6941static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6942{
Johannes Berg4c476992010-10-04 21:36:35 +02006943 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006944 enum nl80211_ps_state ps_state;
6945 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006946 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006947 struct sk_buff *msg;
6948 void *hdr;
6949 int err;
6950
Kalle Valoffb9eb32010-02-17 17:58:10 +02006951 wdev = dev->ieee80211_ptr;
6952
Johannes Berg4c476992010-10-04 21:36:35 +02006953 if (!rdev->ops->set_power_mgmt)
6954 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006955
6956 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006957 if (!msg)
6958 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006959
Eric W. Biederman15e47302012-09-07 20:12:54 +00006960 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006961 NL80211_CMD_GET_POWER_SAVE);
6962 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006963 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006964 goto free_msg;
6965 }
6966
6967 if (wdev->ps)
6968 ps_state = NL80211_PS_ENABLED;
6969 else
6970 ps_state = NL80211_PS_DISABLED;
6971
David S. Miller9360ffd2012-03-29 04:41:26 -04006972 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6973 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006974
6975 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006976 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006977
Johannes Berg4c476992010-10-04 21:36:35 +02006978 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006979 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006980 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006981 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006982 return err;
6983}
6984
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006985static struct nla_policy
6986nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6987 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6988 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6989 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006990 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6991 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6992 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006993};
6994
Thomas Pedersen84f10702012-07-12 16:17:33 -07006995static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006996 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006997{
6998 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6999 struct wireless_dev *wdev;
7000 struct net_device *dev = info->user_ptr[1];
7001
Johannes Bergd9d8b012012-11-26 12:51:52 +01007002 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007003 return -EINVAL;
7004
7005 wdev = dev->ieee80211_ptr;
7006
7007 if (!rdev->ops->set_cqm_txe_config)
7008 return -EOPNOTSUPP;
7009
7010 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7011 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7012 return -EOPNOTSUPP;
7013
Hila Gonene35e4d22012-06-27 17:19:42 +03007014 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007015}
7016
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007017static int nl80211_set_cqm_rssi(struct genl_info *info,
7018 s32 threshold, u32 hysteresis)
7019{
Johannes Berg4c476992010-10-04 21:36:35 +02007020 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007021 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007022 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007023
7024 if (threshold > 0)
7025 return -EINVAL;
7026
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007027 wdev = dev->ieee80211_ptr;
7028
Johannes Berg4c476992010-10-04 21:36:35 +02007029 if (!rdev->ops->set_cqm_rssi_config)
7030 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007031
Johannes Berg074ac8d2010-09-16 14:58:22 +02007032 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007033 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7034 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007035
Hila Gonene35e4d22012-06-27 17:19:42 +03007036 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007037}
7038
7039static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7040{
7041 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7042 struct nlattr *cqm;
7043 int err;
7044
7045 cqm = info->attrs[NL80211_ATTR_CQM];
7046 if (!cqm) {
7047 err = -EINVAL;
7048 goto out;
7049 }
7050
7051 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7052 nl80211_attr_cqm_policy);
7053 if (err)
7054 goto out;
7055
7056 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7057 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7058 s32 threshold;
7059 u32 hysteresis;
7060 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7061 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7062 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007063 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7064 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7065 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7066 u32 rate, pkts, intvl;
7067 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7068 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7069 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7070 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007071 } else
7072 err = -EINVAL;
7073
7074out:
7075 return err;
7076}
7077
Johannes Berg29cbe682010-12-03 09:20:44 +01007078static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7079{
7080 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7081 struct net_device *dev = info->user_ptr[1];
7082 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007083 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007084 int err;
7085
7086 /* start with default */
7087 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007088 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007089
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007090 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007091 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007092 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007093 if (err)
7094 return err;
7095 }
7096
7097 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7098 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7099 return -EINVAL;
7100
Javier Cardonac80d5452010-12-16 17:37:49 -08007101 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7102 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7103
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007104 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7105 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7106 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7107 return -EINVAL;
7108
Marco Porsch9bdbf042013-01-07 16:04:51 +01007109 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7110 setup.beacon_interval =
7111 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7112 if (setup.beacon_interval < 10 ||
7113 setup.beacon_interval > 10000)
7114 return -EINVAL;
7115 }
7116
7117 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7118 setup.dtim_period =
7119 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7120 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7121 return -EINVAL;
7122 }
7123
Javier Cardonac80d5452010-12-16 17:37:49 -08007124 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7125 /* parse additional setup parameters if given */
7126 err = nl80211_parse_mesh_setup(info, &setup);
7127 if (err)
7128 return err;
7129 }
7130
Johannes Bergcc1d2802012-05-16 23:50:20 +02007131 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007132 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7133 if (err)
7134 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007135 } else {
7136 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007137 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007138 }
7139
Javier Cardonac80d5452010-12-16 17:37:49 -08007140 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007141}
7142
7143static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7144{
7145 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7146 struct net_device *dev = info->user_ptr[1];
7147
7148 return cfg80211_leave_mesh(rdev, dev);
7149}
7150
Johannes Bergdfb89c52012-06-27 09:23:48 +02007151#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007152static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7153 struct cfg80211_registered_device *rdev)
7154{
7155 struct nlattr *nl_pats, *nl_pat;
7156 int i, pat_len;
7157
7158 if (!rdev->wowlan->n_patterns)
7159 return 0;
7160
7161 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7162 if (!nl_pats)
7163 return -ENOBUFS;
7164
7165 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7166 nl_pat = nla_nest_start(msg, i + 1);
7167 if (!nl_pat)
7168 return -ENOBUFS;
7169 pat_len = rdev->wowlan->patterns[i].pattern_len;
7170 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7171 DIV_ROUND_UP(pat_len, 8),
7172 rdev->wowlan->patterns[i].mask) ||
7173 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7174 pat_len, rdev->wowlan->patterns[i].pattern) ||
7175 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7176 rdev->wowlan->patterns[i].pkt_offset))
7177 return -ENOBUFS;
7178 nla_nest_end(msg, nl_pat);
7179 }
7180 nla_nest_end(msg, nl_pats);
7181
7182 return 0;
7183}
7184
Johannes Berg2a0e0472013-01-23 22:57:40 +01007185static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7186 struct cfg80211_wowlan_tcp *tcp)
7187{
7188 struct nlattr *nl_tcp;
7189
7190 if (!tcp)
7191 return 0;
7192
7193 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7194 if (!nl_tcp)
7195 return -ENOBUFS;
7196
7197 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7198 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7199 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7200 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7201 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7202 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7203 tcp->payload_len, tcp->payload) ||
7204 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7205 tcp->data_interval) ||
7206 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7207 tcp->wake_len, tcp->wake_data) ||
7208 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7209 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7210 return -ENOBUFS;
7211
7212 if (tcp->payload_seq.len &&
7213 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7214 sizeof(tcp->payload_seq), &tcp->payload_seq))
7215 return -ENOBUFS;
7216
7217 if (tcp->payload_tok.len &&
7218 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7219 sizeof(tcp->payload_tok) + tcp->tokens_size,
7220 &tcp->payload_tok))
7221 return -ENOBUFS;
7222
7223 return 0;
7224}
7225
Johannes Bergff1b6e62011-05-04 15:37:28 +02007226static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7227{
7228 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7229 struct sk_buff *msg;
7230 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007231 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007232
Johannes Berg2a0e0472013-01-23 22:57:40 +01007233 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7234 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007235 return -EOPNOTSUPP;
7236
Johannes Berg2a0e0472013-01-23 22:57:40 +01007237 if (rdev->wowlan && rdev->wowlan->tcp) {
7238 /* adjust size to have room for all the data */
7239 size += rdev->wowlan->tcp->tokens_size +
7240 rdev->wowlan->tcp->payload_len +
7241 rdev->wowlan->tcp->wake_len +
7242 rdev->wowlan->tcp->wake_len / 8;
7243 }
7244
7245 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007246 if (!msg)
7247 return -ENOMEM;
7248
Eric W. Biederman15e47302012-09-07 20:12:54 +00007249 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007250 NL80211_CMD_GET_WOWLAN);
7251 if (!hdr)
7252 goto nla_put_failure;
7253
7254 if (rdev->wowlan) {
7255 struct nlattr *nl_wowlan;
7256
7257 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7258 if (!nl_wowlan)
7259 goto nla_put_failure;
7260
David S. Miller9360ffd2012-03-29 04:41:26 -04007261 if ((rdev->wowlan->any &&
7262 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7263 (rdev->wowlan->disconnect &&
7264 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7265 (rdev->wowlan->magic_pkt &&
7266 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7267 (rdev->wowlan->gtk_rekey_failure &&
7268 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7269 (rdev->wowlan->eap_identity_req &&
7270 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7271 (rdev->wowlan->four_way_handshake &&
7272 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7273 (rdev->wowlan->rfkill_release &&
7274 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7275 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007276
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007277 if (nl80211_send_wowlan_patterns(msg, rdev))
7278 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007279
7280 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7281 goto nla_put_failure;
7282
Johannes Bergff1b6e62011-05-04 15:37:28 +02007283 nla_nest_end(msg, nl_wowlan);
7284 }
7285
7286 genlmsg_end(msg, hdr);
7287 return genlmsg_reply(msg, info);
7288
7289nla_put_failure:
7290 nlmsg_free(msg);
7291 return -ENOBUFS;
7292}
7293
Johannes Berg2a0e0472013-01-23 22:57:40 +01007294static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7295 struct nlattr *attr,
7296 struct cfg80211_wowlan *trig)
7297{
7298 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7299 struct cfg80211_wowlan_tcp *cfg;
7300 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7301 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7302 u32 size;
7303 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7304 int err, port;
7305
7306 if (!rdev->wiphy.wowlan.tcp)
7307 return -EINVAL;
7308
7309 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7310 nla_data(attr), nla_len(attr),
7311 nl80211_wowlan_tcp_policy);
7312 if (err)
7313 return err;
7314
7315 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7316 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7317 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7318 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7319 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7320 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7321 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7322 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7323 return -EINVAL;
7324
7325 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7326 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7327 return -EINVAL;
7328
7329 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7330 rdev->wiphy.wowlan.tcp->data_interval_max)
7331 return -EINVAL;
7332
7333 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7334 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7335 return -EINVAL;
7336
7337 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7338 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7339 return -EINVAL;
7340
7341 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7342 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7343
7344 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7345 tokens_size = tokln - sizeof(*tok);
7346
7347 if (!tok->len || tokens_size % tok->len)
7348 return -EINVAL;
7349 if (!rdev->wiphy.wowlan.tcp->tok)
7350 return -EINVAL;
7351 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7352 return -EINVAL;
7353 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7354 return -EINVAL;
7355 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7356 return -EINVAL;
7357 if (tok->offset + tok->len > data_size)
7358 return -EINVAL;
7359 }
7360
7361 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7362 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7363 if (!rdev->wiphy.wowlan.tcp->seq)
7364 return -EINVAL;
7365 if (seq->len == 0 || seq->len > 4)
7366 return -EINVAL;
7367 if (seq->len + seq->offset > data_size)
7368 return -EINVAL;
7369 }
7370
7371 size = sizeof(*cfg);
7372 size += data_size;
7373 size += wake_size + wake_mask_size;
7374 size += tokens_size;
7375
7376 cfg = kzalloc(size, GFP_KERNEL);
7377 if (!cfg)
7378 return -ENOMEM;
7379 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7380 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7381 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7382 ETH_ALEN);
7383 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7384 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7385 else
7386 port = 0;
7387#ifdef CONFIG_INET
7388 /* allocate a socket and port for it and use it */
7389 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7390 IPPROTO_TCP, &cfg->sock, 1);
7391 if (err) {
7392 kfree(cfg);
7393 return err;
7394 }
7395 if (inet_csk_get_port(cfg->sock->sk, port)) {
7396 sock_release(cfg->sock);
7397 kfree(cfg);
7398 return -EADDRINUSE;
7399 }
7400 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7401#else
7402 if (!port) {
7403 kfree(cfg);
7404 return -EINVAL;
7405 }
7406 cfg->src_port = port;
7407#endif
7408
7409 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7410 cfg->payload_len = data_size;
7411 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7412 memcpy((void *)cfg->payload,
7413 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7414 data_size);
7415 if (seq)
7416 cfg->payload_seq = *seq;
7417 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7418 cfg->wake_len = wake_size;
7419 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7420 memcpy((void *)cfg->wake_data,
7421 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7422 wake_size);
7423 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7424 data_size + wake_size;
7425 memcpy((void *)cfg->wake_mask,
7426 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7427 wake_mask_size);
7428 if (tok) {
7429 cfg->tokens_size = tokens_size;
7430 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7431 }
7432
7433 trig->tcp = cfg;
7434
7435 return 0;
7436}
7437
Johannes Bergff1b6e62011-05-04 15:37:28 +02007438static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7439{
7440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7441 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007442 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007443 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007444 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7445 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007446 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007447
Johannes Berg2a0e0472013-01-23 22:57:40 +01007448 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7449 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007450 return -EOPNOTSUPP;
7451
Johannes Bergae33bd82012-07-12 16:25:02 +02007452 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7453 cfg80211_rdev_free_wowlan(rdev);
7454 rdev->wowlan = NULL;
7455 goto set_wakeup;
7456 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007457
7458 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7459 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7460 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7461 nl80211_wowlan_policy);
7462 if (err)
7463 return err;
7464
7465 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7466 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7467 return -EINVAL;
7468 new_triggers.any = true;
7469 }
7470
7471 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7472 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7473 return -EINVAL;
7474 new_triggers.disconnect = true;
7475 }
7476
7477 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7478 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7479 return -EINVAL;
7480 new_triggers.magic_pkt = true;
7481 }
7482
Johannes Berg77dbbb132011-07-13 10:48:55 +02007483 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7484 return -EINVAL;
7485
7486 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7487 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7488 return -EINVAL;
7489 new_triggers.gtk_rekey_failure = true;
7490 }
7491
7492 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7493 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7494 return -EINVAL;
7495 new_triggers.eap_identity_req = true;
7496 }
7497
7498 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7499 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7500 return -EINVAL;
7501 new_triggers.four_way_handshake = true;
7502 }
7503
7504 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7505 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7506 return -EINVAL;
7507 new_triggers.rfkill_release = true;
7508 }
7509
Johannes Bergff1b6e62011-05-04 15:37:28 +02007510 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7511 struct nlattr *pat;
7512 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007513 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007514 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7515
7516 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7517 rem)
7518 n_patterns++;
7519 if (n_patterns > wowlan->n_patterns)
7520 return -EINVAL;
7521
7522 new_triggers.patterns = kcalloc(n_patterns,
7523 sizeof(new_triggers.patterns[0]),
7524 GFP_KERNEL);
7525 if (!new_triggers.patterns)
7526 return -ENOMEM;
7527
7528 new_triggers.n_patterns = n_patterns;
7529 i = 0;
7530
7531 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7532 rem) {
7533 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7534 nla_data(pat), nla_len(pat), NULL);
7535 err = -EINVAL;
7536 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7537 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7538 goto error;
7539 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7540 mask_len = DIV_ROUND_UP(pat_len, 8);
7541 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7542 mask_len)
7543 goto error;
7544 if (pat_len > wowlan->pattern_max_len ||
7545 pat_len < wowlan->pattern_min_len)
7546 goto error;
7547
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007548 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7549 pkt_offset = 0;
7550 else
7551 pkt_offset = nla_get_u32(
7552 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7553 if (pkt_offset > wowlan->max_pkt_offset)
7554 goto error;
7555 new_triggers.patterns[i].pkt_offset = pkt_offset;
7556
Johannes Bergff1b6e62011-05-04 15:37:28 +02007557 new_triggers.patterns[i].mask =
7558 kmalloc(mask_len + pat_len, GFP_KERNEL);
7559 if (!new_triggers.patterns[i].mask) {
7560 err = -ENOMEM;
7561 goto error;
7562 }
7563 new_triggers.patterns[i].pattern =
7564 new_triggers.patterns[i].mask + mask_len;
7565 memcpy(new_triggers.patterns[i].mask,
7566 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7567 mask_len);
7568 new_triggers.patterns[i].pattern_len = pat_len;
7569 memcpy(new_triggers.patterns[i].pattern,
7570 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7571 pat_len);
7572 i++;
7573 }
7574 }
7575
Johannes Berg2a0e0472013-01-23 22:57:40 +01007576 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7577 err = nl80211_parse_wowlan_tcp(
7578 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7579 &new_triggers);
7580 if (err)
7581 goto error;
7582 }
7583
Johannes Bergae33bd82012-07-12 16:25:02 +02007584 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7585 if (!ntrig) {
7586 err = -ENOMEM;
7587 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007588 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007589 cfg80211_rdev_free_wowlan(rdev);
7590 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007591
Johannes Bergae33bd82012-07-12 16:25:02 +02007592 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007593 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007594 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007595
Johannes Bergff1b6e62011-05-04 15:37:28 +02007596 return 0;
7597 error:
7598 for (i = 0; i < new_triggers.n_patterns; i++)
7599 kfree(new_triggers.patterns[i].mask);
7600 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007601 if (new_triggers.tcp && new_triggers.tcp->sock)
7602 sock_release(new_triggers.tcp->sock);
7603 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007604 return err;
7605}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007606#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007607
Johannes Berge5497d72011-07-05 16:35:40 +02007608static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7609{
7610 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7611 struct net_device *dev = info->user_ptr[1];
7612 struct wireless_dev *wdev = dev->ieee80211_ptr;
7613 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7614 struct cfg80211_gtk_rekey_data rekey_data;
7615 int err;
7616
7617 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7618 return -EINVAL;
7619
7620 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7621 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7622 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7623 nl80211_rekey_policy);
7624 if (err)
7625 return err;
7626
7627 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7628 return -ERANGE;
7629 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7630 return -ERANGE;
7631 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7632 return -ERANGE;
7633
7634 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7635 NL80211_KEK_LEN);
7636 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7637 NL80211_KCK_LEN);
7638 memcpy(rekey_data.replay_ctr,
7639 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7640 NL80211_REPLAY_CTR_LEN);
7641
7642 wdev_lock(wdev);
7643 if (!wdev->current_bss) {
7644 err = -ENOTCONN;
7645 goto out;
7646 }
7647
7648 if (!rdev->ops->set_rekey_data) {
7649 err = -EOPNOTSUPP;
7650 goto out;
7651 }
7652
Hila Gonene35e4d22012-06-27 17:19:42 +03007653 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007654 out:
7655 wdev_unlock(wdev);
7656 return err;
7657}
7658
Johannes Berg28946da2011-11-04 11:18:12 +01007659static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7660 struct genl_info *info)
7661{
7662 struct net_device *dev = info->user_ptr[1];
7663 struct wireless_dev *wdev = dev->ieee80211_ptr;
7664
7665 if (wdev->iftype != NL80211_IFTYPE_AP &&
7666 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7667 return -EINVAL;
7668
Eric W. Biederman15e47302012-09-07 20:12:54 +00007669 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007670 return -EBUSY;
7671
Eric W. Biederman15e47302012-09-07 20:12:54 +00007672 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007673 return 0;
7674}
7675
Johannes Berg7f6cf312011-11-04 11:18:15 +01007676static int nl80211_probe_client(struct sk_buff *skb,
7677 struct genl_info *info)
7678{
7679 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7680 struct net_device *dev = info->user_ptr[1];
7681 struct wireless_dev *wdev = dev->ieee80211_ptr;
7682 struct sk_buff *msg;
7683 void *hdr;
7684 const u8 *addr;
7685 u64 cookie;
7686 int err;
7687
7688 if (wdev->iftype != NL80211_IFTYPE_AP &&
7689 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7690 return -EOPNOTSUPP;
7691
7692 if (!info->attrs[NL80211_ATTR_MAC])
7693 return -EINVAL;
7694
7695 if (!rdev->ops->probe_client)
7696 return -EOPNOTSUPP;
7697
7698 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7699 if (!msg)
7700 return -ENOMEM;
7701
Eric W. Biederman15e47302012-09-07 20:12:54 +00007702 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007703 NL80211_CMD_PROBE_CLIENT);
7704
7705 if (IS_ERR(hdr)) {
7706 err = PTR_ERR(hdr);
7707 goto free_msg;
7708 }
7709
7710 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7711
Hila Gonene35e4d22012-06-27 17:19:42 +03007712 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007713 if (err)
7714 goto free_msg;
7715
David S. Miller9360ffd2012-03-29 04:41:26 -04007716 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7717 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007718
7719 genlmsg_end(msg, hdr);
7720
7721 return genlmsg_reply(msg, info);
7722
7723 nla_put_failure:
7724 err = -ENOBUFS;
7725 free_msg:
7726 nlmsg_free(msg);
7727 return err;
7728}
7729
Johannes Berg5e760232011-11-04 11:18:17 +01007730static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7731{
7732 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007733 struct cfg80211_beacon_registration *reg, *nreg;
7734 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007735
7736 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7737 return -EOPNOTSUPP;
7738
Ben Greear37c73b52012-10-26 14:49:25 -07007739 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7740 if (!nreg)
7741 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007742
Ben Greear37c73b52012-10-26 14:49:25 -07007743 /* First, check if already registered. */
7744 spin_lock_bh(&rdev->beacon_registrations_lock);
7745 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7746 if (reg->nlportid == info->snd_portid) {
7747 rv = -EALREADY;
7748 goto out_err;
7749 }
7750 }
7751 /* Add it to the list */
7752 nreg->nlportid = info->snd_portid;
7753 list_add(&nreg->list, &rdev->beacon_registrations);
7754
7755 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007756
7757 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007758out_err:
7759 spin_unlock_bh(&rdev->beacon_registrations_lock);
7760 kfree(nreg);
7761 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007762}
7763
Johannes Berg98104fde2012-06-16 00:19:54 +02007764static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7765{
7766 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7767 struct wireless_dev *wdev = info->user_ptr[1];
7768 int err;
7769
7770 if (!rdev->ops->start_p2p_device)
7771 return -EOPNOTSUPP;
7772
7773 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7774 return -EOPNOTSUPP;
7775
7776 if (wdev->p2p_started)
7777 return 0;
7778
7779 mutex_lock(&rdev->devlist_mtx);
7780 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7781 mutex_unlock(&rdev->devlist_mtx);
7782 if (err)
7783 return err;
7784
Johannes Bergeeb126e2012-10-23 15:16:50 +02007785 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007786 if (err)
7787 return err;
7788
7789 wdev->p2p_started = true;
7790 mutex_lock(&rdev->devlist_mtx);
7791 rdev->opencount++;
7792 mutex_unlock(&rdev->devlist_mtx);
7793
7794 return 0;
7795}
7796
7797static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7798{
7799 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7800 struct wireless_dev *wdev = info->user_ptr[1];
7801
7802 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7803 return -EOPNOTSUPP;
7804
7805 if (!rdev->ops->stop_p2p_device)
7806 return -EOPNOTSUPP;
7807
7808 if (!wdev->p2p_started)
7809 return 0;
7810
Johannes Bergeeb126e2012-10-23 15:16:50 +02007811 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007812 wdev->p2p_started = false;
7813
7814 mutex_lock(&rdev->devlist_mtx);
7815 rdev->opencount--;
7816 mutex_unlock(&rdev->devlist_mtx);
7817
7818 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7819 rdev->scan_req->aborted = true;
7820 ___cfg80211_scan_done(rdev, true);
7821 }
7822
7823 return 0;
7824}
7825
Johannes Berg4c476992010-10-04 21:36:35 +02007826#define NL80211_FLAG_NEED_WIPHY 0x01
7827#define NL80211_FLAG_NEED_NETDEV 0x02
7828#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007829#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7830#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7831 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007832#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007833/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007834#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7835 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007836
7837static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7838 struct genl_info *info)
7839{
7840 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007841 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007842 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007843 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7844
7845 if (rtnl)
7846 rtnl_lock();
7847
7848 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007849 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007850 if (IS_ERR(rdev)) {
7851 if (rtnl)
7852 rtnl_unlock();
7853 return PTR_ERR(rdev);
7854 }
7855 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007856 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7857 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007858 mutex_lock(&cfg80211_mutex);
7859 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7860 info->attrs);
7861 if (IS_ERR(wdev)) {
7862 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007863 if (rtnl)
7864 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007865 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007866 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007867
Johannes Berg89a54e42012-06-15 14:33:17 +02007868 dev = wdev->netdev;
7869 rdev = wiphy_to_dev(wdev->wiphy);
7870
Johannes Berg1bf614e2012-06-15 15:23:36 +02007871 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7872 if (!dev) {
7873 mutex_unlock(&cfg80211_mutex);
7874 if (rtnl)
7875 rtnl_unlock();
7876 return -EINVAL;
7877 }
7878
7879 info->user_ptr[1] = dev;
7880 } else {
7881 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007882 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007883
Johannes Berg1bf614e2012-06-15 15:23:36 +02007884 if (dev) {
7885 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7886 !netif_running(dev)) {
7887 mutex_unlock(&cfg80211_mutex);
7888 if (rtnl)
7889 rtnl_unlock();
7890 return -ENETDOWN;
7891 }
7892
7893 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007894 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7895 if (!wdev->p2p_started) {
7896 mutex_unlock(&cfg80211_mutex);
7897 if (rtnl)
7898 rtnl_unlock();
7899 return -ENETDOWN;
7900 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007901 }
7902
Johannes Berg89a54e42012-06-15 14:33:17 +02007903 cfg80211_lock_rdev(rdev);
7904
7905 mutex_unlock(&cfg80211_mutex);
7906
Johannes Berg4c476992010-10-04 21:36:35 +02007907 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007908 }
7909
7910 return 0;
7911}
7912
7913static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7914 struct genl_info *info)
7915{
7916 if (info->user_ptr[0])
7917 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007918 if (info->user_ptr[1]) {
7919 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7920 struct wireless_dev *wdev = info->user_ptr[1];
7921
7922 if (wdev->netdev)
7923 dev_put(wdev->netdev);
7924 } else {
7925 dev_put(info->user_ptr[1]);
7926 }
7927 }
Johannes Berg4c476992010-10-04 21:36:35 +02007928 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7929 rtnl_unlock();
7930}
7931
Johannes Berg55682962007-09-20 13:09:35 -04007932static struct genl_ops nl80211_ops[] = {
7933 {
7934 .cmd = NL80211_CMD_GET_WIPHY,
7935 .doit = nl80211_get_wiphy,
7936 .dumpit = nl80211_dump_wiphy,
7937 .policy = nl80211_policy,
7938 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007939 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007940 },
7941 {
7942 .cmd = NL80211_CMD_SET_WIPHY,
7943 .doit = nl80211_set_wiphy,
7944 .policy = nl80211_policy,
7945 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007946 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007947 },
7948 {
7949 .cmd = NL80211_CMD_GET_INTERFACE,
7950 .doit = nl80211_get_interface,
7951 .dumpit = nl80211_dump_interface,
7952 .policy = nl80211_policy,
7953 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007954 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007955 },
7956 {
7957 .cmd = NL80211_CMD_SET_INTERFACE,
7958 .doit = nl80211_set_interface,
7959 .policy = nl80211_policy,
7960 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007961 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7962 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007963 },
7964 {
7965 .cmd = NL80211_CMD_NEW_INTERFACE,
7966 .doit = nl80211_new_interface,
7967 .policy = nl80211_policy,
7968 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007969 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7970 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007971 },
7972 {
7973 .cmd = NL80211_CMD_DEL_INTERFACE,
7974 .doit = nl80211_del_interface,
7975 .policy = nl80211_policy,
7976 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007977 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007978 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007979 },
Johannes Berg41ade002007-12-19 02:03:29 +01007980 {
7981 .cmd = NL80211_CMD_GET_KEY,
7982 .doit = nl80211_get_key,
7983 .policy = nl80211_policy,
7984 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007985 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007986 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007987 },
7988 {
7989 .cmd = NL80211_CMD_SET_KEY,
7990 .doit = nl80211_set_key,
7991 .policy = nl80211_policy,
7992 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007993 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007994 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007995 },
7996 {
7997 .cmd = NL80211_CMD_NEW_KEY,
7998 .doit = nl80211_new_key,
7999 .policy = nl80211_policy,
8000 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008001 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008002 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008003 },
8004 {
8005 .cmd = NL80211_CMD_DEL_KEY,
8006 .doit = nl80211_del_key,
8007 .policy = nl80211_policy,
8008 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008009 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008010 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008011 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008012 {
8013 .cmd = NL80211_CMD_SET_BEACON,
8014 .policy = nl80211_policy,
8015 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008016 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008017 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008018 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008019 },
8020 {
Johannes Berg88600202012-02-13 15:17:18 +01008021 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008022 .policy = nl80211_policy,
8023 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008024 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008025 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008026 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008027 },
8028 {
Johannes Berg88600202012-02-13 15:17:18 +01008029 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008030 .policy = nl80211_policy,
8031 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008032 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008033 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008034 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008035 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008036 {
8037 .cmd = NL80211_CMD_GET_STATION,
8038 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008039 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008040 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008041 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8042 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008043 },
8044 {
8045 .cmd = NL80211_CMD_SET_STATION,
8046 .doit = nl80211_set_station,
8047 .policy = nl80211_policy,
8048 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008049 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008050 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008051 },
8052 {
8053 .cmd = NL80211_CMD_NEW_STATION,
8054 .doit = nl80211_new_station,
8055 .policy = nl80211_policy,
8056 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008057 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008058 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008059 },
8060 {
8061 .cmd = NL80211_CMD_DEL_STATION,
8062 .doit = nl80211_del_station,
8063 .policy = nl80211_policy,
8064 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008065 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008066 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008067 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008068 {
8069 .cmd = NL80211_CMD_GET_MPATH,
8070 .doit = nl80211_get_mpath,
8071 .dumpit = nl80211_dump_mpath,
8072 .policy = nl80211_policy,
8073 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008074 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008075 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008076 },
8077 {
8078 .cmd = NL80211_CMD_SET_MPATH,
8079 .doit = nl80211_set_mpath,
8080 .policy = nl80211_policy,
8081 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008082 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008083 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008084 },
8085 {
8086 .cmd = NL80211_CMD_NEW_MPATH,
8087 .doit = nl80211_new_mpath,
8088 .policy = nl80211_policy,
8089 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008090 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008091 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008092 },
8093 {
8094 .cmd = NL80211_CMD_DEL_MPATH,
8095 .doit = nl80211_del_mpath,
8096 .policy = nl80211_policy,
8097 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008098 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008099 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008100 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008101 {
8102 .cmd = NL80211_CMD_SET_BSS,
8103 .doit = nl80211_set_bss,
8104 .policy = nl80211_policy,
8105 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008106 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008107 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008108 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008109 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008110 .cmd = NL80211_CMD_GET_REG,
8111 .doit = nl80211_get_reg,
8112 .policy = nl80211_policy,
8113 /* can be retrieved by unprivileged users */
8114 },
8115 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008116 .cmd = NL80211_CMD_SET_REG,
8117 .doit = nl80211_set_reg,
8118 .policy = nl80211_policy,
8119 .flags = GENL_ADMIN_PERM,
8120 },
8121 {
8122 .cmd = NL80211_CMD_REQ_SET_REG,
8123 .doit = nl80211_req_set_reg,
8124 .policy = nl80211_policy,
8125 .flags = GENL_ADMIN_PERM,
8126 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008127 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008128 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8129 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008130 .policy = nl80211_policy,
8131 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008132 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008133 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008134 },
8135 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008136 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8137 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008138 .policy = nl80211_policy,
8139 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008140 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008141 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008142 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008143 {
Johannes Berg2a519312009-02-10 21:25:55 +01008144 .cmd = NL80211_CMD_TRIGGER_SCAN,
8145 .doit = nl80211_trigger_scan,
8146 .policy = nl80211_policy,
8147 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008148 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008149 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008150 },
8151 {
8152 .cmd = NL80211_CMD_GET_SCAN,
8153 .policy = nl80211_policy,
8154 .dumpit = nl80211_dump_scan,
8155 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008156 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008157 .cmd = NL80211_CMD_START_SCHED_SCAN,
8158 .doit = nl80211_start_sched_scan,
8159 .policy = nl80211_policy,
8160 .flags = GENL_ADMIN_PERM,
8161 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8162 NL80211_FLAG_NEED_RTNL,
8163 },
8164 {
8165 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8166 .doit = nl80211_stop_sched_scan,
8167 .policy = nl80211_policy,
8168 .flags = GENL_ADMIN_PERM,
8169 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8170 NL80211_FLAG_NEED_RTNL,
8171 },
8172 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008173 .cmd = NL80211_CMD_AUTHENTICATE,
8174 .doit = nl80211_authenticate,
8175 .policy = nl80211_policy,
8176 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008177 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008178 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008179 },
8180 {
8181 .cmd = NL80211_CMD_ASSOCIATE,
8182 .doit = nl80211_associate,
8183 .policy = nl80211_policy,
8184 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008185 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008186 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008187 },
8188 {
8189 .cmd = NL80211_CMD_DEAUTHENTICATE,
8190 .doit = nl80211_deauthenticate,
8191 .policy = nl80211_policy,
8192 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008193 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008194 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008195 },
8196 {
8197 .cmd = NL80211_CMD_DISASSOCIATE,
8198 .doit = nl80211_disassociate,
8199 .policy = nl80211_policy,
8200 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008201 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008202 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008203 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008204 {
8205 .cmd = NL80211_CMD_JOIN_IBSS,
8206 .doit = nl80211_join_ibss,
8207 .policy = nl80211_policy,
8208 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008209 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008210 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008211 },
8212 {
8213 .cmd = NL80211_CMD_LEAVE_IBSS,
8214 .doit = nl80211_leave_ibss,
8215 .policy = nl80211_policy,
8216 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008217 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008218 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008219 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008220#ifdef CONFIG_NL80211_TESTMODE
8221 {
8222 .cmd = NL80211_CMD_TESTMODE,
8223 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008224 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008225 .policy = nl80211_policy,
8226 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008227 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8228 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008229 },
8230#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008231 {
8232 .cmd = NL80211_CMD_CONNECT,
8233 .doit = nl80211_connect,
8234 .policy = nl80211_policy,
8235 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008236 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008237 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008238 },
8239 {
8240 .cmd = NL80211_CMD_DISCONNECT,
8241 .doit = nl80211_disconnect,
8242 .policy = nl80211_policy,
8243 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008244 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008245 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008246 },
Johannes Berg463d0182009-07-14 00:33:35 +02008247 {
8248 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8249 .doit = nl80211_wiphy_netns,
8250 .policy = nl80211_policy,
8251 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008252 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8253 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008254 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008255 {
8256 .cmd = NL80211_CMD_GET_SURVEY,
8257 .policy = nl80211_policy,
8258 .dumpit = nl80211_dump_survey,
8259 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008260 {
8261 .cmd = NL80211_CMD_SET_PMKSA,
8262 .doit = nl80211_setdel_pmksa,
8263 .policy = nl80211_policy,
8264 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008265 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008266 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008267 },
8268 {
8269 .cmd = NL80211_CMD_DEL_PMKSA,
8270 .doit = nl80211_setdel_pmksa,
8271 .policy = nl80211_policy,
8272 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008273 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008274 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008275 },
8276 {
8277 .cmd = NL80211_CMD_FLUSH_PMKSA,
8278 .doit = nl80211_flush_pmksa,
8279 .policy = nl80211_policy,
8280 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008281 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008282 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008283 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008284 {
8285 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8286 .doit = nl80211_remain_on_channel,
8287 .policy = nl80211_policy,
8288 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008289 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008290 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008291 },
8292 {
8293 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8294 .doit = nl80211_cancel_remain_on_channel,
8295 .policy = nl80211_policy,
8296 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008297 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008298 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008299 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008300 {
8301 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8302 .doit = nl80211_set_tx_bitrate_mask,
8303 .policy = nl80211_policy,
8304 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008305 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8306 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008307 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008308 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008309 .cmd = NL80211_CMD_REGISTER_FRAME,
8310 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008311 .policy = nl80211_policy,
8312 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008313 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008314 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008315 },
8316 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008317 .cmd = NL80211_CMD_FRAME,
8318 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008319 .policy = nl80211_policy,
8320 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008321 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008322 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008323 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008324 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008325 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8326 .doit = nl80211_tx_mgmt_cancel_wait,
8327 .policy = nl80211_policy,
8328 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008329 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008330 NL80211_FLAG_NEED_RTNL,
8331 },
8332 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008333 .cmd = NL80211_CMD_SET_POWER_SAVE,
8334 .doit = nl80211_set_power_save,
8335 .policy = nl80211_policy,
8336 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008337 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8338 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008339 },
8340 {
8341 .cmd = NL80211_CMD_GET_POWER_SAVE,
8342 .doit = nl80211_get_power_save,
8343 .policy = nl80211_policy,
8344 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008345 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8346 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008347 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008348 {
8349 .cmd = NL80211_CMD_SET_CQM,
8350 .doit = nl80211_set_cqm,
8351 .policy = nl80211_policy,
8352 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008353 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8354 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008355 },
Johannes Bergf444de02010-05-05 15:25:02 +02008356 {
8357 .cmd = NL80211_CMD_SET_CHANNEL,
8358 .doit = nl80211_set_channel,
8359 .policy = nl80211_policy,
8360 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008361 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8362 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008363 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008364 {
8365 .cmd = NL80211_CMD_SET_WDS_PEER,
8366 .doit = nl80211_set_wds_peer,
8367 .policy = nl80211_policy,
8368 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008369 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8370 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008371 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008372 {
8373 .cmd = NL80211_CMD_JOIN_MESH,
8374 .doit = nl80211_join_mesh,
8375 .policy = nl80211_policy,
8376 .flags = GENL_ADMIN_PERM,
8377 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8378 NL80211_FLAG_NEED_RTNL,
8379 },
8380 {
8381 .cmd = NL80211_CMD_LEAVE_MESH,
8382 .doit = nl80211_leave_mesh,
8383 .policy = nl80211_policy,
8384 .flags = GENL_ADMIN_PERM,
8385 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8386 NL80211_FLAG_NEED_RTNL,
8387 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008388#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008389 {
8390 .cmd = NL80211_CMD_GET_WOWLAN,
8391 .doit = nl80211_get_wowlan,
8392 .policy = nl80211_policy,
8393 /* can be retrieved by unprivileged users */
8394 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8395 NL80211_FLAG_NEED_RTNL,
8396 },
8397 {
8398 .cmd = NL80211_CMD_SET_WOWLAN,
8399 .doit = nl80211_set_wowlan,
8400 .policy = nl80211_policy,
8401 .flags = GENL_ADMIN_PERM,
8402 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8403 NL80211_FLAG_NEED_RTNL,
8404 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008405#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008406 {
8407 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8408 .doit = nl80211_set_rekey_data,
8409 .policy = nl80211_policy,
8410 .flags = GENL_ADMIN_PERM,
8411 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8412 NL80211_FLAG_NEED_RTNL,
8413 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008414 {
8415 .cmd = NL80211_CMD_TDLS_MGMT,
8416 .doit = nl80211_tdls_mgmt,
8417 .policy = nl80211_policy,
8418 .flags = GENL_ADMIN_PERM,
8419 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8420 NL80211_FLAG_NEED_RTNL,
8421 },
8422 {
8423 .cmd = NL80211_CMD_TDLS_OPER,
8424 .doit = nl80211_tdls_oper,
8425 .policy = nl80211_policy,
8426 .flags = GENL_ADMIN_PERM,
8427 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8428 NL80211_FLAG_NEED_RTNL,
8429 },
Johannes Berg28946da2011-11-04 11:18:12 +01008430 {
8431 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8432 .doit = nl80211_register_unexpected_frame,
8433 .policy = nl80211_policy,
8434 .flags = GENL_ADMIN_PERM,
8435 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8436 NL80211_FLAG_NEED_RTNL,
8437 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008438 {
8439 .cmd = NL80211_CMD_PROBE_CLIENT,
8440 .doit = nl80211_probe_client,
8441 .policy = nl80211_policy,
8442 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008443 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008444 NL80211_FLAG_NEED_RTNL,
8445 },
Johannes Berg5e760232011-11-04 11:18:17 +01008446 {
8447 .cmd = NL80211_CMD_REGISTER_BEACONS,
8448 .doit = nl80211_register_beacons,
8449 .policy = nl80211_policy,
8450 .flags = GENL_ADMIN_PERM,
8451 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8452 NL80211_FLAG_NEED_RTNL,
8453 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008454 {
8455 .cmd = NL80211_CMD_SET_NOACK_MAP,
8456 .doit = nl80211_set_noack_map,
8457 .policy = nl80211_policy,
8458 .flags = GENL_ADMIN_PERM,
8459 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8460 NL80211_FLAG_NEED_RTNL,
8461 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008462 {
8463 .cmd = NL80211_CMD_START_P2P_DEVICE,
8464 .doit = nl80211_start_p2p_device,
8465 .policy = nl80211_policy,
8466 .flags = GENL_ADMIN_PERM,
8467 .internal_flags = NL80211_FLAG_NEED_WDEV |
8468 NL80211_FLAG_NEED_RTNL,
8469 },
8470 {
8471 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8472 .doit = nl80211_stop_p2p_device,
8473 .policy = nl80211_policy,
8474 .flags = GENL_ADMIN_PERM,
8475 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8476 NL80211_FLAG_NEED_RTNL,
8477 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008478 {
8479 .cmd = NL80211_CMD_SET_MCAST_RATE,
8480 .doit = nl80211_set_mcast_rate,
8481 .policy = nl80211_policy,
8482 .flags = GENL_ADMIN_PERM,
8483 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8484 NL80211_FLAG_NEED_RTNL,
8485 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308486 {
8487 .cmd = NL80211_CMD_SET_MAC_ACL,
8488 .doit = nl80211_set_mac_acl,
8489 .policy = nl80211_policy,
8490 .flags = GENL_ADMIN_PERM,
8491 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8492 NL80211_FLAG_NEED_RTNL,
8493 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008494 {
8495 .cmd = NL80211_CMD_RADAR_DETECT,
8496 .doit = nl80211_start_radar_detection,
8497 .policy = nl80211_policy,
8498 .flags = GENL_ADMIN_PERM,
8499 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8500 NL80211_FLAG_NEED_RTNL,
8501 },
Johannes Berg55682962007-09-20 13:09:35 -04008502};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008503
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008504static struct genl_multicast_group nl80211_mlme_mcgrp = {
8505 .name = "mlme",
8506};
Johannes Berg55682962007-09-20 13:09:35 -04008507
8508/* multicast groups */
8509static struct genl_multicast_group nl80211_config_mcgrp = {
8510 .name = "config",
8511};
Johannes Berg2a519312009-02-10 21:25:55 +01008512static struct genl_multicast_group nl80211_scan_mcgrp = {
8513 .name = "scan",
8514};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008515static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8516 .name = "regulatory",
8517};
Johannes Berg55682962007-09-20 13:09:35 -04008518
8519/* notification functions */
8520
8521void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8522{
8523 struct sk_buff *msg;
8524
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008525 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008526 if (!msg)
8527 return;
8528
8529 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8530 nlmsg_free(msg);
8531 return;
8532 }
8533
Johannes Berg463d0182009-07-14 00:33:35 +02008534 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8535 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008536}
8537
Johannes Berg362a4152009-05-24 16:43:15 +02008538static int nl80211_add_scan_req(struct sk_buff *msg,
8539 struct cfg80211_registered_device *rdev)
8540{
8541 struct cfg80211_scan_request *req = rdev->scan_req;
8542 struct nlattr *nest;
8543 int i;
8544
Johannes Berg667503d2009-07-07 03:56:11 +02008545 ASSERT_RDEV_LOCK(rdev);
8546
Johannes Berg362a4152009-05-24 16:43:15 +02008547 if (WARN_ON(!req))
8548 return 0;
8549
8550 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8551 if (!nest)
8552 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008553 for (i = 0; i < req->n_ssids; i++) {
8554 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8555 goto nla_put_failure;
8556 }
Johannes Berg362a4152009-05-24 16:43:15 +02008557 nla_nest_end(msg, nest);
8558
8559 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8560 if (!nest)
8561 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008562 for (i = 0; i < req->n_channels; i++) {
8563 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8564 goto nla_put_failure;
8565 }
Johannes Berg362a4152009-05-24 16:43:15 +02008566 nla_nest_end(msg, nest);
8567
David S. Miller9360ffd2012-03-29 04:41:26 -04008568 if (req->ie &&
8569 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8570 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008571
Sam Lefflered4737712012-10-11 21:03:31 -07008572 if (req->flags)
8573 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8574
Johannes Berg362a4152009-05-24 16:43:15 +02008575 return 0;
8576 nla_put_failure:
8577 return -ENOBUFS;
8578}
8579
Johannes Berga538e2d2009-06-16 19:56:42 +02008580static int nl80211_send_scan_msg(struct sk_buff *msg,
8581 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008582 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008583 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008584 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008585{
8586 void *hdr;
8587
Eric W. Biederman15e47302012-09-07 20:12:54 +00008588 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008589 if (!hdr)
8590 return -1;
8591
David S. Miller9360ffd2012-03-29 04:41:26 -04008592 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008593 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8594 wdev->netdev->ifindex)) ||
8595 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008596 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008597
Johannes Berg362a4152009-05-24 16:43:15 +02008598 /* ignore errors and send incomplete event anyway */
8599 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008600
8601 return genlmsg_end(msg, hdr);
8602
8603 nla_put_failure:
8604 genlmsg_cancel(msg, hdr);
8605 return -EMSGSIZE;
8606}
8607
Luciano Coelho807f8a82011-05-11 17:09:35 +03008608static int
8609nl80211_send_sched_scan_msg(struct sk_buff *msg,
8610 struct cfg80211_registered_device *rdev,
8611 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008612 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008613{
8614 void *hdr;
8615
Eric W. Biederman15e47302012-09-07 20:12:54 +00008616 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008617 if (!hdr)
8618 return -1;
8619
David S. Miller9360ffd2012-03-29 04:41:26 -04008620 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8621 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8622 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008623
8624 return genlmsg_end(msg, hdr);
8625
8626 nla_put_failure:
8627 genlmsg_cancel(msg, hdr);
8628 return -EMSGSIZE;
8629}
8630
Johannes Berga538e2d2009-06-16 19:56:42 +02008631void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008632 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008633{
8634 struct sk_buff *msg;
8635
Thomas Graf58050fc2012-06-28 03:57:45 +00008636 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008637 if (!msg)
8638 return;
8639
Johannes Bergfd014282012-06-18 19:17:03 +02008640 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008641 NL80211_CMD_TRIGGER_SCAN) < 0) {
8642 nlmsg_free(msg);
8643 return;
8644 }
8645
Johannes Berg463d0182009-07-14 00:33:35 +02008646 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8647 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008648}
8649
Johannes Berg2a519312009-02-10 21:25:55 +01008650void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008651 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008652{
8653 struct sk_buff *msg;
8654
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008656 if (!msg)
8657 return;
8658
Johannes Bergfd014282012-06-18 19:17:03 +02008659 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008660 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008661 nlmsg_free(msg);
8662 return;
8663 }
8664
Johannes Berg463d0182009-07-14 00:33:35 +02008665 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8666 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008667}
8668
8669void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008670 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008671{
8672 struct sk_buff *msg;
8673
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008674 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008675 if (!msg)
8676 return;
8677
Johannes Bergfd014282012-06-18 19:17:03 +02008678 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008679 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008680 nlmsg_free(msg);
8681 return;
8682 }
8683
Johannes Berg463d0182009-07-14 00:33:35 +02008684 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8685 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008686}
8687
Luciano Coelho807f8a82011-05-11 17:09:35 +03008688void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8689 struct net_device *netdev)
8690{
8691 struct sk_buff *msg;
8692
8693 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8694 if (!msg)
8695 return;
8696
8697 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8698 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8699 nlmsg_free(msg);
8700 return;
8701 }
8702
8703 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8704 nl80211_scan_mcgrp.id, GFP_KERNEL);
8705}
8706
8707void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8708 struct net_device *netdev, u32 cmd)
8709{
8710 struct sk_buff *msg;
8711
Thomas Graf58050fc2012-06-28 03:57:45 +00008712 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008713 if (!msg)
8714 return;
8715
8716 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8717 nlmsg_free(msg);
8718 return;
8719 }
8720
8721 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8722 nl80211_scan_mcgrp.id, GFP_KERNEL);
8723}
8724
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008725/*
8726 * This can happen on global regulatory changes or device specific settings
8727 * based on custom world regulatory domains.
8728 */
8729void nl80211_send_reg_change_event(struct regulatory_request *request)
8730{
8731 struct sk_buff *msg;
8732 void *hdr;
8733
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008734 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008735 if (!msg)
8736 return;
8737
8738 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8739 if (!hdr) {
8740 nlmsg_free(msg);
8741 return;
8742 }
8743
8744 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008745 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8746 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008747
David S. Miller9360ffd2012-03-29 04:41:26 -04008748 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8749 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8750 NL80211_REGDOM_TYPE_WORLD))
8751 goto nla_put_failure;
8752 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8753 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8754 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8755 goto nla_put_failure;
8756 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8757 request->intersect) {
8758 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8759 NL80211_REGDOM_TYPE_INTERSECTION))
8760 goto nla_put_failure;
8761 } else {
8762 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8763 NL80211_REGDOM_TYPE_COUNTRY) ||
8764 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8765 request->alpha2))
8766 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008767 }
8768
Johannes Bergf4173762012-12-03 18:23:37 +01008769 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008770 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8771 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008772
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008773 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008774
Johannes Bergbc43b282009-07-25 10:54:13 +02008775 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008776 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008777 GFP_ATOMIC);
8778 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008779
8780 return;
8781
8782nla_put_failure:
8783 genlmsg_cancel(msg, hdr);
8784 nlmsg_free(msg);
8785}
8786
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008787static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8788 struct net_device *netdev,
8789 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008790 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008791{
8792 struct sk_buff *msg;
8793 void *hdr;
8794
Johannes Berge6d6e342009-07-01 21:26:47 +02008795 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008796 if (!msg)
8797 return;
8798
8799 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8800 if (!hdr) {
8801 nlmsg_free(msg);
8802 return;
8803 }
8804
David S. Miller9360ffd2012-03-29 04:41:26 -04008805 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8806 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8807 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8808 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008809
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008810 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008811
Johannes Berg463d0182009-07-14 00:33:35 +02008812 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8813 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008814 return;
8815
8816 nla_put_failure:
8817 genlmsg_cancel(msg, hdr);
8818 nlmsg_free(msg);
8819}
8820
8821void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008822 struct net_device *netdev, const u8 *buf,
8823 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008824{
8825 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008826 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008827}
8828
8829void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8830 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008831 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008832{
Johannes Berge6d6e342009-07-01 21:26:47 +02008833 nl80211_send_mlme_event(rdev, netdev, buf, len,
8834 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008835}
8836
Jouni Malinen53b46b82009-03-27 20:53:56 +02008837void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008838 struct net_device *netdev, const u8 *buf,
8839 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008840{
8841 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008842 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008843}
8844
Jouni Malinen53b46b82009-03-27 20:53:56 +02008845void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8846 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008847 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008848{
8849 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008850 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008851}
8852
Jouni Malinencf4e5942010-12-16 00:52:40 +02008853void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8854 struct net_device *netdev, const u8 *buf,
8855 size_t len, gfp_t gfp)
8856{
8857 nl80211_send_mlme_event(rdev, netdev, buf, len,
8858 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8859}
8860
8861void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8862 struct net_device *netdev, const u8 *buf,
8863 size_t len, gfp_t gfp)
8864{
8865 nl80211_send_mlme_event(rdev, netdev, buf, len,
8866 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8867}
8868
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008869static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8870 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008871 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008872{
8873 struct sk_buff *msg;
8874 void *hdr;
8875
Johannes Berge6d6e342009-07-01 21:26:47 +02008876 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008877 if (!msg)
8878 return;
8879
8880 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8881 if (!hdr) {
8882 nlmsg_free(msg);
8883 return;
8884 }
8885
David S. Miller9360ffd2012-03-29 04:41:26 -04008886 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8887 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8888 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8889 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8890 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008891
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008892 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008893
Johannes Berg463d0182009-07-14 00:33:35 +02008894 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8895 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008896 return;
8897
8898 nla_put_failure:
8899 genlmsg_cancel(msg, hdr);
8900 nlmsg_free(msg);
8901}
8902
8903void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008904 struct net_device *netdev, const u8 *addr,
8905 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008906{
8907 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008908 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008909}
8910
8911void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008912 struct net_device *netdev, const u8 *addr,
8913 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008914{
Johannes Berge6d6e342009-07-01 21:26:47 +02008915 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8916 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008917}
8918
Samuel Ortizb23aa672009-07-01 21:26:54 +02008919void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8920 struct net_device *netdev, const u8 *bssid,
8921 const u8 *req_ie, size_t req_ie_len,
8922 const u8 *resp_ie, size_t resp_ie_len,
8923 u16 status, gfp_t gfp)
8924{
8925 struct sk_buff *msg;
8926 void *hdr;
8927
Thomas Graf58050fc2012-06-28 03:57:45 +00008928 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008929 if (!msg)
8930 return;
8931
8932 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8933 if (!hdr) {
8934 nlmsg_free(msg);
8935 return;
8936 }
8937
David S. Miller9360ffd2012-03-29 04:41:26 -04008938 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8939 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8940 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8941 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8942 (req_ie &&
8943 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8944 (resp_ie &&
8945 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8946 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008947
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008948 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008949
Johannes Berg463d0182009-07-14 00:33:35 +02008950 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8951 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008952 return;
8953
8954 nla_put_failure:
8955 genlmsg_cancel(msg, hdr);
8956 nlmsg_free(msg);
8957
8958}
8959
8960void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8961 struct net_device *netdev, const u8 *bssid,
8962 const u8 *req_ie, size_t req_ie_len,
8963 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8964{
8965 struct sk_buff *msg;
8966 void *hdr;
8967
Thomas Graf58050fc2012-06-28 03:57:45 +00008968 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008969 if (!msg)
8970 return;
8971
8972 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8973 if (!hdr) {
8974 nlmsg_free(msg);
8975 return;
8976 }
8977
David S. Miller9360ffd2012-03-29 04:41:26 -04008978 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8979 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8980 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8981 (req_ie &&
8982 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8983 (resp_ie &&
8984 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8985 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008986
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008987 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008988
Johannes Berg463d0182009-07-14 00:33:35 +02008989 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8990 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008991 return;
8992
8993 nla_put_failure:
8994 genlmsg_cancel(msg, hdr);
8995 nlmsg_free(msg);
8996
8997}
8998
8999void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9000 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02009001 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009002{
9003 struct sk_buff *msg;
9004 void *hdr;
9005
Thomas Graf58050fc2012-06-28 03:57:45 +00009006 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009007 if (!msg)
9008 return;
9009
9010 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9011 if (!hdr) {
9012 nlmsg_free(msg);
9013 return;
9014 }
9015
David S. Miller9360ffd2012-03-29 04:41:26 -04009016 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9017 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9018 (from_ap && reason &&
9019 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9020 (from_ap &&
9021 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9022 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9023 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009024
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009025 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009026
Johannes Berg463d0182009-07-14 00:33:35 +02009027 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9028 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009029 return;
9030
9031 nla_put_failure:
9032 genlmsg_cancel(msg, hdr);
9033 nlmsg_free(msg);
9034
9035}
9036
Johannes Berg04a773a2009-04-19 21:24:32 +02009037void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9038 struct net_device *netdev, const u8 *bssid,
9039 gfp_t gfp)
9040{
9041 struct sk_buff *msg;
9042 void *hdr;
9043
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009044 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009045 if (!msg)
9046 return;
9047
9048 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9049 if (!hdr) {
9050 nlmsg_free(msg);
9051 return;
9052 }
9053
David S. Miller9360ffd2012-03-29 04:41:26 -04009054 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9055 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9056 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9057 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009058
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009059 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009060
Johannes Berg463d0182009-07-14 00:33:35 +02009061 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9062 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009063 return;
9064
9065 nla_put_failure:
9066 genlmsg_cancel(msg, hdr);
9067 nlmsg_free(msg);
9068}
9069
Javier Cardonac93b5e72011-04-07 15:08:34 -07009070void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
9071 struct net_device *netdev,
9072 const u8 *macaddr, const u8* ie, u8 ie_len,
9073 gfp_t gfp)
9074{
9075 struct sk_buff *msg;
9076 void *hdr;
9077
9078 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9079 if (!msg)
9080 return;
9081
9082 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9083 if (!hdr) {
9084 nlmsg_free(msg);
9085 return;
9086 }
9087
David S. Miller9360ffd2012-03-29 04:41:26 -04009088 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9089 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9090 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
9091 (ie_len && ie &&
9092 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9093 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009094
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009095 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009096
9097 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9098 nl80211_mlme_mcgrp.id, gfp);
9099 return;
9100
9101 nla_put_failure:
9102 genlmsg_cancel(msg, hdr);
9103 nlmsg_free(msg);
9104}
9105
Jouni Malinena3b8b052009-03-27 21:59:49 +02009106void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9107 struct net_device *netdev, const u8 *addr,
9108 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009109 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009110{
9111 struct sk_buff *msg;
9112 void *hdr;
9113
Johannes Berge6d6e342009-07-01 21:26:47 +02009114 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009115 if (!msg)
9116 return;
9117
9118 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9119 if (!hdr) {
9120 nlmsg_free(msg);
9121 return;
9122 }
9123
David S. Miller9360ffd2012-03-29 04:41:26 -04009124 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9125 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9126 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9127 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9128 (key_id != -1 &&
9129 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9130 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9131 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009132
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009133 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009134
Johannes Berg463d0182009-07-14 00:33:35 +02009135 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9136 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009137 return;
9138
9139 nla_put_failure:
9140 genlmsg_cancel(msg, hdr);
9141 nlmsg_free(msg);
9142}
9143
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009144void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9145 struct ieee80211_channel *channel_before,
9146 struct ieee80211_channel *channel_after)
9147{
9148 struct sk_buff *msg;
9149 void *hdr;
9150 struct nlattr *nl_freq;
9151
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009152 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009153 if (!msg)
9154 return;
9155
9156 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9157 if (!hdr) {
9158 nlmsg_free(msg);
9159 return;
9160 }
9161
9162 /*
9163 * Since we are applying the beacon hint to a wiphy we know its
9164 * wiphy_idx is valid
9165 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009166 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9167 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009168
9169 /* Before */
9170 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9171 if (!nl_freq)
9172 goto nla_put_failure;
9173 if (nl80211_msg_put_channel(msg, channel_before))
9174 goto nla_put_failure;
9175 nla_nest_end(msg, nl_freq);
9176
9177 /* After */
9178 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9179 if (!nl_freq)
9180 goto nla_put_failure;
9181 if (nl80211_msg_put_channel(msg, channel_after))
9182 goto nla_put_failure;
9183 nla_nest_end(msg, nl_freq);
9184
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009185 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009186
Johannes Berg463d0182009-07-14 00:33:35 +02009187 rcu_read_lock();
9188 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9189 GFP_ATOMIC);
9190 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009191
9192 return;
9193
9194nla_put_failure:
9195 genlmsg_cancel(msg, hdr);
9196 nlmsg_free(msg);
9197}
9198
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009199static void nl80211_send_remain_on_chan_event(
9200 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009201 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009202 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009203 unsigned int duration, gfp_t gfp)
9204{
9205 struct sk_buff *msg;
9206 void *hdr;
9207
9208 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9209 if (!msg)
9210 return;
9211
9212 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9213 if (!hdr) {
9214 nlmsg_free(msg);
9215 return;
9216 }
9217
David S. Miller9360ffd2012-03-29 04:41:26 -04009218 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009219 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9220 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009221 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009222 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009223 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9224 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009225 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9226 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009227
David S. Miller9360ffd2012-03-29 04:41:26 -04009228 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9229 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9230 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009231
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009232 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009233
9234 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9235 nl80211_mlme_mcgrp.id, gfp);
9236 return;
9237
9238 nla_put_failure:
9239 genlmsg_cancel(msg, hdr);
9240 nlmsg_free(msg);
9241}
9242
9243void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009244 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009245 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009246 unsigned int duration, gfp_t gfp)
9247{
9248 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009249 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009250 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009251}
9252
9253void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009254 struct cfg80211_registered_device *rdev,
9255 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009256 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009257{
9258 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009259 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009260}
9261
Johannes Berg98b62182009-12-23 13:15:44 +01009262void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9263 struct net_device *dev, const u8 *mac_addr,
9264 struct station_info *sinfo, gfp_t gfp)
9265{
9266 struct sk_buff *msg;
9267
Thomas Graf58050fc2012-06-28 03:57:45 +00009268 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009269 if (!msg)
9270 return;
9271
John W. Linville66266b32012-03-15 13:25:41 -04009272 if (nl80211_send_station(msg, 0, 0, 0,
9273 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009274 nlmsg_free(msg);
9275 return;
9276 }
9277
9278 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9279 nl80211_mlme_mcgrp.id, gfp);
9280}
9281
Jouni Malinenec15e682011-03-23 15:29:52 +02009282void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9283 struct net_device *dev, const u8 *mac_addr,
9284 gfp_t gfp)
9285{
9286 struct sk_buff *msg;
9287 void *hdr;
9288
Thomas Graf58050fc2012-06-28 03:57:45 +00009289 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009290 if (!msg)
9291 return;
9292
9293 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9294 if (!hdr) {
9295 nlmsg_free(msg);
9296 return;
9297 }
9298
David S. Miller9360ffd2012-03-29 04:41:26 -04009299 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9300 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9301 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009302
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009303 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009304
9305 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9306 nl80211_mlme_mcgrp.id, gfp);
9307 return;
9308
9309 nla_put_failure:
9310 genlmsg_cancel(msg, hdr);
9311 nlmsg_free(msg);
9312}
9313
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309314void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9315 struct net_device *dev, const u8 *mac_addr,
9316 enum nl80211_connect_failed_reason reason,
9317 gfp_t gfp)
9318{
9319 struct sk_buff *msg;
9320 void *hdr;
9321
9322 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9323 if (!msg)
9324 return;
9325
9326 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9327 if (!hdr) {
9328 nlmsg_free(msg);
9329 return;
9330 }
9331
9332 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9333 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9334 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9335 goto nla_put_failure;
9336
9337 genlmsg_end(msg, hdr);
9338
9339 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9340 nl80211_mlme_mcgrp.id, gfp);
9341 return;
9342
9343 nla_put_failure:
9344 genlmsg_cancel(msg, hdr);
9345 nlmsg_free(msg);
9346}
9347
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009348static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9349 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009350{
9351 struct wireless_dev *wdev = dev->ieee80211_ptr;
9352 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9353 struct sk_buff *msg;
9354 void *hdr;
9355 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009356 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009357
Eric W. Biederman15e47302012-09-07 20:12:54 +00009358 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009359 return false;
9360
9361 msg = nlmsg_new(100, gfp);
9362 if (!msg)
9363 return true;
9364
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009365 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009366 if (!hdr) {
9367 nlmsg_free(msg);
9368 return true;
9369 }
9370
David S. Miller9360ffd2012-03-29 04:41:26 -04009371 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9372 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9373 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9374 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009375
9376 err = genlmsg_end(msg, hdr);
9377 if (err < 0) {
9378 nlmsg_free(msg);
9379 return true;
9380 }
9381
Eric W. Biederman15e47302012-09-07 20:12:54 +00009382 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009383 return true;
9384
9385 nla_put_failure:
9386 genlmsg_cancel(msg, hdr);
9387 nlmsg_free(msg);
9388 return true;
9389}
9390
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009391bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9392{
9393 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9394 addr, gfp);
9395}
9396
9397bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9398 const u8 *addr, gfp_t gfp)
9399{
9400 return __nl80211_unexpected_frame(dev,
9401 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9402 addr, gfp);
9403}
9404
Johannes Berg2e161f72010-08-12 15:38:38 +02009405int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009406 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009407 int freq, int sig_dbm,
9408 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009409{
Johannes Berg71bbc992012-06-15 15:30:18 +02009410 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009411 struct sk_buff *msg;
9412 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009413
9414 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9415 if (!msg)
9416 return -ENOMEM;
9417
Johannes Berg2e161f72010-08-12 15:38:38 +02009418 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009419 if (!hdr) {
9420 nlmsg_free(msg);
9421 return -ENOMEM;
9422 }
9423
David S. Miller9360ffd2012-03-29 04:41:26 -04009424 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009425 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9426 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009427 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9428 (sig_dbm &&
9429 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9430 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9431 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009432
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009433 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009434
Eric W. Biederman15e47302012-09-07 20:12:54 +00009435 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009436
9437 nla_put_failure:
9438 genlmsg_cancel(msg, hdr);
9439 nlmsg_free(msg);
9440 return -ENOBUFS;
9441}
9442
Johannes Berg2e161f72010-08-12 15:38:38 +02009443void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009444 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009445 const u8 *buf, size_t len, bool ack,
9446 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009447{
Johannes Berg71bbc992012-06-15 15:30:18 +02009448 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009449 struct sk_buff *msg;
9450 void *hdr;
9451
9452 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9453 if (!msg)
9454 return;
9455
Johannes Berg2e161f72010-08-12 15:38:38 +02009456 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009457 if (!hdr) {
9458 nlmsg_free(msg);
9459 return;
9460 }
9461
David S. Miller9360ffd2012-03-29 04:41:26 -04009462 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009463 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9464 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009465 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9466 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9467 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9468 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009469
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009470 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009471
9472 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9473 return;
9474
9475 nla_put_failure:
9476 genlmsg_cancel(msg, hdr);
9477 nlmsg_free(msg);
9478}
9479
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009480void
9481nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9482 struct net_device *netdev,
9483 enum nl80211_cqm_rssi_threshold_event rssi_event,
9484 gfp_t gfp)
9485{
9486 struct sk_buff *msg;
9487 struct nlattr *pinfoattr;
9488 void *hdr;
9489
Thomas Graf58050fc2012-06-28 03:57:45 +00009490 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009491 if (!msg)
9492 return;
9493
9494 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9495 if (!hdr) {
9496 nlmsg_free(msg);
9497 return;
9498 }
9499
David S. Miller9360ffd2012-03-29 04:41:26 -04009500 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9501 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9502 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009503
9504 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9505 if (!pinfoattr)
9506 goto nla_put_failure;
9507
David S. Miller9360ffd2012-03-29 04:41:26 -04009508 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9509 rssi_event))
9510 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009511
9512 nla_nest_end(msg, pinfoattr);
9513
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009514 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009515
9516 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9517 nl80211_mlme_mcgrp.id, gfp);
9518 return;
9519
9520 nla_put_failure:
9521 genlmsg_cancel(msg, hdr);
9522 nlmsg_free(msg);
9523}
9524
Johannes Berge5497d72011-07-05 16:35:40 +02009525void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9526 struct net_device *netdev, const u8 *bssid,
9527 const u8 *replay_ctr, gfp_t gfp)
9528{
9529 struct sk_buff *msg;
9530 struct nlattr *rekey_attr;
9531 void *hdr;
9532
Thomas Graf58050fc2012-06-28 03:57:45 +00009533 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009534 if (!msg)
9535 return;
9536
9537 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9538 if (!hdr) {
9539 nlmsg_free(msg);
9540 return;
9541 }
9542
David S. Miller9360ffd2012-03-29 04:41:26 -04009543 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9544 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9545 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9546 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009547
9548 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9549 if (!rekey_attr)
9550 goto nla_put_failure;
9551
David S. Miller9360ffd2012-03-29 04:41:26 -04009552 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9553 NL80211_REPLAY_CTR_LEN, replay_ctr))
9554 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009555
9556 nla_nest_end(msg, rekey_attr);
9557
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009558 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009559
9560 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9561 nl80211_mlme_mcgrp.id, gfp);
9562 return;
9563
9564 nla_put_failure:
9565 genlmsg_cancel(msg, hdr);
9566 nlmsg_free(msg);
9567}
9568
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009569void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9570 struct net_device *netdev, int index,
9571 const u8 *bssid, bool preauth, gfp_t gfp)
9572{
9573 struct sk_buff *msg;
9574 struct nlattr *attr;
9575 void *hdr;
9576
Thomas Graf58050fc2012-06-28 03:57:45 +00009577 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009578 if (!msg)
9579 return;
9580
9581 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
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 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009590
9591 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9592 if (!attr)
9593 goto nla_put_failure;
9594
David S. Miller9360ffd2012-03-29 04:41:26 -04009595 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9596 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9597 (preauth &&
9598 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9599 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009600
9601 nla_nest_end(msg, attr);
9602
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009603 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009604
9605 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9606 nl80211_mlme_mcgrp.id, gfp);
9607 return;
9608
9609 nla_put_failure:
9610 genlmsg_cancel(msg, hdr);
9611 nlmsg_free(msg);
9612}
9613
Thomas Pedersen53145262012-04-06 13:35:47 -07009614void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009615 struct net_device *netdev,
9616 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009617{
9618 struct sk_buff *msg;
9619 void *hdr;
9620
Thomas Graf58050fc2012-06-28 03:57:45 +00009621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009622 if (!msg)
9623 return;
9624
9625 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9626 if (!hdr) {
9627 nlmsg_free(msg);
9628 return;
9629 }
9630
Johannes Berg683b6d32012-11-08 21:25:48 +01009631 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9632 goto nla_put_failure;
9633
9634 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009635 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009636
9637 genlmsg_end(msg, hdr);
9638
9639 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9640 nl80211_mlme_mcgrp.id, gfp);
9641 return;
9642
9643 nla_put_failure:
9644 genlmsg_cancel(msg, hdr);
9645 nlmsg_free(msg);
9646}
9647
Johannes Bergc063dbf2010-11-24 08:10:05 +01009648void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009649nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9650 struct net_device *netdev, const u8 *peer,
9651 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9652{
9653 struct sk_buff *msg;
9654 struct nlattr *pinfoattr;
9655 void *hdr;
9656
9657 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9658 if (!msg)
9659 return;
9660
9661 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9662 if (!hdr) {
9663 nlmsg_free(msg);
9664 return;
9665 }
9666
9667 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9668 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9669 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9670 goto nla_put_failure;
9671
9672 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9673 if (!pinfoattr)
9674 goto nla_put_failure;
9675
9676 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9677 goto nla_put_failure;
9678
9679 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9680 goto nla_put_failure;
9681
9682 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9683 goto nla_put_failure;
9684
9685 nla_nest_end(msg, pinfoattr);
9686
9687 genlmsg_end(msg, hdr);
9688
9689 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9690 nl80211_mlme_mcgrp.id, gfp);
9691 return;
9692
9693 nla_put_failure:
9694 genlmsg_cancel(msg, hdr);
9695 nlmsg_free(msg);
9696}
9697
9698void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009699nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9700 struct cfg80211_chan_def *chandef,
9701 enum nl80211_radar_event event,
9702 struct net_device *netdev, gfp_t gfp)
9703{
9704 struct sk_buff *msg;
9705 void *hdr;
9706
9707 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9708 if (!msg)
9709 return;
9710
9711 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9712 if (!hdr) {
9713 nlmsg_free(msg);
9714 return;
9715 }
9716
9717 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9718 goto nla_put_failure;
9719
9720 /* NOP and radar events don't need a netdev parameter */
9721 if (netdev) {
9722 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9723
9724 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9725 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9726 goto nla_put_failure;
9727 }
9728
9729 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9730 goto nla_put_failure;
9731
9732 if (nl80211_send_chandef(msg, chandef))
9733 goto nla_put_failure;
9734
9735 if (genlmsg_end(msg, hdr) < 0) {
9736 nlmsg_free(msg);
9737 return;
9738 }
9739
9740 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9741 nl80211_mlme_mcgrp.id, gfp);
9742 return;
9743
9744 nla_put_failure:
9745 genlmsg_cancel(msg, hdr);
9746 nlmsg_free(msg);
9747}
9748
9749void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009750nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9751 struct net_device *netdev, const u8 *peer,
9752 u32 num_packets, gfp_t gfp)
9753{
9754 struct sk_buff *msg;
9755 struct nlattr *pinfoattr;
9756 void *hdr;
9757
Thomas Graf58050fc2012-06-28 03:57:45 +00009758 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009759 if (!msg)
9760 return;
9761
9762 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9763 if (!hdr) {
9764 nlmsg_free(msg);
9765 return;
9766 }
9767
David S. Miller9360ffd2012-03-29 04:41:26 -04009768 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9769 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9770 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9771 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009772
9773 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9774 if (!pinfoattr)
9775 goto nla_put_failure;
9776
David S. Miller9360ffd2012-03-29 04:41:26 -04009777 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9778 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009779
9780 nla_nest_end(msg, pinfoattr);
9781
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009782 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009783
9784 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9785 nl80211_mlme_mcgrp.id, gfp);
9786 return;
9787
9788 nla_put_failure:
9789 genlmsg_cancel(msg, hdr);
9790 nlmsg_free(msg);
9791}
9792
Johannes Berg7f6cf312011-11-04 11:18:15 +01009793void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9794 u64 cookie, bool acked, gfp_t gfp)
9795{
9796 struct wireless_dev *wdev = dev->ieee80211_ptr;
9797 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9798 struct sk_buff *msg;
9799 void *hdr;
9800 int err;
9801
Beni Lev4ee3e062012-08-27 12:49:39 +03009802 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9803
Thomas Graf58050fc2012-06-28 03:57:45 +00009804 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009805
Johannes Berg7f6cf312011-11-04 11:18:15 +01009806 if (!msg)
9807 return;
9808
9809 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9810 if (!hdr) {
9811 nlmsg_free(msg);
9812 return;
9813 }
9814
David S. Miller9360ffd2012-03-29 04:41:26 -04009815 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9816 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9817 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9818 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9819 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9820 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009821
9822 err = genlmsg_end(msg, hdr);
9823 if (err < 0) {
9824 nlmsg_free(msg);
9825 return;
9826 }
9827
9828 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9829 nl80211_mlme_mcgrp.id, gfp);
9830 return;
9831
9832 nla_put_failure:
9833 genlmsg_cancel(msg, hdr);
9834 nlmsg_free(msg);
9835}
9836EXPORT_SYMBOL(cfg80211_probe_status);
9837
Johannes Berg5e760232011-11-04 11:18:17 +01009838void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9839 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009840 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009841{
9842 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9843 struct sk_buff *msg;
9844 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009845 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009846
Beni Lev4ee3e062012-08-27 12:49:39 +03009847 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9848
Ben Greear37c73b52012-10-26 14:49:25 -07009849 spin_lock_bh(&rdev->beacon_registrations_lock);
9850 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9851 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9852 if (!msg) {
9853 spin_unlock_bh(&rdev->beacon_registrations_lock);
9854 return;
9855 }
Johannes Berg5e760232011-11-04 11:18:17 +01009856
Ben Greear37c73b52012-10-26 14:49:25 -07009857 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9858 if (!hdr)
9859 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009860
Ben Greear37c73b52012-10-26 14:49:25 -07009861 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9862 (freq &&
9863 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9864 (sig_dbm &&
9865 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9866 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9867 goto nla_put_failure;
9868
9869 genlmsg_end(msg, hdr);
9870
9871 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009872 }
Ben Greear37c73b52012-10-26 14:49:25 -07009873 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009874 return;
9875
9876 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009877 spin_unlock_bh(&rdev->beacon_registrations_lock);
9878 if (hdr)
9879 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009880 nlmsg_free(msg);
9881}
9882EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9883
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009884#ifdef CONFIG_PM
9885void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9886 struct cfg80211_wowlan_wakeup *wakeup,
9887 gfp_t gfp)
9888{
9889 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9890 struct sk_buff *msg;
9891 void *hdr;
9892 int err, size = 200;
9893
9894 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9895
9896 if (wakeup)
9897 size += wakeup->packet_present_len;
9898
9899 msg = nlmsg_new(size, gfp);
9900 if (!msg)
9901 return;
9902
9903 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9904 if (!hdr)
9905 goto free_msg;
9906
9907 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9908 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9909 goto free_msg;
9910
9911 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9912 wdev->netdev->ifindex))
9913 goto free_msg;
9914
9915 if (wakeup) {
9916 struct nlattr *reasons;
9917
9918 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9919
9920 if (wakeup->disconnect &&
9921 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9922 goto free_msg;
9923 if (wakeup->magic_pkt &&
9924 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9925 goto free_msg;
9926 if (wakeup->gtk_rekey_failure &&
9927 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9928 goto free_msg;
9929 if (wakeup->eap_identity_req &&
9930 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9931 goto free_msg;
9932 if (wakeup->four_way_handshake &&
9933 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9934 goto free_msg;
9935 if (wakeup->rfkill_release &&
9936 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9937 goto free_msg;
9938
9939 if (wakeup->pattern_idx >= 0 &&
9940 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9941 wakeup->pattern_idx))
9942 goto free_msg;
9943
Johannes Berg2a0e0472013-01-23 22:57:40 +01009944 if (wakeup->tcp_match)
9945 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
9946
9947 if (wakeup->tcp_connlost)
9948 nla_put_flag(msg,
9949 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
9950
9951 if (wakeup->tcp_nomoretokens)
9952 nla_put_flag(msg,
9953 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
9954
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009955 if (wakeup->packet) {
9956 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9957 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9958
9959 if (!wakeup->packet_80211) {
9960 pkt_attr =
9961 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9962 len_attr =
9963 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9964 }
9965
9966 if (wakeup->packet_len &&
9967 nla_put_u32(msg, len_attr, wakeup->packet_len))
9968 goto free_msg;
9969
9970 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9971 wakeup->packet))
9972 goto free_msg;
9973 }
9974
9975 nla_nest_end(msg, reasons);
9976 }
9977
9978 err = genlmsg_end(msg, hdr);
9979 if (err < 0)
9980 goto free_msg;
9981
9982 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9983 nl80211_mlme_mcgrp.id, gfp);
9984 return;
9985
9986 free_msg:
9987 nlmsg_free(msg);
9988}
9989EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9990#endif
9991
Jouni Malinen3475b092012-11-16 22:49:57 +02009992void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9993 enum nl80211_tdls_operation oper,
9994 u16 reason_code, gfp_t gfp)
9995{
9996 struct wireless_dev *wdev = dev->ieee80211_ptr;
9997 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9998 struct sk_buff *msg;
9999 void *hdr;
10000 int err;
10001
10002 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10003 reason_code);
10004
10005 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10006 if (!msg)
10007 return;
10008
10009 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10010 if (!hdr) {
10011 nlmsg_free(msg);
10012 return;
10013 }
10014
10015 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10016 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10017 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10018 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10019 (reason_code > 0 &&
10020 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10021 goto nla_put_failure;
10022
10023 err = genlmsg_end(msg, hdr);
10024 if (err < 0) {
10025 nlmsg_free(msg);
10026 return;
10027 }
10028
10029 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10030 nl80211_mlme_mcgrp.id, gfp);
10031 return;
10032
10033 nla_put_failure:
10034 genlmsg_cancel(msg, hdr);
10035 nlmsg_free(msg);
10036}
10037EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10038
Jouni Malinen026331c2010-02-15 12:53:10 +020010039static int nl80211_netlink_notify(struct notifier_block * nb,
10040 unsigned long state,
10041 void *_notify)
10042{
10043 struct netlink_notify *notify = _notify;
10044 struct cfg80211_registered_device *rdev;
10045 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010046 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010047
10048 if (state != NETLINK_URELEASE)
10049 return NOTIFY_DONE;
10050
10051 rcu_read_lock();
10052
Johannes Berg5e760232011-11-04 11:18:17 +010010053 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010054 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010055 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010056
10057 spin_lock_bh(&rdev->beacon_registrations_lock);
10058 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10059 list) {
10060 if (reg->nlportid == notify->portid) {
10061 list_del(&reg->list);
10062 kfree(reg);
10063 break;
10064 }
10065 }
10066 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010067 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010068
10069 rcu_read_unlock();
10070
10071 return NOTIFY_DONE;
10072}
10073
10074static struct notifier_block nl80211_netlink_notifier = {
10075 .notifier_call = nl80211_netlink_notify,
10076};
10077
Johannes Berg55682962007-09-20 13:09:35 -040010078/* initialisation/exit functions */
10079
10080int nl80211_init(void)
10081{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010082 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010083
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010084 err = genl_register_family_with_ops(&nl80211_fam,
10085 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010086 if (err)
10087 return err;
10088
Johannes Berg55682962007-09-20 13:09:35 -040010089 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10090 if (err)
10091 goto err_out;
10092
Johannes Berg2a519312009-02-10 21:25:55 +010010093 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10094 if (err)
10095 goto err_out;
10096
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010097 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10098 if (err)
10099 goto err_out;
10100
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010101 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10102 if (err)
10103 goto err_out;
10104
Johannes Bergaff89a92009-07-01 21:26:51 +020010105#ifdef CONFIG_NL80211_TESTMODE
10106 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10107 if (err)
10108 goto err_out;
10109#endif
10110
Jouni Malinen026331c2010-02-15 12:53:10 +020010111 err = netlink_register_notifier(&nl80211_netlink_notifier);
10112 if (err)
10113 goto err_out;
10114
Johannes Berg55682962007-09-20 13:09:35 -040010115 return 0;
10116 err_out:
10117 genl_unregister_family(&nl80211_fam);
10118 return err;
10119}
10120
10121void nl80211_exit(void)
10122{
Jouni Malinen026331c2010-02-15 12:53:10 +020010123 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010124 genl_unregister_family(&nl80211_fam);
10125}