blob: 7701538a08829d85a22c1992659c695334b1a052 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100373 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Berg55682962007-09-20 13:09:35 -0400374};
375
Johannes Berge31b8212010-10-05 19:39:30 +0200376/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000377static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200378 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200379 [NL80211_KEY_IDX] = { .type = NLA_U8 },
380 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200381 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200382 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
383 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200384 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100385 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
386};
387
388/* policy for the key default flags */
389static const struct nla_policy
390nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
391 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
392 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200393};
394
Johannes Bergff1b6e62011-05-04 15:37:28 +0200395/* policy for WoWLAN attributes */
396static const struct nla_policy
397nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
398 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
400 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
401 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200402 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
403 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
404 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
405 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100406 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
407};
408
409static const struct nla_policy
410nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
411 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
412 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
413 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
414 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
415 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
416 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
417 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
418 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
419 },
420 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
421 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
422 },
423 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
424 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
425 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200426};
427
Johannes Berge5497d72011-07-05 16:35:40 +0200428/* policy for GTK rekey offload attributes */
429static const struct nla_policy
430nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
431 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
432 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
433 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
434};
435
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300436static const struct nla_policy
437nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200438 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300439 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700440 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300441};
442
Holger Schuriga0438972009-11-11 11:30:02 +0100443/* ifidx get helper */
444static int nl80211_get_ifidx(struct netlink_callback *cb)
445{
446 int res;
447
448 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
449 nl80211_fam.attrbuf, nl80211_fam.maxattr,
450 nl80211_policy);
451 if (res)
452 return res;
453
454 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
455 return -EINVAL;
456
457 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
458 if (!res)
459 return -EINVAL;
460 return res;
461}
462
Johannes Berg67748892010-10-04 21:14:06 +0200463static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
464 struct netlink_callback *cb,
465 struct cfg80211_registered_device **rdev,
466 struct net_device **dev)
467{
468 int ifidx = cb->args[0];
469 int err;
470
471 if (!ifidx)
472 ifidx = nl80211_get_ifidx(cb);
473 if (ifidx < 0)
474 return ifidx;
475
476 cb->args[0] = ifidx;
477
478 rtnl_lock();
479
480 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
481 if (!*dev) {
482 err = -ENODEV;
483 goto out_rtnl;
484 }
485
486 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100487 if (IS_ERR(*rdev)) {
488 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200489 goto out_rtnl;
490 }
491
492 return 0;
493 out_rtnl:
494 rtnl_unlock();
495 return err;
496}
497
498static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
499{
500 cfg80211_unlock_rdev(rdev);
501 rtnl_unlock();
502}
503
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100504/* IE validation */
505static bool is_valid_ie_attr(const struct nlattr *attr)
506{
507 const u8 *pos;
508 int len;
509
510 if (!attr)
511 return true;
512
513 pos = nla_data(attr);
514 len = nla_len(attr);
515
516 while (len) {
517 u8 elemlen;
518
519 if (len < 2)
520 return false;
521 len -= 2;
522
523 elemlen = pos[1];
524 if (elemlen > len)
525 return false;
526
527 len -= elemlen;
528 pos += 2 + elemlen;
529 }
530
531 return true;
532}
533
Johannes Berg55682962007-09-20 13:09:35 -0400534/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000535static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400536 int flags, u8 cmd)
537{
538 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000539 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400540}
541
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400542static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100543 struct ieee80211_channel *chan,
544 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400545{
David S. Miller9360ffd2012-03-29 04:41:26 -0400546 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
547 chan->center_freq))
548 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400549
David S. Miller9360ffd2012-03-29 04:41:26 -0400550 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
551 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
552 goto nla_put_failure;
553 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
554 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
555 goto nla_put_failure;
556 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
557 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
558 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100559 if (chan->flags & IEEE80211_CHAN_RADAR) {
560 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
561 goto nla_put_failure;
562 if (large) {
563 u32 time;
564
565 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
566
567 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
568 chan->dfs_state))
569 goto nla_put_failure;
570 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
571 time))
572 goto nla_put_failure;
573 }
574 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400575
David S. Miller9360ffd2012-03-29 04:41:26 -0400576 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
577 DBM_TO_MBM(chan->max_power)))
578 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400579
580 return 0;
581
582 nla_put_failure:
583 return -ENOBUFS;
584}
585
Johannes Berg55682962007-09-20 13:09:35 -0400586/* netlink command implementations */
587
Johannes Bergb9454e82009-07-08 13:29:08 +0200588struct key_parse {
589 struct key_params p;
590 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200591 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200592 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100593 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200594};
595
596static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
597{
598 struct nlattr *tb[NL80211_KEY_MAX + 1];
599 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
600 nl80211_key_policy);
601 if (err)
602 return err;
603
604 k->def = !!tb[NL80211_KEY_DEFAULT];
605 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
606
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100607 if (k->def) {
608 k->def_uni = true;
609 k->def_multi = true;
610 }
611 if (k->defmgmt)
612 k->def_multi = true;
613
Johannes Bergb9454e82009-07-08 13:29:08 +0200614 if (tb[NL80211_KEY_IDX])
615 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
616
617 if (tb[NL80211_KEY_DATA]) {
618 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
619 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
620 }
621
622 if (tb[NL80211_KEY_SEQ]) {
623 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
624 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
625 }
626
627 if (tb[NL80211_KEY_CIPHER])
628 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
629
Johannes Berge31b8212010-10-05 19:39:30 +0200630 if (tb[NL80211_KEY_TYPE]) {
631 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
632 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
633 return -EINVAL;
634 }
635
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100636 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
637 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100638 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
639 tb[NL80211_KEY_DEFAULT_TYPES],
640 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100641 if (err)
642 return err;
643
644 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
645 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
646 }
647
Johannes Bergb9454e82009-07-08 13:29:08 +0200648 return 0;
649}
650
651static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
652{
653 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
654 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
655 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
656 }
657
658 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
659 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
660 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
661 }
662
663 if (info->attrs[NL80211_ATTR_KEY_IDX])
664 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
665
666 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
667 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
668
669 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
670 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
671
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100672 if (k->def) {
673 k->def_uni = true;
674 k->def_multi = true;
675 }
676 if (k->defmgmt)
677 k->def_multi = true;
678
Johannes Berge31b8212010-10-05 19:39:30 +0200679 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
680 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
681 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
682 return -EINVAL;
683 }
684
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100685 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
686 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
687 int err = nla_parse_nested(
688 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
689 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
690 nl80211_key_default_policy);
691 if (err)
692 return err;
693
694 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
695 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
696 }
697
Johannes Bergb9454e82009-07-08 13:29:08 +0200698 return 0;
699}
700
701static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
702{
703 int err;
704
705 memset(k, 0, sizeof(*k));
706 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200707 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200708
709 if (info->attrs[NL80211_ATTR_KEY])
710 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
711 else
712 err = nl80211_parse_key_old(info, k);
713
714 if (err)
715 return err;
716
717 if (k->def && k->defmgmt)
718 return -EINVAL;
719
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100720 if (k->defmgmt) {
721 if (k->def_uni || !k->def_multi)
722 return -EINVAL;
723 }
724
Johannes Bergb9454e82009-07-08 13:29:08 +0200725 if (k->idx != -1) {
726 if (k->defmgmt) {
727 if (k->idx < 4 || k->idx > 5)
728 return -EINVAL;
729 } else if (k->def) {
730 if (k->idx < 0 || k->idx > 3)
731 return -EINVAL;
732 } else {
733 if (k->idx < 0 || k->idx > 5)
734 return -EINVAL;
735 }
736 }
737
738 return 0;
739}
740
Johannes Bergfffd0932009-07-08 14:22:54 +0200741static struct cfg80211_cached_keys *
742nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530743 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200744{
745 struct key_parse parse;
746 struct nlattr *key;
747 struct cfg80211_cached_keys *result;
748 int rem, err, def = 0;
749
750 result = kzalloc(sizeof(*result), GFP_KERNEL);
751 if (!result)
752 return ERR_PTR(-ENOMEM);
753
754 result->def = -1;
755 result->defmgmt = -1;
756
757 nla_for_each_nested(key, keys, rem) {
758 memset(&parse, 0, sizeof(parse));
759 parse.idx = -1;
760
761 err = nl80211_parse_key_new(key, &parse);
762 if (err)
763 goto error;
764 err = -EINVAL;
765 if (!parse.p.key)
766 goto error;
767 if (parse.idx < 0 || parse.idx > 4)
768 goto error;
769 if (parse.def) {
770 if (def)
771 goto error;
772 def = 1;
773 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100774 if (!parse.def_uni || !parse.def_multi)
775 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200776 } else if (parse.defmgmt)
777 goto error;
778 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200779 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200780 if (err)
781 goto error;
782 result->params[parse.idx].cipher = parse.p.cipher;
783 result->params[parse.idx].key_len = parse.p.key_len;
784 result->params[parse.idx].key = result->data[parse.idx];
785 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530786
787 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
788 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
789 if (no_ht)
790 *no_ht = true;
791 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200792 }
793
794 return result;
795 error:
796 kfree(result);
797 return ERR_PTR(err);
798}
799
800static int nl80211_key_allowed(struct wireless_dev *wdev)
801{
802 ASSERT_WDEV_LOCK(wdev);
803
Johannes Bergfffd0932009-07-08 14:22:54 +0200804 switch (wdev->iftype) {
805 case NL80211_IFTYPE_AP:
806 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200807 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700808 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200809 break;
810 case NL80211_IFTYPE_ADHOC:
811 if (!wdev->current_bss)
812 return -ENOLINK;
813 break;
814 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200815 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200816 if (wdev->sme_state != CFG80211_SME_CONNECTED)
817 return -ENOLINK;
818 break;
819 default:
820 return -EINVAL;
821 }
822
823 return 0;
824}
825
Johannes Berg7527a782011-05-13 10:58:57 +0200826static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
827{
828 struct nlattr *nl_modes = nla_nest_start(msg, attr);
829 int i;
830
831 if (!nl_modes)
832 goto nla_put_failure;
833
834 i = 0;
835 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400836 if ((ifmodes & 1) && nla_put_flag(msg, i))
837 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200838 ifmodes >>= 1;
839 i++;
840 }
841
842 nla_nest_end(msg, nl_modes);
843 return 0;
844
845nla_put_failure:
846 return -ENOBUFS;
847}
848
849static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100850 struct sk_buff *msg,
851 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200852{
853 struct nlattr *nl_combis;
854 int i, j;
855
856 nl_combis = nla_nest_start(msg,
857 NL80211_ATTR_INTERFACE_COMBINATIONS);
858 if (!nl_combis)
859 goto nla_put_failure;
860
861 for (i = 0; i < wiphy->n_iface_combinations; i++) {
862 const struct ieee80211_iface_combination *c;
863 struct nlattr *nl_combi, *nl_limits;
864
865 c = &wiphy->iface_combinations[i];
866
867 nl_combi = nla_nest_start(msg, i + 1);
868 if (!nl_combi)
869 goto nla_put_failure;
870
871 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
872 if (!nl_limits)
873 goto nla_put_failure;
874
875 for (j = 0; j < c->n_limits; j++) {
876 struct nlattr *nl_limit;
877
878 nl_limit = nla_nest_start(msg, j + 1);
879 if (!nl_limit)
880 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400881 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
882 c->limits[j].max))
883 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200884 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
885 c->limits[j].types))
886 goto nla_put_failure;
887 nla_nest_end(msg, nl_limit);
888 }
889
890 nla_nest_end(msg, nl_limits);
891
David S. Miller9360ffd2012-03-29 04:41:26 -0400892 if (c->beacon_int_infra_match &&
893 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
894 goto nla_put_failure;
895 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
896 c->num_different_channels) ||
897 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
898 c->max_interfaces))
899 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100900 if (large &&
901 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
902 c->radar_detect_widths))
903 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200904
905 nla_nest_end(msg, nl_combi);
906 }
907
908 nla_nest_end(msg, nl_combis);
909
910 return 0;
911nla_put_failure:
912 return -ENOBUFS;
913}
914
Johannes Berg3713b4e2013-02-14 16:19:38 +0100915#ifdef CONFIG_PM
916static int nl80211_send_wowlan(struct sk_buff *msg,
917 struct cfg80211_registered_device *dev)
918{
919 struct nlattr *nl_wowlan;
920
921 if (!dev->wiphy.wowlan.flags && !dev->wiphy.wowlan.n_patterns)
922 return 0;
923
924 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
925 if (!nl_wowlan)
926 return -ENOBUFS;
927
928 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
929 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
930 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
931 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
932 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
933 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
934 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
935 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
936 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
937 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
938 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
939 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
940 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
941 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
942 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
943 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
944 return -ENOBUFS;
945
946 if (dev->wiphy.wowlan.n_patterns) {
947 struct nl80211_wowlan_pattern_support pat = {
948 .max_patterns = dev->wiphy.wowlan.n_patterns,
949 .min_pattern_len = dev->wiphy.wowlan.pattern_min_len,
950 .max_pattern_len = dev->wiphy.wowlan.pattern_max_len,
951 .max_pkt_offset = dev->wiphy.wowlan.max_pkt_offset,
952 };
953
954 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
955 sizeof(pat), &pat))
956 return -ENOBUFS;
957 }
958
959 nla_nest_end(msg, nl_wowlan);
960
961 return 0;
962}
963#endif
964
965static int nl80211_send_band_rateinfo(struct sk_buff *msg,
966 struct ieee80211_supported_band *sband)
967{
968 struct nlattr *nl_rates, *nl_rate;
969 struct ieee80211_rate *rate;
970 int i;
971
972 /* add HT info */
973 if (sband->ht_cap.ht_supported &&
974 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
975 sizeof(sband->ht_cap.mcs),
976 &sband->ht_cap.mcs) ||
977 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
978 sband->ht_cap.cap) ||
979 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
980 sband->ht_cap.ampdu_factor) ||
981 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
982 sband->ht_cap.ampdu_density)))
983 return -ENOBUFS;
984
985 /* add VHT info */
986 if (sband->vht_cap.vht_supported &&
987 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
988 sizeof(sband->vht_cap.vht_mcs),
989 &sband->vht_cap.vht_mcs) ||
990 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
991 sband->vht_cap.cap)))
992 return -ENOBUFS;
993
994 /* add bitrates */
995 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
996 if (!nl_rates)
997 return -ENOBUFS;
998
999 for (i = 0; i < sband->n_bitrates; i++) {
1000 nl_rate = nla_nest_start(msg, i);
1001 if (!nl_rate)
1002 return -ENOBUFS;
1003
1004 rate = &sband->bitrates[i];
1005 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1006 rate->bitrate))
1007 return -ENOBUFS;
1008 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1009 nla_put_flag(msg,
1010 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1011 return -ENOBUFS;
1012
1013 nla_nest_end(msg, nl_rate);
1014 }
1015
1016 nla_nest_end(msg, nl_rates);
1017
1018 return 0;
1019}
1020
1021static int
1022nl80211_send_mgmt_stypes(struct sk_buff *msg,
1023 const struct ieee80211_txrx_stypes *mgmt_stypes)
1024{
1025 u16 stypes;
1026 struct nlattr *nl_ftypes, *nl_ifs;
1027 enum nl80211_iftype ift;
1028 int i;
1029
1030 if (!mgmt_stypes)
1031 return 0;
1032
1033 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1034 if (!nl_ifs)
1035 return -ENOBUFS;
1036
1037 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1038 nl_ftypes = nla_nest_start(msg, ift);
1039 if (!nl_ftypes)
1040 return -ENOBUFS;
1041 i = 0;
1042 stypes = mgmt_stypes[ift].tx;
1043 while (stypes) {
1044 if ((stypes & 1) &&
1045 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1046 (i << 4) | IEEE80211_FTYPE_MGMT))
1047 return -ENOBUFS;
1048 stypes >>= 1;
1049 i++;
1050 }
1051 nla_nest_end(msg, nl_ftypes);
1052 }
1053
1054 nla_nest_end(msg, nl_ifs);
1055
1056 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1057 if (!nl_ifs)
1058 return -ENOBUFS;
1059
1060 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1061 nl_ftypes = nla_nest_start(msg, ift);
1062 if (!nl_ftypes)
1063 return -ENOBUFS;
1064 i = 0;
1065 stypes = mgmt_stypes[ift].rx;
1066 while (stypes) {
1067 if ((stypes & 1) &&
1068 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1069 (i << 4) | IEEE80211_FTYPE_MGMT))
1070 return -ENOBUFS;
1071 stypes >>= 1;
1072 i++;
1073 }
1074 nla_nest_end(msg, nl_ftypes);
1075 }
1076 nla_nest_end(msg, nl_ifs);
1077
1078 return 0;
1079}
1080
1081static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1082 struct sk_buff *msg, u32 portid, u32 seq,
1083 int flags, bool split, long *split_start,
1084 long *band_start, long *chan_start)
Johannes Berg55682962007-09-20 13:09:35 -04001085{
1086 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001087 struct nlattr *nl_bands, *nl_band;
1088 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001089 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001090 enum ieee80211_band band;
1091 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001092 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001093 const struct ieee80211_txrx_stypes *mgmt_stypes =
1094 dev->wiphy.mgmt_stypes;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001095 long start = 0, start_chan = 0, start_band = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001096
Eric W. Biederman15e47302012-09-07 20:12:54 +00001097 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001098 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001099 return -ENOBUFS;
1100
1101 /* allow always using the variables */
1102 if (!split) {
1103 split_start = &start;
1104 band_start = &start_band;
1105 chan_start = &start_chan;
1106 }
Johannes Berg55682962007-09-20 13:09:35 -04001107
David S. Miller9360ffd2012-03-29 04:41:26 -04001108 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001109 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1110 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001111 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001112 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001113 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001114
Johannes Berg3713b4e2013-02-14 16:19:38 +01001115 switch (*split_start) {
1116 case 0:
1117 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1118 dev->wiphy.retry_short) ||
1119 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1120 dev->wiphy.retry_long) ||
1121 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1122 dev->wiphy.frag_threshold) ||
1123 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1124 dev->wiphy.rts_threshold) ||
1125 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1126 dev->wiphy.coverage_class) ||
1127 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1128 dev->wiphy.max_scan_ssids) ||
1129 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1130 dev->wiphy.max_sched_scan_ssids) ||
1131 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1132 dev->wiphy.max_scan_ie_len) ||
1133 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1134 dev->wiphy.max_sched_scan_ie_len) ||
1135 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1136 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001137 goto nla_put_failure;
1138
Johannes Berg3713b4e2013-02-14 16:19:38 +01001139 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1140 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1141 goto nla_put_failure;
1142 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1143 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1144 goto nla_put_failure;
1145 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1146 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1147 goto nla_put_failure;
1148 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1149 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1150 goto nla_put_failure;
1151 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1152 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1153 goto nla_put_failure;
1154 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1155 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001156 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001157
Johannes Berg3713b4e2013-02-14 16:19:38 +01001158 (*split_start)++;
1159 if (split)
1160 break;
1161 case 1:
1162 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1163 sizeof(u32) * dev->wiphy.n_cipher_suites,
1164 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001165 goto nla_put_failure;
1166
Johannes Berg3713b4e2013-02-14 16:19:38 +01001167 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1168 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001169 goto nla_put_failure;
1170
Johannes Berg3713b4e2013-02-14 16:19:38 +01001171 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1172 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1173 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001174
Johannes Berg3713b4e2013-02-14 16:19:38 +01001175 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1176 dev->wiphy.available_antennas_tx) ||
1177 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1178 dev->wiphy.available_antennas_rx))
1179 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001180
Johannes Berg3713b4e2013-02-14 16:19:38 +01001181 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1182 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1183 dev->wiphy.probe_resp_offload))
1184 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001185
Johannes Berg3713b4e2013-02-14 16:19:38 +01001186 if ((dev->wiphy.available_antennas_tx ||
1187 dev->wiphy.available_antennas_rx) &&
1188 dev->ops->get_antenna) {
1189 u32 tx_ant = 0, rx_ant = 0;
1190 int res;
1191 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1192 if (!res) {
1193 if (nla_put_u32(msg,
1194 NL80211_ATTR_WIPHY_ANTENNA_TX,
1195 tx_ant) ||
1196 nla_put_u32(msg,
1197 NL80211_ATTR_WIPHY_ANTENNA_RX,
1198 rx_ant))
1199 goto nla_put_failure;
1200 }
Johannes Bergee688b002008-01-24 19:38:39 +01001201 }
1202
Johannes Berg3713b4e2013-02-14 16:19:38 +01001203 (*split_start)++;
1204 if (split)
1205 break;
1206 case 2:
1207 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1208 dev->wiphy.interface_modes))
1209 goto nla_put_failure;
1210 (*split_start)++;
1211 if (split)
1212 break;
1213 case 3:
1214 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1215 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001216 goto nla_put_failure;
1217
Johannes Berg3713b4e2013-02-14 16:19:38 +01001218 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1219 struct ieee80211_supported_band *sband;
1220
1221 sband = dev->wiphy.bands[band];
1222
1223 if (!sband)
1224 continue;
1225
1226 nl_band = nla_nest_start(msg, band);
1227 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001228 goto nla_put_failure;
1229
Johannes Berg3713b4e2013-02-14 16:19:38 +01001230 switch (*chan_start) {
1231 case 0:
1232 if (nl80211_send_band_rateinfo(msg, sband))
1233 goto nla_put_failure;
1234 (*chan_start)++;
1235 if (split)
1236 break;
1237 default:
1238 /* add frequencies */
1239 nl_freqs = nla_nest_start(
1240 msg, NL80211_BAND_ATTR_FREQS);
1241 if (!nl_freqs)
1242 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001243
Johannes Berg3713b4e2013-02-14 16:19:38 +01001244 for (i = *chan_start - 1;
1245 i < sband->n_channels;
1246 i++) {
1247 nl_freq = nla_nest_start(msg, i);
1248 if (!nl_freq)
1249 goto nla_put_failure;
1250
1251 chan = &sband->channels[i];
1252
Johannes Bergcdc89b92013-02-18 23:54:36 +01001253 if (nl80211_msg_put_channel(msg, chan,
1254 split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001255 goto nla_put_failure;
1256
1257 nla_nest_end(msg, nl_freq);
1258 if (split)
1259 break;
1260 }
1261 if (i < sband->n_channels)
1262 *chan_start = i + 2;
1263 else
1264 *chan_start = 0;
1265 nla_nest_end(msg, nl_freqs);
1266 }
1267
1268 nla_nest_end(msg, nl_band);
1269
1270 if (split) {
1271 /* start again here */
1272 if (*chan_start)
1273 band--;
1274 break;
1275 }
Johannes Bergee688b002008-01-24 19:38:39 +01001276 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001277 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001278
Johannes Berg3713b4e2013-02-14 16:19:38 +01001279 if (band < IEEE80211_NUM_BANDS)
1280 *band_start = band + 1;
1281 else
1282 *band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001283
Johannes Berg3713b4e2013-02-14 16:19:38 +01001284 /* if bands & channels are done, continue outside */
1285 if (*band_start == 0 && *chan_start == 0)
1286 (*split_start)++;
1287 if (split)
1288 break;
1289 case 4:
1290 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1291 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001292 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001293
1294 i = 0;
1295#define CMD(op, n) \
1296 do { \
1297 if (dev->ops->op) { \
1298 i++; \
1299 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1300 goto nla_put_failure; \
1301 } \
1302 } while (0)
1303
1304 CMD(add_virtual_intf, NEW_INTERFACE);
1305 CMD(change_virtual_intf, SET_INTERFACE);
1306 CMD(add_key, NEW_KEY);
1307 CMD(start_ap, START_AP);
1308 CMD(add_station, NEW_STATION);
1309 CMD(add_mpath, NEW_MPATH);
1310 CMD(update_mesh_config, SET_MESH_CONFIG);
1311 CMD(change_bss, SET_BSS);
1312 CMD(auth, AUTHENTICATE);
1313 CMD(assoc, ASSOCIATE);
1314 CMD(deauth, DEAUTHENTICATE);
1315 CMD(disassoc, DISASSOCIATE);
1316 CMD(join_ibss, JOIN_IBSS);
1317 CMD(join_mesh, JOIN_MESH);
1318 CMD(set_pmksa, SET_PMKSA);
1319 CMD(del_pmksa, DEL_PMKSA);
1320 CMD(flush_pmksa, FLUSH_PMKSA);
1321 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1322 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1323 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1324 CMD(mgmt_tx, FRAME);
1325 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1326 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1327 i++;
1328 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1329 goto nla_put_failure;
1330 }
1331 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1332 dev->ops->join_mesh) {
1333 i++;
1334 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1335 goto nla_put_failure;
1336 }
1337 CMD(set_wds_peer, SET_WDS_PEER);
1338 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1339 CMD(tdls_mgmt, TDLS_MGMT);
1340 CMD(tdls_oper, TDLS_OPER);
1341 }
1342 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1343 CMD(sched_scan_start, START_SCHED_SCAN);
1344 CMD(probe_client, PROBE_CLIENT);
1345 CMD(set_noack_map, SET_NOACK_MAP);
1346 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1347 i++;
1348 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1349 goto nla_put_failure;
1350 }
1351 CMD(start_p2p_device, START_P2P_DEVICE);
1352 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001353
Kalle Valo4745fc02011-11-17 19:06:10 +02001354#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001355 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001356#endif
1357
Johannes Berg8fdc6212009-03-14 09:34:01 +01001358#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001359
Johannes Berg3713b4e2013-02-14 16:19:38 +01001360 if (dev->ops->connect || dev->ops->auth) {
1361 i++;
1362 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001363 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001364 }
1365
Johannes Berg3713b4e2013-02-14 16:19:38 +01001366 if (dev->ops->disconnect || dev->ops->deauth) {
1367 i++;
1368 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1369 goto nla_put_failure;
1370 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001371
Johannes Berg3713b4e2013-02-14 16:19:38 +01001372 nla_nest_end(msg, nl_cmds);
1373 (*split_start)++;
1374 if (split)
1375 break;
1376 case 5:
1377 if (dev->ops->remain_on_channel &&
1378 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1379 nla_put_u32(msg,
1380 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1381 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001382 goto nla_put_failure;
1383
Johannes Berg3713b4e2013-02-14 16:19:38 +01001384 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1385 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1386 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001387
Johannes Berg3713b4e2013-02-14 16:19:38 +01001388 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1389 goto nla_put_failure;
1390 (*split_start)++;
1391 if (split)
1392 break;
1393 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001394#ifdef CONFIG_PM
Johannes Berg3713b4e2013-02-14 16:19:38 +01001395 if (nl80211_send_wowlan(msg, dev))
1396 goto nla_put_failure;
1397 (*split_start)++;
1398 if (split)
1399 break;
1400#else
1401 (*split_start)++;
1402#endif
1403 case 7:
1404 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1405 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001406 goto nla_put_failure;
1407
Johannes Bergcdc89b92013-02-18 23:54:36 +01001408 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001409 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001410
Johannes Berg3713b4e2013-02-14 16:19:38 +01001411 (*split_start)++;
1412 if (split)
1413 break;
1414 case 8:
1415 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1416 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1417 dev->wiphy.ap_sme_capa))
1418 goto nla_put_failure;
1419
1420 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1421 dev->wiphy.features))
1422 goto nla_put_failure;
1423
1424 if (dev->wiphy.ht_capa_mod_mask &&
1425 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1426 sizeof(*dev->wiphy.ht_capa_mod_mask),
1427 dev->wiphy.ht_capa_mod_mask))
1428 goto nla_put_failure;
1429
1430 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1431 dev->wiphy.max_acl_mac_addrs &&
1432 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1433 dev->wiphy.max_acl_mac_addrs))
1434 goto nla_put_failure;
1435
1436 /*
1437 * Any information below this point is only available to
1438 * applications that can deal with it being split. This
1439 * helps ensure that newly added capabilities don't break
1440 * older tools by overrunning their buffers.
1441 *
1442 * We still increment split_start so that in the split
1443 * case we'll continue with more data in the next round,
1444 * but break unconditionally so unsplit data stops here.
1445 */
1446 (*split_start)++;
1447 break;
1448 case 9:
1449 /* placeholder */
1450
1451 /* done */
1452 *split_start = 0;
1453 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001454 }
Johannes Berg55682962007-09-20 13:09:35 -04001455 return genlmsg_end(msg, hdr);
1456
1457 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001458 genlmsg_cancel(msg, hdr);
1459 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001460}
1461
1462static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1463{
Johannes Berg645e77d2013-03-01 14:03:49 +01001464 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001465 int start = cb->args[0];
1466 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001467 s64 filter_wiphy = -1;
1468 bool split = false;
1469 struct nlattr **tb = nl80211_fam.attrbuf;
1470 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001471
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001472 mutex_lock(&cfg80211_mutex);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001473 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1474 tb, nl80211_fam.maxattr, nl80211_policy);
1475 if (res == 0) {
1476 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1477 if (tb[NL80211_ATTR_WIPHY])
1478 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1479 if (tb[NL80211_ATTR_WDEV])
1480 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1481 if (tb[NL80211_ATTR_IFINDEX]) {
1482 struct net_device *netdev;
1483 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1484
1485 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1486 if (!netdev) {
1487 mutex_unlock(&cfg80211_mutex);
1488 return -ENODEV;
1489 }
1490 if (netdev->ieee80211_ptr) {
1491 dev = wiphy_to_dev(
1492 netdev->ieee80211_ptr->wiphy);
1493 filter_wiphy = dev->wiphy_idx;
1494 }
1495 dev_put(netdev);
1496 }
1497 }
1498
Johannes Berg79c97e92009-07-07 03:56:12 +02001499 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001500 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1501 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001502 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001503 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001504 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1505 continue;
1506 /* attempt to fit multiple wiphy data chunks into the skb */
1507 do {
1508 ret = nl80211_send_wiphy(dev, skb,
1509 NETLINK_CB(cb->skb).portid,
1510 cb->nlh->nlmsg_seq,
1511 NLM_F_MULTI,
1512 split, &cb->args[1],
1513 &cb->args[2],
1514 &cb->args[3]);
1515 if (ret < 0) {
1516 /*
1517 * If sending the wiphy data didn't fit (ENOBUFS
1518 * or EMSGSIZE returned), this SKB is still
1519 * empty (so it's not too big because another
1520 * wiphy dataset is already in the skb) and
1521 * we've not tried to adjust the dump allocation
1522 * yet ... then adjust the alloc size to be
1523 * bigger, and return 1 but with the empty skb.
1524 * This results in an empty message being RX'ed
1525 * in userspace, but that is ignored.
1526 *
1527 * We can then retry with the larger buffer.
1528 */
1529 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1530 !skb->len &&
1531 cb->min_dump_alloc < 4096) {
1532 cb->min_dump_alloc = 4096;
1533 mutex_unlock(&cfg80211_mutex);
1534 return 1;
1535 }
1536 idx--;
1537 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001538 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001539 } while (cb->args[1] > 0);
1540 break;
Johannes Berg55682962007-09-20 13:09:35 -04001541 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001542 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001543
1544 cb->args[0] = idx;
1545
1546 return skb->len;
1547}
1548
1549static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1550{
1551 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001552 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001553
Johannes Berg645e77d2013-03-01 14:03:49 +01001554 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001555 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001556 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001557
Johannes Berg3713b4e2013-02-14 16:19:38 +01001558 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1559 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001560 nlmsg_free(msg);
1561 return -ENOBUFS;
1562 }
Johannes Berg55682962007-09-20 13:09:35 -04001563
Johannes Berg134e6372009-07-10 09:51:34 +00001564 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001565}
1566
Jouni Malinen31888482008-10-30 16:59:24 +02001567static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1568 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1569 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1570 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1571 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1572 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1573};
1574
1575static int parse_txq_params(struct nlattr *tb[],
1576 struct ieee80211_txq_params *txq_params)
1577{
Johannes Berga3304b02012-03-28 11:04:24 +02001578 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001579 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1580 !tb[NL80211_TXQ_ATTR_AIFS])
1581 return -EINVAL;
1582
Johannes Berga3304b02012-03-28 11:04:24 +02001583 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001584 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1585 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1586 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1587 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1588
Johannes Berga3304b02012-03-28 11:04:24 +02001589 if (txq_params->ac >= NL80211_NUM_ACS)
1590 return -EINVAL;
1591
Jouni Malinen31888482008-10-30 16:59:24 +02001592 return 0;
1593}
1594
Johannes Bergf444de02010-05-05 15:25:02 +02001595static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1596{
1597 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001598 * You can only set the channel explicitly for WDS interfaces,
1599 * all others have their channel managed via their respective
1600 * "establish a connection" command (connect, join, ...)
1601 *
1602 * For AP/GO and mesh mode, the channel can be set with the
1603 * channel userspace API, but is only stored and passed to the
1604 * low-level driver when the AP starts or the mesh is joined.
1605 * This is for backward compatibility, userspace can also give
1606 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001607 *
1608 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001609 * whatever else is going on, so they have their own special
1610 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001611 */
1612 return !wdev ||
1613 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001614 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001615 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1616 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001617}
1618
Johannes Berg683b6d32012-11-08 21:25:48 +01001619static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1620 struct genl_info *info,
1621 struct cfg80211_chan_def *chandef)
1622{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301623 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001624
1625 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1626 return -EINVAL;
1627
1628 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1629
1630 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001631 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1632 chandef->center_freq1 = control_freq;
1633 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001634
1635 /* Primary channel not allowed */
1636 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1637 return -EINVAL;
1638
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001639 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1640 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001641
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001642 chantype = nla_get_u32(
1643 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1644
1645 switch (chantype) {
1646 case NL80211_CHAN_NO_HT:
1647 case NL80211_CHAN_HT20:
1648 case NL80211_CHAN_HT40PLUS:
1649 case NL80211_CHAN_HT40MINUS:
1650 cfg80211_chandef_create(chandef, chandef->chan,
1651 chantype);
1652 break;
1653 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001654 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001655 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001656 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1657 chandef->width =
1658 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1659 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1660 chandef->center_freq1 =
1661 nla_get_u32(
1662 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1663 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1664 chandef->center_freq2 =
1665 nla_get_u32(
1666 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1667 }
1668
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001669 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001670 return -EINVAL;
1671
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001672 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1673 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001674 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001675
Johannes Berg683b6d32012-11-08 21:25:48 +01001676 return 0;
1677}
1678
Johannes Bergf444de02010-05-05 15:25:02 +02001679static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1680 struct wireless_dev *wdev,
1681 struct genl_info *info)
1682{
Johannes Berg683b6d32012-11-08 21:25:48 +01001683 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001684 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001685 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1686
1687 if (wdev)
1688 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001689
Johannes Bergf444de02010-05-05 15:25:02 +02001690 if (!nl80211_can_set_dev_channel(wdev))
1691 return -EOPNOTSUPP;
1692
Johannes Berg683b6d32012-11-08 21:25:48 +01001693 result = nl80211_parse_chandef(rdev, info, &chandef);
1694 if (result)
1695 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001696
1697 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001698 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001699 case NL80211_IFTYPE_AP:
1700 case NL80211_IFTYPE_P2P_GO:
1701 if (wdev->beacon_interval) {
1702 result = -EBUSY;
1703 break;
1704 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001705 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001706 result = -EINVAL;
1707 break;
1708 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001709 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001710 result = 0;
1711 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001712 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001713 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001714 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001715 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001716 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001717 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001718 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001719 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001720 }
1721 mutex_unlock(&rdev->devlist_mtx);
1722
1723 return result;
1724}
1725
1726static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1727{
Johannes Berg4c476992010-10-04 21:36:35 +02001728 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1729 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001730
Johannes Berg4c476992010-10-04 21:36:35 +02001731 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001732}
1733
Bill Jordane8347eb2010-10-01 13:54:28 -04001734static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1735{
Johannes Berg43b19952010-10-07 13:10:30 +02001736 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1737 struct net_device *dev = info->user_ptr[1];
1738 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001739 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001740
1741 if (!info->attrs[NL80211_ATTR_MAC])
1742 return -EINVAL;
1743
Johannes Berg43b19952010-10-07 13:10:30 +02001744 if (netif_running(dev))
1745 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001746
Johannes Berg43b19952010-10-07 13:10:30 +02001747 if (!rdev->ops->set_wds_peer)
1748 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001749
Johannes Berg43b19952010-10-07 13:10:30 +02001750 if (wdev->iftype != NL80211_IFTYPE_WDS)
1751 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001752
1753 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001754 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001755}
1756
1757
Johannes Berg55682962007-09-20 13:09:35 -04001758static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1759{
1760 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001761 struct net_device *netdev = NULL;
1762 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001763 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001764 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001765 u32 changed;
1766 u8 retry_short = 0, retry_long = 0;
1767 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001768 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001769
Johannes Bergf444de02010-05-05 15:25:02 +02001770 /*
1771 * Try to find the wiphy and netdev. Normally this
1772 * function shouldn't need the netdev, but this is
1773 * done for backward compatibility -- previously
1774 * setting the channel was done per wiphy, but now
1775 * it is per netdev. Previous userland like hostapd
1776 * also passed a netdev to set_wiphy, so that it is
1777 * possible to let that go to the right netdev!
1778 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001779 mutex_lock(&cfg80211_mutex);
1780
Johannes Bergf444de02010-05-05 15:25:02 +02001781 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1782 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1783
1784 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1785 if (netdev && netdev->ieee80211_ptr) {
1786 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1787 mutex_lock(&rdev->mtx);
1788 } else
1789 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001790 }
1791
Johannes Bergf444de02010-05-05 15:25:02 +02001792 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001793 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1794 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001795 if (IS_ERR(rdev)) {
1796 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001797 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001798 }
1799 wdev = NULL;
1800 netdev = NULL;
1801 result = 0;
1802
1803 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001804 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001805 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001806
1807 /*
1808 * end workaround code, by now the rdev is available
1809 * and locked, and wdev may or may not be NULL.
1810 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001811
1812 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001813 result = cfg80211_dev_rename(
1814 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001815
1816 mutex_unlock(&cfg80211_mutex);
1817
1818 if (result)
1819 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001820
Jouni Malinen31888482008-10-30 16:59:24 +02001821 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1822 struct ieee80211_txq_params txq_params;
1823 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1824
1825 if (!rdev->ops->set_txq_params) {
1826 result = -EOPNOTSUPP;
1827 goto bad_res;
1828 }
1829
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001830 if (!netdev) {
1831 result = -EINVAL;
1832 goto bad_res;
1833 }
1834
Johannes Berg133a3ff2011-11-03 14:50:13 +01001835 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1836 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1837 result = -EINVAL;
1838 goto bad_res;
1839 }
1840
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001841 if (!netif_running(netdev)) {
1842 result = -ENETDOWN;
1843 goto bad_res;
1844 }
1845
Jouni Malinen31888482008-10-30 16:59:24 +02001846 nla_for_each_nested(nl_txq_params,
1847 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1848 rem_txq_params) {
1849 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1850 nla_data(nl_txq_params),
1851 nla_len(nl_txq_params),
1852 txq_params_policy);
1853 result = parse_txq_params(tb, &txq_params);
1854 if (result)
1855 goto bad_res;
1856
Hila Gonene35e4d22012-06-27 17:19:42 +03001857 result = rdev_set_txq_params(rdev, netdev,
1858 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001859 if (result)
1860 goto bad_res;
1861 }
1862 }
1863
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001864 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001865 result = __nl80211_set_channel(rdev,
1866 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1867 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001868 if (result)
1869 goto bad_res;
1870 }
1871
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001872 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001873 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001874 enum nl80211_tx_power_setting type;
1875 int idx, mbm = 0;
1876
Johannes Bergc8442112012-10-24 10:17:18 +02001877 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1878 txp_wdev = NULL;
1879
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001880 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001881 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001882 goto bad_res;
1883 }
1884
1885 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1886 type = nla_get_u32(info->attrs[idx]);
1887
1888 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1889 (type != NL80211_TX_POWER_AUTOMATIC)) {
1890 result = -EINVAL;
1891 goto bad_res;
1892 }
1893
1894 if (type != NL80211_TX_POWER_AUTOMATIC) {
1895 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1896 mbm = nla_get_u32(info->attrs[idx]);
1897 }
1898
Johannes Bergc8442112012-10-24 10:17:18 +02001899 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001900 if (result)
1901 goto bad_res;
1902 }
1903
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001904 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1905 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1906 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001907 if ((!rdev->wiphy.available_antennas_tx &&
1908 !rdev->wiphy.available_antennas_rx) ||
1909 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001910 result = -EOPNOTSUPP;
1911 goto bad_res;
1912 }
1913
1914 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1915 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1916
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001917 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001918 * available antenna masks, except for the "all" mask */
1919 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1920 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001921 result = -EINVAL;
1922 goto bad_res;
1923 }
1924
Bruno Randolf7f531e02010-12-16 11:30:22 +09001925 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1926 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001927
Hila Gonene35e4d22012-06-27 17:19:42 +03001928 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001929 if (result)
1930 goto bad_res;
1931 }
1932
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001933 changed = 0;
1934
1935 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1936 retry_short = nla_get_u8(
1937 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1938 if (retry_short == 0) {
1939 result = -EINVAL;
1940 goto bad_res;
1941 }
1942 changed |= WIPHY_PARAM_RETRY_SHORT;
1943 }
1944
1945 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1946 retry_long = nla_get_u8(
1947 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1948 if (retry_long == 0) {
1949 result = -EINVAL;
1950 goto bad_res;
1951 }
1952 changed |= WIPHY_PARAM_RETRY_LONG;
1953 }
1954
1955 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1956 frag_threshold = nla_get_u32(
1957 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1958 if (frag_threshold < 256) {
1959 result = -EINVAL;
1960 goto bad_res;
1961 }
1962 if (frag_threshold != (u32) -1) {
1963 /*
1964 * Fragments (apart from the last one) are required to
1965 * have even length. Make the fragmentation code
1966 * simpler by stripping LSB should someone try to use
1967 * odd threshold value.
1968 */
1969 frag_threshold &= ~0x1;
1970 }
1971 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1972 }
1973
1974 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1975 rts_threshold = nla_get_u32(
1976 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1977 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1978 }
1979
Lukáš Turek81077e82009-12-21 22:50:47 +01001980 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1981 coverage_class = nla_get_u8(
1982 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1983 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1984 }
1985
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001986 if (changed) {
1987 u8 old_retry_short, old_retry_long;
1988 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001989 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001990
1991 if (!rdev->ops->set_wiphy_params) {
1992 result = -EOPNOTSUPP;
1993 goto bad_res;
1994 }
1995
1996 old_retry_short = rdev->wiphy.retry_short;
1997 old_retry_long = rdev->wiphy.retry_long;
1998 old_frag_threshold = rdev->wiphy.frag_threshold;
1999 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002000 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002001
2002 if (changed & WIPHY_PARAM_RETRY_SHORT)
2003 rdev->wiphy.retry_short = retry_short;
2004 if (changed & WIPHY_PARAM_RETRY_LONG)
2005 rdev->wiphy.retry_long = retry_long;
2006 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2007 rdev->wiphy.frag_threshold = frag_threshold;
2008 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2009 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002010 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2011 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002012
Hila Gonene35e4d22012-06-27 17:19:42 +03002013 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002014 if (result) {
2015 rdev->wiphy.retry_short = old_retry_short;
2016 rdev->wiphy.retry_long = old_retry_long;
2017 rdev->wiphy.frag_threshold = old_frag_threshold;
2018 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002019 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002020 }
2021 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002022
Johannes Berg306d6112008-12-08 12:39:04 +01002023 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002024 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02002025 if (netdev)
2026 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002027 return result;
2028}
2029
Johannes Berg71bbc992012-06-15 15:30:18 +02002030static inline u64 wdev_id(struct wireless_dev *wdev)
2031{
2032 return (u64)wdev->identifier |
2033 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2034}
Johannes Berg55682962007-09-20 13:09:35 -04002035
Johannes Berg683b6d32012-11-08 21:25:48 +01002036static int nl80211_send_chandef(struct sk_buff *msg,
2037 struct cfg80211_chan_def *chandef)
2038{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002039 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002040
Johannes Berg683b6d32012-11-08 21:25:48 +01002041 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2042 chandef->chan->center_freq))
2043 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002044 switch (chandef->width) {
2045 case NL80211_CHAN_WIDTH_20_NOHT:
2046 case NL80211_CHAN_WIDTH_20:
2047 case NL80211_CHAN_WIDTH_40:
2048 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2049 cfg80211_get_chandef_type(chandef)))
2050 return -ENOBUFS;
2051 break;
2052 default:
2053 break;
2054 }
2055 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2056 return -ENOBUFS;
2057 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2058 return -ENOBUFS;
2059 if (chandef->center_freq2 &&
2060 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002061 return -ENOBUFS;
2062 return 0;
2063}
2064
Eric W. Biederman15e47302012-09-07 20:12:54 +00002065static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002066 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002067 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002068{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002069 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002070 void *hdr;
2071
Eric W. Biederman15e47302012-09-07 20:12:54 +00002072 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002073 if (!hdr)
2074 return -1;
2075
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002076 if (dev &&
2077 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002078 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002079 goto nla_put_failure;
2080
2081 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2082 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002083 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002084 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002085 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2086 rdev->devlist_generation ^
2087 (cfg80211_rdev_list_generation << 2)))
2088 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002089
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002090 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002091 int ret;
2092 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002093
Johannes Berg683b6d32012-11-08 21:25:48 +01002094 ret = rdev_get_channel(rdev, wdev, &chandef);
2095 if (ret == 0) {
2096 if (nl80211_send_chandef(msg, &chandef))
2097 goto nla_put_failure;
2098 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002099 }
2100
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002101 if (wdev->ssid_len) {
2102 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2103 goto nla_put_failure;
2104 }
2105
Johannes Berg55682962007-09-20 13:09:35 -04002106 return genlmsg_end(msg, hdr);
2107
2108 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002109 genlmsg_cancel(msg, hdr);
2110 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002111}
2112
2113static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2114{
2115 int wp_idx = 0;
2116 int if_idx = 0;
2117 int wp_start = cb->args[0];
2118 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002119 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002120 struct wireless_dev *wdev;
2121
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002122 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02002123 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2124 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002125 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002126 if (wp_idx < wp_start) {
2127 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002128 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002129 }
Johannes Berg55682962007-09-20 13:09:35 -04002130 if_idx = 0;
2131
Johannes Bergf5ea9122009-08-07 16:17:38 +02002132 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02002133 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002134 if (if_idx < if_start) {
2135 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002136 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002137 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002138 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002139 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002140 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002141 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002142 goto out;
2143 }
2144 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002145 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002146 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002147
2148 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002149 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002150 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002151 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002152
2153 cb->args[0] = wp_idx;
2154 cb->args[1] = if_idx;
2155
2156 return skb->len;
2157}
2158
2159static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2160{
2161 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002162 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002163 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002164
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002165 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002166 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002167 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002168
Eric W. Biederman15e47302012-09-07 20:12:54 +00002169 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002170 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002171 nlmsg_free(msg);
2172 return -ENOBUFS;
2173 }
Johannes Berg55682962007-09-20 13:09:35 -04002174
Johannes Berg134e6372009-07-10 09:51:34 +00002175 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002176}
2177
Michael Wu66f7ac52008-01-31 19:48:22 +01002178static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2179 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2180 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2181 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2182 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2183 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2184};
2185
2186static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2187{
2188 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2189 int flag;
2190
2191 *mntrflags = 0;
2192
2193 if (!nla)
2194 return -EINVAL;
2195
2196 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2197 nla, mntr_flags_policy))
2198 return -EINVAL;
2199
2200 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2201 if (flags[flag])
2202 *mntrflags |= (1<<flag);
2203
2204 return 0;
2205}
2206
Johannes Berg9bc383d2009-11-19 11:55:19 +01002207static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002208 struct net_device *netdev, u8 use_4addr,
2209 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002210{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002211 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002212 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002213 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002214 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002215 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002216
2217 switch (iftype) {
2218 case NL80211_IFTYPE_AP_VLAN:
2219 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2220 return 0;
2221 break;
2222 case NL80211_IFTYPE_STATION:
2223 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2224 return 0;
2225 break;
2226 default:
2227 break;
2228 }
2229
2230 return -EOPNOTSUPP;
2231}
2232
Johannes Berg55682962007-09-20 13:09:35 -04002233static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2234{
Johannes Berg4c476992010-10-04 21:36:35 +02002235 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002236 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002237 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002238 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002239 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002240 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002241 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002242
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002243 memset(&params, 0, sizeof(params));
2244
Johannes Berg04a773a2009-04-19 21:24:32 +02002245 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002246
Johannes Berg723b0382008-09-16 20:22:09 +02002247 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002248 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002249 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002250 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002251 if (ntype > NL80211_IFTYPE_MAX)
2252 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002253 }
2254
Johannes Berg92ffe052008-09-16 20:39:36 +02002255 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002256 struct wireless_dev *wdev = dev->ieee80211_ptr;
2257
Johannes Berg4c476992010-10-04 21:36:35 +02002258 if (ntype != NL80211_IFTYPE_MESH_POINT)
2259 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002260 if (netif_running(dev))
2261 return -EBUSY;
2262
2263 wdev_lock(wdev);
2264 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2265 IEEE80211_MAX_MESH_ID_LEN);
2266 wdev->mesh_id_up_len =
2267 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2268 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2269 wdev->mesh_id_up_len);
2270 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002271 }
2272
Felix Fietkau8b787642009-11-10 18:53:10 +01002273 if (info->attrs[NL80211_ATTR_4ADDR]) {
2274 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2275 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002276 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002277 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002278 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002279 } else {
2280 params.use_4addr = -1;
2281 }
2282
Johannes Berg92ffe052008-09-16 20:39:36 +02002283 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002284 if (ntype != NL80211_IFTYPE_MONITOR)
2285 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002286 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2287 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002288 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002289 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002290
2291 flags = &_flags;
2292 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002293 }
Johannes Berg3b858752009-03-12 09:55:09 +01002294
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002295 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002296 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002297 else
2298 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002299
Johannes Berg9bc383d2009-11-19 11:55:19 +01002300 if (!err && params.use_4addr != -1)
2301 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2302
Johannes Berg55682962007-09-20 13:09:35 -04002303 return err;
2304}
2305
2306static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2307{
Johannes Berg4c476992010-10-04 21:36:35 +02002308 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002309 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002310 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002311 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002312 int err;
2313 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002314 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002315
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002316 memset(&params, 0, sizeof(params));
2317
Johannes Berg55682962007-09-20 13:09:35 -04002318 if (!info->attrs[NL80211_ATTR_IFNAME])
2319 return -EINVAL;
2320
2321 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2322 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2323 if (type > NL80211_IFTYPE_MAX)
2324 return -EINVAL;
2325 }
2326
Johannes Berg79c97e92009-07-07 03:56:12 +02002327 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002328 !(rdev->wiphy.interface_modes & (1 << type)))
2329 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002330
Arend van Spriel1c18f142013-01-08 10:17:27 +01002331 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2332 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2333 ETH_ALEN);
2334 if (!is_valid_ether_addr(params.macaddr))
2335 return -EADDRNOTAVAIL;
2336 }
2337
Johannes Berg9bc383d2009-11-19 11:55:19 +01002338 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002339 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002340 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002341 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002342 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002343 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002344
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002345 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2346 if (!msg)
2347 return -ENOMEM;
2348
Michael Wu66f7ac52008-01-31 19:48:22 +01002349 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2350 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2351 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002352 wdev = rdev_add_virtual_intf(rdev,
2353 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2354 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002355 if (IS_ERR(wdev)) {
2356 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002357 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002358 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002359
Johannes Berg98104fde2012-06-16 00:19:54 +02002360 switch (type) {
2361 case NL80211_IFTYPE_MESH_POINT:
2362 if (!info->attrs[NL80211_ATTR_MESH_ID])
2363 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002364 wdev_lock(wdev);
2365 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2366 IEEE80211_MAX_MESH_ID_LEN);
2367 wdev->mesh_id_up_len =
2368 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2369 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2370 wdev->mesh_id_up_len);
2371 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002372 break;
2373 case NL80211_IFTYPE_P2P_DEVICE:
2374 /*
2375 * P2P Device doesn't have a netdev, so doesn't go
2376 * through the netdev notifier and must be added here
2377 */
2378 mutex_init(&wdev->mtx);
2379 INIT_LIST_HEAD(&wdev->event_list);
2380 spin_lock_init(&wdev->event_lock);
2381 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2382 spin_lock_init(&wdev->mgmt_registrations_lock);
2383
2384 mutex_lock(&rdev->devlist_mtx);
2385 wdev->identifier = ++rdev->wdev_id;
2386 list_add_rcu(&wdev->list, &rdev->wdev_list);
2387 rdev->devlist_generation++;
2388 mutex_unlock(&rdev->devlist_mtx);
2389 break;
2390 default:
2391 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002392 }
2393
Eric W. Biederman15e47302012-09-07 20:12:54 +00002394 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002395 rdev, wdev) < 0) {
2396 nlmsg_free(msg);
2397 return -ENOBUFS;
2398 }
2399
2400 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002401}
2402
2403static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2404{
Johannes Berg4c476992010-10-04 21:36:35 +02002405 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002406 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002407
Johannes Berg4c476992010-10-04 21:36:35 +02002408 if (!rdev->ops->del_virtual_intf)
2409 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002410
Johannes Berg84efbb82012-06-16 00:00:26 +02002411 /*
2412 * If we remove a wireless device without a netdev then clear
2413 * user_ptr[1] so that nl80211_post_doit won't dereference it
2414 * to check if it needs to do dev_put(). Otherwise it crashes
2415 * since the wdev has been freed, unlike with a netdev where
2416 * we need the dev_put() for the netdev to really be freed.
2417 */
2418 if (!wdev->netdev)
2419 info->user_ptr[1] = NULL;
2420
Hila Gonene35e4d22012-06-27 17:19:42 +03002421 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002422}
2423
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002424static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2425{
2426 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2427 struct net_device *dev = info->user_ptr[1];
2428 u16 noack_map;
2429
2430 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2431 return -EINVAL;
2432
2433 if (!rdev->ops->set_noack_map)
2434 return -EOPNOTSUPP;
2435
2436 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2437
Hila Gonene35e4d22012-06-27 17:19:42 +03002438 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002439}
2440
Johannes Berg41ade002007-12-19 02:03:29 +01002441struct get_key_cookie {
2442 struct sk_buff *msg;
2443 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002444 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002445};
2446
2447static void get_key_callback(void *c, struct key_params *params)
2448{
Johannes Bergb9454e82009-07-08 13:29:08 +02002449 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002450 struct get_key_cookie *cookie = c;
2451
David S. Miller9360ffd2012-03-29 04:41:26 -04002452 if ((params->key &&
2453 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2454 params->key_len, params->key)) ||
2455 (params->seq &&
2456 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2457 params->seq_len, params->seq)) ||
2458 (params->cipher &&
2459 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2460 params->cipher)))
2461 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002462
Johannes Bergb9454e82009-07-08 13:29:08 +02002463 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2464 if (!key)
2465 goto nla_put_failure;
2466
David S. Miller9360ffd2012-03-29 04:41:26 -04002467 if ((params->key &&
2468 nla_put(cookie->msg, NL80211_KEY_DATA,
2469 params->key_len, params->key)) ||
2470 (params->seq &&
2471 nla_put(cookie->msg, NL80211_KEY_SEQ,
2472 params->seq_len, params->seq)) ||
2473 (params->cipher &&
2474 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2475 params->cipher)))
2476 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002477
David S. Miller9360ffd2012-03-29 04:41:26 -04002478 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2479 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002480
2481 nla_nest_end(cookie->msg, key);
2482
Johannes Berg41ade002007-12-19 02:03:29 +01002483 return;
2484 nla_put_failure:
2485 cookie->error = 1;
2486}
2487
2488static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2489{
Johannes Berg4c476992010-10-04 21:36:35 +02002490 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002491 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002492 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002493 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002494 const u8 *mac_addr = NULL;
2495 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002496 struct get_key_cookie cookie = {
2497 .error = 0,
2498 };
2499 void *hdr;
2500 struct sk_buff *msg;
2501
2502 if (info->attrs[NL80211_ATTR_KEY_IDX])
2503 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2504
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002505 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002506 return -EINVAL;
2507
2508 if (info->attrs[NL80211_ATTR_MAC])
2509 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2510
Johannes Berge31b8212010-10-05 19:39:30 +02002511 pairwise = !!mac_addr;
2512 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2513 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2514 if (kt >= NUM_NL80211_KEYTYPES)
2515 return -EINVAL;
2516 if (kt != NL80211_KEYTYPE_GROUP &&
2517 kt != NL80211_KEYTYPE_PAIRWISE)
2518 return -EINVAL;
2519 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2520 }
2521
Johannes Berg4c476992010-10-04 21:36:35 +02002522 if (!rdev->ops->get_key)
2523 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002524
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002525 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002526 if (!msg)
2527 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002528
Eric W. Biederman15e47302012-09-07 20:12:54 +00002529 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002530 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002531 if (IS_ERR(hdr))
2532 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002533
2534 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002535 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002536
David S. Miller9360ffd2012-03-29 04:41:26 -04002537 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2538 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2539 goto nla_put_failure;
2540 if (mac_addr &&
2541 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2542 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002543
Johannes Berge31b8212010-10-05 19:39:30 +02002544 if (pairwise && mac_addr &&
2545 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2546 return -ENOENT;
2547
Hila Gonene35e4d22012-06-27 17:19:42 +03002548 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2549 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002550
2551 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002552 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002553
2554 if (cookie.error)
2555 goto nla_put_failure;
2556
2557 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002558 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002559
2560 nla_put_failure:
2561 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002562 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002563 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002564 return err;
2565}
2566
2567static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2568{
Johannes Berg4c476992010-10-04 21:36:35 +02002569 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002570 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002571 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002572 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002573
Johannes Bergb9454e82009-07-08 13:29:08 +02002574 err = nl80211_parse_key(info, &key);
2575 if (err)
2576 return err;
2577
2578 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002579 return -EINVAL;
2580
Johannes Bergb9454e82009-07-08 13:29:08 +02002581 /* only support setting default key */
2582 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002583 return -EINVAL;
2584
Johannes Bergfffd0932009-07-08 14:22:54 +02002585 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002586
2587 if (key.def) {
2588 if (!rdev->ops->set_default_key) {
2589 err = -EOPNOTSUPP;
2590 goto out;
2591 }
2592
2593 err = nl80211_key_allowed(dev->ieee80211_ptr);
2594 if (err)
2595 goto out;
2596
Hila Gonene35e4d22012-06-27 17:19:42 +03002597 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002598 key.def_uni, key.def_multi);
2599
2600 if (err)
2601 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002602
Johannes Berg3d23e342009-09-29 23:27:28 +02002603#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002604 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002605#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002606 } else {
2607 if (key.def_uni || !key.def_multi) {
2608 err = -EINVAL;
2609 goto out;
2610 }
2611
2612 if (!rdev->ops->set_default_mgmt_key) {
2613 err = -EOPNOTSUPP;
2614 goto out;
2615 }
2616
2617 err = nl80211_key_allowed(dev->ieee80211_ptr);
2618 if (err)
2619 goto out;
2620
Hila Gonene35e4d22012-06-27 17:19:42 +03002621 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002622 if (err)
2623 goto out;
2624
2625#ifdef CONFIG_CFG80211_WEXT
2626 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2627#endif
2628 }
2629
2630 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002631 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002632
Johannes Berg41ade002007-12-19 02:03:29 +01002633 return err;
2634}
2635
2636static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2637{
Johannes Berg4c476992010-10-04 21:36:35 +02002638 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002639 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002640 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002641 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002642 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002643
Johannes Bergb9454e82009-07-08 13:29:08 +02002644 err = nl80211_parse_key(info, &key);
2645 if (err)
2646 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002647
Johannes Bergb9454e82009-07-08 13:29:08 +02002648 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002649 return -EINVAL;
2650
Johannes Berg41ade002007-12-19 02:03:29 +01002651 if (info->attrs[NL80211_ATTR_MAC])
2652 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2653
Johannes Berge31b8212010-10-05 19:39:30 +02002654 if (key.type == -1) {
2655 if (mac_addr)
2656 key.type = NL80211_KEYTYPE_PAIRWISE;
2657 else
2658 key.type = NL80211_KEYTYPE_GROUP;
2659 }
2660
2661 /* for now */
2662 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2663 key.type != NL80211_KEYTYPE_GROUP)
2664 return -EINVAL;
2665
Johannes Berg4c476992010-10-04 21:36:35 +02002666 if (!rdev->ops->add_key)
2667 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002668
Johannes Berge31b8212010-10-05 19:39:30 +02002669 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2670 key.type == NL80211_KEYTYPE_PAIRWISE,
2671 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002672 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002673
2674 wdev_lock(dev->ieee80211_ptr);
2675 err = nl80211_key_allowed(dev->ieee80211_ptr);
2676 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002677 err = rdev_add_key(rdev, dev, key.idx,
2678 key.type == NL80211_KEYTYPE_PAIRWISE,
2679 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002680 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002681
Johannes Berg41ade002007-12-19 02:03:29 +01002682 return err;
2683}
2684
2685static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2686{
Johannes Berg4c476992010-10-04 21:36:35 +02002687 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002688 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002689 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002690 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002691 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002692
Johannes Bergb9454e82009-07-08 13:29:08 +02002693 err = nl80211_parse_key(info, &key);
2694 if (err)
2695 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002696
2697 if (info->attrs[NL80211_ATTR_MAC])
2698 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2699
Johannes Berge31b8212010-10-05 19:39:30 +02002700 if (key.type == -1) {
2701 if (mac_addr)
2702 key.type = NL80211_KEYTYPE_PAIRWISE;
2703 else
2704 key.type = NL80211_KEYTYPE_GROUP;
2705 }
2706
2707 /* for now */
2708 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2709 key.type != NL80211_KEYTYPE_GROUP)
2710 return -EINVAL;
2711
Johannes Berg4c476992010-10-04 21:36:35 +02002712 if (!rdev->ops->del_key)
2713 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002714
Johannes Bergfffd0932009-07-08 14:22:54 +02002715 wdev_lock(dev->ieee80211_ptr);
2716 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002717
2718 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2719 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2720 err = -ENOENT;
2721
Johannes Bergfffd0932009-07-08 14:22:54 +02002722 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002723 err = rdev_del_key(rdev, dev, key.idx,
2724 key.type == NL80211_KEYTYPE_PAIRWISE,
2725 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002726
Johannes Berg3d23e342009-09-29 23:27:28 +02002727#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002728 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002729 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002730 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002731 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002732 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2733 }
2734#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002735 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002736
Johannes Berg41ade002007-12-19 02:03:29 +01002737 return err;
2738}
2739
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302740/* This function returns an error or the number of nested attributes */
2741static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2742{
2743 struct nlattr *attr;
2744 int n_entries = 0, tmp;
2745
2746 nla_for_each_nested(attr, nl_attr, tmp) {
2747 if (nla_len(attr) != ETH_ALEN)
2748 return -EINVAL;
2749
2750 n_entries++;
2751 }
2752
2753 return n_entries;
2754}
2755
2756/*
2757 * This function parses ACL information and allocates memory for ACL data.
2758 * On successful return, the calling function is responsible to free the
2759 * ACL buffer returned by this function.
2760 */
2761static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2762 struct genl_info *info)
2763{
2764 enum nl80211_acl_policy acl_policy;
2765 struct nlattr *attr;
2766 struct cfg80211_acl_data *acl;
2767 int i = 0, n_entries, tmp;
2768
2769 if (!wiphy->max_acl_mac_addrs)
2770 return ERR_PTR(-EOPNOTSUPP);
2771
2772 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2773 return ERR_PTR(-EINVAL);
2774
2775 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2776 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2777 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2778 return ERR_PTR(-EINVAL);
2779
2780 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2781 return ERR_PTR(-EINVAL);
2782
2783 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2784 if (n_entries < 0)
2785 return ERR_PTR(n_entries);
2786
2787 if (n_entries > wiphy->max_acl_mac_addrs)
2788 return ERR_PTR(-ENOTSUPP);
2789
2790 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2791 GFP_KERNEL);
2792 if (!acl)
2793 return ERR_PTR(-ENOMEM);
2794
2795 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2796 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2797 i++;
2798 }
2799
2800 acl->n_acl_entries = n_entries;
2801 acl->acl_policy = acl_policy;
2802
2803 return acl;
2804}
2805
2806static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2807{
2808 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2809 struct net_device *dev = info->user_ptr[1];
2810 struct cfg80211_acl_data *acl;
2811 int err;
2812
2813 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2814 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2815 return -EOPNOTSUPP;
2816
2817 if (!dev->ieee80211_ptr->beacon_interval)
2818 return -EINVAL;
2819
2820 acl = parse_acl_data(&rdev->wiphy, info);
2821 if (IS_ERR(acl))
2822 return PTR_ERR(acl);
2823
2824 err = rdev_set_mac_acl(rdev, dev, acl);
2825
2826 kfree(acl);
2827
2828 return err;
2829}
2830
Johannes Berg88600202012-02-13 15:17:18 +01002831static int nl80211_parse_beacon(struct genl_info *info,
2832 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002833{
Johannes Berg88600202012-02-13 15:17:18 +01002834 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002835
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002836 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2837 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2838 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2839 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002840 return -EINVAL;
2841
Johannes Berg88600202012-02-13 15:17:18 +01002842 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002843
Johannes Berged1b6cc2007-12-19 02:03:32 +01002844 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002845 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2846 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2847 if (!bcn->head_len)
2848 return -EINVAL;
2849 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002850 }
2851
2852 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002853 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2854 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002855 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002856 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002857 }
2858
Johannes Berg4c476992010-10-04 21:36:35 +02002859 if (!haveinfo)
2860 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002861
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002862 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002863 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2864 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002865 }
2866
2867 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002868 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002869 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002870 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002871 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2872 }
2873
2874 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002875 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002876 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002877 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002878 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2879 }
2880
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002881 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002882 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002883 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002884 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002885 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2886 }
2887
Johannes Berg88600202012-02-13 15:17:18 +01002888 return 0;
2889}
2890
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002891static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2892 struct cfg80211_ap_settings *params)
2893{
2894 struct wireless_dev *wdev;
2895 bool ret = false;
2896
2897 mutex_lock(&rdev->devlist_mtx);
2898
Johannes Berg89a54e42012-06-15 14:33:17 +02002899 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002900 if (wdev->iftype != NL80211_IFTYPE_AP &&
2901 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2902 continue;
2903
Johannes Berg683b6d32012-11-08 21:25:48 +01002904 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002905 continue;
2906
Johannes Berg683b6d32012-11-08 21:25:48 +01002907 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002908 ret = true;
2909 break;
2910 }
2911
2912 mutex_unlock(&rdev->devlist_mtx);
2913
2914 return ret;
2915}
2916
Jouni Malinene39e5b52012-09-30 19:29:39 +03002917static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2918 enum nl80211_auth_type auth_type,
2919 enum nl80211_commands cmd)
2920{
2921 if (auth_type > NL80211_AUTHTYPE_MAX)
2922 return false;
2923
2924 switch (cmd) {
2925 case NL80211_CMD_AUTHENTICATE:
2926 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2927 auth_type == NL80211_AUTHTYPE_SAE)
2928 return false;
2929 return true;
2930 case NL80211_CMD_CONNECT:
2931 case NL80211_CMD_START_AP:
2932 /* SAE not supported yet */
2933 if (auth_type == NL80211_AUTHTYPE_SAE)
2934 return false;
2935 return true;
2936 default:
2937 return false;
2938 }
2939}
2940
Johannes Berg88600202012-02-13 15:17:18 +01002941static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2942{
2943 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2944 struct net_device *dev = info->user_ptr[1];
2945 struct wireless_dev *wdev = dev->ieee80211_ptr;
2946 struct cfg80211_ap_settings params;
2947 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002948 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002949
2950 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2951 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2952 return -EOPNOTSUPP;
2953
2954 if (!rdev->ops->start_ap)
2955 return -EOPNOTSUPP;
2956
2957 if (wdev->beacon_interval)
2958 return -EALREADY;
2959
2960 memset(&params, 0, sizeof(params));
2961
2962 /* these are required for START_AP */
2963 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2964 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2965 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2966 return -EINVAL;
2967
2968 err = nl80211_parse_beacon(info, &params.beacon);
2969 if (err)
2970 return err;
2971
2972 params.beacon_interval =
2973 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2974 params.dtim_period =
2975 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2976
2977 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2978 if (err)
2979 return err;
2980
2981 /*
2982 * In theory, some of these attributes should be required here
2983 * but since they were not used when the command was originally
2984 * added, keep them optional for old user space programs to let
2985 * them continue to work with drivers that do not need the
2986 * additional information -- drivers must check!
2987 */
2988 if (info->attrs[NL80211_ATTR_SSID]) {
2989 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2990 params.ssid_len =
2991 nla_len(info->attrs[NL80211_ATTR_SSID]);
2992 if (params.ssid_len == 0 ||
2993 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2994 return -EINVAL;
2995 }
2996
2997 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2998 params.hidden_ssid = nla_get_u32(
2999 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3000 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3001 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3002 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3003 return -EINVAL;
3004 }
3005
3006 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3007
3008 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3009 params.auth_type = nla_get_u32(
3010 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003011 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3012 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003013 return -EINVAL;
3014 } else
3015 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3016
3017 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3018 NL80211_MAX_NR_CIPHER_SUITES);
3019 if (err)
3020 return err;
3021
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303022 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3023 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3024 return -EOPNOTSUPP;
3025 params.inactivity_timeout = nla_get_u16(
3026 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3027 }
3028
Johannes Berg53cabad2012-11-14 15:17:28 +01003029 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3030 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3031 return -EINVAL;
3032 params.p2p_ctwindow =
3033 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3034 if (params.p2p_ctwindow > 127)
3035 return -EINVAL;
3036 if (params.p2p_ctwindow != 0 &&
3037 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3038 return -EINVAL;
3039 }
3040
3041 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3042 u8 tmp;
3043
3044 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3045 return -EINVAL;
3046 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3047 if (tmp > 1)
3048 return -EINVAL;
3049 params.p2p_opp_ps = tmp;
3050 if (params.p2p_opp_ps != 0 &&
3051 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3052 return -EINVAL;
3053 }
3054
Johannes Bergaa430da2012-05-16 23:50:18 +02003055 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003056 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3057 if (err)
3058 return err;
3059 } else if (wdev->preset_chandef.chan) {
3060 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003061 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003062 return -EINVAL;
3063
Johannes Berg683b6d32012-11-08 21:25:48 +01003064 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003065 return -EINVAL;
3066
Simon Wunderlich04f39042013-02-08 18:16:19 +01003067 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3068 if (err < 0)
3069 return err;
3070 if (err) {
3071 radar_detect_width = BIT(params.chandef.width);
3072 params.radar_required = true;
3073 }
3074
Michal Kaziore4e32452012-06-29 12:47:08 +02003075 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003076 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3077 params.chandef.chan,
3078 CHAN_MODE_SHARED,
3079 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003080 mutex_unlock(&rdev->devlist_mtx);
3081
3082 if (err)
3083 return err;
3084
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303085 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3086 params.acl = parse_acl_data(&rdev->wiphy, info);
3087 if (IS_ERR(params.acl))
3088 return PTR_ERR(params.acl);
3089 }
3090
Hila Gonene35e4d22012-06-27 17:19:42 +03003091 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003092 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003093 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003094 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003095 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003096 wdev->ssid_len = params.ssid_len;
3097 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003098 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303099
3100 kfree(params.acl);
3101
Johannes Berg56d18932011-05-09 18:41:15 +02003102 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003103}
3104
Johannes Berg88600202012-02-13 15:17:18 +01003105static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3106{
3107 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3108 struct net_device *dev = info->user_ptr[1];
3109 struct wireless_dev *wdev = dev->ieee80211_ptr;
3110 struct cfg80211_beacon_data params;
3111 int err;
3112
3113 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3114 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3115 return -EOPNOTSUPP;
3116
3117 if (!rdev->ops->change_beacon)
3118 return -EOPNOTSUPP;
3119
3120 if (!wdev->beacon_interval)
3121 return -EINVAL;
3122
3123 err = nl80211_parse_beacon(info, &params);
3124 if (err)
3125 return err;
3126
Hila Gonene35e4d22012-06-27 17:19:42 +03003127 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003128}
3129
3130static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003131{
Johannes Berg4c476992010-10-04 21:36:35 +02003132 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3133 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003134
Michal Kazior60771782012-06-29 12:46:56 +02003135 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003136}
3137
Johannes Berg5727ef12007-12-19 02:03:34 +01003138static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3139 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3140 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3141 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003142 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003143 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003144 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003145};
3146
Johannes Bergeccb8e82009-05-11 21:57:56 +03003147static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003148 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003149 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003150{
3151 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003152 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003153 int flag;
3154
Johannes Bergeccb8e82009-05-11 21:57:56 +03003155 /*
3156 * Try parsing the new attribute first so userspace
3157 * can specify both for older kernels.
3158 */
3159 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3160 if (nla) {
3161 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003162
Johannes Bergeccb8e82009-05-11 21:57:56 +03003163 sta_flags = nla_data(nla);
3164 params->sta_flags_mask = sta_flags->mask;
3165 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003166 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003167 if ((params->sta_flags_mask |
3168 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3169 return -EINVAL;
3170 return 0;
3171 }
3172
3173 /* if present, parse the old attribute */
3174
3175 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003176 if (!nla)
3177 return 0;
3178
3179 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3180 nla, sta_flags_policy))
3181 return -EINVAL;
3182
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003183 /*
3184 * Only allow certain flags for interface types so that
3185 * other attributes are silently ignored. Remember that
3186 * this is backward compatibility code with old userspace
3187 * and shouldn't be hit in other cases anyway.
3188 */
3189 switch (iftype) {
3190 case NL80211_IFTYPE_AP:
3191 case NL80211_IFTYPE_AP_VLAN:
3192 case NL80211_IFTYPE_P2P_GO:
3193 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3194 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3195 BIT(NL80211_STA_FLAG_WME) |
3196 BIT(NL80211_STA_FLAG_MFP);
3197 break;
3198 case NL80211_IFTYPE_P2P_CLIENT:
3199 case NL80211_IFTYPE_STATION:
3200 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3201 BIT(NL80211_STA_FLAG_TDLS_PEER);
3202 break;
3203 case NL80211_IFTYPE_MESH_POINT:
3204 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3205 BIT(NL80211_STA_FLAG_MFP) |
3206 BIT(NL80211_STA_FLAG_AUTHORIZED);
3207 default:
3208 return -EINVAL;
3209 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003210
Johannes Berg3383b5a2012-05-10 20:14:43 +02003211 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3212 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003213 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003214
Johannes Berg3383b5a2012-05-10 20:14:43 +02003215 /* no longer support new API additions in old API */
3216 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3217 return -EINVAL;
3218 }
3219 }
3220
Johannes Berg5727ef12007-12-19 02:03:34 +01003221 return 0;
3222}
3223
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003224static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3225 int attr)
3226{
3227 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003228 u32 bitrate;
3229 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003230
3231 rate = nla_nest_start(msg, attr);
3232 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003233 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003234
3235 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3236 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003237 /* report 16-bit bitrate only if we can */
3238 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003239 if (bitrate > 0 &&
3240 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3241 return false;
3242 if (bitrate_compat > 0 &&
3243 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3244 return false;
3245
3246 if (info->flags & RATE_INFO_FLAGS_MCS) {
3247 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3248 return false;
3249 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3250 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3251 return false;
3252 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3253 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3254 return false;
3255 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3256 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3257 return false;
3258 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3259 return false;
3260 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3261 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3262 return false;
3263 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3264 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3265 return false;
3266 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3267 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3268 return false;
3269 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3270 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3271 return false;
3272 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3273 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3274 return false;
3275 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003276
3277 nla_nest_end(msg, rate);
3278 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003279}
3280
Eric W. Biederman15e47302012-09-07 20:12:54 +00003281static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003282 int flags,
3283 struct cfg80211_registered_device *rdev,
3284 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003285 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003286{
3287 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003288 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003289
Eric W. Biederman15e47302012-09-07 20:12:54 +00003290 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003291 if (!hdr)
3292 return -1;
3293
David S. Miller9360ffd2012-03-29 04:41:26 -04003294 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3295 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3296 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3297 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003298
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003299 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3300 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003301 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003302 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3303 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3304 sinfo->connected_time))
3305 goto nla_put_failure;
3306 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3307 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3308 sinfo->inactive_time))
3309 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003310 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3311 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003312 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003313 (u32)sinfo->rx_bytes))
3314 goto nla_put_failure;
3315 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3316 NL80211_STA_INFO_TX_BYTES64)) &&
3317 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3318 (u32)sinfo->tx_bytes))
3319 goto nla_put_failure;
3320 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3321 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003322 sinfo->rx_bytes))
3323 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003324 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3325 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003326 sinfo->tx_bytes))
3327 goto nla_put_failure;
3328 if ((sinfo->filled & STATION_INFO_LLID) &&
3329 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3330 goto nla_put_failure;
3331 if ((sinfo->filled & STATION_INFO_PLID) &&
3332 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3333 goto nla_put_failure;
3334 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3335 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3336 sinfo->plink_state))
3337 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003338 switch (rdev->wiphy.signal_type) {
3339 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003340 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3341 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3342 sinfo->signal))
3343 goto nla_put_failure;
3344 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3345 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3346 sinfo->signal_avg))
3347 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003348 break;
3349 default:
3350 break;
3351 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003352 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003353 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3354 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003355 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003356 }
3357 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3358 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3359 NL80211_STA_INFO_RX_BITRATE))
3360 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003361 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003362 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3363 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3364 sinfo->rx_packets))
3365 goto nla_put_failure;
3366 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3367 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3368 sinfo->tx_packets))
3369 goto nla_put_failure;
3370 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3371 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3372 sinfo->tx_retries))
3373 goto nla_put_failure;
3374 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3375 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3376 sinfo->tx_failed))
3377 goto nla_put_failure;
3378 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3379 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3380 sinfo->beacon_loss_count))
3381 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003382 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3383 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3384 sinfo->local_pm))
3385 goto nla_put_failure;
3386 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3387 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3388 sinfo->peer_pm))
3389 goto nla_put_failure;
3390 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3391 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3392 sinfo->nonpeer_pm))
3393 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003394 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3395 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3396 if (!bss_param)
3397 goto nla_put_failure;
3398
David S. Miller9360ffd2012-03-29 04:41:26 -04003399 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3400 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3401 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3402 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3403 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3404 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3405 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3406 sinfo->bss_param.dtim_period) ||
3407 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3408 sinfo->bss_param.beacon_interval))
3409 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003410
3411 nla_nest_end(msg, bss_param);
3412 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003413 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3414 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3415 sizeof(struct nl80211_sta_flag_update),
3416 &sinfo->sta_flags))
3417 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003418 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3419 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3420 sinfo->t_offset))
3421 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003422 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003423
David S. Miller9360ffd2012-03-29 04:41:26 -04003424 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3425 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3426 sinfo->assoc_req_ies))
3427 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003428
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003429 return genlmsg_end(msg, hdr);
3430
3431 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003432 genlmsg_cancel(msg, hdr);
3433 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003434}
3435
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003436static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003437 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003438{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003439 struct station_info sinfo;
3440 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003441 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003442 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003443 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003444 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003445
Johannes Berg67748892010-10-04 21:14:06 +02003446 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3447 if (err)
3448 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003449
3450 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003451 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003452 goto out_err;
3453 }
3454
Johannes Bergbba95fe2008-07-29 13:22:51 +02003455 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003456 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003457 err = rdev_dump_station(dev, netdev, sta_idx,
3458 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003459 if (err == -ENOENT)
3460 break;
3461 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003462 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003463
3464 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003465 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003466 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003467 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003468 &sinfo) < 0)
3469 goto out;
3470
3471 sta_idx++;
3472 }
3473
3474
3475 out:
3476 cb->args[1] = sta_idx;
3477 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003478 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003479 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003480
3481 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003482}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003483
Johannes Berg5727ef12007-12-19 02:03:34 +01003484static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3485{
Johannes Berg4c476992010-10-04 21:36:35 +02003486 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3487 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003488 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003489 struct sk_buff *msg;
3490 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003491 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003492
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003493 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003494
3495 if (!info->attrs[NL80211_ATTR_MAC])
3496 return -EINVAL;
3497
3498 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3499
Johannes Berg4c476992010-10-04 21:36:35 +02003500 if (!rdev->ops->get_station)
3501 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003502
Hila Gonene35e4d22012-06-27 17:19:42 +03003503 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003504 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003505 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003506
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003507 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003508 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003509 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003510
Eric W. Biederman15e47302012-09-07 20:12:54 +00003511 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003512 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003513 nlmsg_free(msg);
3514 return -ENOBUFS;
3515 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003516
Johannes Berg4c476992010-10-04 21:36:35 +02003517 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003518}
3519
Johannes Berg77ee7c82013-02-15 00:48:33 +01003520int cfg80211_check_station_change(struct wiphy *wiphy,
3521 struct station_parameters *params,
3522 enum cfg80211_station_type statype)
3523{
3524 if (params->listen_interval != -1)
3525 return -EINVAL;
3526 if (params->aid)
3527 return -EINVAL;
3528
3529 /* When you run into this, adjust the code below for the new flag */
3530 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3531
3532 switch (statype) {
3533 case CFG80211_STA_MESH_PEER_NONSEC:
3534 case CFG80211_STA_MESH_PEER_SECURE:
3535 /*
3536 * No ignoring the TDLS flag here -- the userspace mesh
3537 * code doesn't have the bug of including TDLS in the
3538 * mask everywhere.
3539 */
3540 if (params->sta_flags_mask &
3541 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3542 BIT(NL80211_STA_FLAG_MFP) |
3543 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3544 return -EINVAL;
3545 break;
3546 case CFG80211_STA_TDLS_PEER_SETUP:
3547 case CFG80211_STA_TDLS_PEER_ACTIVE:
3548 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3549 return -EINVAL;
3550 /* ignore since it can't change */
3551 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3552 break;
3553 default:
3554 /* disallow mesh-specific things */
3555 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3556 return -EINVAL;
3557 if (params->local_pm)
3558 return -EINVAL;
3559 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3560 return -EINVAL;
3561 }
3562
3563 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3564 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3565 /* TDLS can't be set, ... */
3566 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3567 return -EINVAL;
3568 /*
3569 * ... but don't bother the driver with it. This works around
3570 * a hostapd/wpa_supplicant issue -- it always includes the
3571 * TLDS_PEER flag in the mask even for AP mode.
3572 */
3573 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3574 }
3575
3576 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3577 /* reject other things that can't change */
3578 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3579 return -EINVAL;
3580 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3581 return -EINVAL;
3582 if (params->supported_rates)
3583 return -EINVAL;
3584 if (params->ext_capab || params->ht_capa || params->vht_capa)
3585 return -EINVAL;
3586 }
3587
3588 if (statype != CFG80211_STA_AP_CLIENT) {
3589 if (params->vlan)
3590 return -EINVAL;
3591 }
3592
3593 switch (statype) {
3594 case CFG80211_STA_AP_MLME_CLIENT:
3595 /* Use this only for authorizing/unauthorizing a station */
3596 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3597 return -EOPNOTSUPP;
3598 break;
3599 case CFG80211_STA_AP_CLIENT:
3600 /* accept only the listed bits */
3601 if (params->sta_flags_mask &
3602 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3603 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3604 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3605 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3606 BIT(NL80211_STA_FLAG_WME) |
3607 BIT(NL80211_STA_FLAG_MFP)))
3608 return -EINVAL;
3609
3610 /* but authenticated/associated only if driver handles it */
3611 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3612 params->sta_flags_mask &
3613 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3614 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3615 return -EINVAL;
3616 break;
3617 case CFG80211_STA_IBSS:
3618 case CFG80211_STA_AP_STA:
3619 /* reject any changes other than AUTHORIZED */
3620 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3621 return -EINVAL;
3622 break;
3623 case CFG80211_STA_TDLS_PEER_SETUP:
3624 /* reject any changes other than AUTHORIZED or WME */
3625 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3626 BIT(NL80211_STA_FLAG_WME)))
3627 return -EINVAL;
3628 /* force (at least) rates when authorizing */
3629 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3630 !params->supported_rates)
3631 return -EINVAL;
3632 break;
3633 case CFG80211_STA_TDLS_PEER_ACTIVE:
3634 /* reject any changes */
3635 return -EINVAL;
3636 case CFG80211_STA_MESH_PEER_NONSEC:
3637 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3638 return -EINVAL;
3639 break;
3640 case CFG80211_STA_MESH_PEER_SECURE:
3641 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3642 return -EINVAL;
3643 break;
3644 }
3645
3646 return 0;
3647}
3648EXPORT_SYMBOL(cfg80211_check_station_change);
3649
Johannes Berg5727ef12007-12-19 02:03:34 +01003650/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003651 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003652 */
Johannes Berg80b99892011-11-18 16:23:01 +01003653static struct net_device *get_vlan(struct genl_info *info,
3654 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003655{
Johannes Berg463d0182009-07-14 00:33:35 +02003656 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003657 struct net_device *v;
3658 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003659
Johannes Berg80b99892011-11-18 16:23:01 +01003660 if (!vlanattr)
3661 return NULL;
3662
3663 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3664 if (!v)
3665 return ERR_PTR(-ENODEV);
3666
3667 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3668 ret = -EINVAL;
3669 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003670 }
Johannes Berg80b99892011-11-18 16:23:01 +01003671
Johannes Berg77ee7c82013-02-15 00:48:33 +01003672 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3673 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3674 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3675 ret = -EINVAL;
3676 goto error;
3677 }
3678
Johannes Berg80b99892011-11-18 16:23:01 +01003679 if (!netif_running(v)) {
3680 ret = -ENETDOWN;
3681 goto error;
3682 }
3683
3684 return v;
3685 error:
3686 dev_put(v);
3687 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003688}
3689
Jouni Malinendf881292013-02-14 21:10:54 +02003690static struct nla_policy
3691nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3692 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3693 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3694};
3695
Johannes Bergff276692013-02-15 00:09:01 +01003696static int nl80211_parse_sta_wme(struct genl_info *info,
3697 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003698{
Jouni Malinendf881292013-02-14 21:10:54 +02003699 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3700 struct nlattr *nla;
3701 int err;
3702
Jouni Malinendf881292013-02-14 21:10:54 +02003703 /* parse WME attributes if present */
3704 if (!info->attrs[NL80211_ATTR_STA_WME])
3705 return 0;
3706
3707 nla = info->attrs[NL80211_ATTR_STA_WME];
3708 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3709 nl80211_sta_wme_policy);
3710 if (err)
3711 return err;
3712
3713 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3714 params->uapsd_queues = nla_get_u8(
3715 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3716 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3717 return -EINVAL;
3718
3719 if (tb[NL80211_STA_WME_MAX_SP])
3720 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3721
3722 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3723 return -EINVAL;
3724
3725 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3726
3727 return 0;
3728}
3729
Johannes Bergff276692013-02-15 00:09:01 +01003730static int nl80211_set_station_tdls(struct genl_info *info,
3731 struct station_parameters *params)
3732{
3733 /* Dummy STA entry gets updated once the peer capabilities are known */
3734 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3735 params->ht_capa =
3736 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3737 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3738 params->vht_capa =
3739 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3740
3741 return nl80211_parse_sta_wme(info, params);
3742}
3743
Johannes Berg5727ef12007-12-19 02:03:34 +01003744static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3745{
Johannes Berg4c476992010-10-04 21:36:35 +02003746 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003747 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003748 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003749 u8 *mac_addr;
3750 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003751
3752 memset(&params, 0, sizeof(params));
3753
3754 params.listen_interval = -1;
3755
Johannes Berg77ee7c82013-02-15 00:48:33 +01003756 if (!rdev->ops->change_station)
3757 return -EOPNOTSUPP;
3758
Johannes Berg5727ef12007-12-19 02:03:34 +01003759 if (info->attrs[NL80211_ATTR_STA_AID])
3760 return -EINVAL;
3761
3762 if (!info->attrs[NL80211_ATTR_MAC])
3763 return -EINVAL;
3764
3765 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3766
3767 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3768 params.supported_rates =
3769 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3770 params.supported_rates_len =
3771 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3772 }
3773
Jouni Malinen9d62a982013-02-14 21:10:13 +02003774 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3775 params.capability =
3776 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3777 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3778 }
3779
3780 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3781 params.ext_capab =
3782 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3783 params.ext_capab_len =
3784 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3785 }
3786
Jouni Malinendf881292013-02-14 21:10:54 +02003787 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003788 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03003789
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003790 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003791 return -EINVAL;
3792
Johannes Bergf8bacc22013-02-14 23:27:01 +01003793 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003794 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003795 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3796 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3797 return -EINVAL;
3798 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003799
Johannes Bergf8bacc22013-02-14 23:27:01 +01003800 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003801 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003802 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3803 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3804 return -EINVAL;
3805 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3806 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003807
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003808 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3809 enum nl80211_mesh_power_mode pm = nla_get_u32(
3810 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3811
3812 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3813 pm > NL80211_MESH_POWER_MAX)
3814 return -EINVAL;
3815
3816 params.local_pm = pm;
3817 }
3818
Johannes Berg77ee7c82013-02-15 00:48:33 +01003819 /* Include parameters for TDLS peer (will check later) */
3820 err = nl80211_set_station_tdls(info, &params);
3821 if (err)
3822 return err;
3823
3824 params.vlan = get_vlan(info, rdev);
3825 if (IS_ERR(params.vlan))
3826 return PTR_ERR(params.vlan);
3827
Johannes Berga97f4422009-06-18 17:23:43 +02003828 switch (dev->ieee80211_ptr->iftype) {
3829 case NL80211_IFTYPE_AP:
3830 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003831 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003832 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003833 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003834 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003835 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003836 break;
3837 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003838 err = -EOPNOTSUPP;
3839 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003840 }
3841
Johannes Berg77ee7c82013-02-15 00:48:33 +01003842 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003843 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003844
Johannes Berg77ee7c82013-02-15 00:48:33 +01003845 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003846 if (params.vlan)
3847 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003848
Johannes Berg5727ef12007-12-19 02:03:34 +01003849 return err;
3850}
3851
3852static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3853{
Johannes Berg4c476992010-10-04 21:36:35 +02003854 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003855 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003856 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003857 struct station_parameters params;
3858 u8 *mac_addr = NULL;
3859
3860 memset(&params, 0, sizeof(params));
3861
Johannes Berg984c3112013-02-14 23:43:25 +01003862 if (!rdev->ops->add_station)
3863 return -EOPNOTSUPP;
3864
Johannes Berg5727ef12007-12-19 02:03:34 +01003865 if (!info->attrs[NL80211_ATTR_MAC])
3866 return -EINVAL;
3867
Johannes Berg5727ef12007-12-19 02:03:34 +01003868 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3869 return -EINVAL;
3870
3871 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3872 return -EINVAL;
3873
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003874 if (!info->attrs[NL80211_ATTR_STA_AID])
3875 return -EINVAL;
3876
Johannes Berg5727ef12007-12-19 02:03:34 +01003877 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3878 params.supported_rates =
3879 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3880 params.supported_rates_len =
3881 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3882 params.listen_interval =
3883 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003884
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003885 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3886 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3887 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003888
Jouni Malinen9d62a982013-02-14 21:10:13 +02003889 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3890 params.capability =
3891 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3892 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3893 }
3894
3895 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3896 params.ext_capab =
3897 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3898 params.ext_capab_len =
3899 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3900 }
3901
Jouni Malinen36aedc902008-08-25 11:58:58 +03003902 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3903 params.ht_capa =
3904 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003905
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003906 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3907 params.vht_capa =
3908 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3909
Johannes Bergf8bacc22013-02-14 23:27:01 +01003910 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07003911 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003912 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3913 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3914 return -EINVAL;
3915 }
Javier Cardona96b78df2011-04-07 15:08:33 -07003916
Johannes Bergff276692013-02-15 00:09:01 +01003917 err = nl80211_parse_sta_wme(info, &params);
3918 if (err)
3919 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003920
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003921 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003922 return -EINVAL;
3923
Johannes Berg77ee7c82013-02-15 00:48:33 +01003924 /* When you run into this, adjust the code below for the new flag */
3925 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3926
Johannes Bergbdd90d52011-12-14 12:20:27 +01003927 switch (dev->ieee80211_ptr->iftype) {
3928 case NL80211_IFTYPE_AP:
3929 case NL80211_IFTYPE_AP_VLAN:
3930 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01003931 /* ignore WME attributes if iface/sta is not capable */
3932 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
3933 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
3934 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003935
Johannes Bergbdd90d52011-12-14 12:20:27 +01003936 /* TDLS peers cannot be added */
3937 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003938 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003939 /* but don't bother the driver with it */
3940 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003941
Johannes Bergd582cff2012-10-26 17:53:44 +02003942 /* allow authenticated/associated only if driver handles it */
3943 if (!(rdev->wiphy.features &
3944 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3945 params.sta_flags_mask &
3946 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3947 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3948 return -EINVAL;
3949
Johannes Bergbdd90d52011-12-14 12:20:27 +01003950 /* must be last in here for error handling */
3951 params.vlan = get_vlan(info, rdev);
3952 if (IS_ERR(params.vlan))
3953 return PTR_ERR(params.vlan);
3954 break;
3955 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01003956 /* ignore uAPSD data */
3957 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
3958
Johannes Bergd582cff2012-10-26 17:53:44 +02003959 /* associated is disallowed */
3960 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3961 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003962 /* TDLS peers cannot be added */
3963 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003964 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003965 break;
3966 case NL80211_IFTYPE_STATION:
Johannes Berg984c3112013-02-14 23:43:25 +01003967 /* ignore uAPSD data */
3968 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
3969
Johannes Berg77ee7c82013-02-15 00:48:33 +01003970 /* these are disallowed */
3971 if (params.sta_flags_mask &
3972 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
3973 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02003974 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003975 /* Only TDLS peers can be added */
3976 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3977 return -EINVAL;
3978 /* Can only add if TDLS ... */
3979 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3980 return -EOPNOTSUPP;
3981 /* ... with external setup is supported */
3982 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3983 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003984 /*
3985 * Older wpa_supplicant versions always mark the TDLS peer
3986 * as authorized, but it shouldn't yet be.
3987 */
3988 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01003989 break;
3990 default:
3991 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003992 }
3993
Johannes Bergbdd90d52011-12-14 12:20:27 +01003994 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003995
Hila Gonene35e4d22012-06-27 17:19:42 +03003996 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003997
Johannes Berg5727ef12007-12-19 02:03:34 +01003998 if (params.vlan)
3999 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004000 return err;
4001}
4002
4003static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4004{
Johannes Berg4c476992010-10-04 21:36:35 +02004005 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4006 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004007 u8 *mac_addr = NULL;
4008
4009 if (info->attrs[NL80211_ATTR_MAC])
4010 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4011
Johannes Berge80cf852009-05-11 14:43:13 +02004012 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004013 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004014 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004015 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4016 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004017
Johannes Berg4c476992010-10-04 21:36:35 +02004018 if (!rdev->ops->del_station)
4019 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004020
Hila Gonene35e4d22012-06-27 17:19:42 +03004021 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004022}
4023
Eric W. Biederman15e47302012-09-07 20:12:54 +00004024static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004025 int flags, struct net_device *dev,
4026 u8 *dst, u8 *next_hop,
4027 struct mpath_info *pinfo)
4028{
4029 void *hdr;
4030 struct nlattr *pinfoattr;
4031
Eric W. Biederman15e47302012-09-07 20:12:54 +00004032 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004033 if (!hdr)
4034 return -1;
4035
David S. Miller9360ffd2012-03-29 04:41:26 -04004036 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4037 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4038 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4039 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4040 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004041
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004042 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4043 if (!pinfoattr)
4044 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004045 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4046 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4047 pinfo->frame_qlen))
4048 goto nla_put_failure;
4049 if (((pinfo->filled & MPATH_INFO_SN) &&
4050 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4051 ((pinfo->filled & MPATH_INFO_METRIC) &&
4052 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4053 pinfo->metric)) ||
4054 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4055 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4056 pinfo->exptime)) ||
4057 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4058 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4059 pinfo->flags)) ||
4060 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4061 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4062 pinfo->discovery_timeout)) ||
4063 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4064 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4065 pinfo->discovery_retries)))
4066 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004067
4068 nla_nest_end(msg, pinfoattr);
4069
4070 return genlmsg_end(msg, hdr);
4071
4072 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004073 genlmsg_cancel(msg, hdr);
4074 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004075}
4076
4077static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004078 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004079{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004080 struct mpath_info pinfo;
4081 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004082 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004083 u8 dst[ETH_ALEN];
4084 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02004085 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004086 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004087
Johannes Berg67748892010-10-04 21:14:06 +02004088 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4089 if (err)
4090 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004091
4092 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004093 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004094 goto out_err;
4095 }
4096
Jouni Malineneec60b02009-03-20 21:21:19 +02004097 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
4098 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004099 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004100 }
4101
Johannes Bergbba95fe2008-07-29 13:22:51 +02004102 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03004103 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
4104 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004105 if (err == -ENOENT)
4106 break;
4107 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004108 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004109
Eric W. Biederman15e47302012-09-07 20:12:54 +00004110 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004111 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4112 netdev, dst, next_hop,
4113 &pinfo) < 0)
4114 goto out;
4115
4116 path_idx++;
4117 }
4118
4119
4120 out:
4121 cb->args[1] = path_idx;
4122 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004123 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004124 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004125 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004126}
4127
4128static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4129{
Johannes Berg4c476992010-10-04 21:36:35 +02004130 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004131 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004132 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004133 struct mpath_info pinfo;
4134 struct sk_buff *msg;
4135 u8 *dst = NULL;
4136 u8 next_hop[ETH_ALEN];
4137
4138 memset(&pinfo, 0, sizeof(pinfo));
4139
4140 if (!info->attrs[NL80211_ATTR_MAC])
4141 return -EINVAL;
4142
4143 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4144
Johannes Berg4c476992010-10-04 21:36:35 +02004145 if (!rdev->ops->get_mpath)
4146 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004147
Johannes Berg4c476992010-10-04 21:36:35 +02004148 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4149 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004150
Hila Gonene35e4d22012-06-27 17:19:42 +03004151 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004152 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004153 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004154
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004155 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004156 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004157 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004158
Eric W. Biederman15e47302012-09-07 20:12:54 +00004159 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004160 dev, dst, next_hop, &pinfo) < 0) {
4161 nlmsg_free(msg);
4162 return -ENOBUFS;
4163 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004164
Johannes Berg4c476992010-10-04 21:36:35 +02004165 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004166}
4167
4168static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4169{
Johannes Berg4c476992010-10-04 21:36:35 +02004170 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4171 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004172 u8 *dst = NULL;
4173 u8 *next_hop = NULL;
4174
4175 if (!info->attrs[NL80211_ATTR_MAC])
4176 return -EINVAL;
4177
4178 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4179 return -EINVAL;
4180
4181 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4182 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4183
Johannes Berg4c476992010-10-04 21:36:35 +02004184 if (!rdev->ops->change_mpath)
4185 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004186
Johannes Berg4c476992010-10-04 21:36:35 +02004187 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4188 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004189
Hila Gonene35e4d22012-06-27 17:19:42 +03004190 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004191}
Johannes Berg4c476992010-10-04 21:36:35 +02004192
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004193static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4194{
Johannes Berg4c476992010-10-04 21:36:35 +02004195 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4196 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004197 u8 *dst = NULL;
4198 u8 *next_hop = NULL;
4199
4200 if (!info->attrs[NL80211_ATTR_MAC])
4201 return -EINVAL;
4202
4203 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4204 return -EINVAL;
4205
4206 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4207 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4208
Johannes Berg4c476992010-10-04 21:36:35 +02004209 if (!rdev->ops->add_mpath)
4210 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004211
Johannes Berg4c476992010-10-04 21:36:35 +02004212 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4213 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004214
Hila Gonene35e4d22012-06-27 17:19:42 +03004215 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004216}
4217
4218static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4219{
Johannes Berg4c476992010-10-04 21:36:35 +02004220 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4221 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004222 u8 *dst = NULL;
4223
4224 if (info->attrs[NL80211_ATTR_MAC])
4225 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4226
Johannes Berg4c476992010-10-04 21:36:35 +02004227 if (!rdev->ops->del_mpath)
4228 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004229
Hila Gonene35e4d22012-06-27 17:19:42 +03004230 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004231}
4232
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004233static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4234{
Johannes Berg4c476992010-10-04 21:36:35 +02004235 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4236 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004237 struct bss_parameters params;
4238
4239 memset(&params, 0, sizeof(params));
4240 /* default to not changing parameters */
4241 params.use_cts_prot = -1;
4242 params.use_short_preamble = -1;
4243 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004244 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004245 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004246 params.p2p_ctwindow = -1;
4247 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004248
4249 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4250 params.use_cts_prot =
4251 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4252 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4253 params.use_short_preamble =
4254 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4255 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4256 params.use_short_slot_time =
4257 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004258 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4259 params.basic_rates =
4260 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4261 params.basic_rates_len =
4262 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4263 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004264 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4265 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004266 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4267 params.ht_opmode =
4268 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004269
Johannes Berg53cabad2012-11-14 15:17:28 +01004270 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4271 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4272 return -EINVAL;
4273 params.p2p_ctwindow =
4274 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4275 if (params.p2p_ctwindow < 0)
4276 return -EINVAL;
4277 if (params.p2p_ctwindow != 0 &&
4278 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4279 return -EINVAL;
4280 }
4281
4282 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4283 u8 tmp;
4284
4285 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4286 return -EINVAL;
4287 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4288 if (tmp > 1)
4289 return -EINVAL;
4290 params.p2p_opp_ps = tmp;
4291 if (params.p2p_opp_ps &&
4292 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4293 return -EINVAL;
4294 }
4295
Johannes Berg4c476992010-10-04 21:36:35 +02004296 if (!rdev->ops->change_bss)
4297 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004298
Johannes Berg074ac8d2010-09-16 14:58:22 +02004299 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004300 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4301 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004302
Hila Gonene35e4d22012-06-27 17:19:42 +03004303 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004304}
4305
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004306static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004307 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4308 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4309 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4310 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4311 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4312 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4313};
4314
4315static int parse_reg_rule(struct nlattr *tb[],
4316 struct ieee80211_reg_rule *reg_rule)
4317{
4318 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4319 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4320
4321 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4322 return -EINVAL;
4323 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4324 return -EINVAL;
4325 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4326 return -EINVAL;
4327 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4328 return -EINVAL;
4329 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4330 return -EINVAL;
4331
4332 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4333
4334 freq_range->start_freq_khz =
4335 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4336 freq_range->end_freq_khz =
4337 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4338 freq_range->max_bandwidth_khz =
4339 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4340
4341 power_rule->max_eirp =
4342 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4343
4344 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4345 power_rule->max_antenna_gain =
4346 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4347
4348 return 0;
4349}
4350
4351static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4352{
4353 int r;
4354 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004355 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004356
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004357 /*
4358 * You should only get this when cfg80211 hasn't yet initialized
4359 * completely when built-in to the kernel right between the time
4360 * window between nl80211_init() and regulatory_init(), if that is
4361 * even possible.
4362 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004363 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004364 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004365
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004366 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4367 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004368
4369 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4370
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004371 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4372 user_reg_hint_type =
4373 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4374 else
4375 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4376
4377 switch (user_reg_hint_type) {
4378 case NL80211_USER_REG_HINT_USER:
4379 case NL80211_USER_REG_HINT_CELL_BASE:
4380 break;
4381 default:
4382 return -EINVAL;
4383 }
4384
4385 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004386
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004387 return r;
4388}
4389
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004390static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004391 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004392{
Johannes Berg4c476992010-10-04 21:36:35 +02004393 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004394 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004395 struct wireless_dev *wdev = dev->ieee80211_ptr;
4396 struct mesh_config cur_params;
4397 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004398 void *hdr;
4399 struct nlattr *pinfoattr;
4400 struct sk_buff *msg;
4401
Johannes Berg29cbe682010-12-03 09:20:44 +01004402 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4403 return -EOPNOTSUPP;
4404
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004405 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004406 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004407
Johannes Berg29cbe682010-12-03 09:20:44 +01004408 wdev_lock(wdev);
4409 /* If not connected, get default parameters */
4410 if (!wdev->mesh_id_len)
4411 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4412 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004413 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004414 wdev_unlock(wdev);
4415
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004416 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004417 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004418
4419 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004420 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004421 if (!msg)
4422 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004423 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004424 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004425 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004426 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004427 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004428 if (!pinfoattr)
4429 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004430 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4431 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4432 cur_params.dot11MeshRetryTimeout) ||
4433 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4434 cur_params.dot11MeshConfirmTimeout) ||
4435 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4436 cur_params.dot11MeshHoldingTimeout) ||
4437 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4438 cur_params.dot11MeshMaxPeerLinks) ||
4439 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4440 cur_params.dot11MeshMaxRetries) ||
4441 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4442 cur_params.dot11MeshTTL) ||
4443 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4444 cur_params.element_ttl) ||
4445 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4446 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004447 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4448 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004449 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4450 cur_params.dot11MeshHWMPmaxPREQretries) ||
4451 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4452 cur_params.path_refresh_time) ||
4453 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4454 cur_params.min_discovery_timeout) ||
4455 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4456 cur_params.dot11MeshHWMPactivePathTimeout) ||
4457 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4458 cur_params.dot11MeshHWMPpreqMinInterval) ||
4459 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4460 cur_params.dot11MeshHWMPperrMinInterval) ||
4461 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4462 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4463 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4464 cur_params.dot11MeshHWMPRootMode) ||
4465 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4466 cur_params.dot11MeshHWMPRannInterval) ||
4467 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4468 cur_params.dot11MeshGateAnnouncementProtocol) ||
4469 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4470 cur_params.dot11MeshForwarding) ||
4471 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004472 cur_params.rssi_threshold) ||
4473 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004474 cur_params.ht_opmode) ||
4475 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4476 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4477 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004478 cur_params.dot11MeshHWMProotInterval) ||
4479 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004480 cur_params.dot11MeshHWMPconfirmationInterval) ||
4481 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4482 cur_params.power_mode) ||
4483 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4484 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004485 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004486 nla_nest_end(msg, pinfoattr);
4487 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004488 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004489
Johannes Berg3b858752009-03-12 09:55:09 +01004490 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004491 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004492 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004493 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004494 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004495}
4496
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004497static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004498 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4499 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4500 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4501 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4502 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4503 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004504 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004505 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004506 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004507 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4508 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4509 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4510 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4511 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004512 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004513 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004514 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004515 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004516 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004517 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004518 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4519 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004520 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4521 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004522 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004523 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4524 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004525};
4526
Javier Cardonac80d5452010-12-16 17:37:49 -08004527static const struct nla_policy
4528 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004529 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004530 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4531 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004532 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004533 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004534 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004535 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004536};
4537
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004538static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004539 struct mesh_config *cfg,
4540 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004541{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004542 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004543 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004544
Marco Porschea54fba2013-01-07 16:04:48 +01004545#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4546do { \
4547 if (tb[attr]) { \
4548 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4549 return -EINVAL; \
4550 cfg->param = fn(tb[attr]); \
4551 mask |= (1 << (attr - 1)); \
4552 } \
4553} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004554
4555
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004556 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004557 return -EINVAL;
4558 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004559 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004560 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004561 return -EINVAL;
4562
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004563 /* This makes sure that there aren't more than 32 mesh config
4564 * parameters (otherwise our bitfield scheme would not work.) */
4565 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4566
4567 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004568 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004569 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4570 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004571 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004572 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4573 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004574 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004575 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4576 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004577 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004578 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4579 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004580 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004581 mask, NL80211_MESHCONF_MAX_RETRIES,
4582 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004583 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004584 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004585 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004586 mask, NL80211_MESHCONF_ELEMENT_TTL,
4587 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004588 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004589 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4590 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004591 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4592 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004593 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4594 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004595 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004596 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4597 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004598 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004599 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4600 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004601 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004602 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4603 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004604 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4605 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004606 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4607 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004608 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004609 1, 65535, mask,
4610 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004611 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004612 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004613 1, 65535, mask,
4614 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004615 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004616 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004617 dot11MeshHWMPnetDiameterTraversalTime,
4618 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004619 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4620 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004621 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4622 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4623 nla_get_u8);
4624 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4625 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004626 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004627 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004628 dot11MeshGateAnnouncementProtocol, 0, 1,
4629 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004630 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004631 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004632 mask, NL80211_MESHCONF_FORWARDING,
4633 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004634 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004635 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4636 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004637 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004638 mask, NL80211_MESHCONF_HT_OPMODE,
4639 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004640 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004641 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004642 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4643 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004644 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004645 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4646 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004647 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004648 dot11MeshHWMPconfirmationInterval,
4649 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004650 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4651 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004652 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4653 NL80211_MESH_POWER_ACTIVE,
4654 NL80211_MESH_POWER_MAX,
4655 mask, NL80211_MESHCONF_POWER_MODE,
4656 nla_get_u32);
4657 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4658 0, 65535, mask,
4659 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004660 if (mask_out)
4661 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004662
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004663 return 0;
4664
4665#undef FILL_IN_MESH_PARAM_IF_SET
4666}
4667
Javier Cardonac80d5452010-12-16 17:37:49 -08004668static int nl80211_parse_mesh_setup(struct genl_info *info,
4669 struct mesh_setup *setup)
4670{
4671 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4672
4673 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4674 return -EINVAL;
4675 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4676 info->attrs[NL80211_ATTR_MESH_SETUP],
4677 nl80211_mesh_setup_params_policy))
4678 return -EINVAL;
4679
Javier Cardonad299a1f2012-03-31 11:31:33 -07004680 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4681 setup->sync_method =
4682 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4683 IEEE80211_SYNC_METHOD_VENDOR :
4684 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4685
Javier Cardonac80d5452010-12-16 17:37:49 -08004686 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4687 setup->path_sel_proto =
4688 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4689 IEEE80211_PATH_PROTOCOL_VENDOR :
4690 IEEE80211_PATH_PROTOCOL_HWMP;
4691
4692 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4693 setup->path_metric =
4694 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4695 IEEE80211_PATH_METRIC_VENDOR :
4696 IEEE80211_PATH_METRIC_AIRTIME;
4697
Javier Cardona581a8b02011-04-07 15:08:27 -07004698
4699 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004700 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004701 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004702 if (!is_valid_ie_attr(ieattr))
4703 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004704 setup->ie = nla_data(ieattr);
4705 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004706 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004707 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4708 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004709
4710 return 0;
4711}
4712
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004713static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004714 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004715{
4716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4717 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004718 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004719 struct mesh_config cfg;
4720 u32 mask;
4721 int err;
4722
Johannes Berg29cbe682010-12-03 09:20:44 +01004723 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4724 return -EOPNOTSUPP;
4725
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004726 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004727 return -EOPNOTSUPP;
4728
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004729 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004730 if (err)
4731 return err;
4732
Johannes Berg29cbe682010-12-03 09:20:44 +01004733 wdev_lock(wdev);
4734 if (!wdev->mesh_id_len)
4735 err = -ENOLINK;
4736
4737 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004738 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004739
4740 wdev_unlock(wdev);
4741
4742 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004743}
4744
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004745static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4746{
Johannes Berg458f4f92012-12-06 15:47:38 +01004747 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004748 struct sk_buff *msg;
4749 void *hdr = NULL;
4750 struct nlattr *nl_reg_rules;
4751 unsigned int i;
4752 int err = -EINVAL;
4753
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004754 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004755
4756 if (!cfg80211_regdomain)
4757 goto out;
4758
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004759 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004760 if (!msg) {
4761 err = -ENOBUFS;
4762 goto out;
4763 }
4764
Eric W. Biederman15e47302012-09-07 20:12:54 +00004765 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004766 NL80211_CMD_GET_REG);
4767 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004768 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004769
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004770 if (reg_last_request_cell_base() &&
4771 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4772 NL80211_USER_REG_HINT_CELL_BASE))
4773 goto nla_put_failure;
4774
Johannes Berg458f4f92012-12-06 15:47:38 +01004775 rcu_read_lock();
4776 regdom = rcu_dereference(cfg80211_regdomain);
4777
4778 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4779 (regdom->dfs_region &&
4780 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4781 goto nla_put_failure_rcu;
4782
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004783 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4784 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004785 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004786
Johannes Berg458f4f92012-12-06 15:47:38 +01004787 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004788 struct nlattr *nl_reg_rule;
4789 const struct ieee80211_reg_rule *reg_rule;
4790 const struct ieee80211_freq_range *freq_range;
4791 const struct ieee80211_power_rule *power_rule;
4792
Johannes Berg458f4f92012-12-06 15:47:38 +01004793 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004794 freq_range = &reg_rule->freq_range;
4795 power_rule = &reg_rule->power_rule;
4796
4797 nl_reg_rule = nla_nest_start(msg, i);
4798 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004799 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004800
David S. Miller9360ffd2012-03-29 04:41:26 -04004801 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4802 reg_rule->flags) ||
4803 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4804 freq_range->start_freq_khz) ||
4805 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4806 freq_range->end_freq_khz) ||
4807 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4808 freq_range->max_bandwidth_khz) ||
4809 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4810 power_rule->max_antenna_gain) ||
4811 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4812 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004813 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004814
4815 nla_nest_end(msg, nl_reg_rule);
4816 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004817 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004818
4819 nla_nest_end(msg, nl_reg_rules);
4820
4821 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004822 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004823 goto out;
4824
Johannes Berg458f4f92012-12-06 15:47:38 +01004825nla_put_failure_rcu:
4826 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004827nla_put_failure:
4828 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004829put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004830 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004831 err = -EMSGSIZE;
4832out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004833 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004834 return err;
4835}
4836
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004837static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4838{
4839 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4840 struct nlattr *nl_reg_rule;
4841 char *alpha2 = NULL;
4842 int rem_reg_rules = 0, r = 0;
4843 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004844 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004845 struct ieee80211_regdomain *rd = NULL;
4846
4847 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4848 return -EINVAL;
4849
4850 if (!info->attrs[NL80211_ATTR_REG_RULES])
4851 return -EINVAL;
4852
4853 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4854
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004855 if (info->attrs[NL80211_ATTR_DFS_REGION])
4856 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4857
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004858 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004859 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004860 num_rules++;
4861 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004862 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004863 }
4864
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004865 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004866 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004867
4868 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004869 if (!rd)
4870 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004871
4872 rd->n_reg_rules = num_rules;
4873 rd->alpha2[0] = alpha2[0];
4874 rd->alpha2[1] = alpha2[1];
4875
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004876 /*
4877 * Disable DFS master mode if the DFS region was
4878 * not supported or known on this kernel.
4879 */
4880 if (reg_supported_dfs_region(dfs_region))
4881 rd->dfs_region = dfs_region;
4882
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004883 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004884 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004885 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004886 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4887 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004888 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4889 if (r)
4890 goto bad_reg;
4891
4892 rule_idx++;
4893
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004894 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4895 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004896 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004897 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004898 }
4899
Johannes Berg6913b492012-12-04 00:48:59 +01004900 mutex_lock(&cfg80211_mutex);
4901
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004902 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004903 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004904 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004905 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004906
Johannes Bergd2372b32008-10-24 20:32:20 +02004907 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004908 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004909 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004910}
4911
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004912static int validate_scan_freqs(struct nlattr *freqs)
4913{
4914 struct nlattr *attr1, *attr2;
4915 int n_channels = 0, tmp1, tmp2;
4916
4917 nla_for_each_nested(attr1, freqs, tmp1) {
4918 n_channels++;
4919 /*
4920 * Some hardware has a limited channel list for
4921 * scanning, and it is pretty much nonsensical
4922 * to scan for a channel twice, so disallow that
4923 * and don't require drivers to check that the
4924 * channel list they get isn't longer than what
4925 * they can scan, as long as they can scan all
4926 * the channels they registered at once.
4927 */
4928 nla_for_each_nested(attr2, freqs, tmp2)
4929 if (attr1 != attr2 &&
4930 nla_get_u32(attr1) == nla_get_u32(attr2))
4931 return 0;
4932 }
4933
4934 return n_channels;
4935}
4936
Johannes Berg2a519312009-02-10 21:25:55 +01004937static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4938{
Johannes Berg4c476992010-10-04 21:36:35 +02004939 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004940 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004941 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004942 struct nlattr *attr;
4943 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004944 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004945 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004946
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004947 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4948 return -EINVAL;
4949
Johannes Berg79c97e92009-07-07 03:56:12 +02004950 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004951
Johannes Berg4c476992010-10-04 21:36:35 +02004952 if (!rdev->ops->scan)
4953 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004954
Johannes Berg4c476992010-10-04 21:36:35 +02004955 if (rdev->scan_req)
4956 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004957
4958 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004959 n_channels = validate_scan_freqs(
4960 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004961 if (!n_channels)
4962 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004963 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004964 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004965 n_channels = 0;
4966
Johannes Berg2a519312009-02-10 21:25:55 +01004967 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4968 if (wiphy->bands[band])
4969 n_channels += wiphy->bands[band]->n_channels;
4970 }
4971
4972 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4973 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4974 n_ssids++;
4975
Johannes Berg4c476992010-10-04 21:36:35 +02004976 if (n_ssids > wiphy->max_scan_ssids)
4977 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004978
Jouni Malinen70692ad2009-02-16 19:39:13 +02004979 if (info->attrs[NL80211_ATTR_IE])
4980 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4981 else
4982 ie_len = 0;
4983
Johannes Berg4c476992010-10-04 21:36:35 +02004984 if (ie_len > wiphy->max_scan_ie_len)
4985 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004986
Johannes Berg2a519312009-02-10 21:25:55 +01004987 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004988 + sizeof(*request->ssids) * n_ssids
4989 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004990 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004991 if (!request)
4992 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004993
Johannes Berg2a519312009-02-10 21:25:55 +01004994 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004995 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004996 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004997 if (ie_len) {
4998 if (request->ssids)
4999 request->ie = (void *)(request->ssids + n_ssids);
5000 else
5001 request->ie = (void *)(request->channels + n_channels);
5002 }
Johannes Berg2a519312009-02-10 21:25:55 +01005003
Johannes Berg584991d2009-11-02 13:32:03 +01005004 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005005 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5006 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005007 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005008 struct ieee80211_channel *chan;
5009
5010 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5011
5012 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005013 err = -EINVAL;
5014 goto out_free;
5015 }
Johannes Berg584991d2009-11-02 13:32:03 +01005016
5017 /* ignore disabled channels */
5018 if (chan->flags & IEEE80211_CHAN_DISABLED)
5019 continue;
5020
5021 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005022 i++;
5023 }
5024 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005025 enum ieee80211_band band;
5026
Johannes Berg2a519312009-02-10 21:25:55 +01005027 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005028 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5029 int j;
5030 if (!wiphy->bands[band])
5031 continue;
5032 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005033 struct ieee80211_channel *chan;
5034
5035 chan = &wiphy->bands[band]->channels[j];
5036
5037 if (chan->flags & IEEE80211_CHAN_DISABLED)
5038 continue;
5039
5040 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005041 i++;
5042 }
5043 }
5044 }
5045
Johannes Berg584991d2009-11-02 13:32:03 +01005046 if (!i) {
5047 err = -EINVAL;
5048 goto out_free;
5049 }
5050
5051 request->n_channels = i;
5052
Johannes Berg2a519312009-02-10 21:25:55 +01005053 i = 0;
5054 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5055 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005056 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005057 err = -EINVAL;
5058 goto out_free;
5059 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005060 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005061 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005062 i++;
5063 }
5064 }
5065
Jouni Malinen70692ad2009-02-16 19:39:13 +02005066 if (info->attrs[NL80211_ATTR_IE]) {
5067 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005068 memcpy((void *)request->ie,
5069 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005070 request->ie_len);
5071 }
5072
Johannes Berg34850ab2011-07-18 18:08:35 +02005073 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005074 if (wiphy->bands[i])
5075 request->rates[i] =
5076 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005077
5078 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5079 nla_for_each_nested(attr,
5080 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5081 tmp) {
5082 enum ieee80211_band band = nla_type(attr);
5083
Dan Carpenter84404622011-07-29 11:52:18 +03005084 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005085 err = -EINVAL;
5086 goto out_free;
5087 }
5088 err = ieee80211_get_ratemask(wiphy->bands[band],
5089 nla_data(attr),
5090 nla_len(attr),
5091 &request->rates[band]);
5092 if (err)
5093 goto out_free;
5094 }
5095 }
5096
Sam Leffler46856bb2012-10-11 21:03:32 -07005097 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005098 request->flags = nla_get_u32(
5099 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005100 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5101 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5102 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5103 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005104 err = -EOPNOTSUPP;
5105 goto out_free;
5106 }
5107 }
Sam Lefflered4737712012-10-11 21:03:31 -07005108
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305109 request->no_cck =
5110 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5111
Johannes Bergfd014282012-06-18 19:17:03 +02005112 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005113 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005114 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005115
Johannes Berg79c97e92009-07-07 03:56:12 +02005116 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005117 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005118
Johannes Berg463d0182009-07-14 00:33:35 +02005119 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005120 nl80211_send_scan_start(rdev, wdev);
5121 if (wdev->netdev)
5122 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005123 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005124 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005125 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005126 kfree(request);
5127 }
Johannes Berg3b858752009-03-12 09:55:09 +01005128
Johannes Berg2a519312009-02-10 21:25:55 +01005129 return err;
5130}
5131
Luciano Coelho807f8a82011-05-11 17:09:35 +03005132static int nl80211_start_sched_scan(struct sk_buff *skb,
5133 struct genl_info *info)
5134{
5135 struct cfg80211_sched_scan_request *request;
5136 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5137 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005138 struct nlattr *attr;
5139 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005140 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005141 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005142 enum ieee80211_band band;
5143 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005144 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005145
5146 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5147 !rdev->ops->sched_scan_start)
5148 return -EOPNOTSUPP;
5149
5150 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5151 return -EINVAL;
5152
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005153 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5154 return -EINVAL;
5155
5156 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5157 if (interval == 0)
5158 return -EINVAL;
5159
Luciano Coelho807f8a82011-05-11 17:09:35 +03005160 wiphy = &rdev->wiphy;
5161
5162 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5163 n_channels = validate_scan_freqs(
5164 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5165 if (!n_channels)
5166 return -EINVAL;
5167 } else {
5168 n_channels = 0;
5169
5170 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5171 if (wiphy->bands[band])
5172 n_channels += wiphy->bands[band]->n_channels;
5173 }
5174
5175 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5176 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5177 tmp)
5178 n_ssids++;
5179
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005180 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005181 return -EINVAL;
5182
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005183 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5184 nla_for_each_nested(attr,
5185 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5186 tmp)
5187 n_match_sets++;
5188
5189 if (n_match_sets > wiphy->max_match_sets)
5190 return -EINVAL;
5191
Luciano Coelho807f8a82011-05-11 17:09:35 +03005192 if (info->attrs[NL80211_ATTR_IE])
5193 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5194 else
5195 ie_len = 0;
5196
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005197 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005198 return -EINVAL;
5199
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005200 mutex_lock(&rdev->sched_scan_mtx);
5201
5202 if (rdev->sched_scan_req) {
5203 err = -EINPROGRESS;
5204 goto out;
5205 }
5206
Luciano Coelho807f8a82011-05-11 17:09:35 +03005207 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005208 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005209 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005210 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005211 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005212 if (!request) {
5213 err = -ENOMEM;
5214 goto out;
5215 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005216
5217 if (n_ssids)
5218 request->ssids = (void *)&request->channels[n_channels];
5219 request->n_ssids = n_ssids;
5220 if (ie_len) {
5221 if (request->ssids)
5222 request->ie = (void *)(request->ssids + n_ssids);
5223 else
5224 request->ie = (void *)(request->channels + n_channels);
5225 }
5226
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005227 if (n_match_sets) {
5228 if (request->ie)
5229 request->match_sets = (void *)(request->ie + ie_len);
5230 else if (request->ssids)
5231 request->match_sets =
5232 (void *)(request->ssids + n_ssids);
5233 else
5234 request->match_sets =
5235 (void *)(request->channels + n_channels);
5236 }
5237 request->n_match_sets = n_match_sets;
5238
Luciano Coelho807f8a82011-05-11 17:09:35 +03005239 i = 0;
5240 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5241 /* user specified, bail out if channel not found */
5242 nla_for_each_nested(attr,
5243 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5244 tmp) {
5245 struct ieee80211_channel *chan;
5246
5247 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5248
5249 if (!chan) {
5250 err = -EINVAL;
5251 goto out_free;
5252 }
5253
5254 /* ignore disabled channels */
5255 if (chan->flags & IEEE80211_CHAN_DISABLED)
5256 continue;
5257
5258 request->channels[i] = chan;
5259 i++;
5260 }
5261 } else {
5262 /* all channels */
5263 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5264 int j;
5265 if (!wiphy->bands[band])
5266 continue;
5267 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5268 struct ieee80211_channel *chan;
5269
5270 chan = &wiphy->bands[band]->channels[j];
5271
5272 if (chan->flags & IEEE80211_CHAN_DISABLED)
5273 continue;
5274
5275 request->channels[i] = chan;
5276 i++;
5277 }
5278 }
5279 }
5280
5281 if (!i) {
5282 err = -EINVAL;
5283 goto out_free;
5284 }
5285
5286 request->n_channels = i;
5287
5288 i = 0;
5289 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5290 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5291 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005292 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005293 err = -EINVAL;
5294 goto out_free;
5295 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005296 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005297 memcpy(request->ssids[i].ssid, nla_data(attr),
5298 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005299 i++;
5300 }
5301 }
5302
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005303 i = 0;
5304 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5305 nla_for_each_nested(attr,
5306 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5307 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005308 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005309
5310 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5311 nla_data(attr), nla_len(attr),
5312 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005313 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005314 if (ssid) {
5315 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5316 err = -EINVAL;
5317 goto out_free;
5318 }
5319 memcpy(request->match_sets[i].ssid.ssid,
5320 nla_data(ssid), nla_len(ssid));
5321 request->match_sets[i].ssid.ssid_len =
5322 nla_len(ssid);
5323 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005324 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5325 if (rssi)
5326 request->rssi_thold = nla_get_u32(rssi);
5327 else
5328 request->rssi_thold =
5329 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005330 i++;
5331 }
5332 }
5333
Luciano Coelho807f8a82011-05-11 17:09:35 +03005334 if (info->attrs[NL80211_ATTR_IE]) {
5335 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5336 memcpy((void *)request->ie,
5337 nla_data(info->attrs[NL80211_ATTR_IE]),
5338 request->ie_len);
5339 }
5340
Sam Leffler46856bb2012-10-11 21:03:32 -07005341 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005342 request->flags = nla_get_u32(
5343 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005344 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5345 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5346 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5347 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005348 err = -EOPNOTSUPP;
5349 goto out_free;
5350 }
5351 }
Sam Lefflered4737712012-10-11 21:03:31 -07005352
Luciano Coelho807f8a82011-05-11 17:09:35 +03005353 request->dev = dev;
5354 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005355 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005356 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005357
Hila Gonene35e4d22012-06-27 17:19:42 +03005358 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005359 if (!err) {
5360 rdev->sched_scan_req = request;
5361 nl80211_send_sched_scan(rdev, dev,
5362 NL80211_CMD_START_SCHED_SCAN);
5363 goto out;
5364 }
5365
5366out_free:
5367 kfree(request);
5368out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005369 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005370 return err;
5371}
5372
5373static int nl80211_stop_sched_scan(struct sk_buff *skb,
5374 struct genl_info *info)
5375{
5376 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005377 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005378
5379 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5380 !rdev->ops->sched_scan_stop)
5381 return -EOPNOTSUPP;
5382
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005383 mutex_lock(&rdev->sched_scan_mtx);
5384 err = __cfg80211_stop_sched_scan(rdev, false);
5385 mutex_unlock(&rdev->sched_scan_mtx);
5386
5387 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005388}
5389
Simon Wunderlich04f39042013-02-08 18:16:19 +01005390static int nl80211_start_radar_detection(struct sk_buff *skb,
5391 struct genl_info *info)
5392{
5393 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5394 struct net_device *dev = info->user_ptr[1];
5395 struct wireless_dev *wdev = dev->ieee80211_ptr;
5396 struct cfg80211_chan_def chandef;
5397 int err;
5398
5399 err = nl80211_parse_chandef(rdev, info, &chandef);
5400 if (err)
5401 return err;
5402
5403 if (wdev->cac_started)
5404 return -EBUSY;
5405
5406 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5407 if (err < 0)
5408 return err;
5409
5410 if (err == 0)
5411 return -EINVAL;
5412
5413 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5414 return -EINVAL;
5415
5416 if (!rdev->ops->start_radar_detection)
5417 return -EOPNOTSUPP;
5418
5419 mutex_lock(&rdev->devlist_mtx);
5420 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5421 chandef.chan, CHAN_MODE_SHARED,
5422 BIT(chandef.width));
5423 if (err)
5424 goto err_locked;
5425
5426 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5427 if (!err) {
5428 wdev->channel = chandef.chan;
5429 wdev->cac_started = true;
5430 wdev->cac_start_time = jiffies;
5431 }
5432err_locked:
5433 mutex_unlock(&rdev->devlist_mtx);
5434
5435 return err;
5436}
5437
Johannes Berg9720bb32011-06-21 09:45:33 +02005438static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5439 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005440 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005441 struct wireless_dev *wdev,
5442 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005443{
Johannes Berg48ab9052009-07-10 18:42:31 +02005444 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005445 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005446 void *hdr;
5447 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005448 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005449
5450 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005451
Eric W. Biederman15e47302012-09-07 20:12:54 +00005452 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005453 NL80211_CMD_NEW_SCAN_RESULTS);
5454 if (!hdr)
5455 return -1;
5456
Johannes Berg9720bb32011-06-21 09:45:33 +02005457 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5458
David S. Miller9360ffd2012-03-29 04:41:26 -04005459 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5460 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5461 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005462
5463 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5464 if (!bss)
5465 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005466 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005467 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005468 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005469
5470 rcu_read_lock();
5471 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005472 if (ies) {
5473 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5474 goto fail_unlock_rcu;
5475 tsf = true;
5476 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5477 ies->len, ies->data))
5478 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005479 }
5480 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005481 if (ies) {
5482 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5483 goto fail_unlock_rcu;
5484 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5485 ies->len, ies->data))
5486 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005487 }
5488 rcu_read_unlock();
5489
David S. Miller9360ffd2012-03-29 04:41:26 -04005490 if (res->beacon_interval &&
5491 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5492 goto nla_put_failure;
5493 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5494 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5495 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5496 jiffies_to_msecs(jiffies - intbss->ts)))
5497 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005498
Johannes Berg77965c92009-02-18 18:45:06 +01005499 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005500 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005501 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5502 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005503 break;
5504 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005505 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5506 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005507 break;
5508 default:
5509 break;
5510 }
5511
Johannes Berg48ab9052009-07-10 18:42:31 +02005512 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005513 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005514 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005515 if (intbss == wdev->current_bss &&
5516 nla_put_u32(msg, NL80211_BSS_STATUS,
5517 NL80211_BSS_STATUS_ASSOCIATED))
5518 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005519 break;
5520 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005521 if (intbss == wdev->current_bss &&
5522 nla_put_u32(msg, NL80211_BSS_STATUS,
5523 NL80211_BSS_STATUS_IBSS_JOINED))
5524 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005525 break;
5526 default:
5527 break;
5528 }
5529
Johannes Berg2a519312009-02-10 21:25:55 +01005530 nla_nest_end(msg, bss);
5531
5532 return genlmsg_end(msg, hdr);
5533
Johannes Berg8cef2c92013-02-05 16:54:31 +01005534 fail_unlock_rcu:
5535 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005536 nla_put_failure:
5537 genlmsg_cancel(msg, hdr);
5538 return -EMSGSIZE;
5539}
5540
5541static int nl80211_dump_scan(struct sk_buff *skb,
5542 struct netlink_callback *cb)
5543{
Johannes Berg48ab9052009-07-10 18:42:31 +02005544 struct cfg80211_registered_device *rdev;
5545 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005546 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005547 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005548 int start = cb->args[1], idx = 0;
5549 int err;
5550
Johannes Berg67748892010-10-04 21:14:06 +02005551 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5552 if (err)
5553 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005554
Johannes Berg48ab9052009-07-10 18:42:31 +02005555 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005556
Johannes Berg48ab9052009-07-10 18:42:31 +02005557 wdev_lock(wdev);
5558 spin_lock_bh(&rdev->bss_lock);
5559 cfg80211_bss_expire(rdev);
5560
Johannes Berg9720bb32011-06-21 09:45:33 +02005561 cb->seq = rdev->bss_generation;
5562
Johannes Berg48ab9052009-07-10 18:42:31 +02005563 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005564 if (++idx <= start)
5565 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005566 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005567 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005568 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005569 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005570 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005571 }
5572 }
5573
Johannes Berg48ab9052009-07-10 18:42:31 +02005574 spin_unlock_bh(&rdev->bss_lock);
5575 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005576
5577 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005578 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005579
Johannes Berg67748892010-10-04 21:14:06 +02005580 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005581}
5582
Eric W. Biederman15e47302012-09-07 20:12:54 +00005583static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005584 int flags, struct net_device *dev,
5585 struct survey_info *survey)
5586{
5587 void *hdr;
5588 struct nlattr *infoattr;
5589
Eric W. Biederman15e47302012-09-07 20:12:54 +00005590 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005591 NL80211_CMD_NEW_SURVEY_RESULTS);
5592 if (!hdr)
5593 return -ENOMEM;
5594
David S. Miller9360ffd2012-03-29 04:41:26 -04005595 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5596 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005597
5598 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5599 if (!infoattr)
5600 goto nla_put_failure;
5601
David S. Miller9360ffd2012-03-29 04:41:26 -04005602 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5603 survey->channel->center_freq))
5604 goto nla_put_failure;
5605
5606 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5607 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5608 goto nla_put_failure;
5609 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5610 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5611 goto nla_put_failure;
5612 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5613 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5614 survey->channel_time))
5615 goto nla_put_failure;
5616 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5617 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5618 survey->channel_time_busy))
5619 goto nla_put_failure;
5620 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5621 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5622 survey->channel_time_ext_busy))
5623 goto nla_put_failure;
5624 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5625 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5626 survey->channel_time_rx))
5627 goto nla_put_failure;
5628 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5629 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5630 survey->channel_time_tx))
5631 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005632
5633 nla_nest_end(msg, infoattr);
5634
5635 return genlmsg_end(msg, hdr);
5636
5637 nla_put_failure:
5638 genlmsg_cancel(msg, hdr);
5639 return -EMSGSIZE;
5640}
5641
5642static int nl80211_dump_survey(struct sk_buff *skb,
5643 struct netlink_callback *cb)
5644{
5645 struct survey_info survey;
5646 struct cfg80211_registered_device *dev;
5647 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005648 int survey_idx = cb->args[1];
5649 int res;
5650
Johannes Berg67748892010-10-04 21:14:06 +02005651 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5652 if (res)
5653 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005654
5655 if (!dev->ops->dump_survey) {
5656 res = -EOPNOTSUPP;
5657 goto out_err;
5658 }
5659
5660 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005661 struct ieee80211_channel *chan;
5662
Hila Gonene35e4d22012-06-27 17:19:42 +03005663 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005664 if (res == -ENOENT)
5665 break;
5666 if (res)
5667 goto out_err;
5668
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005669 /* Survey without a channel doesn't make sense */
5670 if (!survey.channel) {
5671 res = -EINVAL;
5672 goto out;
5673 }
5674
5675 chan = ieee80211_get_channel(&dev->wiphy,
5676 survey.channel->center_freq);
5677 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5678 survey_idx++;
5679 continue;
5680 }
5681
Holger Schurig61fa7132009-11-11 12:25:40 +01005682 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005683 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005684 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5685 netdev,
5686 &survey) < 0)
5687 goto out;
5688 survey_idx++;
5689 }
5690
5691 out:
5692 cb->args[1] = survey_idx;
5693 res = skb->len;
5694 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005695 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005696 return res;
5697}
5698
Samuel Ortizb23aa672009-07-01 21:26:54 +02005699static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5700{
5701 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5702 NL80211_WPA_VERSION_2));
5703}
5704
Jouni Malinen636a5d32009-03-19 13:39:22 +02005705static int nl80211_authenticate(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 ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005710 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5711 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005712 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005713 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005714 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005715
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005716 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5717 return -EINVAL;
5718
5719 if (!info->attrs[NL80211_ATTR_MAC])
5720 return -EINVAL;
5721
Jouni Malinen17780922009-03-27 20:52:47 +02005722 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5723 return -EINVAL;
5724
Johannes Berg19957bb2009-07-02 17:20:43 +02005725 if (!info->attrs[NL80211_ATTR_SSID])
5726 return -EINVAL;
5727
5728 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5729 return -EINVAL;
5730
Johannes Bergfffd0932009-07-08 14:22:54 +02005731 err = nl80211_parse_key(info, &key);
5732 if (err)
5733 return err;
5734
5735 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005736 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5737 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005738 if (!key.p.key || !key.p.key_len)
5739 return -EINVAL;
5740 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5741 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5742 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5743 key.p.key_len != WLAN_KEY_LEN_WEP104))
5744 return -EINVAL;
5745 if (key.idx > 4)
5746 return -EINVAL;
5747 } else {
5748 key.p.key_len = 0;
5749 key.p.key = NULL;
5750 }
5751
Johannes Bergafea0b72010-08-10 09:46:42 +02005752 if (key.idx >= 0) {
5753 int i;
5754 bool ok = false;
5755 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5756 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5757 ok = true;
5758 break;
5759 }
5760 }
Johannes Berg4c476992010-10-04 21:36:35 +02005761 if (!ok)
5762 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005763 }
5764
Johannes Berg4c476992010-10-04 21:36:35 +02005765 if (!rdev->ops->auth)
5766 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005767
Johannes Berg074ac8d2010-09-16 14:58:22 +02005768 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005769 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5770 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005771
Johannes Berg19957bb2009-07-02 17:20:43 +02005772 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005773 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005774 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005775 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5776 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005777
Johannes Berg19957bb2009-07-02 17:20:43 +02005778 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5779 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5780
5781 if (info->attrs[NL80211_ATTR_IE]) {
5782 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5783 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5784 }
5785
5786 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005787 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005788 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005789
Jouni Malinene39e5b52012-09-30 19:29:39 +03005790 if (auth_type == NL80211_AUTHTYPE_SAE &&
5791 !info->attrs[NL80211_ATTR_SAE_DATA])
5792 return -EINVAL;
5793
5794 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5795 if (auth_type != NL80211_AUTHTYPE_SAE)
5796 return -EINVAL;
5797 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5798 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5799 /* need to include at least Auth Transaction and Status Code */
5800 if (sae_data_len < 4)
5801 return -EINVAL;
5802 }
5803
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005804 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5805
Johannes Berg95de8172012-01-20 13:55:25 +01005806 /*
5807 * Since we no longer track auth state, ignore
5808 * requests to only change local state.
5809 */
5810 if (local_state_change)
5811 return 0;
5812
Johannes Berg4c476992010-10-04 21:36:35 +02005813 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5814 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005815 key.p.key, key.p.key_len, key.idx,
5816 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005817}
5818
Johannes Bergc0692b82010-08-27 14:26:53 +03005819static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5820 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005821 struct cfg80211_crypto_settings *settings,
5822 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005823{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005824 memset(settings, 0, sizeof(*settings));
5825
Samuel Ortizb23aa672009-07-01 21:26:54 +02005826 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5827
Johannes Bergc0692b82010-08-27 14:26:53 +03005828 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5829 u16 proto;
5830 proto = nla_get_u16(
5831 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5832 settings->control_port_ethertype = cpu_to_be16(proto);
5833 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5834 proto != ETH_P_PAE)
5835 return -EINVAL;
5836 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5837 settings->control_port_no_encrypt = true;
5838 } else
5839 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5840
Samuel Ortizb23aa672009-07-01 21:26:54 +02005841 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5842 void *data;
5843 int len, i;
5844
5845 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5846 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5847 settings->n_ciphers_pairwise = len / sizeof(u32);
5848
5849 if (len % sizeof(u32))
5850 return -EINVAL;
5851
Johannes Berg3dc27d22009-07-02 21:36:37 +02005852 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005853 return -EINVAL;
5854
5855 memcpy(settings->ciphers_pairwise, data, len);
5856
5857 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005858 if (!cfg80211_supported_cipher_suite(
5859 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005860 settings->ciphers_pairwise[i]))
5861 return -EINVAL;
5862 }
5863
5864 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5865 settings->cipher_group =
5866 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005867 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5868 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005869 return -EINVAL;
5870 }
5871
5872 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5873 settings->wpa_versions =
5874 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5875 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5876 return -EINVAL;
5877 }
5878
5879 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5880 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005881 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005882
5883 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5884 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5885 settings->n_akm_suites = len / sizeof(u32);
5886
5887 if (len % sizeof(u32))
5888 return -EINVAL;
5889
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005890 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5891 return -EINVAL;
5892
Samuel Ortizb23aa672009-07-01 21:26:54 +02005893 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005894 }
5895
5896 return 0;
5897}
5898
Jouni Malinen636a5d32009-03-19 13:39:22 +02005899static int nl80211_associate(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 Berg19957bb2009-07-02 17:20:43 +02005903 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005904 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005905 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005906 int err, ssid_len, ie_len = 0;
5907 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005908 u32 flags = 0;
5909 struct ieee80211_ht_cap *ht_capa = NULL;
5910 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005911
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005912 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5913 return -EINVAL;
5914
5915 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005916 !info->attrs[NL80211_ATTR_SSID] ||
5917 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005918 return -EINVAL;
5919
Johannes Berg4c476992010-10-04 21:36:35 +02005920 if (!rdev->ops->assoc)
5921 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005922
Johannes Berg074ac8d2010-09-16 14:58:22 +02005923 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005924 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5925 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005926
Johannes Berg19957bb2009-07-02 17:20:43 +02005927 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005928
Johannes Berg19957bb2009-07-02 17:20:43 +02005929 chan = ieee80211_get_channel(&rdev->wiphy,
5930 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005931 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5932 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005933
Johannes Berg19957bb2009-07-02 17:20:43 +02005934 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5935 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005936
5937 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005938 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5939 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005940 }
5941
Jouni Malinendc6382c2009-05-06 22:09:37 +03005942 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005943 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005944 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005945 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005946 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005947 else if (mfp != NL80211_MFP_NO)
5948 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005949 }
5950
Johannes Berg3e5d7642009-07-07 14:37:26 +02005951 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5952 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5953
Ben Greear7e7c8922011-11-18 11:31:59 -08005954 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5955 flags |= ASSOC_REQ_DISABLE_HT;
5956
5957 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5958 ht_capa_mask =
5959 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5960
5961 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5962 if (!ht_capa_mask)
5963 return -EINVAL;
5964 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5965 }
5966
Johannes Bergc0692b82010-08-27 14:26:53 +03005967 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005968 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005969 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5970 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005971 &crypto, flags, ht_capa,
5972 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005973
Jouni Malinen636a5d32009-03-19 13:39:22 +02005974 return err;
5975}
5976
5977static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5978{
Johannes Berg4c476992010-10-04 21:36:35 +02005979 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5980 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005981 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005982 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005983 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005984 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005985
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005986 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5987 return -EINVAL;
5988
5989 if (!info->attrs[NL80211_ATTR_MAC])
5990 return -EINVAL;
5991
5992 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5993 return -EINVAL;
5994
Johannes Berg4c476992010-10-04 21:36:35 +02005995 if (!rdev->ops->deauth)
5996 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005997
Johannes Berg074ac8d2010-09-16 14:58:22 +02005998 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005999 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6000 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006001
Johannes Berg19957bb2009-07-02 17:20:43 +02006002 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006003
Johannes Berg19957bb2009-07-02 17:20:43 +02006004 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6005 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006006 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006007 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006008 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006009
6010 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006011 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6012 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006013 }
6014
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006015 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6016
Johannes Berg4c476992010-10-04 21:36:35 +02006017 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6018 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006019}
6020
6021static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6022{
Johannes Berg4c476992010-10-04 21:36:35 +02006023 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6024 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006025 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006026 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006027 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006028 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006029
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006030 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6031 return -EINVAL;
6032
6033 if (!info->attrs[NL80211_ATTR_MAC])
6034 return -EINVAL;
6035
6036 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6037 return -EINVAL;
6038
Johannes Berg4c476992010-10-04 21:36:35 +02006039 if (!rdev->ops->disassoc)
6040 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006041
Johannes Berg074ac8d2010-09-16 14:58:22 +02006042 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006043 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6044 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006045
Johannes Berg19957bb2009-07-02 17:20:43 +02006046 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006047
Johannes Berg19957bb2009-07-02 17:20:43 +02006048 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6049 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006050 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006051 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006052 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006053
6054 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006055 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6056 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006057 }
6058
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006059 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6060
Johannes Berg4c476992010-10-04 21:36:35 +02006061 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6062 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006063}
6064
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006065static bool
6066nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6067 int mcast_rate[IEEE80211_NUM_BANDS],
6068 int rateval)
6069{
6070 struct wiphy *wiphy = &rdev->wiphy;
6071 bool found = false;
6072 int band, i;
6073
6074 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6075 struct ieee80211_supported_band *sband;
6076
6077 sband = wiphy->bands[band];
6078 if (!sband)
6079 continue;
6080
6081 for (i = 0; i < sband->n_bitrates; i++) {
6082 if (sband->bitrates[i].bitrate == rateval) {
6083 mcast_rate[band] = i + 1;
6084 found = true;
6085 break;
6086 }
6087 }
6088 }
6089
6090 return found;
6091}
6092
Johannes Berg04a773a2009-04-19 21:24:32 +02006093static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6094{
Johannes Berg4c476992010-10-04 21:36:35 +02006095 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6096 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006097 struct cfg80211_ibss_params ibss;
6098 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006099 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006100 int err;
6101
Johannes Berg8e30bc52009-04-22 17:45:38 +02006102 memset(&ibss, 0, sizeof(ibss));
6103
Johannes Berg04a773a2009-04-19 21:24:32 +02006104 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6105 return -EINVAL;
6106
Johannes Berg683b6d32012-11-08 21:25:48 +01006107 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006108 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6109 return -EINVAL;
6110
Johannes Berg8e30bc52009-04-22 17:45:38 +02006111 ibss.beacon_interval = 100;
6112
6113 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6114 ibss.beacon_interval =
6115 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6116 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6117 return -EINVAL;
6118 }
6119
Johannes Berg4c476992010-10-04 21:36:35 +02006120 if (!rdev->ops->join_ibss)
6121 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006122
Johannes Berg4c476992010-10-04 21:36:35 +02006123 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6124 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006125
Johannes Berg79c97e92009-07-07 03:56:12 +02006126 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006127
Johannes Berg39193492011-09-16 13:45:25 +02006128 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006129 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006130
6131 if (!is_valid_ether_addr(ibss.bssid))
6132 return -EINVAL;
6133 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006134 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6135 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6136
6137 if (info->attrs[NL80211_ATTR_IE]) {
6138 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6139 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6140 }
6141
Johannes Berg683b6d32012-11-08 21:25:48 +01006142 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6143 if (err)
6144 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006145
Johannes Berg683b6d32012-11-08 21:25:48 +01006146 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006147 return -EINVAL;
6148
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006149 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6150 return -EINVAL;
6151 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6152 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006153 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006154
Johannes Berg04a773a2009-04-19 21:24:32 +02006155 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006156 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006157
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006158 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6159 u8 *rates =
6160 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6161 int n_rates =
6162 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6163 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006164 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006165
Johannes Berg34850ab2011-07-18 18:08:35 +02006166 err = ieee80211_get_ratemask(sband, rates, n_rates,
6167 &ibss.basic_rates);
6168 if (err)
6169 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006170 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006171
6172 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6173 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6174 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6175 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006176
Johannes Berg4c476992010-10-04 21:36:35 +02006177 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306178 bool no_ht = false;
6179
Johannes Berg4c476992010-10-04 21:36:35 +02006180 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306181 info->attrs[NL80211_ATTR_KEYS],
6182 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006183 if (IS_ERR(connkeys))
6184 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306185
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006186 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6187 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306188 kfree(connkeys);
6189 return -EINVAL;
6190 }
Johannes Berg4c476992010-10-04 21:36:35 +02006191 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006192
Antonio Quartulli267335d2012-01-31 20:25:47 +01006193 ibss.control_port =
6194 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6195
Johannes Berg4c476992010-10-04 21:36:35 +02006196 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006197 if (err)
6198 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006199 return err;
6200}
6201
6202static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6203{
Johannes Berg4c476992010-10-04 21:36:35 +02006204 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6205 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006206
Johannes Berg4c476992010-10-04 21:36:35 +02006207 if (!rdev->ops->leave_ibss)
6208 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006209
Johannes Berg4c476992010-10-04 21:36:35 +02006210 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6211 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006212
Johannes Berg4c476992010-10-04 21:36:35 +02006213 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006214}
6215
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006216static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6217{
6218 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6219 struct net_device *dev = info->user_ptr[1];
6220 int mcast_rate[IEEE80211_NUM_BANDS];
6221 u32 nla_rate;
6222 int err;
6223
6224 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6225 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6226 return -EOPNOTSUPP;
6227
6228 if (!rdev->ops->set_mcast_rate)
6229 return -EOPNOTSUPP;
6230
6231 memset(mcast_rate, 0, sizeof(mcast_rate));
6232
6233 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6234 return -EINVAL;
6235
6236 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6237 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6238 return -EINVAL;
6239
6240 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6241
6242 return err;
6243}
6244
6245
Johannes Bergaff89a92009-07-01 21:26:51 +02006246#ifdef CONFIG_NL80211_TESTMODE
6247static struct genl_multicast_group nl80211_testmode_mcgrp = {
6248 .name = "testmode",
6249};
6250
6251static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6252{
Johannes Berg4c476992010-10-04 21:36:35 +02006253 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006254 int err;
6255
6256 if (!info->attrs[NL80211_ATTR_TESTDATA])
6257 return -EINVAL;
6258
Johannes Bergaff89a92009-07-01 21:26:51 +02006259 err = -EOPNOTSUPP;
6260 if (rdev->ops->testmode_cmd) {
6261 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006262 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006263 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6264 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6265 rdev->testmode_info = NULL;
6266 }
6267
Johannes Bergaff89a92009-07-01 21:26:51 +02006268 return err;
6269}
6270
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006271static int nl80211_testmode_dump(struct sk_buff *skb,
6272 struct netlink_callback *cb)
6273{
Johannes Berg00918d32011-12-13 17:22:05 +01006274 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006275 int err;
6276 long phy_idx;
6277 void *data = NULL;
6278 int data_len = 0;
6279
6280 if (cb->args[0]) {
6281 /*
6282 * 0 is a valid index, but not valid for args[0],
6283 * so we need to offset by 1.
6284 */
6285 phy_idx = cb->args[0] - 1;
6286 } else {
6287 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6288 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6289 nl80211_policy);
6290 if (err)
6291 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006292
Johannes Berg2bd7e352012-06-15 14:23:16 +02006293 mutex_lock(&cfg80211_mutex);
6294 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6295 nl80211_fam.attrbuf);
6296 if (IS_ERR(rdev)) {
6297 mutex_unlock(&cfg80211_mutex);
6298 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006299 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006300 phy_idx = rdev->wiphy_idx;
6301 rdev = NULL;
6302 mutex_unlock(&cfg80211_mutex);
6303
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006304 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6305 cb->args[1] =
6306 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6307 }
6308
6309 if (cb->args[1]) {
6310 data = nla_data((void *)cb->args[1]);
6311 data_len = nla_len((void *)cb->args[1]);
6312 }
6313
6314 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006315 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6316 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006317 mutex_unlock(&cfg80211_mutex);
6318 return -ENOENT;
6319 }
Johannes Berg00918d32011-12-13 17:22:05 +01006320 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006321 mutex_unlock(&cfg80211_mutex);
6322
Johannes Berg00918d32011-12-13 17:22:05 +01006323 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006324 err = -EOPNOTSUPP;
6325 goto out_err;
6326 }
6327
6328 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006329 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006330 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6331 NL80211_CMD_TESTMODE);
6332 struct nlattr *tmdata;
6333
David S. Miller9360ffd2012-03-29 04:41:26 -04006334 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006335 genlmsg_cancel(skb, hdr);
6336 break;
6337 }
6338
6339 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6340 if (!tmdata) {
6341 genlmsg_cancel(skb, hdr);
6342 break;
6343 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006344 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006345 nla_nest_end(skb, tmdata);
6346
6347 if (err == -ENOBUFS || err == -ENOENT) {
6348 genlmsg_cancel(skb, hdr);
6349 break;
6350 } else if (err) {
6351 genlmsg_cancel(skb, hdr);
6352 goto out_err;
6353 }
6354
6355 genlmsg_end(skb, hdr);
6356 }
6357
6358 err = skb->len;
6359 /* see above */
6360 cb->args[0] = phy_idx + 1;
6361 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006362 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006363 return err;
6364}
6365
Johannes Bergaff89a92009-07-01 21:26:51 +02006366static struct sk_buff *
6367__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006368 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006369{
6370 struct sk_buff *skb;
6371 void *hdr;
6372 struct nlattr *data;
6373
6374 skb = nlmsg_new(approxlen + 100, gfp);
6375 if (!skb)
6376 return NULL;
6377
Eric W. Biederman15e47302012-09-07 20:12:54 +00006378 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006379 if (!hdr) {
6380 kfree_skb(skb);
6381 return NULL;
6382 }
6383
David S. Miller9360ffd2012-03-29 04:41:26 -04006384 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6385 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006386 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6387
6388 ((void **)skb->cb)[0] = rdev;
6389 ((void **)skb->cb)[1] = hdr;
6390 ((void **)skb->cb)[2] = data;
6391
6392 return skb;
6393
6394 nla_put_failure:
6395 kfree_skb(skb);
6396 return NULL;
6397}
6398
6399struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6400 int approxlen)
6401{
6402 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6403
6404 if (WARN_ON(!rdev->testmode_info))
6405 return NULL;
6406
6407 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006408 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006409 rdev->testmode_info->snd_seq,
6410 GFP_KERNEL);
6411}
6412EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6413
6414int cfg80211_testmode_reply(struct sk_buff *skb)
6415{
6416 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6417 void *hdr = ((void **)skb->cb)[1];
6418 struct nlattr *data = ((void **)skb->cb)[2];
6419
6420 if (WARN_ON(!rdev->testmode_info)) {
6421 kfree_skb(skb);
6422 return -EINVAL;
6423 }
6424
6425 nla_nest_end(skb, data);
6426 genlmsg_end(skb, hdr);
6427 return genlmsg_reply(skb, rdev->testmode_info);
6428}
6429EXPORT_SYMBOL(cfg80211_testmode_reply);
6430
6431struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6432 int approxlen, gfp_t gfp)
6433{
6434 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6435
6436 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6437}
6438EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6439
6440void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6441{
6442 void *hdr = ((void **)skb->cb)[1];
6443 struct nlattr *data = ((void **)skb->cb)[2];
6444
6445 nla_nest_end(skb, data);
6446 genlmsg_end(skb, hdr);
6447 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6448}
6449EXPORT_SYMBOL(cfg80211_testmode_event);
6450#endif
6451
Samuel Ortizb23aa672009-07-01 21:26:54 +02006452static int nl80211_connect(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 Ortizb23aa672009-07-01 21:26:54 +02006456 struct cfg80211_connect_params connect;
6457 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006458 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006459 int err;
6460
6461 memset(&connect, 0, sizeof(connect));
6462
6463 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6464 return -EINVAL;
6465
6466 if (!info->attrs[NL80211_ATTR_SSID] ||
6467 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6468 return -EINVAL;
6469
6470 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6471 connect.auth_type =
6472 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006473 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6474 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006475 return -EINVAL;
6476 } else
6477 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6478
6479 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6480
Johannes Bergc0692b82010-08-27 14:26:53 +03006481 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006482 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006483 if (err)
6484 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006485
Johannes Berg074ac8d2010-09-16 14:58:22 +02006486 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006487 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6488 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006489
Johannes Berg79c97e92009-07-07 03:56:12 +02006490 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006491
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306492 connect.bg_scan_period = -1;
6493 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6494 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6495 connect.bg_scan_period =
6496 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6497 }
6498
Samuel Ortizb23aa672009-07-01 21:26:54 +02006499 if (info->attrs[NL80211_ATTR_MAC])
6500 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6501 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6502 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6503
6504 if (info->attrs[NL80211_ATTR_IE]) {
6505 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6506 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6507 }
6508
Jouni Malinencee00a92013-01-15 17:15:57 +02006509 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6510 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6511 if (connect.mfp != NL80211_MFP_REQUIRED &&
6512 connect.mfp != NL80211_MFP_NO)
6513 return -EINVAL;
6514 } else {
6515 connect.mfp = NL80211_MFP_NO;
6516 }
6517
Samuel Ortizb23aa672009-07-01 21:26:54 +02006518 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6519 connect.channel =
6520 ieee80211_get_channel(wiphy,
6521 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6522 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006523 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6524 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006525 }
6526
Johannes Bergfffd0932009-07-08 14:22:54 +02006527 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6528 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306529 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006530 if (IS_ERR(connkeys))
6531 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006532 }
6533
Ben Greear7e7c8922011-11-18 11:31:59 -08006534 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6535 connect.flags |= ASSOC_REQ_DISABLE_HT;
6536
6537 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6538 memcpy(&connect.ht_capa_mask,
6539 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6540 sizeof(connect.ht_capa_mask));
6541
6542 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006543 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6544 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006545 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006546 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006547 memcpy(&connect.ht_capa,
6548 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6549 sizeof(connect.ht_capa));
6550 }
6551
Johannes Bergfffd0932009-07-08 14:22:54 +02006552 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006553 if (err)
6554 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006555 return err;
6556}
6557
6558static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6559{
Johannes Berg4c476992010-10-04 21:36:35 +02006560 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6561 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006562 u16 reason;
6563
6564 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6565 reason = WLAN_REASON_DEAUTH_LEAVING;
6566 else
6567 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6568
6569 if (reason == 0)
6570 return -EINVAL;
6571
Johannes Berg074ac8d2010-09-16 14:58:22 +02006572 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006573 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6574 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006575
Johannes Berg4c476992010-10-04 21:36:35 +02006576 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006577}
6578
Johannes Berg463d0182009-07-14 00:33:35 +02006579static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6580{
Johannes Berg4c476992010-10-04 21:36:35 +02006581 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006582 struct net *net;
6583 int err;
6584 u32 pid;
6585
6586 if (!info->attrs[NL80211_ATTR_PID])
6587 return -EINVAL;
6588
6589 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6590
Johannes Berg463d0182009-07-14 00:33:35 +02006591 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006592 if (IS_ERR(net))
6593 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006594
6595 err = 0;
6596
6597 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006598 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6599 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006600
Johannes Berg463d0182009-07-14 00:33:35 +02006601 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006602 return err;
6603}
6604
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006605static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6606{
Johannes Berg4c476992010-10-04 21:36:35 +02006607 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006608 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6609 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006610 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006611 struct cfg80211_pmksa pmksa;
6612
6613 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6614
6615 if (!info->attrs[NL80211_ATTR_MAC])
6616 return -EINVAL;
6617
6618 if (!info->attrs[NL80211_ATTR_PMKID])
6619 return -EINVAL;
6620
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006621 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6622 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6623
Johannes Berg074ac8d2010-09-16 14:58:22 +02006624 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006625 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6626 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006627
6628 switch (info->genlhdr->cmd) {
6629 case NL80211_CMD_SET_PMKSA:
6630 rdev_ops = rdev->ops->set_pmksa;
6631 break;
6632 case NL80211_CMD_DEL_PMKSA:
6633 rdev_ops = rdev->ops->del_pmksa;
6634 break;
6635 default:
6636 WARN_ON(1);
6637 break;
6638 }
6639
Johannes Berg4c476992010-10-04 21:36:35 +02006640 if (!rdev_ops)
6641 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006642
Johannes Berg4c476992010-10-04 21:36:35 +02006643 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006644}
6645
6646static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6647{
Johannes Berg4c476992010-10-04 21:36:35 +02006648 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6649 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006650
Johannes Berg074ac8d2010-09-16 14:58:22 +02006651 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006652 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6653 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006654
Johannes Berg4c476992010-10-04 21:36:35 +02006655 if (!rdev->ops->flush_pmksa)
6656 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006657
Hila Gonene35e4d22012-06-27 17:19:42 +03006658 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006659}
6660
Arik Nemtsov109086c2011-09-28 14:12:50 +03006661static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6662{
6663 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6664 struct net_device *dev = info->user_ptr[1];
6665 u8 action_code, dialog_token;
6666 u16 status_code;
6667 u8 *peer;
6668
6669 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6670 !rdev->ops->tdls_mgmt)
6671 return -EOPNOTSUPP;
6672
6673 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6674 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6675 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6676 !info->attrs[NL80211_ATTR_IE] ||
6677 !info->attrs[NL80211_ATTR_MAC])
6678 return -EINVAL;
6679
6680 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6681 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6682 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6683 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6684
Hila Gonene35e4d22012-06-27 17:19:42 +03006685 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6686 dialog_token, status_code,
6687 nla_data(info->attrs[NL80211_ATTR_IE]),
6688 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006689}
6690
6691static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6692{
6693 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6694 struct net_device *dev = info->user_ptr[1];
6695 enum nl80211_tdls_operation operation;
6696 u8 *peer;
6697
6698 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6699 !rdev->ops->tdls_oper)
6700 return -EOPNOTSUPP;
6701
6702 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6703 !info->attrs[NL80211_ATTR_MAC])
6704 return -EINVAL;
6705
6706 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6707 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6708
Hila Gonene35e4d22012-06-27 17:19:42 +03006709 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006710}
6711
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006712static int nl80211_remain_on_channel(struct sk_buff *skb,
6713 struct genl_info *info)
6714{
Johannes Berg4c476992010-10-04 21:36:35 +02006715 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006716 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006717 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006718 struct sk_buff *msg;
6719 void *hdr;
6720 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006721 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006722 int err;
6723
6724 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6725 !info->attrs[NL80211_ATTR_DURATION])
6726 return -EINVAL;
6727
6728 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6729
Johannes Berg7c4ef712011-11-18 15:33:48 +01006730 if (!rdev->ops->remain_on_channel ||
6731 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006732 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006733
Johannes Bergebf348f2012-06-01 12:50:54 +02006734 /*
6735 * We should be on that channel for at least a minimum amount of
6736 * time (10ms) but no longer than the driver supports.
6737 */
6738 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6739 duration > rdev->wiphy.max_remain_on_channel_duration)
6740 return -EINVAL;
6741
Johannes Berg683b6d32012-11-08 21:25:48 +01006742 err = nl80211_parse_chandef(rdev, info, &chandef);
6743 if (err)
6744 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006745
6746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006747 if (!msg)
6748 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006749
Eric W. Biederman15e47302012-09-07 20:12:54 +00006750 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006751 NL80211_CMD_REMAIN_ON_CHANNEL);
6752
6753 if (IS_ERR(hdr)) {
6754 err = PTR_ERR(hdr);
6755 goto free_msg;
6756 }
6757
Johannes Berg683b6d32012-11-08 21:25:48 +01006758 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6759 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006760
6761 if (err)
6762 goto free_msg;
6763
David S. Miller9360ffd2012-03-29 04:41:26 -04006764 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6765 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006766
6767 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006768
6769 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006770
6771 nla_put_failure:
6772 err = -ENOBUFS;
6773 free_msg:
6774 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006775 return err;
6776}
6777
6778static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6779 struct genl_info *info)
6780{
Johannes Berg4c476992010-10-04 21:36:35 +02006781 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006782 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006783 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006784
6785 if (!info->attrs[NL80211_ATTR_COOKIE])
6786 return -EINVAL;
6787
Johannes Berg4c476992010-10-04 21:36:35 +02006788 if (!rdev->ops->cancel_remain_on_channel)
6789 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006790
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006791 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6792
Hila Gonene35e4d22012-06-27 17:19:42 +03006793 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006794}
6795
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006796static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6797 u8 *rates, u8 rates_len)
6798{
6799 u8 i;
6800 u32 mask = 0;
6801
6802 for (i = 0; i < rates_len; i++) {
6803 int rate = (rates[i] & 0x7f) * 5;
6804 int ridx;
6805 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6806 struct ieee80211_rate *srate =
6807 &sband->bitrates[ridx];
6808 if (rate == srate->bitrate) {
6809 mask |= 1 << ridx;
6810 break;
6811 }
6812 }
6813 if (ridx == sband->n_bitrates)
6814 return 0; /* rate not found */
6815 }
6816
6817 return mask;
6818}
6819
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006820static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6821 u8 *rates, u8 rates_len,
6822 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6823{
6824 u8 i;
6825
6826 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6827
6828 for (i = 0; i < rates_len; i++) {
6829 int ridx, rbit;
6830
6831 ridx = rates[i] / 8;
6832 rbit = BIT(rates[i] % 8);
6833
6834 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006835 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006836 return false;
6837
6838 /* check availability */
6839 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6840 mcs[ridx] |= rbit;
6841 else
6842 return false;
6843 }
6844
6845 return true;
6846}
6847
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006848static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006849 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6850 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006851 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6852 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006853};
6854
6855static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6856 struct genl_info *info)
6857{
6858 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006859 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006860 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006861 int rem, i;
6862 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006863 struct nlattr *tx_rates;
6864 struct ieee80211_supported_band *sband;
6865
6866 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6867 return -EINVAL;
6868
Johannes Berg4c476992010-10-04 21:36:35 +02006869 if (!rdev->ops->set_bitrate_mask)
6870 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006871
6872 memset(&mask, 0, sizeof(mask));
6873 /* Default to all rates enabled */
6874 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6875 sband = rdev->wiphy.bands[i];
6876 mask.control[i].legacy =
6877 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006878 if (sband)
6879 memcpy(mask.control[i].mcs,
6880 sband->ht_cap.mcs.rx_mask,
6881 sizeof(mask.control[i].mcs));
6882 else
6883 memset(mask.control[i].mcs, 0,
6884 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006885 }
6886
6887 /*
6888 * The nested attribute uses enum nl80211_band as the index. This maps
6889 * directly to the enum ieee80211_band values used in cfg80211.
6890 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006891 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006892 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6893 {
6894 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006895 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6896 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006897 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006898 if (sband == NULL)
6899 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006900 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6901 nla_len(tx_rates), nl80211_txattr_policy);
6902 if (tb[NL80211_TXRATE_LEGACY]) {
6903 mask.control[band].legacy = rateset_to_mask(
6904 sband,
6905 nla_data(tb[NL80211_TXRATE_LEGACY]),
6906 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306907 if ((mask.control[band].legacy == 0) &&
6908 nla_len(tb[NL80211_TXRATE_LEGACY]))
6909 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006910 }
6911 if (tb[NL80211_TXRATE_MCS]) {
6912 if (!ht_rateset_to_mask(
6913 sband,
6914 nla_data(tb[NL80211_TXRATE_MCS]),
6915 nla_len(tb[NL80211_TXRATE_MCS]),
6916 mask.control[band].mcs))
6917 return -EINVAL;
6918 }
6919
6920 if (mask.control[band].legacy == 0) {
6921 /* don't allow empty legacy rates if HT
6922 * is not even supported. */
6923 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6924 return -EINVAL;
6925
6926 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6927 if (mask.control[band].mcs[i])
6928 break;
6929
6930 /* legacy and mcs rates may not be both empty */
6931 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006932 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006933 }
6934 }
6935
Hila Gonene35e4d22012-06-27 17:19:42 +03006936 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006937}
6938
Johannes Berg2e161f72010-08-12 15:38:38 +02006939static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006940{
Johannes Berg4c476992010-10-04 21:36:35 +02006941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006942 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006943 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006944
6945 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6946 return -EINVAL;
6947
Johannes Berg2e161f72010-08-12 15:38:38 +02006948 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6949 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006950
Johannes Berg71bbc992012-06-15 15:30:18 +02006951 switch (wdev->iftype) {
6952 case NL80211_IFTYPE_STATION:
6953 case NL80211_IFTYPE_ADHOC:
6954 case NL80211_IFTYPE_P2P_CLIENT:
6955 case NL80211_IFTYPE_AP:
6956 case NL80211_IFTYPE_AP_VLAN:
6957 case NL80211_IFTYPE_MESH_POINT:
6958 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006959 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006960 break;
6961 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006962 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006963 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006964
6965 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006966 if (!rdev->ops->mgmt_tx)
6967 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006968
Eric W. Biederman15e47302012-09-07 20:12:54 +00006969 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006970 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6971 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006972}
6973
Johannes Berg2e161f72010-08-12 15:38:38 +02006974static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006975{
Johannes Berg4c476992010-10-04 21:36:35 +02006976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006977 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006978 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006979 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006980 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006981 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006982 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006983 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006984 bool offchan, no_cck, dont_wait_for_ack;
6985
6986 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006987
Johannes Berg683b6d32012-11-08 21:25:48 +01006988 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006989 return -EINVAL;
6990
Johannes Berg4c476992010-10-04 21:36:35 +02006991 if (!rdev->ops->mgmt_tx)
6992 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006993
Johannes Berg71bbc992012-06-15 15:30:18 +02006994 switch (wdev->iftype) {
6995 case NL80211_IFTYPE_STATION:
6996 case NL80211_IFTYPE_ADHOC:
6997 case NL80211_IFTYPE_P2P_CLIENT:
6998 case NL80211_IFTYPE_AP:
6999 case NL80211_IFTYPE_AP_VLAN:
7000 case NL80211_IFTYPE_MESH_POINT:
7001 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007002 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007003 break;
7004 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007005 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007006 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007007
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007008 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007009 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007010 return -EINVAL;
7011 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007012
7013 /*
7014 * We should wait on the channel for at least a minimum amount
7015 * of time (10ms) but no longer than the driver supports.
7016 */
7017 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7018 wait > rdev->wiphy.max_remain_on_channel_duration)
7019 return -EINVAL;
7020
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007021 }
7022
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007023 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7024
Johannes Berg7c4ef712011-11-18 15:33:48 +01007025 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7026 return -EINVAL;
7027
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307028 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7029
Johannes Berg683b6d32012-11-08 21:25:48 +01007030 err = nl80211_parse_chandef(rdev, info, &chandef);
7031 if (err)
7032 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007033
Johannes Berge247bd902011-11-04 11:18:21 +01007034 if (!dont_wait_for_ack) {
7035 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7036 if (!msg)
7037 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007038
Eric W. Biederman15e47302012-09-07 20:12:54 +00007039 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007040 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007041
Johannes Berge247bd902011-11-04 11:18:21 +01007042 if (IS_ERR(hdr)) {
7043 err = PTR_ERR(hdr);
7044 goto free_msg;
7045 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007046 }
Johannes Berge247bd902011-11-04 11:18:21 +01007047
Johannes Berg683b6d32012-11-08 21:25:48 +01007048 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007049 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7050 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007051 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007052 if (err)
7053 goto free_msg;
7054
Johannes Berge247bd902011-11-04 11:18:21 +01007055 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007056 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7057 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007058
Johannes Berge247bd902011-11-04 11:18:21 +01007059 genlmsg_end(msg, hdr);
7060 return genlmsg_reply(msg, info);
7061 }
7062
7063 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007064
7065 nla_put_failure:
7066 err = -ENOBUFS;
7067 free_msg:
7068 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007069 return err;
7070}
7071
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007072static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7073{
7074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007075 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007076 u64 cookie;
7077
7078 if (!info->attrs[NL80211_ATTR_COOKIE])
7079 return -EINVAL;
7080
7081 if (!rdev->ops->mgmt_tx_cancel_wait)
7082 return -EOPNOTSUPP;
7083
Johannes Berg71bbc992012-06-15 15:30:18 +02007084 switch (wdev->iftype) {
7085 case NL80211_IFTYPE_STATION:
7086 case NL80211_IFTYPE_ADHOC:
7087 case NL80211_IFTYPE_P2P_CLIENT:
7088 case NL80211_IFTYPE_AP:
7089 case NL80211_IFTYPE_AP_VLAN:
7090 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007091 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007092 break;
7093 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007094 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007095 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007096
7097 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7098
Hila Gonene35e4d22012-06-27 17:19:42 +03007099 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007100}
7101
Kalle Valoffb9eb32010-02-17 17:58:10 +02007102static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7103{
Johannes Berg4c476992010-10-04 21:36:35 +02007104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007105 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007106 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007107 u8 ps_state;
7108 bool state;
7109 int err;
7110
Johannes Berg4c476992010-10-04 21:36:35 +02007111 if (!info->attrs[NL80211_ATTR_PS_STATE])
7112 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007113
7114 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7115
Johannes Berg4c476992010-10-04 21:36:35 +02007116 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7117 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007118
7119 wdev = dev->ieee80211_ptr;
7120
Johannes Berg4c476992010-10-04 21:36:35 +02007121 if (!rdev->ops->set_power_mgmt)
7122 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007123
7124 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7125
7126 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007127 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007128
Hila Gonene35e4d22012-06-27 17:19:42 +03007129 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007130 if (!err)
7131 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007132 return err;
7133}
7134
7135static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7136{
Johannes Berg4c476992010-10-04 21:36:35 +02007137 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007138 enum nl80211_ps_state ps_state;
7139 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007140 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007141 struct sk_buff *msg;
7142 void *hdr;
7143 int err;
7144
Kalle Valoffb9eb32010-02-17 17:58:10 +02007145 wdev = dev->ieee80211_ptr;
7146
Johannes Berg4c476992010-10-04 21:36:35 +02007147 if (!rdev->ops->set_power_mgmt)
7148 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007149
7150 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007151 if (!msg)
7152 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007153
Eric W. Biederman15e47302012-09-07 20:12:54 +00007154 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007155 NL80211_CMD_GET_POWER_SAVE);
7156 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007157 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007158 goto free_msg;
7159 }
7160
7161 if (wdev->ps)
7162 ps_state = NL80211_PS_ENABLED;
7163 else
7164 ps_state = NL80211_PS_DISABLED;
7165
David S. Miller9360ffd2012-03-29 04:41:26 -04007166 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7167 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007168
7169 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007170 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007171
Johannes Berg4c476992010-10-04 21:36:35 +02007172 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007173 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007174 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007175 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007176 return err;
7177}
7178
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007179static struct nla_policy
7180nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7181 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7182 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7183 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007184 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7185 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7186 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007187};
7188
Thomas Pedersen84f10702012-07-12 16:17:33 -07007189static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007190 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007191{
7192 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7193 struct wireless_dev *wdev;
7194 struct net_device *dev = info->user_ptr[1];
7195
Johannes Bergd9d8b012012-11-26 12:51:52 +01007196 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007197 return -EINVAL;
7198
7199 wdev = dev->ieee80211_ptr;
7200
7201 if (!rdev->ops->set_cqm_txe_config)
7202 return -EOPNOTSUPP;
7203
7204 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7205 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7206 return -EOPNOTSUPP;
7207
Hila Gonene35e4d22012-06-27 17:19:42 +03007208 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007209}
7210
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007211static int nl80211_set_cqm_rssi(struct genl_info *info,
7212 s32 threshold, u32 hysteresis)
7213{
Johannes Berg4c476992010-10-04 21:36:35 +02007214 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007215 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007216 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007217
7218 if (threshold > 0)
7219 return -EINVAL;
7220
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007221 wdev = dev->ieee80211_ptr;
7222
Johannes Berg4c476992010-10-04 21:36:35 +02007223 if (!rdev->ops->set_cqm_rssi_config)
7224 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007225
Johannes Berg074ac8d2010-09-16 14:58:22 +02007226 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007227 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7228 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007229
Hila Gonene35e4d22012-06-27 17:19:42 +03007230 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007231}
7232
7233static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7234{
7235 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7236 struct nlattr *cqm;
7237 int err;
7238
7239 cqm = info->attrs[NL80211_ATTR_CQM];
7240 if (!cqm) {
7241 err = -EINVAL;
7242 goto out;
7243 }
7244
7245 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7246 nl80211_attr_cqm_policy);
7247 if (err)
7248 goto out;
7249
7250 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7251 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7252 s32 threshold;
7253 u32 hysteresis;
7254 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7255 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7256 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007257 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7258 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7259 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7260 u32 rate, pkts, intvl;
7261 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7262 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7263 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7264 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007265 } else
7266 err = -EINVAL;
7267
7268out:
7269 return err;
7270}
7271
Johannes Berg29cbe682010-12-03 09:20:44 +01007272static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7273{
7274 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7275 struct net_device *dev = info->user_ptr[1];
7276 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007277 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007278 int err;
7279
7280 /* start with default */
7281 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007282 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007283
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007284 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007285 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007286 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007287 if (err)
7288 return err;
7289 }
7290
7291 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7292 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7293 return -EINVAL;
7294
Javier Cardonac80d5452010-12-16 17:37:49 -08007295 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7296 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7297
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007298 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7299 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7300 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7301 return -EINVAL;
7302
Marco Porsch9bdbf042013-01-07 16:04:51 +01007303 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7304 setup.beacon_interval =
7305 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7306 if (setup.beacon_interval < 10 ||
7307 setup.beacon_interval > 10000)
7308 return -EINVAL;
7309 }
7310
7311 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7312 setup.dtim_period =
7313 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7314 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7315 return -EINVAL;
7316 }
7317
Javier Cardonac80d5452010-12-16 17:37:49 -08007318 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7319 /* parse additional setup parameters if given */
7320 err = nl80211_parse_mesh_setup(info, &setup);
7321 if (err)
7322 return err;
7323 }
7324
Johannes Bergcc1d2802012-05-16 23:50:20 +02007325 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007326 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7327 if (err)
7328 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007329 } else {
7330 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007331 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007332 }
7333
Javier Cardonac80d5452010-12-16 17:37:49 -08007334 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007335}
7336
7337static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7338{
7339 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7340 struct net_device *dev = info->user_ptr[1];
7341
7342 return cfg80211_leave_mesh(rdev, dev);
7343}
7344
Johannes Bergdfb89c52012-06-27 09:23:48 +02007345#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007346static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7347 struct cfg80211_registered_device *rdev)
7348{
7349 struct nlattr *nl_pats, *nl_pat;
7350 int i, pat_len;
7351
7352 if (!rdev->wowlan->n_patterns)
7353 return 0;
7354
7355 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7356 if (!nl_pats)
7357 return -ENOBUFS;
7358
7359 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7360 nl_pat = nla_nest_start(msg, i + 1);
7361 if (!nl_pat)
7362 return -ENOBUFS;
7363 pat_len = rdev->wowlan->patterns[i].pattern_len;
7364 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7365 DIV_ROUND_UP(pat_len, 8),
7366 rdev->wowlan->patterns[i].mask) ||
7367 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7368 pat_len, rdev->wowlan->patterns[i].pattern) ||
7369 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7370 rdev->wowlan->patterns[i].pkt_offset))
7371 return -ENOBUFS;
7372 nla_nest_end(msg, nl_pat);
7373 }
7374 nla_nest_end(msg, nl_pats);
7375
7376 return 0;
7377}
7378
Johannes Berg2a0e0472013-01-23 22:57:40 +01007379static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7380 struct cfg80211_wowlan_tcp *tcp)
7381{
7382 struct nlattr *nl_tcp;
7383
7384 if (!tcp)
7385 return 0;
7386
7387 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7388 if (!nl_tcp)
7389 return -ENOBUFS;
7390
7391 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7392 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7393 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7394 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7395 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7396 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7397 tcp->payload_len, tcp->payload) ||
7398 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7399 tcp->data_interval) ||
7400 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7401 tcp->wake_len, tcp->wake_data) ||
7402 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7403 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7404 return -ENOBUFS;
7405
7406 if (tcp->payload_seq.len &&
7407 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7408 sizeof(tcp->payload_seq), &tcp->payload_seq))
7409 return -ENOBUFS;
7410
7411 if (tcp->payload_tok.len &&
7412 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7413 sizeof(tcp->payload_tok) + tcp->tokens_size,
7414 &tcp->payload_tok))
7415 return -ENOBUFS;
7416
7417 return 0;
7418}
7419
Johannes Bergff1b6e62011-05-04 15:37:28 +02007420static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7421{
7422 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7423 struct sk_buff *msg;
7424 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007425 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007426
Johannes Berg2a0e0472013-01-23 22:57:40 +01007427 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7428 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007429 return -EOPNOTSUPP;
7430
Johannes Berg2a0e0472013-01-23 22:57:40 +01007431 if (rdev->wowlan && rdev->wowlan->tcp) {
7432 /* adjust size to have room for all the data */
7433 size += rdev->wowlan->tcp->tokens_size +
7434 rdev->wowlan->tcp->payload_len +
7435 rdev->wowlan->tcp->wake_len +
7436 rdev->wowlan->tcp->wake_len / 8;
7437 }
7438
7439 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007440 if (!msg)
7441 return -ENOMEM;
7442
Eric W. Biederman15e47302012-09-07 20:12:54 +00007443 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007444 NL80211_CMD_GET_WOWLAN);
7445 if (!hdr)
7446 goto nla_put_failure;
7447
7448 if (rdev->wowlan) {
7449 struct nlattr *nl_wowlan;
7450
7451 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7452 if (!nl_wowlan)
7453 goto nla_put_failure;
7454
David S. Miller9360ffd2012-03-29 04:41:26 -04007455 if ((rdev->wowlan->any &&
7456 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7457 (rdev->wowlan->disconnect &&
7458 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7459 (rdev->wowlan->magic_pkt &&
7460 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7461 (rdev->wowlan->gtk_rekey_failure &&
7462 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7463 (rdev->wowlan->eap_identity_req &&
7464 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7465 (rdev->wowlan->four_way_handshake &&
7466 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7467 (rdev->wowlan->rfkill_release &&
7468 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7469 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007470
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007471 if (nl80211_send_wowlan_patterns(msg, rdev))
7472 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007473
7474 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7475 goto nla_put_failure;
7476
Johannes Bergff1b6e62011-05-04 15:37:28 +02007477 nla_nest_end(msg, nl_wowlan);
7478 }
7479
7480 genlmsg_end(msg, hdr);
7481 return genlmsg_reply(msg, info);
7482
7483nla_put_failure:
7484 nlmsg_free(msg);
7485 return -ENOBUFS;
7486}
7487
Johannes Berg2a0e0472013-01-23 22:57:40 +01007488static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7489 struct nlattr *attr,
7490 struct cfg80211_wowlan *trig)
7491{
7492 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7493 struct cfg80211_wowlan_tcp *cfg;
7494 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7495 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7496 u32 size;
7497 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7498 int err, port;
7499
7500 if (!rdev->wiphy.wowlan.tcp)
7501 return -EINVAL;
7502
7503 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7504 nla_data(attr), nla_len(attr),
7505 nl80211_wowlan_tcp_policy);
7506 if (err)
7507 return err;
7508
7509 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7510 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7511 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7512 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7513 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7514 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7515 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7516 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7517 return -EINVAL;
7518
7519 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7520 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7521 return -EINVAL;
7522
7523 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7524 rdev->wiphy.wowlan.tcp->data_interval_max)
7525 return -EINVAL;
7526
7527 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7528 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7529 return -EINVAL;
7530
7531 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7532 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7533 return -EINVAL;
7534
7535 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7536 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7537
7538 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7539 tokens_size = tokln - sizeof(*tok);
7540
7541 if (!tok->len || tokens_size % tok->len)
7542 return -EINVAL;
7543 if (!rdev->wiphy.wowlan.tcp->tok)
7544 return -EINVAL;
7545 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7546 return -EINVAL;
7547 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7548 return -EINVAL;
7549 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7550 return -EINVAL;
7551 if (tok->offset + tok->len > data_size)
7552 return -EINVAL;
7553 }
7554
7555 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7556 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7557 if (!rdev->wiphy.wowlan.tcp->seq)
7558 return -EINVAL;
7559 if (seq->len == 0 || seq->len > 4)
7560 return -EINVAL;
7561 if (seq->len + seq->offset > data_size)
7562 return -EINVAL;
7563 }
7564
7565 size = sizeof(*cfg);
7566 size += data_size;
7567 size += wake_size + wake_mask_size;
7568 size += tokens_size;
7569
7570 cfg = kzalloc(size, GFP_KERNEL);
7571 if (!cfg)
7572 return -ENOMEM;
7573 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7574 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7575 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7576 ETH_ALEN);
7577 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7578 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7579 else
7580 port = 0;
7581#ifdef CONFIG_INET
7582 /* allocate a socket and port for it and use it */
7583 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7584 IPPROTO_TCP, &cfg->sock, 1);
7585 if (err) {
7586 kfree(cfg);
7587 return err;
7588 }
7589 if (inet_csk_get_port(cfg->sock->sk, port)) {
7590 sock_release(cfg->sock);
7591 kfree(cfg);
7592 return -EADDRINUSE;
7593 }
7594 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7595#else
7596 if (!port) {
7597 kfree(cfg);
7598 return -EINVAL;
7599 }
7600 cfg->src_port = port;
7601#endif
7602
7603 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7604 cfg->payload_len = data_size;
7605 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7606 memcpy((void *)cfg->payload,
7607 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7608 data_size);
7609 if (seq)
7610 cfg->payload_seq = *seq;
7611 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7612 cfg->wake_len = wake_size;
7613 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7614 memcpy((void *)cfg->wake_data,
7615 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7616 wake_size);
7617 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7618 data_size + wake_size;
7619 memcpy((void *)cfg->wake_mask,
7620 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7621 wake_mask_size);
7622 if (tok) {
7623 cfg->tokens_size = tokens_size;
7624 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7625 }
7626
7627 trig->tcp = cfg;
7628
7629 return 0;
7630}
7631
Johannes Bergff1b6e62011-05-04 15:37:28 +02007632static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7633{
7634 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7635 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007636 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007637 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007638 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7639 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007640 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007641
Johannes Berg2a0e0472013-01-23 22:57:40 +01007642 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7643 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007644 return -EOPNOTSUPP;
7645
Johannes Bergae33bd82012-07-12 16:25:02 +02007646 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7647 cfg80211_rdev_free_wowlan(rdev);
7648 rdev->wowlan = NULL;
7649 goto set_wakeup;
7650 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007651
7652 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7653 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7654 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7655 nl80211_wowlan_policy);
7656 if (err)
7657 return err;
7658
7659 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7660 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7661 return -EINVAL;
7662 new_triggers.any = true;
7663 }
7664
7665 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7666 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7667 return -EINVAL;
7668 new_triggers.disconnect = true;
7669 }
7670
7671 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7672 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7673 return -EINVAL;
7674 new_triggers.magic_pkt = true;
7675 }
7676
Johannes Berg77dbbb12011-07-13 10:48:55 +02007677 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7678 return -EINVAL;
7679
7680 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7681 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7682 return -EINVAL;
7683 new_triggers.gtk_rekey_failure = true;
7684 }
7685
7686 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7687 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7688 return -EINVAL;
7689 new_triggers.eap_identity_req = true;
7690 }
7691
7692 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7693 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7694 return -EINVAL;
7695 new_triggers.four_way_handshake = true;
7696 }
7697
7698 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7699 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7700 return -EINVAL;
7701 new_triggers.rfkill_release = true;
7702 }
7703
Johannes Bergff1b6e62011-05-04 15:37:28 +02007704 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7705 struct nlattr *pat;
7706 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007707 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007708 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7709
7710 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7711 rem)
7712 n_patterns++;
7713 if (n_patterns > wowlan->n_patterns)
7714 return -EINVAL;
7715
7716 new_triggers.patterns = kcalloc(n_patterns,
7717 sizeof(new_triggers.patterns[0]),
7718 GFP_KERNEL);
7719 if (!new_triggers.patterns)
7720 return -ENOMEM;
7721
7722 new_triggers.n_patterns = n_patterns;
7723 i = 0;
7724
7725 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7726 rem) {
7727 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7728 nla_data(pat), nla_len(pat), NULL);
7729 err = -EINVAL;
7730 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7731 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7732 goto error;
7733 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7734 mask_len = DIV_ROUND_UP(pat_len, 8);
7735 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7736 mask_len)
7737 goto error;
7738 if (pat_len > wowlan->pattern_max_len ||
7739 pat_len < wowlan->pattern_min_len)
7740 goto error;
7741
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007742 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7743 pkt_offset = 0;
7744 else
7745 pkt_offset = nla_get_u32(
7746 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7747 if (pkt_offset > wowlan->max_pkt_offset)
7748 goto error;
7749 new_triggers.patterns[i].pkt_offset = pkt_offset;
7750
Johannes Bergff1b6e62011-05-04 15:37:28 +02007751 new_triggers.patterns[i].mask =
7752 kmalloc(mask_len + pat_len, GFP_KERNEL);
7753 if (!new_triggers.patterns[i].mask) {
7754 err = -ENOMEM;
7755 goto error;
7756 }
7757 new_triggers.patterns[i].pattern =
7758 new_triggers.patterns[i].mask + mask_len;
7759 memcpy(new_triggers.patterns[i].mask,
7760 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7761 mask_len);
7762 new_triggers.patterns[i].pattern_len = pat_len;
7763 memcpy(new_triggers.patterns[i].pattern,
7764 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7765 pat_len);
7766 i++;
7767 }
7768 }
7769
Johannes Berg2a0e0472013-01-23 22:57:40 +01007770 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7771 err = nl80211_parse_wowlan_tcp(
7772 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7773 &new_triggers);
7774 if (err)
7775 goto error;
7776 }
7777
Johannes Bergae33bd82012-07-12 16:25:02 +02007778 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7779 if (!ntrig) {
7780 err = -ENOMEM;
7781 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007782 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007783 cfg80211_rdev_free_wowlan(rdev);
7784 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007785
Johannes Bergae33bd82012-07-12 16:25:02 +02007786 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007787 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007788 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007789
Johannes Bergff1b6e62011-05-04 15:37:28 +02007790 return 0;
7791 error:
7792 for (i = 0; i < new_triggers.n_patterns; i++)
7793 kfree(new_triggers.patterns[i].mask);
7794 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007795 if (new_triggers.tcp && new_triggers.tcp->sock)
7796 sock_release(new_triggers.tcp->sock);
7797 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007798 return err;
7799}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007800#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007801
Johannes Berge5497d72011-07-05 16:35:40 +02007802static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7803{
7804 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7805 struct net_device *dev = info->user_ptr[1];
7806 struct wireless_dev *wdev = dev->ieee80211_ptr;
7807 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7808 struct cfg80211_gtk_rekey_data rekey_data;
7809 int err;
7810
7811 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7812 return -EINVAL;
7813
7814 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7815 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7816 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7817 nl80211_rekey_policy);
7818 if (err)
7819 return err;
7820
7821 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7822 return -ERANGE;
7823 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7824 return -ERANGE;
7825 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7826 return -ERANGE;
7827
7828 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7829 NL80211_KEK_LEN);
7830 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7831 NL80211_KCK_LEN);
7832 memcpy(rekey_data.replay_ctr,
7833 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7834 NL80211_REPLAY_CTR_LEN);
7835
7836 wdev_lock(wdev);
7837 if (!wdev->current_bss) {
7838 err = -ENOTCONN;
7839 goto out;
7840 }
7841
7842 if (!rdev->ops->set_rekey_data) {
7843 err = -EOPNOTSUPP;
7844 goto out;
7845 }
7846
Hila Gonene35e4d22012-06-27 17:19:42 +03007847 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007848 out:
7849 wdev_unlock(wdev);
7850 return err;
7851}
7852
Johannes Berg28946da2011-11-04 11:18:12 +01007853static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7854 struct genl_info *info)
7855{
7856 struct net_device *dev = info->user_ptr[1];
7857 struct wireless_dev *wdev = dev->ieee80211_ptr;
7858
7859 if (wdev->iftype != NL80211_IFTYPE_AP &&
7860 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7861 return -EINVAL;
7862
Eric W. Biederman15e47302012-09-07 20:12:54 +00007863 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007864 return -EBUSY;
7865
Eric W. Biederman15e47302012-09-07 20:12:54 +00007866 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007867 return 0;
7868}
7869
Johannes Berg7f6cf312011-11-04 11:18:15 +01007870static int nl80211_probe_client(struct sk_buff *skb,
7871 struct genl_info *info)
7872{
7873 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7874 struct net_device *dev = info->user_ptr[1];
7875 struct wireless_dev *wdev = dev->ieee80211_ptr;
7876 struct sk_buff *msg;
7877 void *hdr;
7878 const u8 *addr;
7879 u64 cookie;
7880 int err;
7881
7882 if (wdev->iftype != NL80211_IFTYPE_AP &&
7883 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7884 return -EOPNOTSUPP;
7885
7886 if (!info->attrs[NL80211_ATTR_MAC])
7887 return -EINVAL;
7888
7889 if (!rdev->ops->probe_client)
7890 return -EOPNOTSUPP;
7891
7892 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7893 if (!msg)
7894 return -ENOMEM;
7895
Eric W. Biederman15e47302012-09-07 20:12:54 +00007896 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007897 NL80211_CMD_PROBE_CLIENT);
7898
7899 if (IS_ERR(hdr)) {
7900 err = PTR_ERR(hdr);
7901 goto free_msg;
7902 }
7903
7904 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7905
Hila Gonene35e4d22012-06-27 17:19:42 +03007906 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007907 if (err)
7908 goto free_msg;
7909
David S. Miller9360ffd2012-03-29 04:41:26 -04007910 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7911 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007912
7913 genlmsg_end(msg, hdr);
7914
7915 return genlmsg_reply(msg, info);
7916
7917 nla_put_failure:
7918 err = -ENOBUFS;
7919 free_msg:
7920 nlmsg_free(msg);
7921 return err;
7922}
7923
Johannes Berg5e7602302011-11-04 11:18:17 +01007924static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7925{
7926 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007927 struct cfg80211_beacon_registration *reg, *nreg;
7928 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007929
7930 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7931 return -EOPNOTSUPP;
7932
Ben Greear37c73b52012-10-26 14:49:25 -07007933 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7934 if (!nreg)
7935 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01007936
Ben Greear37c73b52012-10-26 14:49:25 -07007937 /* First, check if already registered. */
7938 spin_lock_bh(&rdev->beacon_registrations_lock);
7939 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7940 if (reg->nlportid == info->snd_portid) {
7941 rv = -EALREADY;
7942 goto out_err;
7943 }
7944 }
7945 /* Add it to the list */
7946 nreg->nlportid = info->snd_portid;
7947 list_add(&nreg->list, &rdev->beacon_registrations);
7948
7949 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01007950
7951 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007952out_err:
7953 spin_unlock_bh(&rdev->beacon_registrations_lock);
7954 kfree(nreg);
7955 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01007956}
7957
Johannes Berg98104fde2012-06-16 00:19:54 +02007958static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7959{
7960 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7961 struct wireless_dev *wdev = info->user_ptr[1];
7962 int err;
7963
7964 if (!rdev->ops->start_p2p_device)
7965 return -EOPNOTSUPP;
7966
7967 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7968 return -EOPNOTSUPP;
7969
7970 if (wdev->p2p_started)
7971 return 0;
7972
7973 mutex_lock(&rdev->devlist_mtx);
7974 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7975 mutex_unlock(&rdev->devlist_mtx);
7976 if (err)
7977 return err;
7978
Johannes Bergeeb126e2012-10-23 15:16:50 +02007979 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007980 if (err)
7981 return err;
7982
7983 wdev->p2p_started = true;
7984 mutex_lock(&rdev->devlist_mtx);
7985 rdev->opencount++;
7986 mutex_unlock(&rdev->devlist_mtx);
7987
7988 return 0;
7989}
7990
7991static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7992{
7993 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7994 struct wireless_dev *wdev = info->user_ptr[1];
7995
7996 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7997 return -EOPNOTSUPP;
7998
7999 if (!rdev->ops->stop_p2p_device)
8000 return -EOPNOTSUPP;
8001
8002 if (!wdev->p2p_started)
8003 return 0;
8004
Johannes Bergeeb126e2012-10-23 15:16:50 +02008005 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008006 wdev->p2p_started = false;
8007
8008 mutex_lock(&rdev->devlist_mtx);
8009 rdev->opencount--;
8010 mutex_unlock(&rdev->devlist_mtx);
8011
8012 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
8013 rdev->scan_req->aborted = true;
8014 ___cfg80211_scan_done(rdev, true);
8015 }
8016
8017 return 0;
8018}
8019
Johannes Berg3713b4e2013-02-14 16:19:38 +01008020static int nl80211_get_protocol_features(struct sk_buff *skb,
8021 struct genl_info *info)
8022{
8023 void *hdr;
8024 struct sk_buff *msg;
8025
8026 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8027 if (!msg)
8028 return -ENOMEM;
8029
8030 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8031 NL80211_CMD_GET_PROTOCOL_FEATURES);
8032 if (!hdr)
8033 goto nla_put_failure;
8034
8035 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8036 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8037 goto nla_put_failure;
8038
8039 genlmsg_end(msg, hdr);
8040 return genlmsg_reply(msg, info);
8041
8042 nla_put_failure:
8043 kfree_skb(msg);
8044 return -ENOBUFS;
8045}
8046
Johannes Berg4c476992010-10-04 21:36:35 +02008047#define NL80211_FLAG_NEED_WIPHY 0x01
8048#define NL80211_FLAG_NEED_NETDEV 0x02
8049#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008050#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8051#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8052 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008053#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008054/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008055#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8056 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008057
8058static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8059 struct genl_info *info)
8060{
8061 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008062 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008063 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008064 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8065
8066 if (rtnl)
8067 rtnl_lock();
8068
8069 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008070 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008071 if (IS_ERR(rdev)) {
8072 if (rtnl)
8073 rtnl_unlock();
8074 return PTR_ERR(rdev);
8075 }
8076 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008077 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8078 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008079 mutex_lock(&cfg80211_mutex);
8080 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8081 info->attrs);
8082 if (IS_ERR(wdev)) {
8083 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02008084 if (rtnl)
8085 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008086 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008087 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008088
Johannes Berg89a54e42012-06-15 14:33:17 +02008089 dev = wdev->netdev;
8090 rdev = wiphy_to_dev(wdev->wiphy);
8091
Johannes Berg1bf614e2012-06-15 15:23:36 +02008092 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8093 if (!dev) {
8094 mutex_unlock(&cfg80211_mutex);
8095 if (rtnl)
8096 rtnl_unlock();
8097 return -EINVAL;
8098 }
8099
8100 info->user_ptr[1] = dev;
8101 } else {
8102 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008103 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008104
Johannes Berg1bf614e2012-06-15 15:23:36 +02008105 if (dev) {
8106 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8107 !netif_running(dev)) {
8108 mutex_unlock(&cfg80211_mutex);
8109 if (rtnl)
8110 rtnl_unlock();
8111 return -ENETDOWN;
8112 }
8113
8114 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008115 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8116 if (!wdev->p2p_started) {
8117 mutex_unlock(&cfg80211_mutex);
8118 if (rtnl)
8119 rtnl_unlock();
8120 return -ENETDOWN;
8121 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008122 }
8123
Johannes Berg89a54e42012-06-15 14:33:17 +02008124 cfg80211_lock_rdev(rdev);
8125
8126 mutex_unlock(&cfg80211_mutex);
8127
Johannes Berg4c476992010-10-04 21:36:35 +02008128 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008129 }
8130
8131 return 0;
8132}
8133
8134static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8135 struct genl_info *info)
8136{
8137 if (info->user_ptr[0])
8138 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02008139 if (info->user_ptr[1]) {
8140 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8141 struct wireless_dev *wdev = info->user_ptr[1];
8142
8143 if (wdev->netdev)
8144 dev_put(wdev->netdev);
8145 } else {
8146 dev_put(info->user_ptr[1]);
8147 }
8148 }
Johannes Berg4c476992010-10-04 21:36:35 +02008149 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8150 rtnl_unlock();
8151}
8152
Johannes Berg55682962007-09-20 13:09:35 -04008153static struct genl_ops nl80211_ops[] = {
8154 {
8155 .cmd = NL80211_CMD_GET_WIPHY,
8156 .doit = nl80211_get_wiphy,
8157 .dumpit = nl80211_dump_wiphy,
8158 .policy = nl80211_policy,
8159 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008160 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04008161 },
8162 {
8163 .cmd = NL80211_CMD_SET_WIPHY,
8164 .doit = nl80211_set_wiphy,
8165 .policy = nl80211_policy,
8166 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008167 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008168 },
8169 {
8170 .cmd = NL80211_CMD_GET_INTERFACE,
8171 .doit = nl80211_get_interface,
8172 .dumpit = nl80211_dump_interface,
8173 .policy = nl80211_policy,
8174 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02008175 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04008176 },
8177 {
8178 .cmd = NL80211_CMD_SET_INTERFACE,
8179 .doit = nl80211_set_interface,
8180 .policy = nl80211_policy,
8181 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008182 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8183 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008184 },
8185 {
8186 .cmd = NL80211_CMD_NEW_INTERFACE,
8187 .doit = nl80211_new_interface,
8188 .policy = nl80211_policy,
8189 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008190 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8191 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008192 },
8193 {
8194 .cmd = NL80211_CMD_DEL_INTERFACE,
8195 .doit = nl80211_del_interface,
8196 .policy = nl80211_policy,
8197 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008198 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008199 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008200 },
Johannes Berg41ade002007-12-19 02:03:29 +01008201 {
8202 .cmd = NL80211_CMD_GET_KEY,
8203 .doit = nl80211_get_key,
8204 .policy = nl80211_policy,
8205 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008206 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008207 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008208 },
8209 {
8210 .cmd = NL80211_CMD_SET_KEY,
8211 .doit = nl80211_set_key,
8212 .policy = nl80211_policy,
8213 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008214 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008215 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008216 },
8217 {
8218 .cmd = NL80211_CMD_NEW_KEY,
8219 .doit = nl80211_new_key,
8220 .policy = nl80211_policy,
8221 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008222 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008223 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008224 },
8225 {
8226 .cmd = NL80211_CMD_DEL_KEY,
8227 .doit = nl80211_del_key,
8228 .policy = nl80211_policy,
8229 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008230 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008231 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008232 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008233 {
8234 .cmd = NL80211_CMD_SET_BEACON,
8235 .policy = nl80211_policy,
8236 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008237 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008238 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008239 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008240 },
8241 {
Johannes Berg88600202012-02-13 15:17:18 +01008242 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008243 .policy = nl80211_policy,
8244 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008245 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008246 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008247 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008248 },
8249 {
Johannes Berg88600202012-02-13 15:17:18 +01008250 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008251 .policy = nl80211_policy,
8252 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008253 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008254 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008255 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008256 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008257 {
8258 .cmd = NL80211_CMD_GET_STATION,
8259 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008260 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008261 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008262 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8263 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008264 },
8265 {
8266 .cmd = NL80211_CMD_SET_STATION,
8267 .doit = nl80211_set_station,
8268 .policy = nl80211_policy,
8269 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008270 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008271 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008272 },
8273 {
8274 .cmd = NL80211_CMD_NEW_STATION,
8275 .doit = nl80211_new_station,
8276 .policy = nl80211_policy,
8277 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008278 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008279 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008280 },
8281 {
8282 .cmd = NL80211_CMD_DEL_STATION,
8283 .doit = nl80211_del_station,
8284 .policy = nl80211_policy,
8285 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008286 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008287 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008288 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008289 {
8290 .cmd = NL80211_CMD_GET_MPATH,
8291 .doit = nl80211_get_mpath,
8292 .dumpit = nl80211_dump_mpath,
8293 .policy = nl80211_policy,
8294 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008295 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008296 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008297 },
8298 {
8299 .cmd = NL80211_CMD_SET_MPATH,
8300 .doit = nl80211_set_mpath,
8301 .policy = nl80211_policy,
8302 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008303 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008304 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008305 },
8306 {
8307 .cmd = NL80211_CMD_NEW_MPATH,
8308 .doit = nl80211_new_mpath,
8309 .policy = nl80211_policy,
8310 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008311 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008312 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008313 },
8314 {
8315 .cmd = NL80211_CMD_DEL_MPATH,
8316 .doit = nl80211_del_mpath,
8317 .policy = nl80211_policy,
8318 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008319 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008320 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008321 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008322 {
8323 .cmd = NL80211_CMD_SET_BSS,
8324 .doit = nl80211_set_bss,
8325 .policy = nl80211_policy,
8326 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008327 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008328 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008329 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008330 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008331 .cmd = NL80211_CMD_GET_REG,
8332 .doit = nl80211_get_reg,
8333 .policy = nl80211_policy,
8334 /* can be retrieved by unprivileged users */
8335 },
8336 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008337 .cmd = NL80211_CMD_SET_REG,
8338 .doit = nl80211_set_reg,
8339 .policy = nl80211_policy,
8340 .flags = GENL_ADMIN_PERM,
8341 },
8342 {
8343 .cmd = NL80211_CMD_REQ_SET_REG,
8344 .doit = nl80211_req_set_reg,
8345 .policy = nl80211_policy,
8346 .flags = GENL_ADMIN_PERM,
8347 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008348 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008349 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8350 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008351 .policy = nl80211_policy,
8352 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008353 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008354 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008355 },
8356 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008357 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8358 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008359 .policy = nl80211_policy,
8360 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008361 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008362 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008363 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008364 {
Johannes Berg2a519312009-02-10 21:25:55 +01008365 .cmd = NL80211_CMD_TRIGGER_SCAN,
8366 .doit = nl80211_trigger_scan,
8367 .policy = nl80211_policy,
8368 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008369 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008370 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008371 },
8372 {
8373 .cmd = NL80211_CMD_GET_SCAN,
8374 .policy = nl80211_policy,
8375 .dumpit = nl80211_dump_scan,
8376 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008377 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008378 .cmd = NL80211_CMD_START_SCHED_SCAN,
8379 .doit = nl80211_start_sched_scan,
8380 .policy = nl80211_policy,
8381 .flags = GENL_ADMIN_PERM,
8382 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8383 NL80211_FLAG_NEED_RTNL,
8384 },
8385 {
8386 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8387 .doit = nl80211_stop_sched_scan,
8388 .policy = nl80211_policy,
8389 .flags = GENL_ADMIN_PERM,
8390 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8391 NL80211_FLAG_NEED_RTNL,
8392 },
8393 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008394 .cmd = NL80211_CMD_AUTHENTICATE,
8395 .doit = nl80211_authenticate,
8396 .policy = nl80211_policy,
8397 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008398 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008399 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008400 },
8401 {
8402 .cmd = NL80211_CMD_ASSOCIATE,
8403 .doit = nl80211_associate,
8404 .policy = nl80211_policy,
8405 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008406 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008407 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008408 },
8409 {
8410 .cmd = NL80211_CMD_DEAUTHENTICATE,
8411 .doit = nl80211_deauthenticate,
8412 .policy = nl80211_policy,
8413 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008414 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008415 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008416 },
8417 {
8418 .cmd = NL80211_CMD_DISASSOCIATE,
8419 .doit = nl80211_disassociate,
8420 .policy = nl80211_policy,
8421 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008422 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008423 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008424 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008425 {
8426 .cmd = NL80211_CMD_JOIN_IBSS,
8427 .doit = nl80211_join_ibss,
8428 .policy = nl80211_policy,
8429 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008430 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008431 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008432 },
8433 {
8434 .cmd = NL80211_CMD_LEAVE_IBSS,
8435 .doit = nl80211_leave_ibss,
8436 .policy = nl80211_policy,
8437 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008438 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008439 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008440 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008441#ifdef CONFIG_NL80211_TESTMODE
8442 {
8443 .cmd = NL80211_CMD_TESTMODE,
8444 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008445 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008446 .policy = nl80211_policy,
8447 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008448 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8449 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008450 },
8451#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008452 {
8453 .cmd = NL80211_CMD_CONNECT,
8454 .doit = nl80211_connect,
8455 .policy = nl80211_policy,
8456 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008457 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008458 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008459 },
8460 {
8461 .cmd = NL80211_CMD_DISCONNECT,
8462 .doit = nl80211_disconnect,
8463 .policy = nl80211_policy,
8464 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008465 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008466 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008467 },
Johannes Berg463d0182009-07-14 00:33:35 +02008468 {
8469 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8470 .doit = nl80211_wiphy_netns,
8471 .policy = nl80211_policy,
8472 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008473 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8474 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008475 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008476 {
8477 .cmd = NL80211_CMD_GET_SURVEY,
8478 .policy = nl80211_policy,
8479 .dumpit = nl80211_dump_survey,
8480 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008481 {
8482 .cmd = NL80211_CMD_SET_PMKSA,
8483 .doit = nl80211_setdel_pmksa,
8484 .policy = nl80211_policy,
8485 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008486 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008487 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008488 },
8489 {
8490 .cmd = NL80211_CMD_DEL_PMKSA,
8491 .doit = nl80211_setdel_pmksa,
8492 .policy = nl80211_policy,
8493 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008494 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008495 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008496 },
8497 {
8498 .cmd = NL80211_CMD_FLUSH_PMKSA,
8499 .doit = nl80211_flush_pmksa,
8500 .policy = nl80211_policy,
8501 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008502 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008503 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008504 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008505 {
8506 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8507 .doit = nl80211_remain_on_channel,
8508 .policy = nl80211_policy,
8509 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008510 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008511 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008512 },
8513 {
8514 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8515 .doit = nl80211_cancel_remain_on_channel,
8516 .policy = nl80211_policy,
8517 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008518 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008519 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008520 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008521 {
8522 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8523 .doit = nl80211_set_tx_bitrate_mask,
8524 .policy = nl80211_policy,
8525 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008526 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8527 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008528 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008529 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008530 .cmd = NL80211_CMD_REGISTER_FRAME,
8531 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008532 .policy = nl80211_policy,
8533 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008534 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008535 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008536 },
8537 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008538 .cmd = NL80211_CMD_FRAME,
8539 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008540 .policy = nl80211_policy,
8541 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008542 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008543 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008544 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008545 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008546 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8547 .doit = nl80211_tx_mgmt_cancel_wait,
8548 .policy = nl80211_policy,
8549 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008550 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008551 NL80211_FLAG_NEED_RTNL,
8552 },
8553 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008554 .cmd = NL80211_CMD_SET_POWER_SAVE,
8555 .doit = nl80211_set_power_save,
8556 .policy = nl80211_policy,
8557 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008558 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8559 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008560 },
8561 {
8562 .cmd = NL80211_CMD_GET_POWER_SAVE,
8563 .doit = nl80211_get_power_save,
8564 .policy = nl80211_policy,
8565 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008566 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8567 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008568 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008569 {
8570 .cmd = NL80211_CMD_SET_CQM,
8571 .doit = nl80211_set_cqm,
8572 .policy = nl80211_policy,
8573 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008574 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8575 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008576 },
Johannes Bergf444de02010-05-05 15:25:02 +02008577 {
8578 .cmd = NL80211_CMD_SET_CHANNEL,
8579 .doit = nl80211_set_channel,
8580 .policy = nl80211_policy,
8581 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008582 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8583 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008584 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008585 {
8586 .cmd = NL80211_CMD_SET_WDS_PEER,
8587 .doit = nl80211_set_wds_peer,
8588 .policy = nl80211_policy,
8589 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008590 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8591 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008592 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008593 {
8594 .cmd = NL80211_CMD_JOIN_MESH,
8595 .doit = nl80211_join_mesh,
8596 .policy = nl80211_policy,
8597 .flags = GENL_ADMIN_PERM,
8598 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8599 NL80211_FLAG_NEED_RTNL,
8600 },
8601 {
8602 .cmd = NL80211_CMD_LEAVE_MESH,
8603 .doit = nl80211_leave_mesh,
8604 .policy = nl80211_policy,
8605 .flags = GENL_ADMIN_PERM,
8606 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8607 NL80211_FLAG_NEED_RTNL,
8608 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008609#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008610 {
8611 .cmd = NL80211_CMD_GET_WOWLAN,
8612 .doit = nl80211_get_wowlan,
8613 .policy = nl80211_policy,
8614 /* can be retrieved by unprivileged users */
8615 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8616 NL80211_FLAG_NEED_RTNL,
8617 },
8618 {
8619 .cmd = NL80211_CMD_SET_WOWLAN,
8620 .doit = nl80211_set_wowlan,
8621 .policy = nl80211_policy,
8622 .flags = GENL_ADMIN_PERM,
8623 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8624 NL80211_FLAG_NEED_RTNL,
8625 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008626#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008627 {
8628 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8629 .doit = nl80211_set_rekey_data,
8630 .policy = nl80211_policy,
8631 .flags = GENL_ADMIN_PERM,
8632 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8633 NL80211_FLAG_NEED_RTNL,
8634 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008635 {
8636 .cmd = NL80211_CMD_TDLS_MGMT,
8637 .doit = nl80211_tdls_mgmt,
8638 .policy = nl80211_policy,
8639 .flags = GENL_ADMIN_PERM,
8640 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8641 NL80211_FLAG_NEED_RTNL,
8642 },
8643 {
8644 .cmd = NL80211_CMD_TDLS_OPER,
8645 .doit = nl80211_tdls_oper,
8646 .policy = nl80211_policy,
8647 .flags = GENL_ADMIN_PERM,
8648 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8649 NL80211_FLAG_NEED_RTNL,
8650 },
Johannes Berg28946da2011-11-04 11:18:12 +01008651 {
8652 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8653 .doit = nl80211_register_unexpected_frame,
8654 .policy = nl80211_policy,
8655 .flags = GENL_ADMIN_PERM,
8656 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8657 NL80211_FLAG_NEED_RTNL,
8658 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008659 {
8660 .cmd = NL80211_CMD_PROBE_CLIENT,
8661 .doit = nl80211_probe_client,
8662 .policy = nl80211_policy,
8663 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008664 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008665 NL80211_FLAG_NEED_RTNL,
8666 },
Johannes Berg5e7602302011-11-04 11:18:17 +01008667 {
8668 .cmd = NL80211_CMD_REGISTER_BEACONS,
8669 .doit = nl80211_register_beacons,
8670 .policy = nl80211_policy,
8671 .flags = GENL_ADMIN_PERM,
8672 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8673 NL80211_FLAG_NEED_RTNL,
8674 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008675 {
8676 .cmd = NL80211_CMD_SET_NOACK_MAP,
8677 .doit = nl80211_set_noack_map,
8678 .policy = nl80211_policy,
8679 .flags = GENL_ADMIN_PERM,
8680 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8681 NL80211_FLAG_NEED_RTNL,
8682 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008683 {
8684 .cmd = NL80211_CMD_START_P2P_DEVICE,
8685 .doit = nl80211_start_p2p_device,
8686 .policy = nl80211_policy,
8687 .flags = GENL_ADMIN_PERM,
8688 .internal_flags = NL80211_FLAG_NEED_WDEV |
8689 NL80211_FLAG_NEED_RTNL,
8690 },
8691 {
8692 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8693 .doit = nl80211_stop_p2p_device,
8694 .policy = nl80211_policy,
8695 .flags = GENL_ADMIN_PERM,
8696 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8697 NL80211_FLAG_NEED_RTNL,
8698 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008699 {
8700 .cmd = NL80211_CMD_SET_MCAST_RATE,
8701 .doit = nl80211_set_mcast_rate,
8702 .policy = nl80211_policy,
8703 .flags = GENL_ADMIN_PERM,
8704 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8705 NL80211_FLAG_NEED_RTNL,
8706 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308707 {
8708 .cmd = NL80211_CMD_SET_MAC_ACL,
8709 .doit = nl80211_set_mac_acl,
8710 .policy = nl80211_policy,
8711 .flags = GENL_ADMIN_PERM,
8712 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8713 NL80211_FLAG_NEED_RTNL,
8714 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008715 {
8716 .cmd = NL80211_CMD_RADAR_DETECT,
8717 .doit = nl80211_start_radar_detection,
8718 .policy = nl80211_policy,
8719 .flags = GENL_ADMIN_PERM,
8720 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8721 NL80211_FLAG_NEED_RTNL,
8722 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01008723 {
8724 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8725 .doit = nl80211_get_protocol_features,
8726 .policy = nl80211_policy,
8727 },
Johannes Berg55682962007-09-20 13:09:35 -04008728};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008729
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008730static struct genl_multicast_group nl80211_mlme_mcgrp = {
8731 .name = "mlme",
8732};
Johannes Berg55682962007-09-20 13:09:35 -04008733
8734/* multicast groups */
8735static struct genl_multicast_group nl80211_config_mcgrp = {
8736 .name = "config",
8737};
Johannes Berg2a519312009-02-10 21:25:55 +01008738static struct genl_multicast_group nl80211_scan_mcgrp = {
8739 .name = "scan",
8740};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008741static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8742 .name = "regulatory",
8743};
Johannes Berg55682962007-09-20 13:09:35 -04008744
8745/* notification functions */
8746
8747void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8748{
8749 struct sk_buff *msg;
8750
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008751 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008752 if (!msg)
8753 return;
8754
Johannes Berg3713b4e2013-02-14 16:19:38 +01008755 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
8756 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04008757 nlmsg_free(msg);
8758 return;
8759 }
8760
Johannes Berg463d0182009-07-14 00:33:35 +02008761 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8762 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008763}
8764
Johannes Berg362a4152009-05-24 16:43:15 +02008765static int nl80211_add_scan_req(struct sk_buff *msg,
8766 struct cfg80211_registered_device *rdev)
8767{
8768 struct cfg80211_scan_request *req = rdev->scan_req;
8769 struct nlattr *nest;
8770 int i;
8771
Johannes Berg667503dd2009-07-07 03:56:11 +02008772 ASSERT_RDEV_LOCK(rdev);
8773
Johannes Berg362a4152009-05-24 16:43:15 +02008774 if (WARN_ON(!req))
8775 return 0;
8776
8777 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8778 if (!nest)
8779 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008780 for (i = 0; i < req->n_ssids; i++) {
8781 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8782 goto nla_put_failure;
8783 }
Johannes Berg362a4152009-05-24 16:43:15 +02008784 nla_nest_end(msg, nest);
8785
8786 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8787 if (!nest)
8788 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008789 for (i = 0; i < req->n_channels; i++) {
8790 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8791 goto nla_put_failure;
8792 }
Johannes Berg362a4152009-05-24 16:43:15 +02008793 nla_nest_end(msg, nest);
8794
David S. Miller9360ffd2012-03-29 04:41:26 -04008795 if (req->ie &&
8796 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8797 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008798
Sam Lefflered4737712012-10-11 21:03:31 -07008799 if (req->flags)
8800 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8801
Johannes Berg362a4152009-05-24 16:43:15 +02008802 return 0;
8803 nla_put_failure:
8804 return -ENOBUFS;
8805}
8806
Johannes Berga538e2d2009-06-16 19:56:42 +02008807static int nl80211_send_scan_msg(struct sk_buff *msg,
8808 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008809 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008810 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008811 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008812{
8813 void *hdr;
8814
Eric W. Biederman15e47302012-09-07 20:12:54 +00008815 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008816 if (!hdr)
8817 return -1;
8818
David S. Miller9360ffd2012-03-29 04:41:26 -04008819 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008820 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8821 wdev->netdev->ifindex)) ||
8822 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008823 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008824
Johannes Berg362a4152009-05-24 16:43:15 +02008825 /* ignore errors and send incomplete event anyway */
8826 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008827
8828 return genlmsg_end(msg, hdr);
8829
8830 nla_put_failure:
8831 genlmsg_cancel(msg, hdr);
8832 return -EMSGSIZE;
8833}
8834
Luciano Coelho807f8a82011-05-11 17:09:35 +03008835static int
8836nl80211_send_sched_scan_msg(struct sk_buff *msg,
8837 struct cfg80211_registered_device *rdev,
8838 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008839 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008840{
8841 void *hdr;
8842
Eric W. Biederman15e47302012-09-07 20:12:54 +00008843 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008844 if (!hdr)
8845 return -1;
8846
David S. Miller9360ffd2012-03-29 04:41:26 -04008847 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8848 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8849 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008850
8851 return genlmsg_end(msg, hdr);
8852
8853 nla_put_failure:
8854 genlmsg_cancel(msg, hdr);
8855 return -EMSGSIZE;
8856}
8857
Johannes Berga538e2d2009-06-16 19:56:42 +02008858void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008859 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008860{
8861 struct sk_buff *msg;
8862
Thomas Graf58050fc2012-06-28 03:57:45 +00008863 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008864 if (!msg)
8865 return;
8866
Johannes Bergfd014282012-06-18 19:17:03 +02008867 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008868 NL80211_CMD_TRIGGER_SCAN) < 0) {
8869 nlmsg_free(msg);
8870 return;
8871 }
8872
Johannes Berg463d0182009-07-14 00:33:35 +02008873 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8874 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008875}
8876
Johannes Berg2a519312009-02-10 21:25:55 +01008877void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008878 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008879{
8880 struct sk_buff *msg;
8881
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008882 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008883 if (!msg)
8884 return;
8885
Johannes Bergfd014282012-06-18 19:17:03 +02008886 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008887 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008888 nlmsg_free(msg);
8889 return;
8890 }
8891
Johannes Berg463d0182009-07-14 00:33:35 +02008892 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8893 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008894}
8895
8896void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008897 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008898{
8899 struct sk_buff *msg;
8900
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008901 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008902 if (!msg)
8903 return;
8904
Johannes Bergfd014282012-06-18 19:17:03 +02008905 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008906 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008907 nlmsg_free(msg);
8908 return;
8909 }
8910
Johannes Berg463d0182009-07-14 00:33:35 +02008911 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8912 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008913}
8914
Luciano Coelho807f8a82011-05-11 17:09:35 +03008915void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8916 struct net_device *netdev)
8917{
8918 struct sk_buff *msg;
8919
8920 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8921 if (!msg)
8922 return;
8923
8924 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8925 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8926 nlmsg_free(msg);
8927 return;
8928 }
8929
8930 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8931 nl80211_scan_mcgrp.id, GFP_KERNEL);
8932}
8933
8934void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8935 struct net_device *netdev, u32 cmd)
8936{
8937 struct sk_buff *msg;
8938
Thomas Graf58050fc2012-06-28 03:57:45 +00008939 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008940 if (!msg)
8941 return;
8942
8943 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8944 nlmsg_free(msg);
8945 return;
8946 }
8947
8948 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8949 nl80211_scan_mcgrp.id, GFP_KERNEL);
8950}
8951
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008952/*
8953 * This can happen on global regulatory changes or device specific settings
8954 * based on custom world regulatory domains.
8955 */
8956void nl80211_send_reg_change_event(struct regulatory_request *request)
8957{
8958 struct sk_buff *msg;
8959 void *hdr;
8960
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008961 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008962 if (!msg)
8963 return;
8964
8965 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8966 if (!hdr) {
8967 nlmsg_free(msg);
8968 return;
8969 }
8970
8971 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008972 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8973 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008974
David S. Miller9360ffd2012-03-29 04:41:26 -04008975 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8976 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8977 NL80211_REGDOM_TYPE_WORLD))
8978 goto nla_put_failure;
8979 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8980 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8981 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8982 goto nla_put_failure;
8983 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8984 request->intersect) {
8985 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8986 NL80211_REGDOM_TYPE_INTERSECTION))
8987 goto nla_put_failure;
8988 } else {
8989 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8990 NL80211_REGDOM_TYPE_COUNTRY) ||
8991 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8992 request->alpha2))
8993 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008994 }
8995
Johannes Bergf4173762012-12-03 18:23:37 +01008996 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008997 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8998 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008999
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009000 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009001
Johannes Bergbc43b282009-07-25 10:54:13 +02009002 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009003 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009004 GFP_ATOMIC);
9005 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009006
9007 return;
9008
9009nla_put_failure:
9010 genlmsg_cancel(msg, hdr);
9011 nlmsg_free(msg);
9012}
9013
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009014static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9015 struct net_device *netdev,
9016 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009017 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009018{
9019 struct sk_buff *msg;
9020 void *hdr;
9021
Johannes Berge6d6e342009-07-01 21:26:47 +02009022 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009023 if (!msg)
9024 return;
9025
9026 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9027 if (!hdr) {
9028 nlmsg_free(msg);
9029 return;
9030 }
9031
David S. Miller9360ffd2012-03-29 04:41:26 -04009032 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9033 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9034 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9035 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009036
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009037 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009038
Johannes Berg463d0182009-07-14 00:33:35 +02009039 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9040 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009041 return;
9042
9043 nla_put_failure:
9044 genlmsg_cancel(msg, hdr);
9045 nlmsg_free(msg);
9046}
9047
9048void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009049 struct net_device *netdev, const u8 *buf,
9050 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009051{
9052 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009053 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009054}
9055
9056void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9057 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009058 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009059{
Johannes Berge6d6e342009-07-01 21:26:47 +02009060 nl80211_send_mlme_event(rdev, netdev, buf, len,
9061 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009062}
9063
Jouni Malinen53b46b82009-03-27 20:53:56 +02009064void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009065 struct net_device *netdev, const u8 *buf,
9066 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009067{
9068 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009069 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009070}
9071
Jouni Malinen53b46b82009-03-27 20:53:56 +02009072void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9073 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009074 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009075{
9076 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009077 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009078}
9079
Jouni Malinencf4e5942010-12-16 00:52:40 +02009080void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
9081 struct net_device *netdev, const u8 *buf,
9082 size_t len, gfp_t gfp)
9083{
9084 nl80211_send_mlme_event(rdev, netdev, buf, len,
9085 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
9086}
9087
9088void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
9089 struct net_device *netdev, const u8 *buf,
9090 size_t len, gfp_t gfp)
9091{
9092 nl80211_send_mlme_event(rdev, netdev, buf, len,
9093 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
9094}
9095
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009096static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9097 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009098 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009099{
9100 struct sk_buff *msg;
9101 void *hdr;
9102
Johannes Berge6d6e342009-07-01 21:26:47 +02009103 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009104 if (!msg)
9105 return;
9106
9107 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9108 if (!hdr) {
9109 nlmsg_free(msg);
9110 return;
9111 }
9112
David S. Miller9360ffd2012-03-29 04:41:26 -04009113 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9114 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9115 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9116 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9117 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009118
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009119 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009120
Johannes Berg463d0182009-07-14 00:33:35 +02009121 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9122 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009123 return;
9124
9125 nla_put_failure:
9126 genlmsg_cancel(msg, hdr);
9127 nlmsg_free(msg);
9128}
9129
9130void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009131 struct net_device *netdev, const u8 *addr,
9132 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009133{
9134 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009135 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009136}
9137
9138void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009139 struct net_device *netdev, const u8 *addr,
9140 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009141{
Johannes Berge6d6e342009-07-01 21:26:47 +02009142 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9143 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009144}
9145
Samuel Ortizb23aa672009-07-01 21:26:54 +02009146void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9147 struct net_device *netdev, const u8 *bssid,
9148 const u8 *req_ie, size_t req_ie_len,
9149 const u8 *resp_ie, size_t resp_ie_len,
9150 u16 status, gfp_t gfp)
9151{
9152 struct sk_buff *msg;
9153 void *hdr;
9154
Thomas Graf58050fc2012-06-28 03:57:45 +00009155 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009156 if (!msg)
9157 return;
9158
9159 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9160 if (!hdr) {
9161 nlmsg_free(msg);
9162 return;
9163 }
9164
David S. Miller9360ffd2012-03-29 04:41:26 -04009165 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9166 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9167 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9168 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9169 (req_ie &&
9170 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9171 (resp_ie &&
9172 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9173 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009174
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009175 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009176
Johannes Berg463d0182009-07-14 00:33:35 +02009177 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9178 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009179 return;
9180
9181 nla_put_failure:
9182 genlmsg_cancel(msg, hdr);
9183 nlmsg_free(msg);
9184
9185}
9186
9187void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9188 struct net_device *netdev, const u8 *bssid,
9189 const u8 *req_ie, size_t req_ie_len,
9190 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9191{
9192 struct sk_buff *msg;
9193 void *hdr;
9194
Thomas Graf58050fc2012-06-28 03:57:45 +00009195 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009196 if (!msg)
9197 return;
9198
9199 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9200 if (!hdr) {
9201 nlmsg_free(msg);
9202 return;
9203 }
9204
David S. Miller9360ffd2012-03-29 04:41:26 -04009205 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9206 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9207 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9208 (req_ie &&
9209 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9210 (resp_ie &&
9211 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9212 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009213
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009214 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009215
Johannes Berg463d0182009-07-14 00:33:35 +02009216 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9217 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009218 return;
9219
9220 nla_put_failure:
9221 genlmsg_cancel(msg, hdr);
9222 nlmsg_free(msg);
9223
9224}
9225
9226void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9227 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02009228 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009229{
9230 struct sk_buff *msg;
9231 void *hdr;
9232
Thomas Graf58050fc2012-06-28 03:57:45 +00009233 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009234 if (!msg)
9235 return;
9236
9237 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9238 if (!hdr) {
9239 nlmsg_free(msg);
9240 return;
9241 }
9242
David S. Miller9360ffd2012-03-29 04:41:26 -04009243 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9244 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9245 (from_ap && reason &&
9246 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9247 (from_ap &&
9248 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9249 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9250 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009251
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009252 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009253
Johannes Berg463d0182009-07-14 00:33:35 +02009254 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9255 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009256 return;
9257
9258 nla_put_failure:
9259 genlmsg_cancel(msg, hdr);
9260 nlmsg_free(msg);
9261
9262}
9263
Johannes Berg04a773a2009-04-19 21:24:32 +02009264void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9265 struct net_device *netdev, const u8 *bssid,
9266 gfp_t gfp)
9267{
9268 struct sk_buff *msg;
9269 void *hdr;
9270
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009271 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009272 if (!msg)
9273 return;
9274
9275 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9276 if (!hdr) {
9277 nlmsg_free(msg);
9278 return;
9279 }
9280
David S. Miller9360ffd2012-03-29 04:41:26 -04009281 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9282 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9283 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9284 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009285
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009286 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009287
Johannes Berg463d0182009-07-14 00:33:35 +02009288 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9289 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009290 return;
9291
9292 nla_put_failure:
9293 genlmsg_cancel(msg, hdr);
9294 nlmsg_free(msg);
9295}
9296
Javier Cardonac93b5e72011-04-07 15:08:34 -07009297void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
9298 struct net_device *netdev,
9299 const u8 *macaddr, const u8* ie, u8 ie_len,
9300 gfp_t gfp)
9301{
9302 struct sk_buff *msg;
9303 void *hdr;
9304
9305 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9306 if (!msg)
9307 return;
9308
9309 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9310 if (!hdr) {
9311 nlmsg_free(msg);
9312 return;
9313 }
9314
David S. Miller9360ffd2012-03-29 04:41:26 -04009315 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9316 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9317 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
9318 (ie_len && ie &&
9319 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9320 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009321
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009322 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009323
9324 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9325 nl80211_mlme_mcgrp.id, gfp);
9326 return;
9327
9328 nla_put_failure:
9329 genlmsg_cancel(msg, hdr);
9330 nlmsg_free(msg);
9331}
9332
Jouni Malinena3b8b052009-03-27 21:59:49 +02009333void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9334 struct net_device *netdev, const u8 *addr,
9335 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009336 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009337{
9338 struct sk_buff *msg;
9339 void *hdr;
9340
Johannes Berge6d6e342009-07-01 21:26:47 +02009341 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009342 if (!msg)
9343 return;
9344
9345 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9346 if (!hdr) {
9347 nlmsg_free(msg);
9348 return;
9349 }
9350
David S. Miller9360ffd2012-03-29 04:41:26 -04009351 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9352 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9353 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9354 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9355 (key_id != -1 &&
9356 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9357 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9358 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009359
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009360 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009361
Johannes Berg463d0182009-07-14 00:33:35 +02009362 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9363 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009364 return;
9365
9366 nla_put_failure:
9367 genlmsg_cancel(msg, hdr);
9368 nlmsg_free(msg);
9369}
9370
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009371void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9372 struct ieee80211_channel *channel_before,
9373 struct ieee80211_channel *channel_after)
9374{
9375 struct sk_buff *msg;
9376 void *hdr;
9377 struct nlattr *nl_freq;
9378
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009380 if (!msg)
9381 return;
9382
9383 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9384 if (!hdr) {
9385 nlmsg_free(msg);
9386 return;
9387 }
9388
9389 /*
9390 * Since we are applying the beacon hint to a wiphy we know its
9391 * wiphy_idx is valid
9392 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009393 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9394 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009395
9396 /* Before */
9397 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9398 if (!nl_freq)
9399 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009400 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009401 goto nla_put_failure;
9402 nla_nest_end(msg, nl_freq);
9403
9404 /* After */
9405 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9406 if (!nl_freq)
9407 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009408 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009409 goto nla_put_failure;
9410 nla_nest_end(msg, nl_freq);
9411
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009412 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009413
Johannes Berg463d0182009-07-14 00:33:35 +02009414 rcu_read_lock();
9415 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9416 GFP_ATOMIC);
9417 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009418
9419 return;
9420
9421nla_put_failure:
9422 genlmsg_cancel(msg, hdr);
9423 nlmsg_free(msg);
9424}
9425
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009426static void nl80211_send_remain_on_chan_event(
9427 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009428 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009429 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009430 unsigned int duration, gfp_t gfp)
9431{
9432 struct sk_buff *msg;
9433 void *hdr;
9434
9435 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9436 if (!msg)
9437 return;
9438
9439 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9440 if (!hdr) {
9441 nlmsg_free(msg);
9442 return;
9443 }
9444
David S. Miller9360ffd2012-03-29 04:41:26 -04009445 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009446 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9447 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009448 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009449 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009450 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9451 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009452 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9453 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009454
David S. Miller9360ffd2012-03-29 04:41:26 -04009455 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9456 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9457 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009458
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009459 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009460
9461 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9462 nl80211_mlme_mcgrp.id, gfp);
9463 return;
9464
9465 nla_put_failure:
9466 genlmsg_cancel(msg, hdr);
9467 nlmsg_free(msg);
9468}
9469
9470void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009471 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009472 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009473 unsigned int duration, gfp_t gfp)
9474{
9475 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009476 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009477 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009478}
9479
9480void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009481 struct cfg80211_registered_device *rdev,
9482 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009483 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009484{
9485 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009486 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009487}
9488
Johannes Berg98b62182009-12-23 13:15:44 +01009489void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9490 struct net_device *dev, const u8 *mac_addr,
9491 struct station_info *sinfo, gfp_t gfp)
9492{
9493 struct sk_buff *msg;
9494
Thomas Graf58050fc2012-06-28 03:57:45 +00009495 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009496 if (!msg)
9497 return;
9498
John W. Linville66266b32012-03-15 13:25:41 -04009499 if (nl80211_send_station(msg, 0, 0, 0,
9500 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009501 nlmsg_free(msg);
9502 return;
9503 }
9504
9505 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9506 nl80211_mlme_mcgrp.id, gfp);
9507}
9508
Jouni Malinenec15e682011-03-23 15:29:52 +02009509void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9510 struct net_device *dev, const u8 *mac_addr,
9511 gfp_t gfp)
9512{
9513 struct sk_buff *msg;
9514 void *hdr;
9515
Thomas Graf58050fc2012-06-28 03:57:45 +00009516 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009517 if (!msg)
9518 return;
9519
9520 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9521 if (!hdr) {
9522 nlmsg_free(msg);
9523 return;
9524 }
9525
David S. Miller9360ffd2012-03-29 04:41:26 -04009526 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9527 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9528 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009529
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009530 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009531
9532 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9533 nl80211_mlme_mcgrp.id, gfp);
9534 return;
9535
9536 nla_put_failure:
9537 genlmsg_cancel(msg, hdr);
9538 nlmsg_free(msg);
9539}
9540
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309541void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9542 struct net_device *dev, const u8 *mac_addr,
9543 enum nl80211_connect_failed_reason reason,
9544 gfp_t gfp)
9545{
9546 struct sk_buff *msg;
9547 void *hdr;
9548
9549 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9550 if (!msg)
9551 return;
9552
9553 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9554 if (!hdr) {
9555 nlmsg_free(msg);
9556 return;
9557 }
9558
9559 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9560 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9561 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9562 goto nla_put_failure;
9563
9564 genlmsg_end(msg, hdr);
9565
9566 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9567 nl80211_mlme_mcgrp.id, gfp);
9568 return;
9569
9570 nla_put_failure:
9571 genlmsg_cancel(msg, hdr);
9572 nlmsg_free(msg);
9573}
9574
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009575static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9576 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009577{
9578 struct wireless_dev *wdev = dev->ieee80211_ptr;
9579 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9580 struct sk_buff *msg;
9581 void *hdr;
9582 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009583 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009584
Eric W. Biederman15e47302012-09-07 20:12:54 +00009585 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009586 return false;
9587
9588 msg = nlmsg_new(100, gfp);
9589 if (!msg)
9590 return true;
9591
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009592 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009593 if (!hdr) {
9594 nlmsg_free(msg);
9595 return true;
9596 }
9597
David S. Miller9360ffd2012-03-29 04:41:26 -04009598 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9599 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9600 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9601 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009602
9603 err = genlmsg_end(msg, hdr);
9604 if (err < 0) {
9605 nlmsg_free(msg);
9606 return true;
9607 }
9608
Eric W. Biederman15e47302012-09-07 20:12:54 +00009609 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009610 return true;
9611
9612 nla_put_failure:
9613 genlmsg_cancel(msg, hdr);
9614 nlmsg_free(msg);
9615 return true;
9616}
9617
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009618bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9619{
9620 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9621 addr, gfp);
9622}
9623
9624bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9625 const u8 *addr, gfp_t gfp)
9626{
9627 return __nl80211_unexpected_frame(dev,
9628 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9629 addr, gfp);
9630}
9631
Johannes Berg2e161f72010-08-12 15:38:38 +02009632int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009633 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009634 int freq, int sig_dbm,
9635 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009636{
Johannes Berg71bbc992012-06-15 15:30:18 +02009637 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009638 struct sk_buff *msg;
9639 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009640
9641 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9642 if (!msg)
9643 return -ENOMEM;
9644
Johannes Berg2e161f72010-08-12 15:38:38 +02009645 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009646 if (!hdr) {
9647 nlmsg_free(msg);
9648 return -ENOMEM;
9649 }
9650
David S. Miller9360ffd2012-03-29 04:41:26 -04009651 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009652 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9653 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009654 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9655 (sig_dbm &&
9656 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9657 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9658 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009659
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009660 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009661
Eric W. Biederman15e47302012-09-07 20:12:54 +00009662 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009663
9664 nla_put_failure:
9665 genlmsg_cancel(msg, hdr);
9666 nlmsg_free(msg);
9667 return -ENOBUFS;
9668}
9669
Johannes Berg2e161f72010-08-12 15:38:38 +02009670void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009671 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009672 const u8 *buf, size_t len, bool ack,
9673 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009674{
Johannes Berg71bbc992012-06-15 15:30:18 +02009675 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009676 struct sk_buff *msg;
9677 void *hdr;
9678
9679 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9680 if (!msg)
9681 return;
9682
Johannes Berg2e161f72010-08-12 15:38:38 +02009683 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009684 if (!hdr) {
9685 nlmsg_free(msg);
9686 return;
9687 }
9688
David S. Miller9360ffd2012-03-29 04:41:26 -04009689 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009690 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9691 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009692 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9693 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9694 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9695 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009696
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009697 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009698
9699 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9700 return;
9701
9702 nla_put_failure:
9703 genlmsg_cancel(msg, hdr);
9704 nlmsg_free(msg);
9705}
9706
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009707void
9708nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9709 struct net_device *netdev,
9710 enum nl80211_cqm_rssi_threshold_event rssi_event,
9711 gfp_t gfp)
9712{
9713 struct sk_buff *msg;
9714 struct nlattr *pinfoattr;
9715 void *hdr;
9716
Thomas Graf58050fc2012-06-28 03:57:45 +00009717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009718 if (!msg)
9719 return;
9720
9721 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9722 if (!hdr) {
9723 nlmsg_free(msg);
9724 return;
9725 }
9726
David S. Miller9360ffd2012-03-29 04:41:26 -04009727 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9728 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9729 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009730
9731 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9732 if (!pinfoattr)
9733 goto nla_put_failure;
9734
David S. Miller9360ffd2012-03-29 04:41:26 -04009735 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9736 rssi_event))
9737 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009738
9739 nla_nest_end(msg, pinfoattr);
9740
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009741 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009742
9743 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9744 nl80211_mlme_mcgrp.id, gfp);
9745 return;
9746
9747 nla_put_failure:
9748 genlmsg_cancel(msg, hdr);
9749 nlmsg_free(msg);
9750}
9751
Johannes Berge5497d72011-07-05 16:35:40 +02009752void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9753 struct net_device *netdev, const u8 *bssid,
9754 const u8 *replay_ctr, gfp_t gfp)
9755{
9756 struct sk_buff *msg;
9757 struct nlattr *rekey_attr;
9758 void *hdr;
9759
Thomas Graf58050fc2012-06-28 03:57:45 +00009760 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009761 if (!msg)
9762 return;
9763
9764 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9765 if (!hdr) {
9766 nlmsg_free(msg);
9767 return;
9768 }
9769
David S. Miller9360ffd2012-03-29 04:41:26 -04009770 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9771 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9772 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9773 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009774
9775 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9776 if (!rekey_attr)
9777 goto nla_put_failure;
9778
David S. Miller9360ffd2012-03-29 04:41:26 -04009779 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9780 NL80211_REPLAY_CTR_LEN, replay_ctr))
9781 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009782
9783 nla_nest_end(msg, rekey_attr);
9784
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009785 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009786
9787 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9788 nl80211_mlme_mcgrp.id, gfp);
9789 return;
9790
9791 nla_put_failure:
9792 genlmsg_cancel(msg, hdr);
9793 nlmsg_free(msg);
9794}
9795
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009796void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9797 struct net_device *netdev, int index,
9798 const u8 *bssid, bool preauth, gfp_t gfp)
9799{
9800 struct sk_buff *msg;
9801 struct nlattr *attr;
9802 void *hdr;
9803
Thomas Graf58050fc2012-06-28 03:57:45 +00009804 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009805 if (!msg)
9806 return;
9807
9808 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9809 if (!hdr) {
9810 nlmsg_free(msg);
9811 return;
9812 }
9813
David S. Miller9360ffd2012-03-29 04:41:26 -04009814 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9815 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9816 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009817
9818 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9819 if (!attr)
9820 goto nla_put_failure;
9821
David S. Miller9360ffd2012-03-29 04:41:26 -04009822 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9823 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9824 (preauth &&
9825 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9826 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009827
9828 nla_nest_end(msg, attr);
9829
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009830 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009831
9832 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9833 nl80211_mlme_mcgrp.id, gfp);
9834 return;
9835
9836 nla_put_failure:
9837 genlmsg_cancel(msg, hdr);
9838 nlmsg_free(msg);
9839}
9840
Thomas Pedersen53145262012-04-06 13:35:47 -07009841void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009842 struct net_device *netdev,
9843 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009844{
9845 struct sk_buff *msg;
9846 void *hdr;
9847
Thomas Graf58050fc2012-06-28 03:57:45 +00009848 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009849 if (!msg)
9850 return;
9851
9852 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9853 if (!hdr) {
9854 nlmsg_free(msg);
9855 return;
9856 }
9857
Johannes Berg683b6d32012-11-08 21:25:48 +01009858 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9859 goto nla_put_failure;
9860
9861 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009862 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009863
9864 genlmsg_end(msg, hdr);
9865
9866 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9867 nl80211_mlme_mcgrp.id, gfp);
9868 return;
9869
9870 nla_put_failure:
9871 genlmsg_cancel(msg, hdr);
9872 nlmsg_free(msg);
9873}
9874
Johannes Bergc063dbf2010-11-24 08:10:05 +01009875void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009876nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9877 struct net_device *netdev, const u8 *peer,
9878 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9879{
9880 struct sk_buff *msg;
9881 struct nlattr *pinfoattr;
9882 void *hdr;
9883
9884 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9885 if (!msg)
9886 return;
9887
9888 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9889 if (!hdr) {
9890 nlmsg_free(msg);
9891 return;
9892 }
9893
9894 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9895 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9896 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9897 goto nla_put_failure;
9898
9899 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9900 if (!pinfoattr)
9901 goto nla_put_failure;
9902
9903 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9904 goto nla_put_failure;
9905
9906 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9907 goto nla_put_failure;
9908
9909 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9910 goto nla_put_failure;
9911
9912 nla_nest_end(msg, pinfoattr);
9913
9914 genlmsg_end(msg, hdr);
9915
9916 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9917 nl80211_mlme_mcgrp.id, gfp);
9918 return;
9919
9920 nla_put_failure:
9921 genlmsg_cancel(msg, hdr);
9922 nlmsg_free(msg);
9923}
9924
9925void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009926nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9927 struct cfg80211_chan_def *chandef,
9928 enum nl80211_radar_event event,
9929 struct net_device *netdev, gfp_t gfp)
9930{
9931 struct sk_buff *msg;
9932 void *hdr;
9933
9934 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9935 if (!msg)
9936 return;
9937
9938 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9939 if (!hdr) {
9940 nlmsg_free(msg);
9941 return;
9942 }
9943
9944 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9945 goto nla_put_failure;
9946
9947 /* NOP and radar events don't need a netdev parameter */
9948 if (netdev) {
9949 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9950
9951 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9952 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9953 goto nla_put_failure;
9954 }
9955
9956 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9957 goto nla_put_failure;
9958
9959 if (nl80211_send_chandef(msg, chandef))
9960 goto nla_put_failure;
9961
9962 if (genlmsg_end(msg, hdr) < 0) {
9963 nlmsg_free(msg);
9964 return;
9965 }
9966
9967 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9968 nl80211_mlme_mcgrp.id, gfp);
9969 return;
9970
9971 nla_put_failure:
9972 genlmsg_cancel(msg, hdr);
9973 nlmsg_free(msg);
9974}
9975
9976void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009977nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9978 struct net_device *netdev, const u8 *peer,
9979 u32 num_packets, gfp_t gfp)
9980{
9981 struct sk_buff *msg;
9982 struct nlattr *pinfoattr;
9983 void *hdr;
9984
Thomas Graf58050fc2012-06-28 03:57:45 +00009985 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009986 if (!msg)
9987 return;
9988
9989 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9990 if (!hdr) {
9991 nlmsg_free(msg);
9992 return;
9993 }
9994
David S. Miller9360ffd2012-03-29 04:41:26 -04009995 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9996 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9997 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9998 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009999
10000 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10001 if (!pinfoattr)
10002 goto nla_put_failure;
10003
David S. Miller9360ffd2012-03-29 04:41:26 -040010004 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10005 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010006
10007 nla_nest_end(msg, pinfoattr);
10008
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010009 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010010
10011 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10012 nl80211_mlme_mcgrp.id, gfp);
10013 return;
10014
10015 nla_put_failure:
10016 genlmsg_cancel(msg, hdr);
10017 nlmsg_free(msg);
10018}
10019
Johannes Berg7f6cf312011-11-04 11:18:15 +010010020void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10021 u64 cookie, bool acked, gfp_t gfp)
10022{
10023 struct wireless_dev *wdev = dev->ieee80211_ptr;
10024 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10025 struct sk_buff *msg;
10026 void *hdr;
10027 int err;
10028
Beni Lev4ee3e062012-08-27 12:49:39 +030010029 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10030
Thomas Graf58050fc2012-06-28 03:57:45 +000010031 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010032
Johannes Berg7f6cf312011-11-04 11:18:15 +010010033 if (!msg)
10034 return;
10035
10036 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10037 if (!hdr) {
10038 nlmsg_free(msg);
10039 return;
10040 }
10041
David S. Miller9360ffd2012-03-29 04:41:26 -040010042 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10043 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10044 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10045 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10046 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10047 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010048
10049 err = genlmsg_end(msg, hdr);
10050 if (err < 0) {
10051 nlmsg_free(msg);
10052 return;
10053 }
10054
10055 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10056 nl80211_mlme_mcgrp.id, gfp);
10057 return;
10058
10059 nla_put_failure:
10060 genlmsg_cancel(msg, hdr);
10061 nlmsg_free(msg);
10062}
10063EXPORT_SYMBOL(cfg80211_probe_status);
10064
Johannes Berg5e7602302011-11-04 11:18:17 +010010065void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10066 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010067 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010010068{
10069 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10070 struct sk_buff *msg;
10071 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010072 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010010073
Beni Lev4ee3e062012-08-27 12:49:39 +030010074 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10075
Ben Greear37c73b52012-10-26 14:49:25 -070010076 spin_lock_bh(&rdev->beacon_registrations_lock);
10077 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10078 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10079 if (!msg) {
10080 spin_unlock_bh(&rdev->beacon_registrations_lock);
10081 return;
10082 }
Johannes Berg5e7602302011-11-04 11:18:17 +010010083
Ben Greear37c73b52012-10-26 14:49:25 -070010084 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10085 if (!hdr)
10086 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010010087
Ben Greear37c73b52012-10-26 14:49:25 -070010088 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10089 (freq &&
10090 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10091 (sig_dbm &&
10092 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10093 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10094 goto nla_put_failure;
10095
10096 genlmsg_end(msg, hdr);
10097
10098 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010010099 }
Ben Greear37c73b52012-10-26 14:49:25 -070010100 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010101 return;
10102
10103 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010104 spin_unlock_bh(&rdev->beacon_registrations_lock);
10105 if (hdr)
10106 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010010107 nlmsg_free(msg);
10108}
10109EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10110
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010111#ifdef CONFIG_PM
10112void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10113 struct cfg80211_wowlan_wakeup *wakeup,
10114 gfp_t gfp)
10115{
10116 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10117 struct sk_buff *msg;
10118 void *hdr;
10119 int err, size = 200;
10120
10121 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10122
10123 if (wakeup)
10124 size += wakeup->packet_present_len;
10125
10126 msg = nlmsg_new(size, gfp);
10127 if (!msg)
10128 return;
10129
10130 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10131 if (!hdr)
10132 goto free_msg;
10133
10134 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10135 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10136 goto free_msg;
10137
10138 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10139 wdev->netdev->ifindex))
10140 goto free_msg;
10141
10142 if (wakeup) {
10143 struct nlattr *reasons;
10144
10145 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10146
10147 if (wakeup->disconnect &&
10148 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10149 goto free_msg;
10150 if (wakeup->magic_pkt &&
10151 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10152 goto free_msg;
10153 if (wakeup->gtk_rekey_failure &&
10154 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10155 goto free_msg;
10156 if (wakeup->eap_identity_req &&
10157 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10158 goto free_msg;
10159 if (wakeup->four_way_handshake &&
10160 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10161 goto free_msg;
10162 if (wakeup->rfkill_release &&
10163 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10164 goto free_msg;
10165
10166 if (wakeup->pattern_idx >= 0 &&
10167 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10168 wakeup->pattern_idx))
10169 goto free_msg;
10170
Johannes Berg2a0e0472013-01-23 22:57:40 +010010171 if (wakeup->tcp_match)
10172 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10173
10174 if (wakeup->tcp_connlost)
10175 nla_put_flag(msg,
10176 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10177
10178 if (wakeup->tcp_nomoretokens)
10179 nla_put_flag(msg,
10180 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10181
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010182 if (wakeup->packet) {
10183 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10184 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10185
10186 if (!wakeup->packet_80211) {
10187 pkt_attr =
10188 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10189 len_attr =
10190 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10191 }
10192
10193 if (wakeup->packet_len &&
10194 nla_put_u32(msg, len_attr, wakeup->packet_len))
10195 goto free_msg;
10196
10197 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10198 wakeup->packet))
10199 goto free_msg;
10200 }
10201
10202 nla_nest_end(msg, reasons);
10203 }
10204
10205 err = genlmsg_end(msg, hdr);
10206 if (err < 0)
10207 goto free_msg;
10208
10209 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10210 nl80211_mlme_mcgrp.id, gfp);
10211 return;
10212
10213 free_msg:
10214 nlmsg_free(msg);
10215}
10216EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10217#endif
10218
Jouni Malinen3475b092012-11-16 22:49:57 +020010219void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10220 enum nl80211_tdls_operation oper,
10221 u16 reason_code, gfp_t gfp)
10222{
10223 struct wireless_dev *wdev = dev->ieee80211_ptr;
10224 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10225 struct sk_buff *msg;
10226 void *hdr;
10227 int err;
10228
10229 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10230 reason_code);
10231
10232 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10233 if (!msg)
10234 return;
10235
10236 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10237 if (!hdr) {
10238 nlmsg_free(msg);
10239 return;
10240 }
10241
10242 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10243 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10244 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10245 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10246 (reason_code > 0 &&
10247 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10248 goto nla_put_failure;
10249
10250 err = genlmsg_end(msg, hdr);
10251 if (err < 0) {
10252 nlmsg_free(msg);
10253 return;
10254 }
10255
10256 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10257 nl80211_mlme_mcgrp.id, gfp);
10258 return;
10259
10260 nla_put_failure:
10261 genlmsg_cancel(msg, hdr);
10262 nlmsg_free(msg);
10263}
10264EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10265
Jouni Malinen026331c2010-02-15 12:53:10 +020010266static int nl80211_netlink_notify(struct notifier_block * nb,
10267 unsigned long state,
10268 void *_notify)
10269{
10270 struct netlink_notify *notify = _notify;
10271 struct cfg80211_registered_device *rdev;
10272 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010273 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010274
10275 if (state != NETLINK_URELEASE)
10276 return NOTIFY_DONE;
10277
10278 rcu_read_lock();
10279
Johannes Berg5e7602302011-11-04 11:18:17 +010010280 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010281 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010282 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010283
10284 spin_lock_bh(&rdev->beacon_registrations_lock);
10285 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10286 list) {
10287 if (reg->nlportid == notify->portid) {
10288 list_del(&reg->list);
10289 kfree(reg);
10290 break;
10291 }
10292 }
10293 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010294 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010295
10296 rcu_read_unlock();
10297
10298 return NOTIFY_DONE;
10299}
10300
10301static struct notifier_block nl80211_netlink_notifier = {
10302 .notifier_call = nl80211_netlink_notify,
10303};
10304
Johannes Berg55682962007-09-20 13:09:35 -040010305/* initialisation/exit functions */
10306
10307int nl80211_init(void)
10308{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010309 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010310
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010311 err = genl_register_family_with_ops(&nl80211_fam,
10312 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010313 if (err)
10314 return err;
10315
Johannes Berg55682962007-09-20 13:09:35 -040010316 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10317 if (err)
10318 goto err_out;
10319
Johannes Berg2a519312009-02-10 21:25:55 +010010320 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10321 if (err)
10322 goto err_out;
10323
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010324 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10325 if (err)
10326 goto err_out;
10327
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010328 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10329 if (err)
10330 goto err_out;
10331
Johannes Bergaff89a92009-07-01 21:26:51 +020010332#ifdef CONFIG_NL80211_TESTMODE
10333 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10334 if (err)
10335 goto err_out;
10336#endif
10337
Jouni Malinen026331c2010-02-15 12:53:10 +020010338 err = netlink_register_notifier(&nl80211_netlink_notifier);
10339 if (err)
10340 goto err_out;
10341
Johannes Berg55682962007-09-20 13:09:35 -040010342 return 0;
10343 err_out:
10344 genl_unregister_family(&nl80211_fam);
10345 return err;
10346}
10347
10348void nl80211_exit(void)
10349{
Jouni Malinen026331c2010-02-15 12:53:10 +020010350 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010351 genl_unregister_family(&nl80211_fam);
10352}