blob: be9f2b5a403f19c209dbeab6eb89d557d72384ac [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg55682962007-09-20 13:09:35 -0400373};
374
Johannes Berge31b8212010-10-05 19:39:30 +0200375/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000376static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200377 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200378 [NL80211_KEY_IDX] = { .type = NLA_U8 },
379 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200380 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200381 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
382 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200383 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100384 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
385};
386
387/* policy for the key default flags */
388static const struct nla_policy
389nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
390 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
391 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200392};
393
Johannes Bergff1b6e62011-05-04 15:37:28 +0200394/* policy for WoWLAN attributes */
395static const struct nla_policy
396nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
397 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
398 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
399 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
400 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200401 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
402 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
403 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
404 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100405 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
406};
407
408static const struct nla_policy
409nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
410 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
411 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
412 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
413 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
414 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
415 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
416 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
417 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
418 },
419 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
420 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
421 },
422 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
423 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200425};
426
Johannes Berge5497d72011-07-05 16:35:40 +0200427/* policy for GTK rekey offload attributes */
428static const struct nla_policy
429nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
430 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
431 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
432 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
433};
434
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300435static const struct nla_policy
436nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200437 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300438 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700439 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300440};
441
Holger Schuriga0438972009-11-11 11:30:02 +0100442/* ifidx get helper */
443static int nl80211_get_ifidx(struct netlink_callback *cb)
444{
445 int res;
446
447 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
448 nl80211_fam.attrbuf, nl80211_fam.maxattr,
449 nl80211_policy);
450 if (res)
451 return res;
452
453 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
454 return -EINVAL;
455
456 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
457 if (!res)
458 return -EINVAL;
459 return res;
460}
461
Johannes Berg67748892010-10-04 21:14:06 +0200462static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
463 struct netlink_callback *cb,
464 struct cfg80211_registered_device **rdev,
465 struct net_device **dev)
466{
467 int ifidx = cb->args[0];
468 int err;
469
470 if (!ifidx)
471 ifidx = nl80211_get_ifidx(cb);
472 if (ifidx < 0)
473 return ifidx;
474
475 cb->args[0] = ifidx;
476
477 rtnl_lock();
478
479 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
480 if (!*dev) {
481 err = -ENODEV;
482 goto out_rtnl;
483 }
484
485 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100486 if (IS_ERR(*rdev)) {
487 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200488 goto out_rtnl;
489 }
490
491 return 0;
492 out_rtnl:
493 rtnl_unlock();
494 return err;
495}
496
497static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
498{
499 cfg80211_unlock_rdev(rdev);
500 rtnl_unlock();
501}
502
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100503/* IE validation */
504static bool is_valid_ie_attr(const struct nlattr *attr)
505{
506 const u8 *pos;
507 int len;
508
509 if (!attr)
510 return true;
511
512 pos = nla_data(attr);
513 len = nla_len(attr);
514
515 while (len) {
516 u8 elemlen;
517
518 if (len < 2)
519 return false;
520 len -= 2;
521
522 elemlen = pos[1];
523 if (elemlen > len)
524 return false;
525
526 len -= elemlen;
527 pos += 2 + elemlen;
528 }
529
530 return true;
531}
532
Johannes Berg55682962007-09-20 13:09:35 -0400533/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000534static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400535 int flags, u8 cmd)
536{
537 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000538 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400539}
540
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400541static int nl80211_msg_put_channel(struct sk_buff *msg,
542 struct ieee80211_channel *chan)
543{
David S. Miller9360ffd2012-03-29 04:41:26 -0400544 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
545 chan->center_freq))
546 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400547
David S. Miller9360ffd2012-03-29 04:41:26 -0400548 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
549 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
550 goto nla_put_failure;
551 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
552 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
553 goto nla_put_failure;
554 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
555 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
556 goto nla_put_failure;
Simon Wunderlich04f39042013-02-08 18:16:19 +0100557 if (chan->flags & IEEE80211_CHAN_RADAR) {
558 u32 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
559 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
560 goto nla_put_failure;
561 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
562 chan->dfs_state))
563 goto nla_put_failure;
564 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME, time))
565 goto nla_put_failure;
566 }
Johannes Berg50640f12012-12-12 17:59:39 +0100567 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
568 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
569 goto nla_put_failure;
570 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
571 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
572 goto nla_put_failure;
573 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
574 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
575 goto nla_put_failure;
576 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
577 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
578 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400579
David S. Miller9360ffd2012-03-29 04:41:26 -0400580 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
581 DBM_TO_MBM(chan->max_power)))
582 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400583
584 return 0;
585
586 nla_put_failure:
587 return -ENOBUFS;
588}
589
Johannes Berg55682962007-09-20 13:09:35 -0400590/* netlink command implementations */
591
Johannes Bergb9454e82009-07-08 13:29:08 +0200592struct key_parse {
593 struct key_params p;
594 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200595 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200596 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100597 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200598};
599
600static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
601{
602 struct nlattr *tb[NL80211_KEY_MAX + 1];
603 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
604 nl80211_key_policy);
605 if (err)
606 return err;
607
608 k->def = !!tb[NL80211_KEY_DEFAULT];
609 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
610
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100611 if (k->def) {
612 k->def_uni = true;
613 k->def_multi = true;
614 }
615 if (k->defmgmt)
616 k->def_multi = true;
617
Johannes Bergb9454e82009-07-08 13:29:08 +0200618 if (tb[NL80211_KEY_IDX])
619 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
620
621 if (tb[NL80211_KEY_DATA]) {
622 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
623 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
624 }
625
626 if (tb[NL80211_KEY_SEQ]) {
627 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
628 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
629 }
630
631 if (tb[NL80211_KEY_CIPHER])
632 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
633
Johannes Berge31b8212010-10-05 19:39:30 +0200634 if (tb[NL80211_KEY_TYPE]) {
635 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
636 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
637 return -EINVAL;
638 }
639
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100640 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
641 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100642 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
643 tb[NL80211_KEY_DEFAULT_TYPES],
644 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100645 if (err)
646 return err;
647
648 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
649 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
650 }
651
Johannes Bergb9454e82009-07-08 13:29:08 +0200652 return 0;
653}
654
655static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
656{
657 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
658 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
659 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
660 }
661
662 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
663 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
664 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
665 }
666
667 if (info->attrs[NL80211_ATTR_KEY_IDX])
668 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
669
670 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
671 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
672
673 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
674 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
675
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100676 if (k->def) {
677 k->def_uni = true;
678 k->def_multi = true;
679 }
680 if (k->defmgmt)
681 k->def_multi = true;
682
Johannes Berge31b8212010-10-05 19:39:30 +0200683 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
684 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
685 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
686 return -EINVAL;
687 }
688
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100689 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
690 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
691 int err = nla_parse_nested(
692 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
693 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
694 nl80211_key_default_policy);
695 if (err)
696 return err;
697
698 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
699 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
700 }
701
Johannes Bergb9454e82009-07-08 13:29:08 +0200702 return 0;
703}
704
705static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
706{
707 int err;
708
709 memset(k, 0, sizeof(*k));
710 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200711 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200712
713 if (info->attrs[NL80211_ATTR_KEY])
714 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
715 else
716 err = nl80211_parse_key_old(info, k);
717
718 if (err)
719 return err;
720
721 if (k->def && k->defmgmt)
722 return -EINVAL;
723
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100724 if (k->defmgmt) {
725 if (k->def_uni || !k->def_multi)
726 return -EINVAL;
727 }
728
Johannes Bergb9454e82009-07-08 13:29:08 +0200729 if (k->idx != -1) {
730 if (k->defmgmt) {
731 if (k->idx < 4 || k->idx > 5)
732 return -EINVAL;
733 } else if (k->def) {
734 if (k->idx < 0 || k->idx > 3)
735 return -EINVAL;
736 } else {
737 if (k->idx < 0 || k->idx > 5)
738 return -EINVAL;
739 }
740 }
741
742 return 0;
743}
744
Johannes Bergfffd0932009-07-08 14:22:54 +0200745static struct cfg80211_cached_keys *
746nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530747 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200748{
749 struct key_parse parse;
750 struct nlattr *key;
751 struct cfg80211_cached_keys *result;
752 int rem, err, def = 0;
753
754 result = kzalloc(sizeof(*result), GFP_KERNEL);
755 if (!result)
756 return ERR_PTR(-ENOMEM);
757
758 result->def = -1;
759 result->defmgmt = -1;
760
761 nla_for_each_nested(key, keys, rem) {
762 memset(&parse, 0, sizeof(parse));
763 parse.idx = -1;
764
765 err = nl80211_parse_key_new(key, &parse);
766 if (err)
767 goto error;
768 err = -EINVAL;
769 if (!parse.p.key)
770 goto error;
771 if (parse.idx < 0 || parse.idx > 4)
772 goto error;
773 if (parse.def) {
774 if (def)
775 goto error;
776 def = 1;
777 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100778 if (!parse.def_uni || !parse.def_multi)
779 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200780 } else if (parse.defmgmt)
781 goto error;
782 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200783 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200784 if (err)
785 goto error;
786 result->params[parse.idx].cipher = parse.p.cipher;
787 result->params[parse.idx].key_len = parse.p.key_len;
788 result->params[parse.idx].key = result->data[parse.idx];
789 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530790
791 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
792 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
793 if (no_ht)
794 *no_ht = true;
795 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200796 }
797
798 return result;
799 error:
800 kfree(result);
801 return ERR_PTR(err);
802}
803
804static int nl80211_key_allowed(struct wireless_dev *wdev)
805{
806 ASSERT_WDEV_LOCK(wdev);
807
Johannes Bergfffd0932009-07-08 14:22:54 +0200808 switch (wdev->iftype) {
809 case NL80211_IFTYPE_AP:
810 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200811 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700812 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200813 break;
814 case NL80211_IFTYPE_ADHOC:
815 if (!wdev->current_bss)
816 return -ENOLINK;
817 break;
818 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200819 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200820 if (wdev->sme_state != CFG80211_SME_CONNECTED)
821 return -ENOLINK;
822 break;
823 default:
824 return -EINVAL;
825 }
826
827 return 0;
828}
829
Johannes Berg7527a782011-05-13 10:58:57 +0200830static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
831{
832 struct nlattr *nl_modes = nla_nest_start(msg, attr);
833 int i;
834
835 if (!nl_modes)
836 goto nla_put_failure;
837
838 i = 0;
839 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400840 if ((ifmodes & 1) && nla_put_flag(msg, i))
841 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200842 ifmodes >>= 1;
843 i++;
844 }
845
846 nla_nest_end(msg, nl_modes);
847 return 0;
848
849nla_put_failure:
850 return -ENOBUFS;
851}
852
853static int nl80211_put_iface_combinations(struct wiphy *wiphy,
854 struct sk_buff *msg)
855{
856 struct nlattr *nl_combis;
857 int i, j;
858
859 nl_combis = nla_nest_start(msg,
860 NL80211_ATTR_INTERFACE_COMBINATIONS);
861 if (!nl_combis)
862 goto nla_put_failure;
863
864 for (i = 0; i < wiphy->n_iface_combinations; i++) {
865 const struct ieee80211_iface_combination *c;
866 struct nlattr *nl_combi, *nl_limits;
867
868 c = &wiphy->iface_combinations[i];
869
870 nl_combi = nla_nest_start(msg, i + 1);
871 if (!nl_combi)
872 goto nla_put_failure;
873
874 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
875 if (!nl_limits)
876 goto nla_put_failure;
877
878 for (j = 0; j < c->n_limits; j++) {
879 struct nlattr *nl_limit;
880
881 nl_limit = nla_nest_start(msg, j + 1);
882 if (!nl_limit)
883 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400884 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
885 c->limits[j].max))
886 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200887 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
888 c->limits[j].types))
889 goto nla_put_failure;
890 nla_nest_end(msg, nl_limit);
891 }
892
893 nla_nest_end(msg, nl_limits);
894
David S. Miller9360ffd2012-03-29 04:41:26 -0400895 if (c->beacon_int_infra_match &&
896 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
897 goto nla_put_failure;
898 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
899 c->num_different_channels) ||
900 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
901 c->max_interfaces))
902 goto nla_put_failure;
Simon Wunderlich11c4a072013-01-08 14:04:07 +0100903 if (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
904 c->radar_detect_widths))
905 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200906
907 nla_nest_end(msg, nl_combi);
908 }
909
910 nla_nest_end(msg, nl_combis);
911
912 return 0;
913nla_put_failure:
914 return -ENOBUFS;
915}
916
Johannes Berg2a0e0472013-01-23 22:57:40 +0100917#ifdef CONFIG_PM
918static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
919 struct sk_buff *msg)
920{
921 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
922 struct nlattr *nl_tcp;
923
924 if (!tcp)
925 return 0;
926
927 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
928 if (!nl_tcp)
929 return -ENOBUFS;
930
931 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
932 tcp->data_payload_max))
933 return -ENOBUFS;
934
935 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
936 tcp->data_payload_max))
937 return -ENOBUFS;
938
939 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
940 return -ENOBUFS;
941
942 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
943 sizeof(*tcp->tok), tcp->tok))
944 return -ENOBUFS;
945
946 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
947 tcp->data_interval_max))
948 return -ENOBUFS;
949
950 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
951 tcp->wake_payload_max))
952 return -ENOBUFS;
953
954 nla_nest_end(msg, nl_tcp);
955 return 0;
956}
957#endif
958
Eric W. Biederman15e47302012-09-07 20:12:54 +0000959static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400960 struct cfg80211_registered_device *dev)
961{
962 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100963 struct nlattr *nl_bands, *nl_band;
964 struct nlattr *nl_freqs, *nl_freq;
965 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100966 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100967 enum ieee80211_band band;
968 struct ieee80211_channel *chan;
969 struct ieee80211_rate *rate;
970 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200971 const struct ieee80211_txrx_stypes *mgmt_stypes =
972 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400973
Eric W. Biederman15e47302012-09-07 20:12:54 +0000974 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400975 if (!hdr)
976 return -1;
977
David S. Miller9360ffd2012-03-29 04:41:26 -0400978 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
979 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
980 nla_put_u32(msg, NL80211_ATTR_GENERATION,
981 cfg80211_rdev_list_generation) ||
982 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
983 dev->wiphy.retry_short) ||
984 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
985 dev->wiphy.retry_long) ||
986 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
987 dev->wiphy.frag_threshold) ||
988 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
989 dev->wiphy.rts_threshold) ||
990 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
991 dev->wiphy.coverage_class) ||
992 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
993 dev->wiphy.max_scan_ssids) ||
994 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
995 dev->wiphy.max_sched_scan_ssids) ||
996 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
997 dev->wiphy.max_scan_ie_len) ||
998 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
999 dev->wiphy.max_sched_scan_ie_len) ||
1000 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1001 dev->wiphy.max_match_sets))
1002 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001003
David S. Miller9360ffd2012-03-29 04:41:26 -04001004 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1005 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1006 goto nla_put_failure;
1007 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1008 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1009 goto nla_put_failure;
1010 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1011 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1012 goto nla_put_failure;
1013 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1014 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1015 goto nla_put_failure;
1016 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1017 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1018 goto nla_put_failure;
1019 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1020 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1021 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001022
David S. Miller9360ffd2012-03-29 04:41:26 -04001023 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1024 sizeof(u32) * dev->wiphy.n_cipher_suites,
1025 dev->wiphy.cipher_suites))
1026 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001027
David S. Miller9360ffd2012-03-29 04:41:26 -04001028 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1029 dev->wiphy.max_num_pmkids))
1030 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +05301031
David S. Miller9360ffd2012-03-29 04:41:26 -04001032 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1033 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1034 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +02001035
David S. Miller9360ffd2012-03-29 04:41:26 -04001036 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1037 dev->wiphy.available_antennas_tx) ||
1038 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1039 dev->wiphy.available_antennas_rx))
1040 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001041
David S. Miller9360ffd2012-03-29 04:41:26 -04001042 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1043 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1044 dev->wiphy.probe_resp_offload))
1045 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +02001046
Bruno Randolf7f531e02010-12-16 11:30:22 +09001047 if ((dev->wiphy.available_antennas_tx ||
1048 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001049 u32 tx_ant = 0, rx_ant = 0;
1050 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +03001051 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001052 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001053 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
1054 tx_ant) ||
1055 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
1056 rx_ant))
1057 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001058 }
1059 }
1060
Johannes Berg7527a782011-05-13 10:58:57 +02001061 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1062 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07001063 goto nla_put_failure;
1064
Johannes Bergee688b002008-01-24 19:38:39 +01001065 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1066 if (!nl_bands)
1067 goto nla_put_failure;
1068
1069 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1070 if (!dev->wiphy.bands[band])
1071 continue;
1072
1073 nl_band = nla_nest_start(msg, band);
1074 if (!nl_band)
1075 goto nla_put_failure;
1076
Johannes Bergd51626d2008-10-09 12:20:13 +02001077 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -04001078 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
1079 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1080 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
1081 &dev->wiphy.bands[band]->ht_cap.mcs) ||
1082 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1083 dev->wiphy.bands[band]->ht_cap.cap) ||
1084 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1085 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
1086 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1087 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
1088 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001089
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001090 /* add VHT info */
1091 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
1092 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1093 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
1094 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
1095 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1096 dev->wiphy.bands[band]->vht_cap.cap)))
1097 goto nla_put_failure;
1098
Johannes Bergee688b002008-01-24 19:38:39 +01001099 /* add frequencies */
1100 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1101 if (!nl_freqs)
1102 goto nla_put_failure;
1103
1104 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1105 nl_freq = nla_nest_start(msg, i);
1106 if (!nl_freq)
1107 goto nla_put_failure;
1108
1109 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001110
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001111 if (nl80211_msg_put_channel(msg, chan))
1112 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001113
Johannes Bergee688b002008-01-24 19:38:39 +01001114 nla_nest_end(msg, nl_freq);
1115 }
1116
1117 nla_nest_end(msg, nl_freqs);
1118
1119 /* add bitrates */
1120 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1121 if (!nl_rates)
1122 goto nla_put_failure;
1123
1124 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1125 nl_rate = nla_nest_start(msg, i);
1126 if (!nl_rate)
1127 goto nla_put_failure;
1128
1129 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001130 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1131 rate->bitrate))
1132 goto nla_put_failure;
1133 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1134 nla_put_flag(msg,
1135 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1136 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001137
1138 nla_nest_end(msg, nl_rate);
1139 }
1140
1141 nla_nest_end(msg, nl_rates);
1142
1143 nla_nest_end(msg, nl_band);
1144 }
1145 nla_nest_end(msg, nl_bands);
1146
Johannes Berg8fdc6212009-03-14 09:34:01 +01001147 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1148 if (!nl_cmds)
1149 goto nla_put_failure;
1150
1151 i = 0;
1152#define CMD(op, n) \
1153 do { \
1154 if (dev->ops->op) { \
1155 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001156 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1157 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001158 } \
1159 } while (0)
1160
1161 CMD(add_virtual_intf, NEW_INTERFACE);
1162 CMD(change_virtual_intf, SET_INTERFACE);
1163 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001164 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001165 CMD(add_station, NEW_STATION);
1166 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001167 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001168 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001169 CMD(auth, AUTHENTICATE);
1170 CMD(assoc, ASSOCIATE);
1171 CMD(deauth, DEAUTHENTICATE);
1172 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001173 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001174 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001175 CMD(set_pmksa, SET_PMKSA);
1176 CMD(del_pmksa, DEL_PMKSA);
1177 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001178 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1179 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001180 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001181 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001182 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001183 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001184 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001185 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1186 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001187 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001188 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001189 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001190 i++;
1191 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1192 goto nla_put_failure;
1193 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001194 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001195 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1196 CMD(tdls_mgmt, TDLS_MGMT);
1197 CMD(tdls_oper, TDLS_OPER);
1198 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001199 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1200 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001201 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001202 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001203 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1204 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001205 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1206 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001207 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001208 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001209 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001210
Kalle Valo4745fc02011-11-17 19:06:10 +02001211#ifdef CONFIG_NL80211_TESTMODE
1212 CMD(testmode_cmd, TESTMODE);
1213#endif
1214
Johannes Berg8fdc6212009-03-14 09:34:01 +01001215#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001216
Johannes Berg6829c872009-07-02 09:13:27 +02001217 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001218 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001219 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1220 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001221 }
1222
Johannes Berg6829c872009-07-02 09:13:27 +02001223 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001224 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001225 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1226 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001227 }
1228
Johannes Berg8fdc6212009-03-14 09:34:01 +01001229 nla_nest_end(msg, nl_cmds);
1230
Johannes Berg7c4ef712011-11-18 15:33:48 +01001231 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001232 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1233 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1234 dev->wiphy.max_remain_on_channel_duration))
1235 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001236
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1238 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1239 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001240
Johannes Berg2e161f72010-08-12 15:38:38 +02001241 if (mgmt_stypes) {
1242 u16 stypes;
1243 struct nlattr *nl_ftypes, *nl_ifs;
1244 enum nl80211_iftype ift;
1245
1246 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1247 if (!nl_ifs)
1248 goto nla_put_failure;
1249
1250 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1251 nl_ftypes = nla_nest_start(msg, ift);
1252 if (!nl_ftypes)
1253 goto nla_put_failure;
1254 i = 0;
1255 stypes = mgmt_stypes[ift].tx;
1256 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001257 if ((stypes & 1) &&
1258 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1259 (i << 4) | IEEE80211_FTYPE_MGMT))
1260 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001261 stypes >>= 1;
1262 i++;
1263 }
1264 nla_nest_end(msg, nl_ftypes);
1265 }
1266
Johannes Berg74b70a42010-08-24 12:15:53 +02001267 nla_nest_end(msg, nl_ifs);
1268
Johannes Berg2e161f72010-08-12 15:38:38 +02001269 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1270 if (!nl_ifs)
1271 goto nla_put_failure;
1272
1273 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1274 nl_ftypes = nla_nest_start(msg, ift);
1275 if (!nl_ftypes)
1276 goto nla_put_failure;
1277 i = 0;
1278 stypes = mgmt_stypes[ift].rx;
1279 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001280 if ((stypes & 1) &&
1281 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1282 (i << 4) | IEEE80211_FTYPE_MGMT))
1283 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001284 stypes >>= 1;
1285 i++;
1286 }
1287 nla_nest_end(msg, nl_ftypes);
1288 }
1289 nla_nest_end(msg, nl_ifs);
1290 }
1291
Johannes Bergdfb89c52012-06-27 09:23:48 +02001292#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001293 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1294 struct nlattr *nl_wowlan;
1295
1296 nl_wowlan = nla_nest_start(msg,
1297 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1298 if (!nl_wowlan)
1299 goto nla_put_failure;
1300
David S. Miller9360ffd2012-03-29 04:41:26 -04001301 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1302 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1303 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1304 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1305 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1306 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1307 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1308 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1309 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1310 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1311 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1312 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1313 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1314 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1315 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1316 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1317 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001318 if (dev->wiphy.wowlan.n_patterns) {
1319 struct nl80211_wowlan_pattern_support pat = {
1320 .max_patterns = dev->wiphy.wowlan.n_patterns,
1321 .min_pattern_len =
1322 dev->wiphy.wowlan.pattern_min_len,
1323 .max_pattern_len =
1324 dev->wiphy.wowlan.pattern_max_len,
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08001325 .max_pkt_offset =
1326 dev->wiphy.wowlan.max_pkt_offset,
Johannes Bergff1b6e62011-05-04 15:37:28 +02001327 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001328 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1329 sizeof(pat), &pat))
1330 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001331 }
1332
Johannes Berg2a0e0472013-01-23 22:57:40 +01001333 if (nl80211_send_wowlan_tcp_caps(dev, msg))
1334 goto nla_put_failure;
1335
Johannes Bergff1b6e62011-05-04 15:37:28 +02001336 nla_nest_end(msg, nl_wowlan);
1337 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001338#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001339
Johannes Berg7527a782011-05-13 10:58:57 +02001340 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1341 dev->wiphy.software_iftypes))
1342 goto nla_put_failure;
1343
1344 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1345 goto nla_put_failure;
1346
David S. Miller9360ffd2012-03-29 04:41:26 -04001347 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1348 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1349 dev->wiphy.ap_sme_capa))
1350 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001351
David S. Miller9360ffd2012-03-29 04:41:26 -04001352 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1353 dev->wiphy.features))
1354 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001355
David S. Miller9360ffd2012-03-29 04:41:26 -04001356 if (dev->wiphy.ht_capa_mod_mask &&
1357 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1358 sizeof(*dev->wiphy.ht_capa_mod_mask),
1359 dev->wiphy.ht_capa_mod_mask))
1360 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001361
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05301362 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1363 dev->wiphy.max_acl_mac_addrs &&
1364 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1365 dev->wiphy.max_acl_mac_addrs))
1366 goto nla_put_failure;
1367
Johannes Berga50df0c2013-02-11 14:20:05 +01001368 if (dev->wiphy.extended_capabilities &&
1369 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1370 dev->wiphy.extended_capabilities_len,
1371 dev->wiphy.extended_capabilities) ||
1372 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1373 dev->wiphy.extended_capabilities_len,
1374 dev->wiphy.extended_capabilities_mask)))
1375 goto nla_put_failure;
1376
Johannes Berg55682962007-09-20 13:09:35 -04001377 return genlmsg_end(msg, hdr);
1378
1379 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001380 genlmsg_cancel(msg, hdr);
1381 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001382}
1383
1384static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1385{
1386 int idx = 0;
1387 int start = cb->args[0];
1388 struct cfg80211_registered_device *dev;
1389
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001390 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001391 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001392 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1393 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001394 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001395 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001396 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001397 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001398 dev) < 0) {
1399 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001400 break;
Julius Volzb4637272008-07-08 14:02:19 +02001401 }
Johannes Berg55682962007-09-20 13:09:35 -04001402 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001403 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001404
1405 cb->args[0] = idx;
1406
1407 return skb->len;
1408}
1409
1410static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1411{
1412 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001413 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001414
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001415 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001416 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001417 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001418
Eric W. Biederman15e47302012-09-07 20:12:54 +00001419 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001420 nlmsg_free(msg);
1421 return -ENOBUFS;
1422 }
Johannes Berg55682962007-09-20 13:09:35 -04001423
Johannes Berg134e6372009-07-10 09:51:34 +00001424 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001425}
1426
Jouni Malinen31888482008-10-30 16:59:24 +02001427static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1428 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1429 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1430 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1431 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1432 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1433};
1434
1435static int parse_txq_params(struct nlattr *tb[],
1436 struct ieee80211_txq_params *txq_params)
1437{
Johannes Berga3304b02012-03-28 11:04:24 +02001438 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001439 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1440 !tb[NL80211_TXQ_ATTR_AIFS])
1441 return -EINVAL;
1442
Johannes Berga3304b02012-03-28 11:04:24 +02001443 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001444 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1445 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1446 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1447 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1448
Johannes Berga3304b02012-03-28 11:04:24 +02001449 if (txq_params->ac >= NL80211_NUM_ACS)
1450 return -EINVAL;
1451
Jouni Malinen31888482008-10-30 16:59:24 +02001452 return 0;
1453}
1454
Johannes Bergf444de02010-05-05 15:25:02 +02001455static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1456{
1457 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001458 * You can only set the channel explicitly for WDS interfaces,
1459 * all others have their channel managed via their respective
1460 * "establish a connection" command (connect, join, ...)
1461 *
1462 * For AP/GO and mesh mode, the channel can be set with the
1463 * channel userspace API, but is only stored and passed to the
1464 * low-level driver when the AP starts or the mesh is joined.
1465 * This is for backward compatibility, userspace can also give
1466 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001467 *
1468 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001469 * whatever else is going on, so they have their own special
1470 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001471 */
1472 return !wdev ||
1473 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001474 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001475 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1476 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001477}
1478
Johannes Berg683b6d32012-11-08 21:25:48 +01001479static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1480 struct genl_info *info,
1481 struct cfg80211_chan_def *chandef)
1482{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301483 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001484
1485 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1486 return -EINVAL;
1487
1488 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1489
1490 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001491 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1492 chandef->center_freq1 = control_freq;
1493 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001494
1495 /* Primary channel not allowed */
1496 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1497 return -EINVAL;
1498
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001499 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1500 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001501
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001502 chantype = nla_get_u32(
1503 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1504
1505 switch (chantype) {
1506 case NL80211_CHAN_NO_HT:
1507 case NL80211_CHAN_HT20:
1508 case NL80211_CHAN_HT40PLUS:
1509 case NL80211_CHAN_HT40MINUS:
1510 cfg80211_chandef_create(chandef, chandef->chan,
1511 chantype);
1512 break;
1513 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001514 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001515 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001516 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1517 chandef->width =
1518 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1519 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1520 chandef->center_freq1 =
1521 nla_get_u32(
1522 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1523 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1524 chandef->center_freq2 =
1525 nla_get_u32(
1526 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1527 }
1528
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001529 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001530 return -EINVAL;
1531
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001532 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1533 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001534 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001535
Johannes Berg683b6d32012-11-08 21:25:48 +01001536 return 0;
1537}
1538
Johannes Bergf444de02010-05-05 15:25:02 +02001539static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1540 struct wireless_dev *wdev,
1541 struct genl_info *info)
1542{
Johannes Berg683b6d32012-11-08 21:25:48 +01001543 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001544 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001545 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1546
1547 if (wdev)
1548 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001549
Johannes Bergf444de02010-05-05 15:25:02 +02001550 if (!nl80211_can_set_dev_channel(wdev))
1551 return -EOPNOTSUPP;
1552
Johannes Berg683b6d32012-11-08 21:25:48 +01001553 result = nl80211_parse_chandef(rdev, info, &chandef);
1554 if (result)
1555 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001556
1557 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001558 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001559 case NL80211_IFTYPE_AP:
1560 case NL80211_IFTYPE_P2P_GO:
1561 if (wdev->beacon_interval) {
1562 result = -EBUSY;
1563 break;
1564 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001565 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001566 result = -EINVAL;
1567 break;
1568 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001569 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001570 result = 0;
1571 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001572 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001573 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001574 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001575 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001576 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001577 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001578 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001579 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001580 }
1581 mutex_unlock(&rdev->devlist_mtx);
1582
1583 return result;
1584}
1585
1586static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1587{
Johannes Berg4c476992010-10-04 21:36:35 +02001588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1589 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001590
Johannes Berg4c476992010-10-04 21:36:35 +02001591 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001592}
1593
Bill Jordane8347eb2010-10-01 13:54:28 -04001594static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1595{
Johannes Berg43b19952010-10-07 13:10:30 +02001596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1597 struct net_device *dev = info->user_ptr[1];
1598 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001599 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001600
1601 if (!info->attrs[NL80211_ATTR_MAC])
1602 return -EINVAL;
1603
Johannes Berg43b19952010-10-07 13:10:30 +02001604 if (netif_running(dev))
1605 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001606
Johannes Berg43b19952010-10-07 13:10:30 +02001607 if (!rdev->ops->set_wds_peer)
1608 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001609
Johannes Berg43b19952010-10-07 13:10:30 +02001610 if (wdev->iftype != NL80211_IFTYPE_WDS)
1611 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001612
1613 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001614 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001615}
1616
1617
Johannes Berg55682962007-09-20 13:09:35 -04001618static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1619{
1620 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001621 struct net_device *netdev = NULL;
1622 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001623 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001624 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001625 u32 changed;
1626 u8 retry_short = 0, retry_long = 0;
1627 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001628 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001629
Johannes Bergf444de02010-05-05 15:25:02 +02001630 /*
1631 * Try to find the wiphy and netdev. Normally this
1632 * function shouldn't need the netdev, but this is
1633 * done for backward compatibility -- previously
1634 * setting the channel was done per wiphy, but now
1635 * it is per netdev. Previous userland like hostapd
1636 * also passed a netdev to set_wiphy, so that it is
1637 * possible to let that go to the right netdev!
1638 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001639 mutex_lock(&cfg80211_mutex);
1640
Johannes Bergf444de02010-05-05 15:25:02 +02001641 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1642 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1643
1644 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1645 if (netdev && netdev->ieee80211_ptr) {
1646 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1647 mutex_lock(&rdev->mtx);
1648 } else
1649 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001650 }
1651
Johannes Bergf444de02010-05-05 15:25:02 +02001652 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001653 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1654 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001655 if (IS_ERR(rdev)) {
1656 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001657 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001658 }
1659 wdev = NULL;
1660 netdev = NULL;
1661 result = 0;
1662
1663 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001664 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001665 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001666
1667 /*
1668 * end workaround code, by now the rdev is available
1669 * and locked, and wdev may or may not be NULL.
1670 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001671
1672 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001673 result = cfg80211_dev_rename(
1674 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001675
1676 mutex_unlock(&cfg80211_mutex);
1677
1678 if (result)
1679 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001680
Jouni Malinen31888482008-10-30 16:59:24 +02001681 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1682 struct ieee80211_txq_params txq_params;
1683 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1684
1685 if (!rdev->ops->set_txq_params) {
1686 result = -EOPNOTSUPP;
1687 goto bad_res;
1688 }
1689
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001690 if (!netdev) {
1691 result = -EINVAL;
1692 goto bad_res;
1693 }
1694
Johannes Berg133a3ff2011-11-03 14:50:13 +01001695 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1696 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1697 result = -EINVAL;
1698 goto bad_res;
1699 }
1700
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001701 if (!netif_running(netdev)) {
1702 result = -ENETDOWN;
1703 goto bad_res;
1704 }
1705
Jouni Malinen31888482008-10-30 16:59:24 +02001706 nla_for_each_nested(nl_txq_params,
1707 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1708 rem_txq_params) {
1709 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1710 nla_data(nl_txq_params),
1711 nla_len(nl_txq_params),
1712 txq_params_policy);
1713 result = parse_txq_params(tb, &txq_params);
1714 if (result)
1715 goto bad_res;
1716
Hila Gonene35e4d22012-06-27 17:19:42 +03001717 result = rdev_set_txq_params(rdev, netdev,
1718 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001719 if (result)
1720 goto bad_res;
1721 }
1722 }
1723
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001724 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001725 result = __nl80211_set_channel(rdev,
1726 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1727 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001728 if (result)
1729 goto bad_res;
1730 }
1731
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001732 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001733 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001734 enum nl80211_tx_power_setting type;
1735 int idx, mbm = 0;
1736
Johannes Bergc8442112012-10-24 10:17:18 +02001737 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1738 txp_wdev = NULL;
1739
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001740 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001741 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001742 goto bad_res;
1743 }
1744
1745 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1746 type = nla_get_u32(info->attrs[idx]);
1747
1748 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1749 (type != NL80211_TX_POWER_AUTOMATIC)) {
1750 result = -EINVAL;
1751 goto bad_res;
1752 }
1753
1754 if (type != NL80211_TX_POWER_AUTOMATIC) {
1755 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1756 mbm = nla_get_u32(info->attrs[idx]);
1757 }
1758
Johannes Bergc8442112012-10-24 10:17:18 +02001759 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001760 if (result)
1761 goto bad_res;
1762 }
1763
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001764 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1765 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1766 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001767 if ((!rdev->wiphy.available_antennas_tx &&
1768 !rdev->wiphy.available_antennas_rx) ||
1769 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001770 result = -EOPNOTSUPP;
1771 goto bad_res;
1772 }
1773
1774 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1775 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1776
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001777 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001778 * available antenna masks, except for the "all" mask */
1779 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1780 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001781 result = -EINVAL;
1782 goto bad_res;
1783 }
1784
Bruno Randolf7f531e02010-12-16 11:30:22 +09001785 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1786 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001787
Hila Gonene35e4d22012-06-27 17:19:42 +03001788 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001789 if (result)
1790 goto bad_res;
1791 }
1792
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001793 changed = 0;
1794
1795 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1796 retry_short = nla_get_u8(
1797 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1798 if (retry_short == 0) {
1799 result = -EINVAL;
1800 goto bad_res;
1801 }
1802 changed |= WIPHY_PARAM_RETRY_SHORT;
1803 }
1804
1805 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1806 retry_long = nla_get_u8(
1807 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1808 if (retry_long == 0) {
1809 result = -EINVAL;
1810 goto bad_res;
1811 }
1812 changed |= WIPHY_PARAM_RETRY_LONG;
1813 }
1814
1815 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1816 frag_threshold = nla_get_u32(
1817 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1818 if (frag_threshold < 256) {
1819 result = -EINVAL;
1820 goto bad_res;
1821 }
1822 if (frag_threshold != (u32) -1) {
1823 /*
1824 * Fragments (apart from the last one) are required to
1825 * have even length. Make the fragmentation code
1826 * simpler by stripping LSB should someone try to use
1827 * odd threshold value.
1828 */
1829 frag_threshold &= ~0x1;
1830 }
1831 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1832 }
1833
1834 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1835 rts_threshold = nla_get_u32(
1836 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1837 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1838 }
1839
Lukáš Turek81077e82009-12-21 22:50:47 +01001840 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1841 coverage_class = nla_get_u8(
1842 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1843 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1844 }
1845
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001846 if (changed) {
1847 u8 old_retry_short, old_retry_long;
1848 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001849 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001850
1851 if (!rdev->ops->set_wiphy_params) {
1852 result = -EOPNOTSUPP;
1853 goto bad_res;
1854 }
1855
1856 old_retry_short = rdev->wiphy.retry_short;
1857 old_retry_long = rdev->wiphy.retry_long;
1858 old_frag_threshold = rdev->wiphy.frag_threshold;
1859 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001860 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001861
1862 if (changed & WIPHY_PARAM_RETRY_SHORT)
1863 rdev->wiphy.retry_short = retry_short;
1864 if (changed & WIPHY_PARAM_RETRY_LONG)
1865 rdev->wiphy.retry_long = retry_long;
1866 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1867 rdev->wiphy.frag_threshold = frag_threshold;
1868 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1869 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001870 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1871 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001872
Hila Gonene35e4d22012-06-27 17:19:42 +03001873 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001874 if (result) {
1875 rdev->wiphy.retry_short = old_retry_short;
1876 rdev->wiphy.retry_long = old_retry_long;
1877 rdev->wiphy.frag_threshold = old_frag_threshold;
1878 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001879 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001880 }
1881 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001882
Johannes Berg306d6112008-12-08 12:39:04 +01001883 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001884 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001885 if (netdev)
1886 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001887 return result;
1888}
1889
Johannes Berg71bbc992012-06-15 15:30:18 +02001890static inline u64 wdev_id(struct wireless_dev *wdev)
1891{
1892 return (u64)wdev->identifier |
1893 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1894}
Johannes Berg55682962007-09-20 13:09:35 -04001895
Johannes Berg683b6d32012-11-08 21:25:48 +01001896static int nl80211_send_chandef(struct sk_buff *msg,
1897 struct cfg80211_chan_def *chandef)
1898{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001899 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001900
Johannes Berg683b6d32012-11-08 21:25:48 +01001901 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1902 chandef->chan->center_freq))
1903 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001904 switch (chandef->width) {
1905 case NL80211_CHAN_WIDTH_20_NOHT:
1906 case NL80211_CHAN_WIDTH_20:
1907 case NL80211_CHAN_WIDTH_40:
1908 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1909 cfg80211_get_chandef_type(chandef)))
1910 return -ENOBUFS;
1911 break;
1912 default:
1913 break;
1914 }
1915 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
1916 return -ENOBUFS;
1917 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
1918 return -ENOBUFS;
1919 if (chandef->center_freq2 &&
1920 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01001921 return -ENOBUFS;
1922 return 0;
1923}
1924
Eric W. Biederman15e47302012-09-07 20:12:54 +00001925static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001926 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001927 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001928{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001929 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001930 void *hdr;
1931
Eric W. Biederman15e47302012-09-07 20:12:54 +00001932 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001933 if (!hdr)
1934 return -1;
1935
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001936 if (dev &&
1937 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001938 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001939 goto nla_put_failure;
1940
1941 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1942 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001943 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001944 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001945 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1946 rdev->devlist_generation ^
1947 (cfg80211_rdev_list_generation << 2)))
1948 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001949
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001950 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001951 int ret;
1952 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001953
Johannes Berg683b6d32012-11-08 21:25:48 +01001954 ret = rdev_get_channel(rdev, wdev, &chandef);
1955 if (ret == 0) {
1956 if (nl80211_send_chandef(msg, &chandef))
1957 goto nla_put_failure;
1958 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001959 }
1960
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001961 if (wdev->ssid_len) {
1962 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1963 goto nla_put_failure;
1964 }
1965
Johannes Berg55682962007-09-20 13:09:35 -04001966 return genlmsg_end(msg, hdr);
1967
1968 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001969 genlmsg_cancel(msg, hdr);
1970 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001971}
1972
1973static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1974{
1975 int wp_idx = 0;
1976 int if_idx = 0;
1977 int wp_start = cb->args[0];
1978 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001979 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001980 struct wireless_dev *wdev;
1981
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001982 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001983 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1984 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001985 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001986 if (wp_idx < wp_start) {
1987 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001988 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001989 }
Johannes Berg55682962007-09-20 13:09:35 -04001990 if_idx = 0;
1991
Johannes Bergf5ea9122009-08-07 16:17:38 +02001992 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001993 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001994 if (if_idx < if_start) {
1995 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001996 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001997 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001998 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001999 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002000 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002001 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002002 goto out;
2003 }
2004 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002005 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002006 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002007
2008 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002009 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002010 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002011 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002012
2013 cb->args[0] = wp_idx;
2014 cb->args[1] = if_idx;
2015
2016 return skb->len;
2017}
2018
2019static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2020{
2021 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002022 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002023 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002024
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002025 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002026 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002027 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002028
Eric W. Biederman15e47302012-09-07 20:12:54 +00002029 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002030 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002031 nlmsg_free(msg);
2032 return -ENOBUFS;
2033 }
Johannes Berg55682962007-09-20 13:09:35 -04002034
Johannes Berg134e6372009-07-10 09:51:34 +00002035 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002036}
2037
Michael Wu66f7ac52008-01-31 19:48:22 +01002038static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2039 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2040 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2041 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2042 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2043 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2044};
2045
2046static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2047{
2048 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2049 int flag;
2050
2051 *mntrflags = 0;
2052
2053 if (!nla)
2054 return -EINVAL;
2055
2056 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2057 nla, mntr_flags_policy))
2058 return -EINVAL;
2059
2060 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2061 if (flags[flag])
2062 *mntrflags |= (1<<flag);
2063
2064 return 0;
2065}
2066
Johannes Berg9bc383d2009-11-19 11:55:19 +01002067static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002068 struct net_device *netdev, u8 use_4addr,
2069 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002070{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002071 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002072 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002073 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002074 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002075 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002076
2077 switch (iftype) {
2078 case NL80211_IFTYPE_AP_VLAN:
2079 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2080 return 0;
2081 break;
2082 case NL80211_IFTYPE_STATION:
2083 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2084 return 0;
2085 break;
2086 default:
2087 break;
2088 }
2089
2090 return -EOPNOTSUPP;
2091}
2092
Johannes Berg55682962007-09-20 13:09:35 -04002093static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2094{
Johannes Berg4c476992010-10-04 21:36:35 +02002095 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002096 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002097 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002098 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002099 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002100 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002101 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002102
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002103 memset(&params, 0, sizeof(params));
2104
Johannes Berg04a773a2009-04-19 21:24:32 +02002105 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002106
Johannes Berg723b0382008-09-16 20:22:09 +02002107 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002108 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002109 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002110 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002111 if (ntype > NL80211_IFTYPE_MAX)
2112 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002113 }
2114
Johannes Berg92ffe052008-09-16 20:39:36 +02002115 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002116 struct wireless_dev *wdev = dev->ieee80211_ptr;
2117
Johannes Berg4c476992010-10-04 21:36:35 +02002118 if (ntype != NL80211_IFTYPE_MESH_POINT)
2119 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002120 if (netif_running(dev))
2121 return -EBUSY;
2122
2123 wdev_lock(wdev);
2124 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2125 IEEE80211_MAX_MESH_ID_LEN);
2126 wdev->mesh_id_up_len =
2127 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2128 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2129 wdev->mesh_id_up_len);
2130 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002131 }
2132
Felix Fietkau8b787642009-11-10 18:53:10 +01002133 if (info->attrs[NL80211_ATTR_4ADDR]) {
2134 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2135 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002136 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002137 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002138 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002139 } else {
2140 params.use_4addr = -1;
2141 }
2142
Johannes Berg92ffe052008-09-16 20:39:36 +02002143 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002144 if (ntype != NL80211_IFTYPE_MONITOR)
2145 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002146 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2147 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002148 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002149 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002150
2151 flags = &_flags;
2152 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002153 }
Johannes Berg3b858752009-03-12 09:55:09 +01002154
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002155 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002156 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002157 else
2158 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002159
Johannes Berg9bc383d2009-11-19 11:55:19 +01002160 if (!err && params.use_4addr != -1)
2161 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2162
Johannes Berg55682962007-09-20 13:09:35 -04002163 return err;
2164}
2165
2166static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2167{
Johannes Berg4c476992010-10-04 21:36:35 +02002168 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002169 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002170 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002171 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002172 int err;
2173 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002174 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002175
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002176 memset(&params, 0, sizeof(params));
2177
Johannes Berg55682962007-09-20 13:09:35 -04002178 if (!info->attrs[NL80211_ATTR_IFNAME])
2179 return -EINVAL;
2180
2181 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2182 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2183 if (type > NL80211_IFTYPE_MAX)
2184 return -EINVAL;
2185 }
2186
Johannes Berg79c97e92009-07-07 03:56:12 +02002187 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002188 !(rdev->wiphy.interface_modes & (1 << type)))
2189 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002190
Arend van Spriel1c18f142013-01-08 10:17:27 +01002191 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2192 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2193 ETH_ALEN);
2194 if (!is_valid_ether_addr(params.macaddr))
2195 return -EADDRNOTAVAIL;
2196 }
2197
Johannes Berg9bc383d2009-11-19 11:55:19 +01002198 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002199 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002200 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002201 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002202 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002203 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002204
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2206 if (!msg)
2207 return -ENOMEM;
2208
Michael Wu66f7ac52008-01-31 19:48:22 +01002209 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2210 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2211 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002212 wdev = rdev_add_virtual_intf(rdev,
2213 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2214 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002215 if (IS_ERR(wdev)) {
2216 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002217 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002218 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002219
Johannes Berg98104fde2012-06-16 00:19:54 +02002220 switch (type) {
2221 case NL80211_IFTYPE_MESH_POINT:
2222 if (!info->attrs[NL80211_ATTR_MESH_ID])
2223 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002224 wdev_lock(wdev);
2225 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2226 IEEE80211_MAX_MESH_ID_LEN);
2227 wdev->mesh_id_up_len =
2228 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2229 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2230 wdev->mesh_id_up_len);
2231 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002232 break;
2233 case NL80211_IFTYPE_P2P_DEVICE:
2234 /*
2235 * P2P Device doesn't have a netdev, so doesn't go
2236 * through the netdev notifier and must be added here
2237 */
2238 mutex_init(&wdev->mtx);
2239 INIT_LIST_HEAD(&wdev->event_list);
2240 spin_lock_init(&wdev->event_lock);
2241 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2242 spin_lock_init(&wdev->mgmt_registrations_lock);
2243
2244 mutex_lock(&rdev->devlist_mtx);
2245 wdev->identifier = ++rdev->wdev_id;
2246 list_add_rcu(&wdev->list, &rdev->wdev_list);
2247 rdev->devlist_generation++;
2248 mutex_unlock(&rdev->devlist_mtx);
2249 break;
2250 default:
2251 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002252 }
2253
Eric W. Biederman15e47302012-09-07 20:12:54 +00002254 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002255 rdev, wdev) < 0) {
2256 nlmsg_free(msg);
2257 return -ENOBUFS;
2258 }
2259
2260 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002261}
2262
2263static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2264{
Johannes Berg4c476992010-10-04 21:36:35 +02002265 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002266 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002267
Johannes Berg4c476992010-10-04 21:36:35 +02002268 if (!rdev->ops->del_virtual_intf)
2269 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002270
Johannes Berg84efbb82012-06-16 00:00:26 +02002271 /*
2272 * If we remove a wireless device without a netdev then clear
2273 * user_ptr[1] so that nl80211_post_doit won't dereference it
2274 * to check if it needs to do dev_put(). Otherwise it crashes
2275 * since the wdev has been freed, unlike with a netdev where
2276 * we need the dev_put() for the netdev to really be freed.
2277 */
2278 if (!wdev->netdev)
2279 info->user_ptr[1] = NULL;
2280
Hila Gonene35e4d22012-06-27 17:19:42 +03002281 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002282}
2283
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002284static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2285{
2286 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2287 struct net_device *dev = info->user_ptr[1];
2288 u16 noack_map;
2289
2290 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2291 return -EINVAL;
2292
2293 if (!rdev->ops->set_noack_map)
2294 return -EOPNOTSUPP;
2295
2296 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2297
Hila Gonene35e4d22012-06-27 17:19:42 +03002298 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002299}
2300
Johannes Berg41ade002007-12-19 02:03:29 +01002301struct get_key_cookie {
2302 struct sk_buff *msg;
2303 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002304 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002305};
2306
2307static void get_key_callback(void *c, struct key_params *params)
2308{
Johannes Bergb9454e82009-07-08 13:29:08 +02002309 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002310 struct get_key_cookie *cookie = c;
2311
David S. Miller9360ffd2012-03-29 04:41:26 -04002312 if ((params->key &&
2313 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2314 params->key_len, params->key)) ||
2315 (params->seq &&
2316 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2317 params->seq_len, params->seq)) ||
2318 (params->cipher &&
2319 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2320 params->cipher)))
2321 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002322
Johannes Bergb9454e82009-07-08 13:29:08 +02002323 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2324 if (!key)
2325 goto nla_put_failure;
2326
David S. Miller9360ffd2012-03-29 04:41:26 -04002327 if ((params->key &&
2328 nla_put(cookie->msg, NL80211_KEY_DATA,
2329 params->key_len, params->key)) ||
2330 (params->seq &&
2331 nla_put(cookie->msg, NL80211_KEY_SEQ,
2332 params->seq_len, params->seq)) ||
2333 (params->cipher &&
2334 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2335 params->cipher)))
2336 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002337
David S. Miller9360ffd2012-03-29 04:41:26 -04002338 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2339 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002340
2341 nla_nest_end(cookie->msg, key);
2342
Johannes Berg41ade002007-12-19 02:03:29 +01002343 return;
2344 nla_put_failure:
2345 cookie->error = 1;
2346}
2347
2348static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2349{
Johannes Berg4c476992010-10-04 21:36:35 +02002350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002351 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002352 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002353 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002354 const u8 *mac_addr = NULL;
2355 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002356 struct get_key_cookie cookie = {
2357 .error = 0,
2358 };
2359 void *hdr;
2360 struct sk_buff *msg;
2361
2362 if (info->attrs[NL80211_ATTR_KEY_IDX])
2363 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2364
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002365 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002366 return -EINVAL;
2367
2368 if (info->attrs[NL80211_ATTR_MAC])
2369 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2370
Johannes Berge31b8212010-10-05 19:39:30 +02002371 pairwise = !!mac_addr;
2372 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2373 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2374 if (kt >= NUM_NL80211_KEYTYPES)
2375 return -EINVAL;
2376 if (kt != NL80211_KEYTYPE_GROUP &&
2377 kt != NL80211_KEYTYPE_PAIRWISE)
2378 return -EINVAL;
2379 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2380 }
2381
Johannes Berg4c476992010-10-04 21:36:35 +02002382 if (!rdev->ops->get_key)
2383 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002384
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002386 if (!msg)
2387 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002388
Eric W. Biederman15e47302012-09-07 20:12:54 +00002389 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002390 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002391 if (IS_ERR(hdr))
2392 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002393
2394 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002395 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002396
David S. Miller9360ffd2012-03-29 04:41:26 -04002397 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2398 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2399 goto nla_put_failure;
2400 if (mac_addr &&
2401 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2402 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002403
Johannes Berge31b8212010-10-05 19:39:30 +02002404 if (pairwise && mac_addr &&
2405 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2406 return -ENOENT;
2407
Hila Gonene35e4d22012-06-27 17:19:42 +03002408 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2409 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002410
2411 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002412 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002413
2414 if (cookie.error)
2415 goto nla_put_failure;
2416
2417 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002418 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002419
2420 nla_put_failure:
2421 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002422 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002423 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002424 return err;
2425}
2426
2427static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2428{
Johannes Berg4c476992010-10-04 21:36:35 +02002429 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002430 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002431 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002432 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002433
Johannes Bergb9454e82009-07-08 13:29:08 +02002434 err = nl80211_parse_key(info, &key);
2435 if (err)
2436 return err;
2437
2438 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002439 return -EINVAL;
2440
Johannes Bergb9454e82009-07-08 13:29:08 +02002441 /* only support setting default key */
2442 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002443 return -EINVAL;
2444
Johannes Bergfffd0932009-07-08 14:22:54 +02002445 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002446
2447 if (key.def) {
2448 if (!rdev->ops->set_default_key) {
2449 err = -EOPNOTSUPP;
2450 goto out;
2451 }
2452
2453 err = nl80211_key_allowed(dev->ieee80211_ptr);
2454 if (err)
2455 goto out;
2456
Hila Gonene35e4d22012-06-27 17:19:42 +03002457 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002458 key.def_uni, key.def_multi);
2459
2460 if (err)
2461 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002462
Johannes Berg3d23e342009-09-29 23:27:28 +02002463#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002464 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002465#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002466 } else {
2467 if (key.def_uni || !key.def_multi) {
2468 err = -EINVAL;
2469 goto out;
2470 }
2471
2472 if (!rdev->ops->set_default_mgmt_key) {
2473 err = -EOPNOTSUPP;
2474 goto out;
2475 }
2476
2477 err = nl80211_key_allowed(dev->ieee80211_ptr);
2478 if (err)
2479 goto out;
2480
Hila Gonene35e4d22012-06-27 17:19:42 +03002481 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002482 if (err)
2483 goto out;
2484
2485#ifdef CONFIG_CFG80211_WEXT
2486 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2487#endif
2488 }
2489
2490 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002491 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002492
Johannes Berg41ade002007-12-19 02:03:29 +01002493 return err;
2494}
2495
2496static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2497{
Johannes Berg4c476992010-10-04 21:36:35 +02002498 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002499 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002500 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002501 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002502 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002503
Johannes Bergb9454e82009-07-08 13:29:08 +02002504 err = nl80211_parse_key(info, &key);
2505 if (err)
2506 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002507
Johannes Bergb9454e82009-07-08 13:29:08 +02002508 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002509 return -EINVAL;
2510
Johannes Berg41ade002007-12-19 02:03:29 +01002511 if (info->attrs[NL80211_ATTR_MAC])
2512 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2513
Johannes Berge31b8212010-10-05 19:39:30 +02002514 if (key.type == -1) {
2515 if (mac_addr)
2516 key.type = NL80211_KEYTYPE_PAIRWISE;
2517 else
2518 key.type = NL80211_KEYTYPE_GROUP;
2519 }
2520
2521 /* for now */
2522 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2523 key.type != NL80211_KEYTYPE_GROUP)
2524 return -EINVAL;
2525
Johannes Berg4c476992010-10-04 21:36:35 +02002526 if (!rdev->ops->add_key)
2527 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002528
Johannes Berge31b8212010-10-05 19:39:30 +02002529 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2530 key.type == NL80211_KEYTYPE_PAIRWISE,
2531 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002532 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002533
2534 wdev_lock(dev->ieee80211_ptr);
2535 err = nl80211_key_allowed(dev->ieee80211_ptr);
2536 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002537 err = rdev_add_key(rdev, dev, key.idx,
2538 key.type == NL80211_KEYTYPE_PAIRWISE,
2539 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002540 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002541
Johannes Berg41ade002007-12-19 02:03:29 +01002542 return err;
2543}
2544
2545static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2546{
Johannes Berg4c476992010-10-04 21:36:35 +02002547 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002548 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002549 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002550 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002551 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002552
Johannes Bergb9454e82009-07-08 13:29:08 +02002553 err = nl80211_parse_key(info, &key);
2554 if (err)
2555 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002556
2557 if (info->attrs[NL80211_ATTR_MAC])
2558 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2559
Johannes Berge31b8212010-10-05 19:39:30 +02002560 if (key.type == -1) {
2561 if (mac_addr)
2562 key.type = NL80211_KEYTYPE_PAIRWISE;
2563 else
2564 key.type = NL80211_KEYTYPE_GROUP;
2565 }
2566
2567 /* for now */
2568 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2569 key.type != NL80211_KEYTYPE_GROUP)
2570 return -EINVAL;
2571
Johannes Berg4c476992010-10-04 21:36:35 +02002572 if (!rdev->ops->del_key)
2573 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002574
Johannes Bergfffd0932009-07-08 14:22:54 +02002575 wdev_lock(dev->ieee80211_ptr);
2576 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002577
2578 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2579 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2580 err = -ENOENT;
2581
Johannes Bergfffd0932009-07-08 14:22:54 +02002582 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002583 err = rdev_del_key(rdev, dev, key.idx,
2584 key.type == NL80211_KEYTYPE_PAIRWISE,
2585 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002586
Johannes Berg3d23e342009-09-29 23:27:28 +02002587#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002588 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002589 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002590 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002591 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002592 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2593 }
2594#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002595 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002596
Johannes Berg41ade002007-12-19 02:03:29 +01002597 return err;
2598}
2599
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302600/* This function returns an error or the number of nested attributes */
2601static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2602{
2603 struct nlattr *attr;
2604 int n_entries = 0, tmp;
2605
2606 nla_for_each_nested(attr, nl_attr, tmp) {
2607 if (nla_len(attr) != ETH_ALEN)
2608 return -EINVAL;
2609
2610 n_entries++;
2611 }
2612
2613 return n_entries;
2614}
2615
2616/*
2617 * This function parses ACL information and allocates memory for ACL data.
2618 * On successful return, the calling function is responsible to free the
2619 * ACL buffer returned by this function.
2620 */
2621static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2622 struct genl_info *info)
2623{
2624 enum nl80211_acl_policy acl_policy;
2625 struct nlattr *attr;
2626 struct cfg80211_acl_data *acl;
2627 int i = 0, n_entries, tmp;
2628
2629 if (!wiphy->max_acl_mac_addrs)
2630 return ERR_PTR(-EOPNOTSUPP);
2631
2632 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2633 return ERR_PTR(-EINVAL);
2634
2635 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2636 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2637 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2638 return ERR_PTR(-EINVAL);
2639
2640 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2641 return ERR_PTR(-EINVAL);
2642
2643 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2644 if (n_entries < 0)
2645 return ERR_PTR(n_entries);
2646
2647 if (n_entries > wiphy->max_acl_mac_addrs)
2648 return ERR_PTR(-ENOTSUPP);
2649
2650 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2651 GFP_KERNEL);
2652 if (!acl)
2653 return ERR_PTR(-ENOMEM);
2654
2655 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2656 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2657 i++;
2658 }
2659
2660 acl->n_acl_entries = n_entries;
2661 acl->acl_policy = acl_policy;
2662
2663 return acl;
2664}
2665
2666static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2667{
2668 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2669 struct net_device *dev = info->user_ptr[1];
2670 struct cfg80211_acl_data *acl;
2671 int err;
2672
2673 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2674 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2675 return -EOPNOTSUPP;
2676
2677 if (!dev->ieee80211_ptr->beacon_interval)
2678 return -EINVAL;
2679
2680 acl = parse_acl_data(&rdev->wiphy, info);
2681 if (IS_ERR(acl))
2682 return PTR_ERR(acl);
2683
2684 err = rdev_set_mac_acl(rdev, dev, acl);
2685
2686 kfree(acl);
2687
2688 return err;
2689}
2690
Johannes Berg88600202012-02-13 15:17:18 +01002691static int nl80211_parse_beacon(struct genl_info *info,
2692 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002693{
Johannes Berg88600202012-02-13 15:17:18 +01002694 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002695
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002696 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2697 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2698 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2699 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002700 return -EINVAL;
2701
Johannes Berg88600202012-02-13 15:17:18 +01002702 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002703
Johannes Berged1b6cc2007-12-19 02:03:32 +01002704 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002705 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2706 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2707 if (!bcn->head_len)
2708 return -EINVAL;
2709 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002710 }
2711
2712 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002713 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2714 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002715 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002716 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002717 }
2718
Johannes Berg4c476992010-10-04 21:36:35 +02002719 if (!haveinfo)
2720 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002721
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002722 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002723 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2724 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002725 }
2726
2727 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002728 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002729 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002730 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002731 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2732 }
2733
2734 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002735 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002736 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002737 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002738 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2739 }
2740
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002741 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002742 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002743 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002744 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002745 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2746 }
2747
Johannes Berg88600202012-02-13 15:17:18 +01002748 return 0;
2749}
2750
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002751static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2752 struct cfg80211_ap_settings *params)
2753{
2754 struct wireless_dev *wdev;
2755 bool ret = false;
2756
2757 mutex_lock(&rdev->devlist_mtx);
2758
Johannes Berg89a54e42012-06-15 14:33:17 +02002759 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002760 if (wdev->iftype != NL80211_IFTYPE_AP &&
2761 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2762 continue;
2763
Johannes Berg683b6d32012-11-08 21:25:48 +01002764 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002765 continue;
2766
Johannes Berg683b6d32012-11-08 21:25:48 +01002767 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002768 ret = true;
2769 break;
2770 }
2771
2772 mutex_unlock(&rdev->devlist_mtx);
2773
2774 return ret;
2775}
2776
Jouni Malinene39e5b52012-09-30 19:29:39 +03002777static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2778 enum nl80211_auth_type auth_type,
2779 enum nl80211_commands cmd)
2780{
2781 if (auth_type > NL80211_AUTHTYPE_MAX)
2782 return false;
2783
2784 switch (cmd) {
2785 case NL80211_CMD_AUTHENTICATE:
2786 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2787 auth_type == NL80211_AUTHTYPE_SAE)
2788 return false;
2789 return true;
2790 case NL80211_CMD_CONNECT:
2791 case NL80211_CMD_START_AP:
2792 /* SAE not supported yet */
2793 if (auth_type == NL80211_AUTHTYPE_SAE)
2794 return false;
2795 return true;
2796 default:
2797 return false;
2798 }
2799}
2800
Johannes Berg88600202012-02-13 15:17:18 +01002801static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2802{
2803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2804 struct net_device *dev = info->user_ptr[1];
2805 struct wireless_dev *wdev = dev->ieee80211_ptr;
2806 struct cfg80211_ap_settings params;
2807 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002808 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002809
2810 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2811 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2812 return -EOPNOTSUPP;
2813
2814 if (!rdev->ops->start_ap)
2815 return -EOPNOTSUPP;
2816
2817 if (wdev->beacon_interval)
2818 return -EALREADY;
2819
2820 memset(&params, 0, sizeof(params));
2821
2822 /* these are required for START_AP */
2823 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2824 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2825 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2826 return -EINVAL;
2827
2828 err = nl80211_parse_beacon(info, &params.beacon);
2829 if (err)
2830 return err;
2831
2832 params.beacon_interval =
2833 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2834 params.dtim_period =
2835 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2836
2837 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2838 if (err)
2839 return err;
2840
2841 /*
2842 * In theory, some of these attributes should be required here
2843 * but since they were not used when the command was originally
2844 * added, keep them optional for old user space programs to let
2845 * them continue to work with drivers that do not need the
2846 * additional information -- drivers must check!
2847 */
2848 if (info->attrs[NL80211_ATTR_SSID]) {
2849 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2850 params.ssid_len =
2851 nla_len(info->attrs[NL80211_ATTR_SSID]);
2852 if (params.ssid_len == 0 ||
2853 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2854 return -EINVAL;
2855 }
2856
2857 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2858 params.hidden_ssid = nla_get_u32(
2859 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2860 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2861 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2862 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2863 return -EINVAL;
2864 }
2865
2866 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2867
2868 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2869 params.auth_type = nla_get_u32(
2870 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002871 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2872 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002873 return -EINVAL;
2874 } else
2875 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2876
2877 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2878 NL80211_MAX_NR_CIPHER_SUITES);
2879 if (err)
2880 return err;
2881
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302882 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2883 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2884 return -EOPNOTSUPP;
2885 params.inactivity_timeout = nla_get_u16(
2886 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2887 }
2888
Johannes Berg53cabad2012-11-14 15:17:28 +01002889 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
2890 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2891 return -EINVAL;
2892 params.p2p_ctwindow =
2893 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
2894 if (params.p2p_ctwindow > 127)
2895 return -EINVAL;
2896 if (params.p2p_ctwindow != 0 &&
2897 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
2898 return -EINVAL;
2899 }
2900
2901 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
2902 u8 tmp;
2903
2904 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2905 return -EINVAL;
2906 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
2907 if (tmp > 1)
2908 return -EINVAL;
2909 params.p2p_opp_ps = tmp;
2910 if (params.p2p_opp_ps != 0 &&
2911 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
2912 return -EINVAL;
2913 }
2914
Johannes Bergaa430da2012-05-16 23:50:18 +02002915 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002916 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2917 if (err)
2918 return err;
2919 } else if (wdev->preset_chandef.chan) {
2920 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002921 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002922 return -EINVAL;
2923
Johannes Berg683b6d32012-11-08 21:25:48 +01002924 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002925 return -EINVAL;
2926
Simon Wunderlich04f39042013-02-08 18:16:19 +01002927 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
2928 if (err < 0)
2929 return err;
2930 if (err) {
2931 radar_detect_width = BIT(params.chandef.width);
2932 params.radar_required = true;
2933 }
2934
Michal Kaziore4e32452012-06-29 12:47:08 +02002935 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01002936 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
2937 params.chandef.chan,
2938 CHAN_MODE_SHARED,
2939 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02002940 mutex_unlock(&rdev->devlist_mtx);
2941
2942 if (err)
2943 return err;
2944
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302945 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
2946 params.acl = parse_acl_data(&rdev->wiphy, info);
2947 if (IS_ERR(params.acl))
2948 return PTR_ERR(params.acl);
2949 }
2950
Hila Gonene35e4d22012-06-27 17:19:42 +03002951 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002952 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002953 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002954 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002955 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002956 wdev->ssid_len = params.ssid_len;
2957 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002958 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302959
2960 kfree(params.acl);
2961
Johannes Berg56d18932011-05-09 18:41:15 +02002962 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002963}
2964
Johannes Berg88600202012-02-13 15:17:18 +01002965static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2966{
2967 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2968 struct net_device *dev = info->user_ptr[1];
2969 struct wireless_dev *wdev = dev->ieee80211_ptr;
2970 struct cfg80211_beacon_data params;
2971 int err;
2972
2973 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2974 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2975 return -EOPNOTSUPP;
2976
2977 if (!rdev->ops->change_beacon)
2978 return -EOPNOTSUPP;
2979
2980 if (!wdev->beacon_interval)
2981 return -EINVAL;
2982
2983 err = nl80211_parse_beacon(info, &params);
2984 if (err)
2985 return err;
2986
Hila Gonene35e4d22012-06-27 17:19:42 +03002987 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002988}
2989
2990static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002991{
Johannes Berg4c476992010-10-04 21:36:35 +02002992 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2993 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002994
Michal Kazior60771782012-06-29 12:46:56 +02002995 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002996}
2997
Johannes Berg5727ef12007-12-19 02:03:34 +01002998static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2999 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3000 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3001 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003002 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003003 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003004 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003005};
3006
Johannes Bergeccb8e82009-05-11 21:57:56 +03003007static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003008 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003009 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003010{
3011 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003012 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003013 int flag;
3014
Johannes Bergeccb8e82009-05-11 21:57:56 +03003015 /*
3016 * Try parsing the new attribute first so userspace
3017 * can specify both for older kernels.
3018 */
3019 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3020 if (nla) {
3021 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003022
Johannes Bergeccb8e82009-05-11 21:57:56 +03003023 sta_flags = nla_data(nla);
3024 params->sta_flags_mask = sta_flags->mask;
3025 params->sta_flags_set = sta_flags->set;
3026 if ((params->sta_flags_mask |
3027 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3028 return -EINVAL;
3029 return 0;
3030 }
3031
3032 /* if present, parse the old attribute */
3033
3034 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003035 if (!nla)
3036 return 0;
3037
3038 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3039 nla, sta_flags_policy))
3040 return -EINVAL;
3041
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003042 /*
3043 * Only allow certain flags for interface types so that
3044 * other attributes are silently ignored. Remember that
3045 * this is backward compatibility code with old userspace
3046 * and shouldn't be hit in other cases anyway.
3047 */
3048 switch (iftype) {
3049 case NL80211_IFTYPE_AP:
3050 case NL80211_IFTYPE_AP_VLAN:
3051 case NL80211_IFTYPE_P2P_GO:
3052 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3053 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3054 BIT(NL80211_STA_FLAG_WME) |
3055 BIT(NL80211_STA_FLAG_MFP);
3056 break;
3057 case NL80211_IFTYPE_P2P_CLIENT:
3058 case NL80211_IFTYPE_STATION:
3059 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3060 BIT(NL80211_STA_FLAG_TDLS_PEER);
3061 break;
3062 case NL80211_IFTYPE_MESH_POINT:
3063 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3064 BIT(NL80211_STA_FLAG_MFP) |
3065 BIT(NL80211_STA_FLAG_AUTHORIZED);
3066 default:
3067 return -EINVAL;
3068 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003069
Johannes Berg3383b5a2012-05-10 20:14:43 +02003070 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3071 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003072 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003073
Johannes Berg3383b5a2012-05-10 20:14:43 +02003074 /* no longer support new API additions in old API */
3075 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3076 return -EINVAL;
3077 }
3078 }
3079
Johannes Berg5727ef12007-12-19 02:03:34 +01003080 return 0;
3081}
3082
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003083static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3084 int attr)
3085{
3086 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003087 u32 bitrate;
3088 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003089
3090 rate = nla_nest_start(msg, attr);
3091 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003092 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003093
3094 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3095 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003096 /* report 16-bit bitrate only if we can */
3097 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003098 if (bitrate > 0 &&
3099 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3100 return false;
3101 if (bitrate_compat > 0 &&
3102 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3103 return false;
3104
3105 if (info->flags & RATE_INFO_FLAGS_MCS) {
3106 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3107 return false;
3108 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3109 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3110 return false;
3111 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3112 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3113 return false;
3114 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3115 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3116 return false;
3117 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3118 return false;
3119 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3120 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3121 return false;
3122 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3123 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3124 return false;
3125 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3126 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3127 return false;
3128 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3129 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3130 return false;
3131 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3132 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3133 return false;
3134 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003135
3136 nla_nest_end(msg, rate);
3137 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003138}
3139
Eric W. Biederman15e47302012-09-07 20:12:54 +00003140static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003141 int flags,
3142 struct cfg80211_registered_device *rdev,
3143 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003144 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003145{
3146 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003147 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003148
Eric W. Biederman15e47302012-09-07 20:12:54 +00003149 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003150 if (!hdr)
3151 return -1;
3152
David S. Miller9360ffd2012-03-29 04:41:26 -04003153 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3154 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3155 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3156 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003157
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003158 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3159 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003160 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003161 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3162 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3163 sinfo->connected_time))
3164 goto nla_put_failure;
3165 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3166 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3167 sinfo->inactive_time))
3168 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003169 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3170 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003171 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003172 (u32)sinfo->rx_bytes))
3173 goto nla_put_failure;
3174 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3175 NL80211_STA_INFO_TX_BYTES64)) &&
3176 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3177 (u32)sinfo->tx_bytes))
3178 goto nla_put_failure;
3179 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3180 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003181 sinfo->rx_bytes))
3182 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003183 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3184 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003185 sinfo->tx_bytes))
3186 goto nla_put_failure;
3187 if ((sinfo->filled & STATION_INFO_LLID) &&
3188 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3189 goto nla_put_failure;
3190 if ((sinfo->filled & STATION_INFO_PLID) &&
3191 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3192 goto nla_put_failure;
3193 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3194 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3195 sinfo->plink_state))
3196 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003197 switch (rdev->wiphy.signal_type) {
3198 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003199 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3200 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3201 sinfo->signal))
3202 goto nla_put_failure;
3203 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3204 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3205 sinfo->signal_avg))
3206 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003207 break;
3208 default:
3209 break;
3210 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003211 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003212 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3213 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003214 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003215 }
3216 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3217 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3218 NL80211_STA_INFO_RX_BITRATE))
3219 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003220 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003221 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3222 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3223 sinfo->rx_packets))
3224 goto nla_put_failure;
3225 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3226 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3227 sinfo->tx_packets))
3228 goto nla_put_failure;
3229 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3230 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3231 sinfo->tx_retries))
3232 goto nla_put_failure;
3233 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3234 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3235 sinfo->tx_failed))
3236 goto nla_put_failure;
3237 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3238 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3239 sinfo->beacon_loss_count))
3240 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003241 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3242 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3243 sinfo->local_pm))
3244 goto nla_put_failure;
3245 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3246 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3247 sinfo->peer_pm))
3248 goto nla_put_failure;
3249 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3250 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3251 sinfo->nonpeer_pm))
3252 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003253 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3254 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3255 if (!bss_param)
3256 goto nla_put_failure;
3257
David S. Miller9360ffd2012-03-29 04:41:26 -04003258 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3259 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3260 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3261 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3262 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3263 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3264 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3265 sinfo->bss_param.dtim_period) ||
3266 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3267 sinfo->bss_param.beacon_interval))
3268 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003269
3270 nla_nest_end(msg, bss_param);
3271 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003272 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3273 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3274 sizeof(struct nl80211_sta_flag_update),
3275 &sinfo->sta_flags))
3276 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003277 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3278 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3279 sinfo->t_offset))
3280 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003281 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003282
David S. Miller9360ffd2012-03-29 04:41:26 -04003283 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3284 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3285 sinfo->assoc_req_ies))
3286 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003287
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003288 return genlmsg_end(msg, hdr);
3289
3290 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003291 genlmsg_cancel(msg, hdr);
3292 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003293}
3294
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003295static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003296 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003297{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003298 struct station_info sinfo;
3299 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003300 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003301 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003302 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003303 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003304
Johannes Berg67748892010-10-04 21:14:06 +02003305 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3306 if (err)
3307 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003308
3309 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003310 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003311 goto out_err;
3312 }
3313
Johannes Bergbba95fe2008-07-29 13:22:51 +02003314 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003315 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003316 err = rdev_dump_station(dev, netdev, sta_idx,
3317 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003318 if (err == -ENOENT)
3319 break;
3320 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003321 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003322
3323 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003324 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003325 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003326 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003327 &sinfo) < 0)
3328 goto out;
3329
3330 sta_idx++;
3331 }
3332
3333
3334 out:
3335 cb->args[1] = sta_idx;
3336 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003337 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003338 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003339
3340 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003341}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003342
Johannes Berg5727ef12007-12-19 02:03:34 +01003343static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3344{
Johannes Berg4c476992010-10-04 21:36:35 +02003345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3346 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003347 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003348 struct sk_buff *msg;
3349 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003350 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003351
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003352 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003353
3354 if (!info->attrs[NL80211_ATTR_MAC])
3355 return -EINVAL;
3356
3357 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3358
Johannes Berg4c476992010-10-04 21:36:35 +02003359 if (!rdev->ops->get_station)
3360 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003361
Hila Gonene35e4d22012-06-27 17:19:42 +03003362 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003363 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003364 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003365
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003366 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003367 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003368 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003369
Eric W. Biederman15e47302012-09-07 20:12:54 +00003370 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003371 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003372 nlmsg_free(msg);
3373 return -ENOBUFS;
3374 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003375
Johannes Berg4c476992010-10-04 21:36:35 +02003376 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003377}
3378
3379/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003380 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003381 */
Johannes Berg80b99892011-11-18 16:23:01 +01003382static struct net_device *get_vlan(struct genl_info *info,
3383 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003384{
Johannes Berg463d0182009-07-14 00:33:35 +02003385 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003386 struct net_device *v;
3387 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003388
Johannes Berg80b99892011-11-18 16:23:01 +01003389 if (!vlanattr)
3390 return NULL;
3391
3392 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3393 if (!v)
3394 return ERR_PTR(-ENODEV);
3395
3396 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3397 ret = -EINVAL;
3398 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003399 }
Johannes Berg80b99892011-11-18 16:23:01 +01003400
3401 if (!netif_running(v)) {
3402 ret = -ENETDOWN;
3403 goto error;
3404 }
3405
3406 return v;
3407 error:
3408 dev_put(v);
3409 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003410}
3411
3412static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3413{
Johannes Berg4c476992010-10-04 21:36:35 +02003414 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003415 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003416 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003417 struct station_parameters params;
3418 u8 *mac_addr = NULL;
3419
3420 memset(&params, 0, sizeof(params));
3421
3422 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003423 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003424
3425 if (info->attrs[NL80211_ATTR_STA_AID])
3426 return -EINVAL;
3427
3428 if (!info->attrs[NL80211_ATTR_MAC])
3429 return -EINVAL;
3430
3431 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3432
3433 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3434 params.supported_rates =
3435 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3436 params.supported_rates_len =
3437 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3438 }
3439
Jouni Malinen9d62a982013-02-14 21:10:13 +02003440 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3441 params.capability =
3442 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3443 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3444 }
3445
3446 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3447 params.ext_capab =
3448 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3449 params.ext_capab_len =
3450 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3451 }
3452
Johannes Bergba23d202012-12-27 17:32:09 +01003453 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL] ||
3454 info->attrs[NL80211_ATTR_HT_CAPABILITY])
3455 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003456
Johannes Bergbdd90d52011-12-14 12:20:27 +01003457 if (!rdev->ops->change_station)
3458 return -EOPNOTSUPP;
3459
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003460 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003461 return -EINVAL;
3462
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003463 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3464 params.plink_action =
3465 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3466
Javier Cardona9c3990a2011-05-03 16:57:11 -07003467 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3468 params.plink_state =
3469 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3470
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003471 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3472 enum nl80211_mesh_power_mode pm = nla_get_u32(
3473 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3474
3475 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3476 pm > NL80211_MESH_POWER_MAX)
3477 return -EINVAL;
3478
3479 params.local_pm = pm;
3480 }
3481
Johannes Berga97f4422009-06-18 17:23:43 +02003482 switch (dev->ieee80211_ptr->iftype) {
3483 case NL80211_IFTYPE_AP:
3484 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003485 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003486 /* disallow mesh-specific things */
3487 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003488 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003489 if (params.local_pm)
3490 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003491
3492 /* TDLS can't be set, ... */
3493 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3494 return -EINVAL;
3495 /*
3496 * ... but don't bother the driver with it. This works around
3497 * a hostapd/wpa_supplicant issue -- it always includes the
3498 * TLDS_PEER flag in the mask even for AP mode.
3499 */
3500 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3501
3502 /* accept only the listed bits */
3503 if (params.sta_flags_mask &
3504 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
Johannes Bergd582cff2012-10-26 17:53:44 +02003505 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3506 BIT(NL80211_STA_FLAG_ASSOCIATED) |
Johannes Bergbdd90d52011-12-14 12:20:27 +01003507 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3508 BIT(NL80211_STA_FLAG_WME) |
3509 BIT(NL80211_STA_FLAG_MFP)))
3510 return -EINVAL;
3511
Johannes Bergd582cff2012-10-26 17:53:44 +02003512 /* but authenticated/associated only if driver handles it */
3513 if (!(rdev->wiphy.features &
3514 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3515 params.sta_flags_mask &
3516 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3517 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3518 return -EINVAL;
3519
Johannes Bergba23d202012-12-27 17:32:09 +01003520 /* reject other things that can't change */
3521 if (params.supported_rates)
3522 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003523 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3524 return -EINVAL;
3525 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3526 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003527
Johannes Bergbdd90d52011-12-14 12:20:27 +01003528 /* must be last in here for error handling */
3529 params.vlan = get_vlan(info, rdev);
3530 if (IS_ERR(params.vlan))
3531 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003532 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003533 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003534 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003535 /*
3536 * Don't allow userspace to change the TDLS_PEER flag,
3537 * but silently ignore attempts to change it since we
3538 * don't have state here to verify that it doesn't try
3539 * to change the flag.
3540 */
3541 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003542 /* fall through */
3543 case NL80211_IFTYPE_ADHOC:
3544 /* disallow things sta doesn't support */
3545 if (params.plink_action)
3546 return -EINVAL;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003547 if (params.local_pm)
3548 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003549 /* reject any changes other than AUTHORIZED */
3550 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3551 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003552 break;
3553 case NL80211_IFTYPE_MESH_POINT:
3554 /* disallow things mesh doesn't support */
3555 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003556 return -EINVAL;
Johannes Bergba23d202012-12-27 17:32:09 +01003557 if (params.supported_rates)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003558 return -EINVAL;
Jouni Malinen9d62a982013-02-14 21:10:13 +02003559 if (info->attrs[NL80211_ATTR_STA_CAPABILITY])
3560 return -EINVAL;
3561 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY])
3562 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003563 /*
3564 * No special handling for TDLS here -- the userspace
3565 * mesh code doesn't have this bug.
3566 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003567 if (params.sta_flags_mask &
3568 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003569 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003570 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003571 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003572 break;
3573 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003574 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003575 }
3576
Johannes Bergbdd90d52011-12-14 12:20:27 +01003577 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003578
Hila Gonene35e4d22012-06-27 17:19:42 +03003579 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003580
Johannes Berg5727ef12007-12-19 02:03:34 +01003581 if (params.vlan)
3582 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003583
Johannes Berg5727ef12007-12-19 02:03:34 +01003584 return err;
3585}
3586
Eliad Pellerc75786c2011-08-23 14:37:46 +03003587static struct nla_policy
3588nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3589 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3590 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3591};
3592
Johannes Berg5727ef12007-12-19 02:03:34 +01003593static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3594{
Johannes Berg4c476992010-10-04 21:36:35 +02003595 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003596 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003597 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003598 struct station_parameters params;
3599 u8 *mac_addr = NULL;
3600
3601 memset(&params, 0, sizeof(params));
3602
3603 if (!info->attrs[NL80211_ATTR_MAC])
3604 return -EINVAL;
3605
Johannes Berg5727ef12007-12-19 02:03:34 +01003606 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3607 return -EINVAL;
3608
3609 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3610 return -EINVAL;
3611
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003612 if (!info->attrs[NL80211_ATTR_STA_AID])
3613 return -EINVAL;
3614
Johannes Berg5727ef12007-12-19 02:03:34 +01003615 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3616 params.supported_rates =
3617 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3618 params.supported_rates_len =
3619 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3620 params.listen_interval =
3621 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003622
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003623 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3624 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3625 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003626
Jouni Malinen9d62a982013-02-14 21:10:13 +02003627 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3628 params.capability =
3629 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3630 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3631 }
3632
3633 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3634 params.ext_capab =
3635 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3636 params.ext_capab_len =
3637 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3638 }
3639
Jouni Malinen36aedc92008-08-25 11:58:58 +03003640 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3641 params.ht_capa =
3642 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003643
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003644 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3645 params.vht_capa =
3646 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3647
Javier Cardona96b78df2011-04-07 15:08:33 -07003648 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3649 params.plink_action =
3650 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3651
Johannes Bergbdd90d52011-12-14 12:20:27 +01003652 if (!rdev->ops->add_station)
3653 return -EOPNOTSUPP;
3654
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003655 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003656 return -EINVAL;
3657
Johannes Bergbdd90d52011-12-14 12:20:27 +01003658 switch (dev->ieee80211_ptr->iftype) {
3659 case NL80211_IFTYPE_AP:
3660 case NL80211_IFTYPE_AP_VLAN:
3661 case NL80211_IFTYPE_P2P_GO:
3662 /* parse WME attributes if sta is WME capable */
3663 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3664 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3665 info->attrs[NL80211_ATTR_STA_WME]) {
3666 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3667 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003668
Johannes Bergbdd90d52011-12-14 12:20:27 +01003669 nla = info->attrs[NL80211_ATTR_STA_WME];
3670 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3671 nl80211_sta_wme_policy);
3672 if (err)
3673 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003674
Johannes Bergbdd90d52011-12-14 12:20:27 +01003675 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3676 params.uapsd_queues =
3677 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3678 if (params.uapsd_queues &
3679 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3680 return -EINVAL;
3681
3682 if (tb[NL80211_STA_WME_MAX_SP])
3683 params.max_sp =
3684 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3685
3686 if (params.max_sp &
3687 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3688 return -EINVAL;
3689
3690 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3691 }
3692 /* TDLS peers cannot be added */
3693 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003694 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003695 /* but don't bother the driver with it */
3696 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003697
Johannes Bergd582cff2012-10-26 17:53:44 +02003698 /* allow authenticated/associated only if driver handles it */
3699 if (!(rdev->wiphy.features &
3700 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3701 params.sta_flags_mask &
3702 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3703 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3704 return -EINVAL;
3705
Johannes Bergbdd90d52011-12-14 12:20:27 +01003706 /* must be last in here for error handling */
3707 params.vlan = get_vlan(info, rdev);
3708 if (IS_ERR(params.vlan))
3709 return PTR_ERR(params.vlan);
3710 break;
3711 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergd582cff2012-10-26 17:53:44 +02003712 /* associated is disallowed */
3713 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3714 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003715 /* TDLS peers cannot be added */
3716 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003717 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003718 break;
3719 case NL80211_IFTYPE_STATION:
Johannes Bergd582cff2012-10-26 17:53:44 +02003720 /* associated is disallowed */
3721 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
3722 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003723 /* Only TDLS peers can be added */
3724 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3725 return -EINVAL;
3726 /* Can only add if TDLS ... */
3727 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3728 return -EOPNOTSUPP;
3729 /* ... with external setup is supported */
3730 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3731 return -EOPNOTSUPP;
3732 break;
3733 default:
3734 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003735 }
3736
Johannes Bergbdd90d52011-12-14 12:20:27 +01003737 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003738
Hila Gonene35e4d22012-06-27 17:19:42 +03003739 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003740
Johannes Berg5727ef12007-12-19 02:03:34 +01003741 if (params.vlan)
3742 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003743 return err;
3744}
3745
3746static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3747{
Johannes Berg4c476992010-10-04 21:36:35 +02003748 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3749 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003750 u8 *mac_addr = NULL;
3751
3752 if (info->attrs[NL80211_ATTR_MAC])
3753 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3754
Johannes Berge80cf852009-05-11 14:43:13 +02003755 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003756 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003757 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003758 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3759 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003760
Johannes Berg4c476992010-10-04 21:36:35 +02003761 if (!rdev->ops->del_station)
3762 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003763
Hila Gonene35e4d22012-06-27 17:19:42 +03003764 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003765}
3766
Eric W. Biederman15e47302012-09-07 20:12:54 +00003767static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003768 int flags, struct net_device *dev,
3769 u8 *dst, u8 *next_hop,
3770 struct mpath_info *pinfo)
3771{
3772 void *hdr;
3773 struct nlattr *pinfoattr;
3774
Eric W. Biederman15e47302012-09-07 20:12:54 +00003775 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003776 if (!hdr)
3777 return -1;
3778
David S. Miller9360ffd2012-03-29 04:41:26 -04003779 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3780 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3781 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3782 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3783 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003784
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003785 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3786 if (!pinfoattr)
3787 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003788 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3789 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3790 pinfo->frame_qlen))
3791 goto nla_put_failure;
3792 if (((pinfo->filled & MPATH_INFO_SN) &&
3793 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3794 ((pinfo->filled & MPATH_INFO_METRIC) &&
3795 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3796 pinfo->metric)) ||
3797 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3798 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3799 pinfo->exptime)) ||
3800 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3801 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3802 pinfo->flags)) ||
3803 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3804 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3805 pinfo->discovery_timeout)) ||
3806 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3807 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3808 pinfo->discovery_retries)))
3809 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003810
3811 nla_nest_end(msg, pinfoattr);
3812
3813 return genlmsg_end(msg, hdr);
3814
3815 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003816 genlmsg_cancel(msg, hdr);
3817 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003818}
3819
3820static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003821 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003822{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003823 struct mpath_info pinfo;
3824 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003825 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003826 u8 dst[ETH_ALEN];
3827 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003828 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003829 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003830
Johannes Berg67748892010-10-04 21:14:06 +02003831 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3832 if (err)
3833 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003834
3835 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003836 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003837 goto out_err;
3838 }
3839
Jouni Malineneec60b02009-03-20 21:21:19 +02003840 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3841 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003842 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003843 }
3844
Johannes Bergbba95fe2008-07-29 13:22:51 +02003845 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003846 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3847 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003848 if (err == -ENOENT)
3849 break;
3850 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003851 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003852
Eric W. Biederman15e47302012-09-07 20:12:54 +00003853 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003854 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3855 netdev, dst, next_hop,
3856 &pinfo) < 0)
3857 goto out;
3858
3859 path_idx++;
3860 }
3861
3862
3863 out:
3864 cb->args[1] = path_idx;
3865 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003866 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003867 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003868 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003869}
3870
3871static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3872{
Johannes Berg4c476992010-10-04 21:36:35 +02003873 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003874 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003875 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003876 struct mpath_info pinfo;
3877 struct sk_buff *msg;
3878 u8 *dst = NULL;
3879 u8 next_hop[ETH_ALEN];
3880
3881 memset(&pinfo, 0, sizeof(pinfo));
3882
3883 if (!info->attrs[NL80211_ATTR_MAC])
3884 return -EINVAL;
3885
3886 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3887
Johannes Berg4c476992010-10-04 21:36:35 +02003888 if (!rdev->ops->get_mpath)
3889 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003890
Johannes Berg4c476992010-10-04 21:36:35 +02003891 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3892 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003893
Hila Gonene35e4d22012-06-27 17:19:42 +03003894 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003895 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003896 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003897
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003898 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003899 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003900 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003901
Eric W. Biederman15e47302012-09-07 20:12:54 +00003902 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003903 dev, dst, next_hop, &pinfo) < 0) {
3904 nlmsg_free(msg);
3905 return -ENOBUFS;
3906 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003907
Johannes Berg4c476992010-10-04 21:36:35 +02003908 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003909}
3910
3911static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3912{
Johannes Berg4c476992010-10-04 21:36:35 +02003913 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3914 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003915 u8 *dst = NULL;
3916 u8 *next_hop = NULL;
3917
3918 if (!info->attrs[NL80211_ATTR_MAC])
3919 return -EINVAL;
3920
3921 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3922 return -EINVAL;
3923
3924 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3925 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3926
Johannes Berg4c476992010-10-04 21:36:35 +02003927 if (!rdev->ops->change_mpath)
3928 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003929
Johannes Berg4c476992010-10-04 21:36:35 +02003930 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3931 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003932
Hila Gonene35e4d22012-06-27 17:19:42 +03003933 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003934}
Johannes Berg4c476992010-10-04 21:36:35 +02003935
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003936static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3937{
Johannes Berg4c476992010-10-04 21:36:35 +02003938 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3939 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003940 u8 *dst = NULL;
3941 u8 *next_hop = NULL;
3942
3943 if (!info->attrs[NL80211_ATTR_MAC])
3944 return -EINVAL;
3945
3946 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3947 return -EINVAL;
3948
3949 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3950 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3951
Johannes Berg4c476992010-10-04 21:36:35 +02003952 if (!rdev->ops->add_mpath)
3953 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003954
Johannes Berg4c476992010-10-04 21:36:35 +02003955 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3956 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003957
Hila Gonene35e4d22012-06-27 17:19:42 +03003958 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003959}
3960
3961static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3962{
Johannes Berg4c476992010-10-04 21:36:35 +02003963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3964 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003965 u8 *dst = NULL;
3966
3967 if (info->attrs[NL80211_ATTR_MAC])
3968 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3969
Johannes Berg4c476992010-10-04 21:36:35 +02003970 if (!rdev->ops->del_mpath)
3971 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003972
Hila Gonene35e4d22012-06-27 17:19:42 +03003973 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003974}
3975
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003976static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3977{
Johannes Berg4c476992010-10-04 21:36:35 +02003978 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3979 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003980 struct bss_parameters params;
3981
3982 memset(&params, 0, sizeof(params));
3983 /* default to not changing parameters */
3984 params.use_cts_prot = -1;
3985 params.use_short_preamble = -1;
3986 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003987 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003988 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01003989 params.p2p_ctwindow = -1;
3990 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003991
3992 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3993 params.use_cts_prot =
3994 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3995 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3996 params.use_short_preamble =
3997 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3998 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3999 params.use_short_slot_time =
4000 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004001 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4002 params.basic_rates =
4003 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4004 params.basic_rates_len =
4005 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4006 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004007 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4008 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004009 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4010 params.ht_opmode =
4011 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004012
Johannes Berg53cabad2012-11-14 15:17:28 +01004013 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4014 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4015 return -EINVAL;
4016 params.p2p_ctwindow =
4017 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4018 if (params.p2p_ctwindow < 0)
4019 return -EINVAL;
4020 if (params.p2p_ctwindow != 0 &&
4021 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4022 return -EINVAL;
4023 }
4024
4025 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4026 u8 tmp;
4027
4028 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4029 return -EINVAL;
4030 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4031 if (tmp > 1)
4032 return -EINVAL;
4033 params.p2p_opp_ps = tmp;
4034 if (params.p2p_opp_ps &&
4035 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4036 return -EINVAL;
4037 }
4038
Johannes Berg4c476992010-10-04 21:36:35 +02004039 if (!rdev->ops->change_bss)
4040 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004041
Johannes Berg074ac8d2010-09-16 14:58:22 +02004042 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004043 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4044 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004045
Hila Gonene35e4d22012-06-27 17:19:42 +03004046 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004047}
4048
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004049static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004050 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4051 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4052 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4053 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4054 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4055 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4056};
4057
4058static int parse_reg_rule(struct nlattr *tb[],
4059 struct ieee80211_reg_rule *reg_rule)
4060{
4061 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4062 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4063
4064 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4065 return -EINVAL;
4066 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4067 return -EINVAL;
4068 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4069 return -EINVAL;
4070 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4071 return -EINVAL;
4072 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4073 return -EINVAL;
4074
4075 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4076
4077 freq_range->start_freq_khz =
4078 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4079 freq_range->end_freq_khz =
4080 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4081 freq_range->max_bandwidth_khz =
4082 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4083
4084 power_rule->max_eirp =
4085 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4086
4087 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4088 power_rule->max_antenna_gain =
4089 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4090
4091 return 0;
4092}
4093
4094static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4095{
4096 int r;
4097 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004098 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004099
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004100 /*
4101 * You should only get this when cfg80211 hasn't yet initialized
4102 * completely when built-in to the kernel right between the time
4103 * window between nl80211_init() and regulatory_init(), if that is
4104 * even possible.
4105 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004106 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004107 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004108
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004109 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4110 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004111
4112 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4113
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004114 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4115 user_reg_hint_type =
4116 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4117 else
4118 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4119
4120 switch (user_reg_hint_type) {
4121 case NL80211_USER_REG_HINT_USER:
4122 case NL80211_USER_REG_HINT_CELL_BASE:
4123 break;
4124 default:
4125 return -EINVAL;
4126 }
4127
4128 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004129
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004130 return r;
4131}
4132
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004133static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004134 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004135{
Johannes Berg4c476992010-10-04 21:36:35 +02004136 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004137 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004138 struct wireless_dev *wdev = dev->ieee80211_ptr;
4139 struct mesh_config cur_params;
4140 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004141 void *hdr;
4142 struct nlattr *pinfoattr;
4143 struct sk_buff *msg;
4144
Johannes Berg29cbe682010-12-03 09:20:44 +01004145 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4146 return -EOPNOTSUPP;
4147
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004148 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004149 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004150
Johannes Berg29cbe682010-12-03 09:20:44 +01004151 wdev_lock(wdev);
4152 /* If not connected, get default parameters */
4153 if (!wdev->mesh_id_len)
4154 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4155 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004156 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004157 wdev_unlock(wdev);
4158
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004159 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004160 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004161
4162 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004164 if (!msg)
4165 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004166 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004167 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004168 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004169 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004170 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004171 if (!pinfoattr)
4172 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004173 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4174 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4175 cur_params.dot11MeshRetryTimeout) ||
4176 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4177 cur_params.dot11MeshConfirmTimeout) ||
4178 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4179 cur_params.dot11MeshHoldingTimeout) ||
4180 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4181 cur_params.dot11MeshMaxPeerLinks) ||
4182 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4183 cur_params.dot11MeshMaxRetries) ||
4184 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4185 cur_params.dot11MeshTTL) ||
4186 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4187 cur_params.element_ttl) ||
4188 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4189 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004190 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4191 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004192 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4193 cur_params.dot11MeshHWMPmaxPREQretries) ||
4194 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4195 cur_params.path_refresh_time) ||
4196 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4197 cur_params.min_discovery_timeout) ||
4198 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4199 cur_params.dot11MeshHWMPactivePathTimeout) ||
4200 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4201 cur_params.dot11MeshHWMPpreqMinInterval) ||
4202 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4203 cur_params.dot11MeshHWMPperrMinInterval) ||
4204 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4205 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4206 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4207 cur_params.dot11MeshHWMPRootMode) ||
4208 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4209 cur_params.dot11MeshHWMPRannInterval) ||
4210 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4211 cur_params.dot11MeshGateAnnouncementProtocol) ||
4212 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4213 cur_params.dot11MeshForwarding) ||
4214 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004215 cur_params.rssi_threshold) ||
4216 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004217 cur_params.ht_opmode) ||
4218 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4219 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4220 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004221 cur_params.dot11MeshHWMProotInterval) ||
4222 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004223 cur_params.dot11MeshHWMPconfirmationInterval) ||
4224 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4225 cur_params.power_mode) ||
4226 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4227 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004228 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004229 nla_nest_end(msg, pinfoattr);
4230 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004231 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004232
Johannes Berg3b858752009-03-12 09:55:09 +01004233 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004234 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004235 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004236 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004237 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004238}
4239
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004240static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004241 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4242 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4243 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4244 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4245 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4246 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004247 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004248 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004249 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004250 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4251 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4252 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4253 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4254 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004255 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004256 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004257 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004258 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004259 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004260 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004261 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4262 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004263 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4264 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004265 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004266 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4267 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004268};
4269
Javier Cardonac80d5452010-12-16 17:37:49 -08004270static const struct nla_policy
4271 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004272 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004273 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4274 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004275 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004276 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004277 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004278 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004279};
4280
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004281static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004282 struct mesh_config *cfg,
4283 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004284{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004285 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004286 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004287
Marco Porschea54fba2013-01-07 16:04:48 +01004288#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4289do { \
4290 if (tb[attr]) { \
4291 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4292 return -EINVAL; \
4293 cfg->param = fn(tb[attr]); \
4294 mask |= (1 << (attr - 1)); \
4295 } \
4296} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004297
4298
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004299 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004300 return -EINVAL;
4301 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004302 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004303 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004304 return -EINVAL;
4305
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004306 /* This makes sure that there aren't more than 32 mesh config
4307 * parameters (otherwise our bitfield scheme would not work.) */
4308 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4309
4310 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004311 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004312 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4313 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004314 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004315 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4316 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004317 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004318 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4319 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004320 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004321 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4322 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004323 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004324 mask, NL80211_MESHCONF_MAX_RETRIES,
4325 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004326 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004327 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004328 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004329 mask, NL80211_MESHCONF_ELEMENT_TTL,
4330 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004331 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004332 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4333 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004334 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4335 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004336 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4337 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004338 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004339 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4340 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004341 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004342 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4343 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004344 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004345 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4346 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004347 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4348 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004349 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4350 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004351 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004352 1, 65535, mask,
4353 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004354 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004355 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004356 1, 65535, mask,
4357 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004358 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004359 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004360 dot11MeshHWMPnetDiameterTraversalTime,
4361 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004362 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4363 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004364 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4365 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4366 nla_get_u8);
4367 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4368 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004369 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004370 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004371 dot11MeshGateAnnouncementProtocol, 0, 1,
4372 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004373 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004374 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004375 mask, NL80211_MESHCONF_FORWARDING,
4376 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004377 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004378 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4379 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004380 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004381 mask, NL80211_MESHCONF_HT_OPMODE,
4382 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004383 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004384 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004385 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4386 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004387 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004388 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4389 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004390 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004391 dot11MeshHWMPconfirmationInterval,
4392 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004393 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4394 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004395 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4396 NL80211_MESH_POWER_ACTIVE,
4397 NL80211_MESH_POWER_MAX,
4398 mask, NL80211_MESHCONF_POWER_MODE,
4399 nla_get_u32);
4400 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4401 0, 65535, mask,
4402 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004403 if (mask_out)
4404 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004405
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004406 return 0;
4407
4408#undef FILL_IN_MESH_PARAM_IF_SET
4409}
4410
Javier Cardonac80d5452010-12-16 17:37:49 -08004411static int nl80211_parse_mesh_setup(struct genl_info *info,
4412 struct mesh_setup *setup)
4413{
4414 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4415
4416 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4417 return -EINVAL;
4418 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4419 info->attrs[NL80211_ATTR_MESH_SETUP],
4420 nl80211_mesh_setup_params_policy))
4421 return -EINVAL;
4422
Javier Cardonad299a1f2012-03-31 11:31:33 -07004423 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4424 setup->sync_method =
4425 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4426 IEEE80211_SYNC_METHOD_VENDOR :
4427 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4428
Javier Cardonac80d5452010-12-16 17:37:49 -08004429 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4430 setup->path_sel_proto =
4431 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4432 IEEE80211_PATH_PROTOCOL_VENDOR :
4433 IEEE80211_PATH_PROTOCOL_HWMP;
4434
4435 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4436 setup->path_metric =
4437 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4438 IEEE80211_PATH_METRIC_VENDOR :
4439 IEEE80211_PATH_METRIC_AIRTIME;
4440
Javier Cardona581a8b02011-04-07 15:08:27 -07004441
4442 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004443 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004444 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004445 if (!is_valid_ie_attr(ieattr))
4446 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004447 setup->ie = nla_data(ieattr);
4448 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004449 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004450 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4451 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004452
4453 return 0;
4454}
4455
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004456static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004457 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004458{
4459 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4460 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004461 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004462 struct mesh_config cfg;
4463 u32 mask;
4464 int err;
4465
Johannes Berg29cbe682010-12-03 09:20:44 +01004466 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4467 return -EOPNOTSUPP;
4468
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004469 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004470 return -EOPNOTSUPP;
4471
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004472 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004473 if (err)
4474 return err;
4475
Johannes Berg29cbe682010-12-03 09:20:44 +01004476 wdev_lock(wdev);
4477 if (!wdev->mesh_id_len)
4478 err = -ENOLINK;
4479
4480 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004481 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004482
4483 wdev_unlock(wdev);
4484
4485 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004486}
4487
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004488static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4489{
Johannes Berg458f4f92012-12-06 15:47:38 +01004490 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004491 struct sk_buff *msg;
4492 void *hdr = NULL;
4493 struct nlattr *nl_reg_rules;
4494 unsigned int i;
4495 int err = -EINVAL;
4496
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004497 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004498
4499 if (!cfg80211_regdomain)
4500 goto out;
4501
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004502 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004503 if (!msg) {
4504 err = -ENOBUFS;
4505 goto out;
4506 }
4507
Eric W. Biederman15e47302012-09-07 20:12:54 +00004508 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004509 NL80211_CMD_GET_REG);
4510 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004511 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004512
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004513 if (reg_last_request_cell_base() &&
4514 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4515 NL80211_USER_REG_HINT_CELL_BASE))
4516 goto nla_put_failure;
4517
Johannes Berg458f4f92012-12-06 15:47:38 +01004518 rcu_read_lock();
4519 regdom = rcu_dereference(cfg80211_regdomain);
4520
4521 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4522 (regdom->dfs_region &&
4523 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4524 goto nla_put_failure_rcu;
4525
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004526 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4527 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004528 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004529
Johannes Berg458f4f92012-12-06 15:47:38 +01004530 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004531 struct nlattr *nl_reg_rule;
4532 const struct ieee80211_reg_rule *reg_rule;
4533 const struct ieee80211_freq_range *freq_range;
4534 const struct ieee80211_power_rule *power_rule;
4535
Johannes Berg458f4f92012-12-06 15:47:38 +01004536 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004537 freq_range = &reg_rule->freq_range;
4538 power_rule = &reg_rule->power_rule;
4539
4540 nl_reg_rule = nla_nest_start(msg, i);
4541 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004542 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004543
David S. Miller9360ffd2012-03-29 04:41:26 -04004544 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4545 reg_rule->flags) ||
4546 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4547 freq_range->start_freq_khz) ||
4548 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4549 freq_range->end_freq_khz) ||
4550 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4551 freq_range->max_bandwidth_khz) ||
4552 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4553 power_rule->max_antenna_gain) ||
4554 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4555 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004556 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004557
4558 nla_nest_end(msg, nl_reg_rule);
4559 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004560 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004561
4562 nla_nest_end(msg, nl_reg_rules);
4563
4564 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004565 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004566 goto out;
4567
Johannes Berg458f4f92012-12-06 15:47:38 +01004568nla_put_failure_rcu:
4569 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004570nla_put_failure:
4571 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004572put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004573 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004574 err = -EMSGSIZE;
4575out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004576 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004577 return err;
4578}
4579
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004580static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4581{
4582 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4583 struct nlattr *nl_reg_rule;
4584 char *alpha2 = NULL;
4585 int rem_reg_rules = 0, r = 0;
4586 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004587 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004588 struct ieee80211_regdomain *rd = NULL;
4589
4590 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4591 return -EINVAL;
4592
4593 if (!info->attrs[NL80211_ATTR_REG_RULES])
4594 return -EINVAL;
4595
4596 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4597
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004598 if (info->attrs[NL80211_ATTR_DFS_REGION])
4599 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4600
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004601 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004602 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004603 num_rules++;
4604 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004605 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004606 }
4607
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004608 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004609 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004610
4611 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004612 if (!rd)
4613 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004614
4615 rd->n_reg_rules = num_rules;
4616 rd->alpha2[0] = alpha2[0];
4617 rd->alpha2[1] = alpha2[1];
4618
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004619 /*
4620 * Disable DFS master mode if the DFS region was
4621 * not supported or known on this kernel.
4622 */
4623 if (reg_supported_dfs_region(dfs_region))
4624 rd->dfs_region = dfs_region;
4625
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004626 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004627 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004628 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004629 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4630 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004631 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4632 if (r)
4633 goto bad_reg;
4634
4635 rule_idx++;
4636
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004637 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4638 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004639 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004640 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004641 }
4642
Johannes Berg6913b492012-12-04 00:48:59 +01004643 mutex_lock(&cfg80211_mutex);
4644
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004645 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01004646 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01004647 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01004648 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004649
Johannes Bergd2372b32008-10-24 20:32:20 +02004650 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004651 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004652 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004653}
4654
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004655static int validate_scan_freqs(struct nlattr *freqs)
4656{
4657 struct nlattr *attr1, *attr2;
4658 int n_channels = 0, tmp1, tmp2;
4659
4660 nla_for_each_nested(attr1, freqs, tmp1) {
4661 n_channels++;
4662 /*
4663 * Some hardware has a limited channel list for
4664 * scanning, and it is pretty much nonsensical
4665 * to scan for a channel twice, so disallow that
4666 * and don't require drivers to check that the
4667 * channel list they get isn't longer than what
4668 * they can scan, as long as they can scan all
4669 * the channels they registered at once.
4670 */
4671 nla_for_each_nested(attr2, freqs, tmp2)
4672 if (attr1 != attr2 &&
4673 nla_get_u32(attr1) == nla_get_u32(attr2))
4674 return 0;
4675 }
4676
4677 return n_channels;
4678}
4679
Johannes Berg2a519312009-02-10 21:25:55 +01004680static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4681{
Johannes Berg4c476992010-10-04 21:36:35 +02004682 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004683 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004684 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004685 struct nlattr *attr;
4686 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004687 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004688 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004689
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004690 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4691 return -EINVAL;
4692
Johannes Berg79c97e92009-07-07 03:56:12 +02004693 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004694
Johannes Berg4c476992010-10-04 21:36:35 +02004695 if (!rdev->ops->scan)
4696 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004697
Johannes Berg4c476992010-10-04 21:36:35 +02004698 if (rdev->scan_req)
4699 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004700
4701 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004702 n_channels = validate_scan_freqs(
4703 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004704 if (!n_channels)
4705 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004706 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004707 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004708 n_channels = 0;
4709
Johannes Berg2a519312009-02-10 21:25:55 +01004710 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4711 if (wiphy->bands[band])
4712 n_channels += wiphy->bands[band]->n_channels;
4713 }
4714
4715 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4716 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4717 n_ssids++;
4718
Johannes Berg4c476992010-10-04 21:36:35 +02004719 if (n_ssids > wiphy->max_scan_ssids)
4720 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004721
Jouni Malinen70692ad2009-02-16 19:39:13 +02004722 if (info->attrs[NL80211_ATTR_IE])
4723 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4724 else
4725 ie_len = 0;
4726
Johannes Berg4c476992010-10-04 21:36:35 +02004727 if (ie_len > wiphy->max_scan_ie_len)
4728 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004729
Johannes Berg2a519312009-02-10 21:25:55 +01004730 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004731 + sizeof(*request->ssids) * n_ssids
4732 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004733 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004734 if (!request)
4735 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004736
Johannes Berg2a519312009-02-10 21:25:55 +01004737 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004738 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004739 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004740 if (ie_len) {
4741 if (request->ssids)
4742 request->ie = (void *)(request->ssids + n_ssids);
4743 else
4744 request->ie = (void *)(request->channels + n_channels);
4745 }
Johannes Berg2a519312009-02-10 21:25:55 +01004746
Johannes Berg584991d2009-11-02 13:32:03 +01004747 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004748 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4749 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004750 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004751 struct ieee80211_channel *chan;
4752
4753 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4754
4755 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004756 err = -EINVAL;
4757 goto out_free;
4758 }
Johannes Berg584991d2009-11-02 13:32:03 +01004759
4760 /* ignore disabled channels */
4761 if (chan->flags & IEEE80211_CHAN_DISABLED)
4762 continue;
4763
4764 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004765 i++;
4766 }
4767 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004768 enum ieee80211_band band;
4769
Johannes Berg2a519312009-02-10 21:25:55 +01004770 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004771 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4772 int j;
4773 if (!wiphy->bands[band])
4774 continue;
4775 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004776 struct ieee80211_channel *chan;
4777
4778 chan = &wiphy->bands[band]->channels[j];
4779
4780 if (chan->flags & IEEE80211_CHAN_DISABLED)
4781 continue;
4782
4783 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004784 i++;
4785 }
4786 }
4787 }
4788
Johannes Berg584991d2009-11-02 13:32:03 +01004789 if (!i) {
4790 err = -EINVAL;
4791 goto out_free;
4792 }
4793
4794 request->n_channels = i;
4795
Johannes Berg2a519312009-02-10 21:25:55 +01004796 i = 0;
4797 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4798 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004799 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004800 err = -EINVAL;
4801 goto out_free;
4802 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004803 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004804 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004805 i++;
4806 }
4807 }
4808
Jouni Malinen70692ad2009-02-16 19:39:13 +02004809 if (info->attrs[NL80211_ATTR_IE]) {
4810 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004811 memcpy((void *)request->ie,
4812 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004813 request->ie_len);
4814 }
4815
Johannes Berg34850ab2011-07-18 18:08:35 +02004816 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004817 if (wiphy->bands[i])
4818 request->rates[i] =
4819 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004820
4821 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4822 nla_for_each_nested(attr,
4823 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4824 tmp) {
4825 enum ieee80211_band band = nla_type(attr);
4826
Dan Carpenter84404622011-07-29 11:52:18 +03004827 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004828 err = -EINVAL;
4829 goto out_free;
4830 }
4831 err = ieee80211_get_ratemask(wiphy->bands[band],
4832 nla_data(attr),
4833 nla_len(attr),
4834 &request->rates[band]);
4835 if (err)
4836 goto out_free;
4837 }
4838 }
4839
Sam Leffler46856bb2012-10-11 21:03:32 -07004840 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004841 request->flags = nla_get_u32(
4842 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004843 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4844 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4845 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4846 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004847 err = -EOPNOTSUPP;
4848 goto out_free;
4849 }
4850 }
Sam Lefflered4737712012-10-11 21:03:31 -07004851
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304852 request->no_cck =
4853 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4854
Johannes Bergfd014282012-06-18 19:17:03 +02004855 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004856 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004857 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004858
Johannes Berg79c97e92009-07-07 03:56:12 +02004859 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004860 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004861
Johannes Berg463d0182009-07-14 00:33:35 +02004862 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004863 nl80211_send_scan_start(rdev, wdev);
4864 if (wdev->netdev)
4865 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004866 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004867 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004868 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004869 kfree(request);
4870 }
Johannes Berg3b858752009-03-12 09:55:09 +01004871
Johannes Berg2a519312009-02-10 21:25:55 +01004872 return err;
4873}
4874
Luciano Coelho807f8a82011-05-11 17:09:35 +03004875static int nl80211_start_sched_scan(struct sk_buff *skb,
4876 struct genl_info *info)
4877{
4878 struct cfg80211_sched_scan_request *request;
4879 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4880 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004881 struct nlattr *attr;
4882 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004883 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004884 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004885 enum ieee80211_band band;
4886 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004887 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004888
4889 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4890 !rdev->ops->sched_scan_start)
4891 return -EOPNOTSUPP;
4892
4893 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4894 return -EINVAL;
4895
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004896 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4897 return -EINVAL;
4898
4899 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4900 if (interval == 0)
4901 return -EINVAL;
4902
Luciano Coelho807f8a82011-05-11 17:09:35 +03004903 wiphy = &rdev->wiphy;
4904
4905 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4906 n_channels = validate_scan_freqs(
4907 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4908 if (!n_channels)
4909 return -EINVAL;
4910 } else {
4911 n_channels = 0;
4912
4913 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4914 if (wiphy->bands[band])
4915 n_channels += wiphy->bands[band]->n_channels;
4916 }
4917
4918 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4919 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4920 tmp)
4921 n_ssids++;
4922
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004923 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004924 return -EINVAL;
4925
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004926 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4927 nla_for_each_nested(attr,
4928 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4929 tmp)
4930 n_match_sets++;
4931
4932 if (n_match_sets > wiphy->max_match_sets)
4933 return -EINVAL;
4934
Luciano Coelho807f8a82011-05-11 17:09:35 +03004935 if (info->attrs[NL80211_ATTR_IE])
4936 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4937 else
4938 ie_len = 0;
4939
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004940 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004941 return -EINVAL;
4942
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004943 mutex_lock(&rdev->sched_scan_mtx);
4944
4945 if (rdev->sched_scan_req) {
4946 err = -EINPROGRESS;
4947 goto out;
4948 }
4949
Luciano Coelho807f8a82011-05-11 17:09:35 +03004950 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004951 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004952 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004953 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004954 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004955 if (!request) {
4956 err = -ENOMEM;
4957 goto out;
4958 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004959
4960 if (n_ssids)
4961 request->ssids = (void *)&request->channels[n_channels];
4962 request->n_ssids = n_ssids;
4963 if (ie_len) {
4964 if (request->ssids)
4965 request->ie = (void *)(request->ssids + n_ssids);
4966 else
4967 request->ie = (void *)(request->channels + n_channels);
4968 }
4969
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004970 if (n_match_sets) {
4971 if (request->ie)
4972 request->match_sets = (void *)(request->ie + ie_len);
4973 else if (request->ssids)
4974 request->match_sets =
4975 (void *)(request->ssids + n_ssids);
4976 else
4977 request->match_sets =
4978 (void *)(request->channels + n_channels);
4979 }
4980 request->n_match_sets = n_match_sets;
4981
Luciano Coelho807f8a82011-05-11 17:09:35 +03004982 i = 0;
4983 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4984 /* user specified, bail out if channel not found */
4985 nla_for_each_nested(attr,
4986 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4987 tmp) {
4988 struct ieee80211_channel *chan;
4989
4990 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4991
4992 if (!chan) {
4993 err = -EINVAL;
4994 goto out_free;
4995 }
4996
4997 /* ignore disabled channels */
4998 if (chan->flags & IEEE80211_CHAN_DISABLED)
4999 continue;
5000
5001 request->channels[i] = chan;
5002 i++;
5003 }
5004 } else {
5005 /* all channels */
5006 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5007 int j;
5008 if (!wiphy->bands[band])
5009 continue;
5010 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5011 struct ieee80211_channel *chan;
5012
5013 chan = &wiphy->bands[band]->channels[j];
5014
5015 if (chan->flags & IEEE80211_CHAN_DISABLED)
5016 continue;
5017
5018 request->channels[i] = chan;
5019 i++;
5020 }
5021 }
5022 }
5023
5024 if (!i) {
5025 err = -EINVAL;
5026 goto out_free;
5027 }
5028
5029 request->n_channels = i;
5030
5031 i = 0;
5032 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5033 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5034 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005035 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005036 err = -EINVAL;
5037 goto out_free;
5038 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005039 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005040 memcpy(request->ssids[i].ssid, nla_data(attr),
5041 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005042 i++;
5043 }
5044 }
5045
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005046 i = 0;
5047 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5048 nla_for_each_nested(attr,
5049 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5050 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005051 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005052
5053 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5054 nla_data(attr), nla_len(attr),
5055 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005056 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005057 if (ssid) {
5058 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5059 err = -EINVAL;
5060 goto out_free;
5061 }
5062 memcpy(request->match_sets[i].ssid.ssid,
5063 nla_data(ssid), nla_len(ssid));
5064 request->match_sets[i].ssid.ssid_len =
5065 nla_len(ssid);
5066 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005067 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5068 if (rssi)
5069 request->rssi_thold = nla_get_u32(rssi);
5070 else
5071 request->rssi_thold =
5072 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005073 i++;
5074 }
5075 }
5076
Luciano Coelho807f8a82011-05-11 17:09:35 +03005077 if (info->attrs[NL80211_ATTR_IE]) {
5078 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5079 memcpy((void *)request->ie,
5080 nla_data(info->attrs[NL80211_ATTR_IE]),
5081 request->ie_len);
5082 }
5083
Sam Leffler46856bb2012-10-11 21:03:32 -07005084 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005085 request->flags = nla_get_u32(
5086 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005087 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5088 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5089 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5090 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005091 err = -EOPNOTSUPP;
5092 goto out_free;
5093 }
5094 }
Sam Lefflered4737712012-10-11 21:03:31 -07005095
Luciano Coelho807f8a82011-05-11 17:09:35 +03005096 request->dev = dev;
5097 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005098 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005099 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005100
Hila Gonene35e4d22012-06-27 17:19:42 +03005101 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005102 if (!err) {
5103 rdev->sched_scan_req = request;
5104 nl80211_send_sched_scan(rdev, dev,
5105 NL80211_CMD_START_SCHED_SCAN);
5106 goto out;
5107 }
5108
5109out_free:
5110 kfree(request);
5111out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005112 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005113 return err;
5114}
5115
5116static int nl80211_stop_sched_scan(struct sk_buff *skb,
5117 struct genl_info *info)
5118{
5119 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005120 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005121
5122 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5123 !rdev->ops->sched_scan_stop)
5124 return -EOPNOTSUPP;
5125
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005126 mutex_lock(&rdev->sched_scan_mtx);
5127 err = __cfg80211_stop_sched_scan(rdev, false);
5128 mutex_unlock(&rdev->sched_scan_mtx);
5129
5130 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005131}
5132
Simon Wunderlich04f39042013-02-08 18:16:19 +01005133static int nl80211_start_radar_detection(struct sk_buff *skb,
5134 struct genl_info *info)
5135{
5136 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5137 struct net_device *dev = info->user_ptr[1];
5138 struct wireless_dev *wdev = dev->ieee80211_ptr;
5139 struct cfg80211_chan_def chandef;
5140 int err;
5141
5142 err = nl80211_parse_chandef(rdev, info, &chandef);
5143 if (err)
5144 return err;
5145
5146 if (wdev->cac_started)
5147 return -EBUSY;
5148
5149 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5150 if (err < 0)
5151 return err;
5152
5153 if (err == 0)
5154 return -EINVAL;
5155
5156 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5157 return -EINVAL;
5158
5159 if (!rdev->ops->start_radar_detection)
5160 return -EOPNOTSUPP;
5161
5162 mutex_lock(&rdev->devlist_mtx);
5163 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5164 chandef.chan, CHAN_MODE_SHARED,
5165 BIT(chandef.width));
5166 if (err)
5167 goto err_locked;
5168
5169 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5170 if (!err) {
5171 wdev->channel = chandef.chan;
5172 wdev->cac_started = true;
5173 wdev->cac_start_time = jiffies;
5174 }
5175err_locked:
5176 mutex_unlock(&rdev->devlist_mtx);
5177
5178 return err;
5179}
5180
Johannes Berg9720bb32011-06-21 09:45:33 +02005181static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5182 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005183 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005184 struct wireless_dev *wdev,
5185 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005186{
Johannes Berg48ab9052009-07-10 18:42:31 +02005187 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005188 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005189 void *hdr;
5190 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005191 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005192
5193 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005194
Eric W. Biederman15e47302012-09-07 20:12:54 +00005195 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005196 NL80211_CMD_NEW_SCAN_RESULTS);
5197 if (!hdr)
5198 return -1;
5199
Johannes Berg9720bb32011-06-21 09:45:33 +02005200 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5201
David S. Miller9360ffd2012-03-29 04:41:26 -04005202 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
5203 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5204 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005205
5206 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5207 if (!bss)
5208 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005209 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005210 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005211 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005212
5213 rcu_read_lock();
5214 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005215 if (ies) {
5216 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5217 goto fail_unlock_rcu;
5218 tsf = true;
5219 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5220 ies->len, ies->data))
5221 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005222 }
5223 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005224 if (ies) {
5225 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5226 goto fail_unlock_rcu;
5227 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5228 ies->len, ies->data))
5229 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005230 }
5231 rcu_read_unlock();
5232
David S. Miller9360ffd2012-03-29 04:41:26 -04005233 if (res->beacon_interval &&
5234 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5235 goto nla_put_failure;
5236 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5237 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5238 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5239 jiffies_to_msecs(jiffies - intbss->ts)))
5240 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005241
Johannes Berg77965c92009-02-18 18:45:06 +01005242 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005243 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005244 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5245 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005246 break;
5247 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005248 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5249 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005250 break;
5251 default:
5252 break;
5253 }
5254
Johannes Berg48ab9052009-07-10 18:42:31 +02005255 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005256 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005257 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005258 if (intbss == wdev->current_bss &&
5259 nla_put_u32(msg, NL80211_BSS_STATUS,
5260 NL80211_BSS_STATUS_ASSOCIATED))
5261 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005262 break;
5263 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005264 if (intbss == wdev->current_bss &&
5265 nla_put_u32(msg, NL80211_BSS_STATUS,
5266 NL80211_BSS_STATUS_IBSS_JOINED))
5267 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005268 break;
5269 default:
5270 break;
5271 }
5272
Johannes Berg2a519312009-02-10 21:25:55 +01005273 nla_nest_end(msg, bss);
5274
5275 return genlmsg_end(msg, hdr);
5276
Johannes Berg8cef2c92013-02-05 16:54:31 +01005277 fail_unlock_rcu:
5278 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005279 nla_put_failure:
5280 genlmsg_cancel(msg, hdr);
5281 return -EMSGSIZE;
5282}
5283
5284static int nl80211_dump_scan(struct sk_buff *skb,
5285 struct netlink_callback *cb)
5286{
Johannes Berg48ab9052009-07-10 18:42:31 +02005287 struct cfg80211_registered_device *rdev;
5288 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01005289 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005290 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005291 int start = cb->args[1], idx = 0;
5292 int err;
5293
Johannes Berg67748892010-10-04 21:14:06 +02005294 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
5295 if (err)
5296 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005297
Johannes Berg48ab9052009-07-10 18:42:31 +02005298 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01005299
Johannes Berg48ab9052009-07-10 18:42:31 +02005300 wdev_lock(wdev);
5301 spin_lock_bh(&rdev->bss_lock);
5302 cfg80211_bss_expire(rdev);
5303
Johannes Berg9720bb32011-06-21 09:45:33 +02005304 cb->seq = rdev->bss_generation;
5305
Johannes Berg48ab9052009-07-10 18:42:31 +02005306 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005307 if (++idx <= start)
5308 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005309 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005310 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005311 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005312 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005313 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005314 }
5315 }
5316
Johannes Berg48ab9052009-07-10 18:42:31 +02005317 spin_unlock_bh(&rdev->bss_lock);
5318 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005319
5320 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02005321 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005322
Johannes Berg67748892010-10-04 21:14:06 +02005323 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005324}
5325
Eric W. Biederman15e47302012-09-07 20:12:54 +00005326static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005327 int flags, struct net_device *dev,
5328 struct survey_info *survey)
5329{
5330 void *hdr;
5331 struct nlattr *infoattr;
5332
Eric W. Biederman15e47302012-09-07 20:12:54 +00005333 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005334 NL80211_CMD_NEW_SURVEY_RESULTS);
5335 if (!hdr)
5336 return -ENOMEM;
5337
David S. Miller9360ffd2012-03-29 04:41:26 -04005338 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5339 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005340
5341 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5342 if (!infoattr)
5343 goto nla_put_failure;
5344
David S. Miller9360ffd2012-03-29 04:41:26 -04005345 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5346 survey->channel->center_freq))
5347 goto nla_put_failure;
5348
5349 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5350 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5351 goto nla_put_failure;
5352 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5353 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5354 goto nla_put_failure;
5355 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5356 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5357 survey->channel_time))
5358 goto nla_put_failure;
5359 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5360 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5361 survey->channel_time_busy))
5362 goto nla_put_failure;
5363 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5364 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5365 survey->channel_time_ext_busy))
5366 goto nla_put_failure;
5367 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5368 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5369 survey->channel_time_rx))
5370 goto nla_put_failure;
5371 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5372 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5373 survey->channel_time_tx))
5374 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005375
5376 nla_nest_end(msg, infoattr);
5377
5378 return genlmsg_end(msg, hdr);
5379
5380 nla_put_failure:
5381 genlmsg_cancel(msg, hdr);
5382 return -EMSGSIZE;
5383}
5384
5385static int nl80211_dump_survey(struct sk_buff *skb,
5386 struct netlink_callback *cb)
5387{
5388 struct survey_info survey;
5389 struct cfg80211_registered_device *dev;
5390 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01005391 int survey_idx = cb->args[1];
5392 int res;
5393
Johannes Berg67748892010-10-04 21:14:06 +02005394 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
5395 if (res)
5396 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005397
5398 if (!dev->ops->dump_survey) {
5399 res = -EOPNOTSUPP;
5400 goto out_err;
5401 }
5402
5403 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005404 struct ieee80211_channel *chan;
5405
Hila Gonene35e4d22012-06-27 17:19:42 +03005406 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005407 if (res == -ENOENT)
5408 break;
5409 if (res)
5410 goto out_err;
5411
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005412 /* Survey without a channel doesn't make sense */
5413 if (!survey.channel) {
5414 res = -EINVAL;
5415 goto out;
5416 }
5417
5418 chan = ieee80211_get_channel(&dev->wiphy,
5419 survey.channel->center_freq);
5420 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5421 survey_idx++;
5422 continue;
5423 }
5424
Holger Schurig61fa7132009-11-11 12:25:40 +01005425 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005426 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005427 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5428 netdev,
5429 &survey) < 0)
5430 goto out;
5431 survey_idx++;
5432 }
5433
5434 out:
5435 cb->args[1] = survey_idx;
5436 res = skb->len;
5437 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02005438 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005439 return res;
5440}
5441
Samuel Ortizb23aa672009-07-01 21:26:54 +02005442static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5443{
5444 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5445 NL80211_WPA_VERSION_2));
5446}
5447
Jouni Malinen636a5d32009-03-19 13:39:22 +02005448static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5449{
Johannes Berg4c476992010-10-04 21:36:35 +02005450 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5451 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005452 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005453 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5454 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005455 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005456 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005457 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005458
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005459 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5460 return -EINVAL;
5461
5462 if (!info->attrs[NL80211_ATTR_MAC])
5463 return -EINVAL;
5464
Jouni Malinen17780922009-03-27 20:52:47 +02005465 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5466 return -EINVAL;
5467
Johannes Berg19957bb2009-07-02 17:20:43 +02005468 if (!info->attrs[NL80211_ATTR_SSID])
5469 return -EINVAL;
5470
5471 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5472 return -EINVAL;
5473
Johannes Bergfffd0932009-07-08 14:22:54 +02005474 err = nl80211_parse_key(info, &key);
5475 if (err)
5476 return err;
5477
5478 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005479 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5480 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005481 if (!key.p.key || !key.p.key_len)
5482 return -EINVAL;
5483 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5484 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5485 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5486 key.p.key_len != WLAN_KEY_LEN_WEP104))
5487 return -EINVAL;
5488 if (key.idx > 4)
5489 return -EINVAL;
5490 } else {
5491 key.p.key_len = 0;
5492 key.p.key = NULL;
5493 }
5494
Johannes Bergafea0b72010-08-10 09:46:42 +02005495 if (key.idx >= 0) {
5496 int i;
5497 bool ok = false;
5498 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5499 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5500 ok = true;
5501 break;
5502 }
5503 }
Johannes Berg4c476992010-10-04 21:36:35 +02005504 if (!ok)
5505 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005506 }
5507
Johannes Berg4c476992010-10-04 21:36:35 +02005508 if (!rdev->ops->auth)
5509 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005510
Johannes Berg074ac8d2010-09-16 14:58:22 +02005511 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005512 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5513 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005514
Johannes Berg19957bb2009-07-02 17:20:43 +02005515 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005516 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005517 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005518 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5519 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005520
Johannes Berg19957bb2009-07-02 17:20:43 +02005521 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5522 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5523
5524 if (info->attrs[NL80211_ATTR_IE]) {
5525 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5526 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5527 }
5528
5529 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005530 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005531 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005532
Jouni Malinene39e5b52012-09-30 19:29:39 +03005533 if (auth_type == NL80211_AUTHTYPE_SAE &&
5534 !info->attrs[NL80211_ATTR_SAE_DATA])
5535 return -EINVAL;
5536
5537 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5538 if (auth_type != NL80211_AUTHTYPE_SAE)
5539 return -EINVAL;
5540 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5541 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5542 /* need to include at least Auth Transaction and Status Code */
5543 if (sae_data_len < 4)
5544 return -EINVAL;
5545 }
5546
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005547 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5548
Johannes Berg95de8172012-01-20 13:55:25 +01005549 /*
5550 * Since we no longer track auth state, ignore
5551 * requests to only change local state.
5552 */
5553 if (local_state_change)
5554 return 0;
5555
Johannes Berg4c476992010-10-04 21:36:35 +02005556 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5557 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005558 key.p.key, key.p.key_len, key.idx,
5559 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005560}
5561
Johannes Bergc0692b82010-08-27 14:26:53 +03005562static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5563 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005564 struct cfg80211_crypto_settings *settings,
5565 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005566{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005567 memset(settings, 0, sizeof(*settings));
5568
Samuel Ortizb23aa672009-07-01 21:26:54 +02005569 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5570
Johannes Bergc0692b82010-08-27 14:26:53 +03005571 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5572 u16 proto;
5573 proto = nla_get_u16(
5574 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5575 settings->control_port_ethertype = cpu_to_be16(proto);
5576 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5577 proto != ETH_P_PAE)
5578 return -EINVAL;
5579 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5580 settings->control_port_no_encrypt = true;
5581 } else
5582 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5583
Samuel Ortizb23aa672009-07-01 21:26:54 +02005584 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5585 void *data;
5586 int len, i;
5587
5588 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5589 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5590 settings->n_ciphers_pairwise = len / sizeof(u32);
5591
5592 if (len % sizeof(u32))
5593 return -EINVAL;
5594
Johannes Berg3dc27d22009-07-02 21:36:37 +02005595 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005596 return -EINVAL;
5597
5598 memcpy(settings->ciphers_pairwise, data, len);
5599
5600 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005601 if (!cfg80211_supported_cipher_suite(
5602 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005603 settings->ciphers_pairwise[i]))
5604 return -EINVAL;
5605 }
5606
5607 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5608 settings->cipher_group =
5609 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005610 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5611 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005612 return -EINVAL;
5613 }
5614
5615 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5616 settings->wpa_versions =
5617 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5618 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5619 return -EINVAL;
5620 }
5621
5622 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5623 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005624 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005625
5626 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5627 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5628 settings->n_akm_suites = len / sizeof(u32);
5629
5630 if (len % sizeof(u32))
5631 return -EINVAL;
5632
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005633 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5634 return -EINVAL;
5635
Samuel Ortizb23aa672009-07-01 21:26:54 +02005636 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005637 }
5638
5639 return 0;
5640}
5641
Jouni Malinen636a5d32009-03-19 13:39:22 +02005642static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5643{
Johannes Berg4c476992010-10-04 21:36:35 +02005644 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5645 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005646 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005647 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005648 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005649 int err, ssid_len, ie_len = 0;
5650 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005651 u32 flags = 0;
5652 struct ieee80211_ht_cap *ht_capa = NULL;
5653 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005654
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005655 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5656 return -EINVAL;
5657
5658 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005659 !info->attrs[NL80211_ATTR_SSID] ||
5660 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005661 return -EINVAL;
5662
Johannes Berg4c476992010-10-04 21:36:35 +02005663 if (!rdev->ops->assoc)
5664 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005665
Johannes Berg074ac8d2010-09-16 14:58:22 +02005666 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005667 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5668 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005669
Johannes Berg19957bb2009-07-02 17:20:43 +02005670 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005671
Johannes Berg19957bb2009-07-02 17:20:43 +02005672 chan = ieee80211_get_channel(&rdev->wiphy,
5673 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005674 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5675 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005676
Johannes Berg19957bb2009-07-02 17:20:43 +02005677 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5678 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005679
5680 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005681 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5682 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005683 }
5684
Jouni Malinendc6382c2009-05-06 22:09:37 +03005685 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005686 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005687 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005688 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005689 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005690 else if (mfp != NL80211_MFP_NO)
5691 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005692 }
5693
Johannes Berg3e5d7642009-07-07 14:37:26 +02005694 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5695 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5696
Ben Greear7e7c8922011-11-18 11:31:59 -08005697 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5698 flags |= ASSOC_REQ_DISABLE_HT;
5699
5700 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5701 ht_capa_mask =
5702 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5703
5704 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5705 if (!ht_capa_mask)
5706 return -EINVAL;
5707 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5708 }
5709
Johannes Bergc0692b82010-08-27 14:26:53 +03005710 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005711 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005712 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5713 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005714 &crypto, flags, ht_capa,
5715 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005716
Jouni Malinen636a5d32009-03-19 13:39:22 +02005717 return err;
5718}
5719
5720static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5721{
Johannes Berg4c476992010-10-04 21:36:35 +02005722 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5723 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005724 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005725 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005726 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005727 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005728
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005729 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5730 return -EINVAL;
5731
5732 if (!info->attrs[NL80211_ATTR_MAC])
5733 return -EINVAL;
5734
5735 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5736 return -EINVAL;
5737
Johannes Berg4c476992010-10-04 21:36:35 +02005738 if (!rdev->ops->deauth)
5739 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005740
Johannes Berg074ac8d2010-09-16 14:58:22 +02005741 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005742 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5743 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005744
Johannes Berg19957bb2009-07-02 17:20:43 +02005745 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005746
Johannes Berg19957bb2009-07-02 17:20:43 +02005747 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5748 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005749 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005750 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005751 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005752
5753 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005754 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5755 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005756 }
5757
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005758 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5759
Johannes Berg4c476992010-10-04 21:36:35 +02005760 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5761 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005762}
5763
5764static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5765{
Johannes Berg4c476992010-10-04 21:36:35 +02005766 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5767 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005768 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005769 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005770 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005771 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005772
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005773 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5774 return -EINVAL;
5775
5776 if (!info->attrs[NL80211_ATTR_MAC])
5777 return -EINVAL;
5778
5779 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5780 return -EINVAL;
5781
Johannes Berg4c476992010-10-04 21:36:35 +02005782 if (!rdev->ops->disassoc)
5783 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005784
Johannes Berg074ac8d2010-09-16 14:58:22 +02005785 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005786 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5787 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005788
Johannes Berg19957bb2009-07-02 17:20:43 +02005789 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005790
Johannes Berg19957bb2009-07-02 17:20:43 +02005791 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5792 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005793 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005794 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005795 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005796
5797 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005798 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5799 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005800 }
5801
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005802 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5803
Johannes Berg4c476992010-10-04 21:36:35 +02005804 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5805 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005806}
5807
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005808static bool
5809nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5810 int mcast_rate[IEEE80211_NUM_BANDS],
5811 int rateval)
5812{
5813 struct wiphy *wiphy = &rdev->wiphy;
5814 bool found = false;
5815 int band, i;
5816
5817 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5818 struct ieee80211_supported_band *sband;
5819
5820 sband = wiphy->bands[band];
5821 if (!sband)
5822 continue;
5823
5824 for (i = 0; i < sband->n_bitrates; i++) {
5825 if (sband->bitrates[i].bitrate == rateval) {
5826 mcast_rate[band] = i + 1;
5827 found = true;
5828 break;
5829 }
5830 }
5831 }
5832
5833 return found;
5834}
5835
Johannes Berg04a773a2009-04-19 21:24:32 +02005836static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5837{
Johannes Berg4c476992010-10-04 21:36:35 +02005838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5839 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005840 struct cfg80211_ibss_params ibss;
5841 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005842 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005843 int err;
5844
Johannes Berg8e30bc52009-04-22 17:45:38 +02005845 memset(&ibss, 0, sizeof(ibss));
5846
Johannes Berg04a773a2009-04-19 21:24:32 +02005847 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5848 return -EINVAL;
5849
Johannes Berg683b6d32012-11-08 21:25:48 +01005850 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005851 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5852 return -EINVAL;
5853
Johannes Berg8e30bc52009-04-22 17:45:38 +02005854 ibss.beacon_interval = 100;
5855
5856 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5857 ibss.beacon_interval =
5858 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5859 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5860 return -EINVAL;
5861 }
5862
Johannes Berg4c476992010-10-04 21:36:35 +02005863 if (!rdev->ops->join_ibss)
5864 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005865
Johannes Berg4c476992010-10-04 21:36:35 +02005866 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5867 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005868
Johannes Berg79c97e92009-07-07 03:56:12 +02005869 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005870
Johannes Berg39193492011-09-16 13:45:25 +02005871 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005872 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005873
5874 if (!is_valid_ether_addr(ibss.bssid))
5875 return -EINVAL;
5876 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005877 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5878 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5879
5880 if (info->attrs[NL80211_ATTR_IE]) {
5881 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5882 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5883 }
5884
Johannes Berg683b6d32012-11-08 21:25:48 +01005885 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5886 if (err)
5887 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005888
Johannes Berg683b6d32012-11-08 21:25:48 +01005889 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005890 return -EINVAL;
5891
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005892 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
5893 return -EINVAL;
5894 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
5895 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01005896 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01005897
Johannes Berg04a773a2009-04-19 21:24:32 +02005898 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005899 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005900
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005901 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5902 u8 *rates =
5903 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5904 int n_rates =
5905 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5906 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005907 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005908
Johannes Berg34850ab2011-07-18 18:08:35 +02005909 err = ieee80211_get_ratemask(sband, rates, n_rates,
5910 &ibss.basic_rates);
5911 if (err)
5912 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005913 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005914
5915 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5916 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5917 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5918 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005919
Johannes Berg4c476992010-10-04 21:36:35 +02005920 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305921 bool no_ht = false;
5922
Johannes Berg4c476992010-10-04 21:36:35 +02005923 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305924 info->attrs[NL80211_ATTR_KEYS],
5925 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005926 if (IS_ERR(connkeys))
5927 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305928
Johannes Berg3d9d1d62012-11-08 23:14:50 +01005929 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
5930 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305931 kfree(connkeys);
5932 return -EINVAL;
5933 }
Johannes Berg4c476992010-10-04 21:36:35 +02005934 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005935
Antonio Quartulli267335d2012-01-31 20:25:47 +01005936 ibss.control_port =
5937 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5938
Johannes Berg4c476992010-10-04 21:36:35 +02005939 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005940 if (err)
5941 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005942 return err;
5943}
5944
5945static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5946{
Johannes Berg4c476992010-10-04 21:36:35 +02005947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5948 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005949
Johannes Berg4c476992010-10-04 21:36:35 +02005950 if (!rdev->ops->leave_ibss)
5951 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005952
Johannes Berg4c476992010-10-04 21:36:35 +02005953 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5954 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005955
Johannes Berg4c476992010-10-04 21:36:35 +02005956 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005957}
5958
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005959static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5960{
5961 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5962 struct net_device *dev = info->user_ptr[1];
5963 int mcast_rate[IEEE80211_NUM_BANDS];
5964 u32 nla_rate;
5965 int err;
5966
5967 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5968 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5969 return -EOPNOTSUPP;
5970
5971 if (!rdev->ops->set_mcast_rate)
5972 return -EOPNOTSUPP;
5973
5974 memset(mcast_rate, 0, sizeof(mcast_rate));
5975
5976 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5977 return -EINVAL;
5978
5979 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5980 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5981 return -EINVAL;
5982
5983 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5984
5985 return err;
5986}
5987
5988
Johannes Bergaff89a92009-07-01 21:26:51 +02005989#ifdef CONFIG_NL80211_TESTMODE
5990static struct genl_multicast_group nl80211_testmode_mcgrp = {
5991 .name = "testmode",
5992};
5993
5994static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5995{
Johannes Berg4c476992010-10-04 21:36:35 +02005996 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005997 int err;
5998
5999 if (!info->attrs[NL80211_ATTR_TESTDATA])
6000 return -EINVAL;
6001
Johannes Bergaff89a92009-07-01 21:26:51 +02006002 err = -EOPNOTSUPP;
6003 if (rdev->ops->testmode_cmd) {
6004 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006005 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006006 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6007 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6008 rdev->testmode_info = NULL;
6009 }
6010
Johannes Bergaff89a92009-07-01 21:26:51 +02006011 return err;
6012}
6013
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006014static int nl80211_testmode_dump(struct sk_buff *skb,
6015 struct netlink_callback *cb)
6016{
Johannes Berg00918d32011-12-13 17:22:05 +01006017 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006018 int err;
6019 long phy_idx;
6020 void *data = NULL;
6021 int data_len = 0;
6022
6023 if (cb->args[0]) {
6024 /*
6025 * 0 is a valid index, but not valid for args[0],
6026 * so we need to offset by 1.
6027 */
6028 phy_idx = cb->args[0] - 1;
6029 } else {
6030 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6031 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6032 nl80211_policy);
6033 if (err)
6034 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006035
Johannes Berg2bd7e352012-06-15 14:23:16 +02006036 mutex_lock(&cfg80211_mutex);
6037 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6038 nl80211_fam.attrbuf);
6039 if (IS_ERR(rdev)) {
6040 mutex_unlock(&cfg80211_mutex);
6041 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006042 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006043 phy_idx = rdev->wiphy_idx;
6044 rdev = NULL;
6045 mutex_unlock(&cfg80211_mutex);
6046
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006047 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6048 cb->args[1] =
6049 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6050 }
6051
6052 if (cb->args[1]) {
6053 data = nla_data((void *)cb->args[1]);
6054 data_len = nla_len((void *)cb->args[1]);
6055 }
6056
6057 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006058 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6059 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006060 mutex_unlock(&cfg80211_mutex);
6061 return -ENOENT;
6062 }
Johannes Berg00918d32011-12-13 17:22:05 +01006063 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006064 mutex_unlock(&cfg80211_mutex);
6065
Johannes Berg00918d32011-12-13 17:22:05 +01006066 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006067 err = -EOPNOTSUPP;
6068 goto out_err;
6069 }
6070
6071 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006072 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006073 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6074 NL80211_CMD_TESTMODE);
6075 struct nlattr *tmdata;
6076
David S. Miller9360ffd2012-03-29 04:41:26 -04006077 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006078 genlmsg_cancel(skb, hdr);
6079 break;
6080 }
6081
6082 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6083 if (!tmdata) {
6084 genlmsg_cancel(skb, hdr);
6085 break;
6086 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006087 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006088 nla_nest_end(skb, tmdata);
6089
6090 if (err == -ENOBUFS || err == -ENOENT) {
6091 genlmsg_cancel(skb, hdr);
6092 break;
6093 } else if (err) {
6094 genlmsg_cancel(skb, hdr);
6095 goto out_err;
6096 }
6097
6098 genlmsg_end(skb, hdr);
6099 }
6100
6101 err = skb->len;
6102 /* see above */
6103 cb->args[0] = phy_idx + 1;
6104 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006105 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006106 return err;
6107}
6108
Johannes Bergaff89a92009-07-01 21:26:51 +02006109static struct sk_buff *
6110__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006111 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006112{
6113 struct sk_buff *skb;
6114 void *hdr;
6115 struct nlattr *data;
6116
6117 skb = nlmsg_new(approxlen + 100, gfp);
6118 if (!skb)
6119 return NULL;
6120
Eric W. Biederman15e47302012-09-07 20:12:54 +00006121 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006122 if (!hdr) {
6123 kfree_skb(skb);
6124 return NULL;
6125 }
6126
David S. Miller9360ffd2012-03-29 04:41:26 -04006127 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6128 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006129 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6130
6131 ((void **)skb->cb)[0] = rdev;
6132 ((void **)skb->cb)[1] = hdr;
6133 ((void **)skb->cb)[2] = data;
6134
6135 return skb;
6136
6137 nla_put_failure:
6138 kfree_skb(skb);
6139 return NULL;
6140}
6141
6142struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6143 int approxlen)
6144{
6145 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6146
6147 if (WARN_ON(!rdev->testmode_info))
6148 return NULL;
6149
6150 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006151 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006152 rdev->testmode_info->snd_seq,
6153 GFP_KERNEL);
6154}
6155EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6156
6157int cfg80211_testmode_reply(struct sk_buff *skb)
6158{
6159 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6160 void *hdr = ((void **)skb->cb)[1];
6161 struct nlattr *data = ((void **)skb->cb)[2];
6162
6163 if (WARN_ON(!rdev->testmode_info)) {
6164 kfree_skb(skb);
6165 return -EINVAL;
6166 }
6167
6168 nla_nest_end(skb, data);
6169 genlmsg_end(skb, hdr);
6170 return genlmsg_reply(skb, rdev->testmode_info);
6171}
6172EXPORT_SYMBOL(cfg80211_testmode_reply);
6173
6174struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6175 int approxlen, gfp_t gfp)
6176{
6177 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6178
6179 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6180}
6181EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6182
6183void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6184{
6185 void *hdr = ((void **)skb->cb)[1];
6186 struct nlattr *data = ((void **)skb->cb)[2];
6187
6188 nla_nest_end(skb, data);
6189 genlmsg_end(skb, hdr);
6190 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6191}
6192EXPORT_SYMBOL(cfg80211_testmode_event);
6193#endif
6194
Samuel Ortizb23aa672009-07-01 21:26:54 +02006195static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6196{
Johannes Berg4c476992010-10-04 21:36:35 +02006197 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6198 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006199 struct cfg80211_connect_params connect;
6200 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006201 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006202 int err;
6203
6204 memset(&connect, 0, sizeof(connect));
6205
6206 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6207 return -EINVAL;
6208
6209 if (!info->attrs[NL80211_ATTR_SSID] ||
6210 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6211 return -EINVAL;
6212
6213 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6214 connect.auth_type =
6215 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006216 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6217 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006218 return -EINVAL;
6219 } else
6220 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6221
6222 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6223
Johannes Bergc0692b82010-08-27 14:26:53 +03006224 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006225 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006226 if (err)
6227 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006228
Johannes Berg074ac8d2010-09-16 14:58:22 +02006229 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006230 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6231 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006232
Johannes Berg79c97e92009-07-07 03:56:12 +02006233 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006234
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306235 connect.bg_scan_period = -1;
6236 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6237 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6238 connect.bg_scan_period =
6239 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6240 }
6241
Samuel Ortizb23aa672009-07-01 21:26:54 +02006242 if (info->attrs[NL80211_ATTR_MAC])
6243 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6244 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6245 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6246
6247 if (info->attrs[NL80211_ATTR_IE]) {
6248 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6249 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6250 }
6251
Jouni Malinencee00a92013-01-15 17:15:57 +02006252 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6253 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6254 if (connect.mfp != NL80211_MFP_REQUIRED &&
6255 connect.mfp != NL80211_MFP_NO)
6256 return -EINVAL;
6257 } else {
6258 connect.mfp = NL80211_MFP_NO;
6259 }
6260
Samuel Ortizb23aa672009-07-01 21:26:54 +02006261 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6262 connect.channel =
6263 ieee80211_get_channel(wiphy,
6264 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6265 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006266 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6267 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006268 }
6269
Johannes Bergfffd0932009-07-08 14:22:54 +02006270 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6271 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306272 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006273 if (IS_ERR(connkeys))
6274 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006275 }
6276
Ben Greear7e7c8922011-11-18 11:31:59 -08006277 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6278 connect.flags |= ASSOC_REQ_DISABLE_HT;
6279
6280 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6281 memcpy(&connect.ht_capa_mask,
6282 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6283 sizeof(connect.ht_capa_mask));
6284
6285 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006286 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6287 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006288 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006289 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006290 memcpy(&connect.ht_capa,
6291 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6292 sizeof(connect.ht_capa));
6293 }
6294
Johannes Bergfffd0932009-07-08 14:22:54 +02006295 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006296 if (err)
6297 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006298 return err;
6299}
6300
6301static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6302{
Johannes Berg4c476992010-10-04 21:36:35 +02006303 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6304 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006305 u16 reason;
6306
6307 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6308 reason = WLAN_REASON_DEAUTH_LEAVING;
6309 else
6310 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6311
6312 if (reason == 0)
6313 return -EINVAL;
6314
Johannes Berg074ac8d2010-09-16 14:58:22 +02006315 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006316 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6317 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006318
Johannes Berg4c476992010-10-04 21:36:35 +02006319 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006320}
6321
Johannes Berg463d0182009-07-14 00:33:35 +02006322static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6323{
Johannes Berg4c476992010-10-04 21:36:35 +02006324 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006325 struct net *net;
6326 int err;
6327 u32 pid;
6328
6329 if (!info->attrs[NL80211_ATTR_PID])
6330 return -EINVAL;
6331
6332 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6333
Johannes Berg463d0182009-07-14 00:33:35 +02006334 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006335 if (IS_ERR(net))
6336 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006337
6338 err = 0;
6339
6340 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006341 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6342 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006343
Johannes Berg463d0182009-07-14 00:33:35 +02006344 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006345 return err;
6346}
6347
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006348static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6349{
Johannes Berg4c476992010-10-04 21:36:35 +02006350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006351 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6352 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006353 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006354 struct cfg80211_pmksa pmksa;
6355
6356 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6357
6358 if (!info->attrs[NL80211_ATTR_MAC])
6359 return -EINVAL;
6360
6361 if (!info->attrs[NL80211_ATTR_PMKID])
6362 return -EINVAL;
6363
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006364 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6365 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6366
Johannes Berg074ac8d2010-09-16 14:58:22 +02006367 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006368 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6369 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006370
6371 switch (info->genlhdr->cmd) {
6372 case NL80211_CMD_SET_PMKSA:
6373 rdev_ops = rdev->ops->set_pmksa;
6374 break;
6375 case NL80211_CMD_DEL_PMKSA:
6376 rdev_ops = rdev->ops->del_pmksa;
6377 break;
6378 default:
6379 WARN_ON(1);
6380 break;
6381 }
6382
Johannes Berg4c476992010-10-04 21:36:35 +02006383 if (!rdev_ops)
6384 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006385
Johannes Berg4c476992010-10-04 21:36:35 +02006386 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006387}
6388
6389static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6390{
Johannes Berg4c476992010-10-04 21:36:35 +02006391 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6392 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006393
Johannes Berg074ac8d2010-09-16 14:58:22 +02006394 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006395 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6396 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006397
Johannes Berg4c476992010-10-04 21:36:35 +02006398 if (!rdev->ops->flush_pmksa)
6399 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006400
Hila Gonene35e4d22012-06-27 17:19:42 +03006401 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006402}
6403
Arik Nemtsov109086c2011-09-28 14:12:50 +03006404static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6405{
6406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6407 struct net_device *dev = info->user_ptr[1];
6408 u8 action_code, dialog_token;
6409 u16 status_code;
6410 u8 *peer;
6411
6412 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6413 !rdev->ops->tdls_mgmt)
6414 return -EOPNOTSUPP;
6415
6416 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6417 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6418 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6419 !info->attrs[NL80211_ATTR_IE] ||
6420 !info->attrs[NL80211_ATTR_MAC])
6421 return -EINVAL;
6422
6423 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6424 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6425 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6426 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6427
Hila Gonene35e4d22012-06-27 17:19:42 +03006428 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6429 dialog_token, status_code,
6430 nla_data(info->attrs[NL80211_ATTR_IE]),
6431 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006432}
6433
6434static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6435{
6436 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6437 struct net_device *dev = info->user_ptr[1];
6438 enum nl80211_tdls_operation operation;
6439 u8 *peer;
6440
6441 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6442 !rdev->ops->tdls_oper)
6443 return -EOPNOTSUPP;
6444
6445 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6446 !info->attrs[NL80211_ATTR_MAC])
6447 return -EINVAL;
6448
6449 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6450 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6451
Hila Gonene35e4d22012-06-27 17:19:42 +03006452 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006453}
6454
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006455static int nl80211_remain_on_channel(struct sk_buff *skb,
6456 struct genl_info *info)
6457{
Johannes Berg4c476992010-10-04 21:36:35 +02006458 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006459 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006460 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006461 struct sk_buff *msg;
6462 void *hdr;
6463 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006464 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006465 int err;
6466
6467 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6468 !info->attrs[NL80211_ATTR_DURATION])
6469 return -EINVAL;
6470
6471 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6472
Johannes Berg7c4ef712011-11-18 15:33:48 +01006473 if (!rdev->ops->remain_on_channel ||
6474 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006475 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006476
Johannes Bergebf348f2012-06-01 12:50:54 +02006477 /*
6478 * We should be on that channel for at least a minimum amount of
6479 * time (10ms) but no longer than the driver supports.
6480 */
6481 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6482 duration > rdev->wiphy.max_remain_on_channel_duration)
6483 return -EINVAL;
6484
Johannes Berg683b6d32012-11-08 21:25:48 +01006485 err = nl80211_parse_chandef(rdev, info, &chandef);
6486 if (err)
6487 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006488
6489 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006490 if (!msg)
6491 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006492
Eric W. Biederman15e47302012-09-07 20:12:54 +00006493 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006494 NL80211_CMD_REMAIN_ON_CHANNEL);
6495
6496 if (IS_ERR(hdr)) {
6497 err = PTR_ERR(hdr);
6498 goto free_msg;
6499 }
6500
Johannes Berg683b6d32012-11-08 21:25:48 +01006501 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6502 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006503
6504 if (err)
6505 goto free_msg;
6506
David S. Miller9360ffd2012-03-29 04:41:26 -04006507 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6508 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006509
6510 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006511
6512 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006513
6514 nla_put_failure:
6515 err = -ENOBUFS;
6516 free_msg:
6517 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006518 return err;
6519}
6520
6521static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6522 struct genl_info *info)
6523{
Johannes Berg4c476992010-10-04 21:36:35 +02006524 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006525 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006526 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006527
6528 if (!info->attrs[NL80211_ATTR_COOKIE])
6529 return -EINVAL;
6530
Johannes Berg4c476992010-10-04 21:36:35 +02006531 if (!rdev->ops->cancel_remain_on_channel)
6532 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006533
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006534 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6535
Hila Gonene35e4d22012-06-27 17:19:42 +03006536 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006537}
6538
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006539static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6540 u8 *rates, u8 rates_len)
6541{
6542 u8 i;
6543 u32 mask = 0;
6544
6545 for (i = 0; i < rates_len; i++) {
6546 int rate = (rates[i] & 0x7f) * 5;
6547 int ridx;
6548 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6549 struct ieee80211_rate *srate =
6550 &sband->bitrates[ridx];
6551 if (rate == srate->bitrate) {
6552 mask |= 1 << ridx;
6553 break;
6554 }
6555 }
6556 if (ridx == sband->n_bitrates)
6557 return 0; /* rate not found */
6558 }
6559
6560 return mask;
6561}
6562
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006563static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6564 u8 *rates, u8 rates_len,
6565 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6566{
6567 u8 i;
6568
6569 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6570
6571 for (i = 0; i < rates_len; i++) {
6572 int ridx, rbit;
6573
6574 ridx = rates[i] / 8;
6575 rbit = BIT(rates[i] % 8);
6576
6577 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006578 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006579 return false;
6580
6581 /* check availability */
6582 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6583 mcs[ridx] |= rbit;
6584 else
6585 return false;
6586 }
6587
6588 return true;
6589}
6590
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006591static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006592 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6593 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006594 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6595 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006596};
6597
6598static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6599 struct genl_info *info)
6600{
6601 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006602 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006603 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006604 int rem, i;
6605 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006606 struct nlattr *tx_rates;
6607 struct ieee80211_supported_band *sband;
6608
6609 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6610 return -EINVAL;
6611
Johannes Berg4c476992010-10-04 21:36:35 +02006612 if (!rdev->ops->set_bitrate_mask)
6613 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006614
6615 memset(&mask, 0, sizeof(mask));
6616 /* Default to all rates enabled */
6617 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6618 sband = rdev->wiphy.bands[i];
6619 mask.control[i].legacy =
6620 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006621 if (sband)
6622 memcpy(mask.control[i].mcs,
6623 sband->ht_cap.mcs.rx_mask,
6624 sizeof(mask.control[i].mcs));
6625 else
6626 memset(mask.control[i].mcs, 0,
6627 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006628 }
6629
6630 /*
6631 * The nested attribute uses enum nl80211_band as the index. This maps
6632 * directly to the enum ieee80211_band values used in cfg80211.
6633 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006634 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006635 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6636 {
6637 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006638 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6639 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006640 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006641 if (sband == NULL)
6642 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006643 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6644 nla_len(tx_rates), nl80211_txattr_policy);
6645 if (tb[NL80211_TXRATE_LEGACY]) {
6646 mask.control[band].legacy = rateset_to_mask(
6647 sband,
6648 nla_data(tb[NL80211_TXRATE_LEGACY]),
6649 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306650 if ((mask.control[band].legacy == 0) &&
6651 nla_len(tb[NL80211_TXRATE_LEGACY]))
6652 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006653 }
6654 if (tb[NL80211_TXRATE_MCS]) {
6655 if (!ht_rateset_to_mask(
6656 sband,
6657 nla_data(tb[NL80211_TXRATE_MCS]),
6658 nla_len(tb[NL80211_TXRATE_MCS]),
6659 mask.control[band].mcs))
6660 return -EINVAL;
6661 }
6662
6663 if (mask.control[band].legacy == 0) {
6664 /* don't allow empty legacy rates if HT
6665 * is not even supported. */
6666 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6667 return -EINVAL;
6668
6669 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6670 if (mask.control[band].mcs[i])
6671 break;
6672
6673 /* legacy and mcs rates may not be both empty */
6674 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006675 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006676 }
6677 }
6678
Hila Gonene35e4d22012-06-27 17:19:42 +03006679 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006680}
6681
Johannes Berg2e161f72010-08-12 15:38:38 +02006682static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006683{
Johannes Berg4c476992010-10-04 21:36:35 +02006684 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006685 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006686 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006687
6688 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6689 return -EINVAL;
6690
Johannes Berg2e161f72010-08-12 15:38:38 +02006691 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6692 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006693
Johannes Berg71bbc992012-06-15 15:30:18 +02006694 switch (wdev->iftype) {
6695 case NL80211_IFTYPE_STATION:
6696 case NL80211_IFTYPE_ADHOC:
6697 case NL80211_IFTYPE_P2P_CLIENT:
6698 case NL80211_IFTYPE_AP:
6699 case NL80211_IFTYPE_AP_VLAN:
6700 case NL80211_IFTYPE_MESH_POINT:
6701 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006702 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006703 break;
6704 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006705 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006706 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006707
6708 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006709 if (!rdev->ops->mgmt_tx)
6710 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006711
Eric W. Biederman15e47302012-09-07 20:12:54 +00006712 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006713 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6714 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006715}
6716
Johannes Berg2e161f72010-08-12 15:38:38 +02006717static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006718{
Johannes Berg4c476992010-10-04 21:36:35 +02006719 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006720 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006721 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006722 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006723 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006724 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006725 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006726 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006727 bool offchan, no_cck, dont_wait_for_ack;
6728
6729 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006730
Johannes Berg683b6d32012-11-08 21:25:48 +01006731 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006732 return -EINVAL;
6733
Johannes Berg4c476992010-10-04 21:36:35 +02006734 if (!rdev->ops->mgmt_tx)
6735 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006736
Johannes Berg71bbc992012-06-15 15:30:18 +02006737 switch (wdev->iftype) {
6738 case NL80211_IFTYPE_STATION:
6739 case NL80211_IFTYPE_ADHOC:
6740 case NL80211_IFTYPE_P2P_CLIENT:
6741 case NL80211_IFTYPE_AP:
6742 case NL80211_IFTYPE_AP_VLAN:
6743 case NL80211_IFTYPE_MESH_POINT:
6744 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006745 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006746 break;
6747 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006748 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006749 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006750
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006751 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006752 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006753 return -EINVAL;
6754 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006755
6756 /*
6757 * We should wait on the channel for at least a minimum amount
6758 * of time (10ms) but no longer than the driver supports.
6759 */
6760 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6761 wait > rdev->wiphy.max_remain_on_channel_duration)
6762 return -EINVAL;
6763
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006764 }
6765
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006766 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6767
Johannes Berg7c4ef712011-11-18 15:33:48 +01006768 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6769 return -EINVAL;
6770
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306771 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6772
Johannes Berg683b6d32012-11-08 21:25:48 +01006773 err = nl80211_parse_chandef(rdev, info, &chandef);
6774 if (err)
6775 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006776
Johannes Berge247bd902011-11-04 11:18:21 +01006777 if (!dont_wait_for_ack) {
6778 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6779 if (!msg)
6780 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006781
Eric W. Biederman15e47302012-09-07 20:12:54 +00006782 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006783 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006784
Johannes Berge247bd902011-11-04 11:18:21 +01006785 if (IS_ERR(hdr)) {
6786 err = PTR_ERR(hdr);
6787 goto free_msg;
6788 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006789 }
Johannes Berge247bd902011-11-04 11:18:21 +01006790
Johannes Berg683b6d32012-11-08 21:25:48 +01006791 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006792 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6793 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006794 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006795 if (err)
6796 goto free_msg;
6797
Johannes Berge247bd902011-11-04 11:18:21 +01006798 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006799 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6800 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006801
Johannes Berge247bd902011-11-04 11:18:21 +01006802 genlmsg_end(msg, hdr);
6803 return genlmsg_reply(msg, info);
6804 }
6805
6806 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006807
6808 nla_put_failure:
6809 err = -ENOBUFS;
6810 free_msg:
6811 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006812 return err;
6813}
6814
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006815static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6816{
6817 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006818 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006819 u64 cookie;
6820
6821 if (!info->attrs[NL80211_ATTR_COOKIE])
6822 return -EINVAL;
6823
6824 if (!rdev->ops->mgmt_tx_cancel_wait)
6825 return -EOPNOTSUPP;
6826
Johannes Berg71bbc992012-06-15 15:30:18 +02006827 switch (wdev->iftype) {
6828 case NL80211_IFTYPE_STATION:
6829 case NL80211_IFTYPE_ADHOC:
6830 case NL80211_IFTYPE_P2P_CLIENT:
6831 case NL80211_IFTYPE_AP:
6832 case NL80211_IFTYPE_AP_VLAN:
6833 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006834 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006835 break;
6836 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006837 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006838 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006839
6840 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6841
Hila Gonene35e4d22012-06-27 17:19:42 +03006842 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006843}
6844
Kalle Valoffb9eb32010-02-17 17:58:10 +02006845static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6846{
Johannes Berg4c476992010-10-04 21:36:35 +02006847 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006848 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006849 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006850 u8 ps_state;
6851 bool state;
6852 int err;
6853
Johannes Berg4c476992010-10-04 21:36:35 +02006854 if (!info->attrs[NL80211_ATTR_PS_STATE])
6855 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006856
6857 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6858
Johannes Berg4c476992010-10-04 21:36:35 +02006859 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6860 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006861
6862 wdev = dev->ieee80211_ptr;
6863
Johannes Berg4c476992010-10-04 21:36:35 +02006864 if (!rdev->ops->set_power_mgmt)
6865 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006866
6867 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6868
6869 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006870 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006871
Hila Gonene35e4d22012-06-27 17:19:42 +03006872 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006873 if (!err)
6874 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006875 return err;
6876}
6877
6878static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6879{
Johannes Berg4c476992010-10-04 21:36:35 +02006880 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006881 enum nl80211_ps_state ps_state;
6882 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006883 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006884 struct sk_buff *msg;
6885 void *hdr;
6886 int err;
6887
Kalle Valoffb9eb32010-02-17 17:58:10 +02006888 wdev = dev->ieee80211_ptr;
6889
Johannes Berg4c476992010-10-04 21:36:35 +02006890 if (!rdev->ops->set_power_mgmt)
6891 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006892
6893 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006894 if (!msg)
6895 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006896
Eric W. Biederman15e47302012-09-07 20:12:54 +00006897 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006898 NL80211_CMD_GET_POWER_SAVE);
6899 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006900 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006901 goto free_msg;
6902 }
6903
6904 if (wdev->ps)
6905 ps_state = NL80211_PS_ENABLED;
6906 else
6907 ps_state = NL80211_PS_DISABLED;
6908
David S. Miller9360ffd2012-03-29 04:41:26 -04006909 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6910 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006911
6912 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006913 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006914
Johannes Berg4c476992010-10-04 21:36:35 +02006915 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006916 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006917 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006918 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006919 return err;
6920}
6921
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006922static struct nla_policy
6923nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6924 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6925 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6926 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006927 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6928 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6929 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006930};
6931
Thomas Pedersen84f10702012-07-12 16:17:33 -07006932static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01006933 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006934{
6935 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6936 struct wireless_dev *wdev;
6937 struct net_device *dev = info->user_ptr[1];
6938
Johannes Bergd9d8b012012-11-26 12:51:52 +01006939 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07006940 return -EINVAL;
6941
6942 wdev = dev->ieee80211_ptr;
6943
6944 if (!rdev->ops->set_cqm_txe_config)
6945 return -EOPNOTSUPP;
6946
6947 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6948 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6949 return -EOPNOTSUPP;
6950
Hila Gonene35e4d22012-06-27 17:19:42 +03006951 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006952}
6953
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006954static int nl80211_set_cqm_rssi(struct genl_info *info,
6955 s32 threshold, u32 hysteresis)
6956{
Johannes Berg4c476992010-10-04 21:36:35 +02006957 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006958 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006959 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006960
6961 if (threshold > 0)
6962 return -EINVAL;
6963
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006964 wdev = dev->ieee80211_ptr;
6965
Johannes Berg4c476992010-10-04 21:36:35 +02006966 if (!rdev->ops->set_cqm_rssi_config)
6967 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006968
Johannes Berg074ac8d2010-09-16 14:58:22 +02006969 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006970 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6971 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006972
Hila Gonene35e4d22012-06-27 17:19:42 +03006973 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006974}
6975
6976static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6977{
6978 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6979 struct nlattr *cqm;
6980 int err;
6981
6982 cqm = info->attrs[NL80211_ATTR_CQM];
6983 if (!cqm) {
6984 err = -EINVAL;
6985 goto out;
6986 }
6987
6988 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6989 nl80211_attr_cqm_policy);
6990 if (err)
6991 goto out;
6992
6993 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6994 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6995 s32 threshold;
6996 u32 hysteresis;
6997 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6998 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6999 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007000 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7001 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7002 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7003 u32 rate, pkts, intvl;
7004 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7005 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7006 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7007 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007008 } else
7009 err = -EINVAL;
7010
7011out:
7012 return err;
7013}
7014
Johannes Berg29cbe682010-12-03 09:20:44 +01007015static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7016{
7017 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7018 struct net_device *dev = info->user_ptr[1];
7019 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007020 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007021 int err;
7022
7023 /* start with default */
7024 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007025 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007026
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007027 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007028 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007029 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007030 if (err)
7031 return err;
7032 }
7033
7034 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7035 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7036 return -EINVAL;
7037
Javier Cardonac80d5452010-12-16 17:37:49 -08007038 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7039 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7040
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007041 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7042 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7043 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7044 return -EINVAL;
7045
Marco Porsch9bdbf042013-01-07 16:04:51 +01007046 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7047 setup.beacon_interval =
7048 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7049 if (setup.beacon_interval < 10 ||
7050 setup.beacon_interval > 10000)
7051 return -EINVAL;
7052 }
7053
7054 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7055 setup.dtim_period =
7056 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7057 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7058 return -EINVAL;
7059 }
7060
Javier Cardonac80d5452010-12-16 17:37:49 -08007061 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7062 /* parse additional setup parameters if given */
7063 err = nl80211_parse_mesh_setup(info, &setup);
7064 if (err)
7065 return err;
7066 }
7067
Johannes Bergcc1d2802012-05-16 23:50:20 +02007068 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007069 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7070 if (err)
7071 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007072 } else {
7073 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007074 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007075 }
7076
Javier Cardonac80d5452010-12-16 17:37:49 -08007077 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007078}
7079
7080static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7081{
7082 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7083 struct net_device *dev = info->user_ptr[1];
7084
7085 return cfg80211_leave_mesh(rdev, dev);
7086}
7087
Johannes Bergdfb89c52012-06-27 09:23:48 +02007088#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007089static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7090 struct cfg80211_registered_device *rdev)
7091{
7092 struct nlattr *nl_pats, *nl_pat;
7093 int i, pat_len;
7094
7095 if (!rdev->wowlan->n_patterns)
7096 return 0;
7097
7098 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7099 if (!nl_pats)
7100 return -ENOBUFS;
7101
7102 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7103 nl_pat = nla_nest_start(msg, i + 1);
7104 if (!nl_pat)
7105 return -ENOBUFS;
7106 pat_len = rdev->wowlan->patterns[i].pattern_len;
7107 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7108 DIV_ROUND_UP(pat_len, 8),
7109 rdev->wowlan->patterns[i].mask) ||
7110 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7111 pat_len, rdev->wowlan->patterns[i].pattern) ||
7112 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7113 rdev->wowlan->patterns[i].pkt_offset))
7114 return -ENOBUFS;
7115 nla_nest_end(msg, nl_pat);
7116 }
7117 nla_nest_end(msg, nl_pats);
7118
7119 return 0;
7120}
7121
Johannes Berg2a0e0472013-01-23 22:57:40 +01007122static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7123 struct cfg80211_wowlan_tcp *tcp)
7124{
7125 struct nlattr *nl_tcp;
7126
7127 if (!tcp)
7128 return 0;
7129
7130 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7131 if (!nl_tcp)
7132 return -ENOBUFS;
7133
7134 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7135 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7136 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7137 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7138 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7139 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7140 tcp->payload_len, tcp->payload) ||
7141 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7142 tcp->data_interval) ||
7143 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7144 tcp->wake_len, tcp->wake_data) ||
7145 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7146 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7147 return -ENOBUFS;
7148
7149 if (tcp->payload_seq.len &&
7150 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7151 sizeof(tcp->payload_seq), &tcp->payload_seq))
7152 return -ENOBUFS;
7153
7154 if (tcp->payload_tok.len &&
7155 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7156 sizeof(tcp->payload_tok) + tcp->tokens_size,
7157 &tcp->payload_tok))
7158 return -ENOBUFS;
7159
7160 return 0;
7161}
7162
Johannes Bergff1b6e62011-05-04 15:37:28 +02007163static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7164{
7165 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7166 struct sk_buff *msg;
7167 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007168 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007169
Johannes Berg2a0e0472013-01-23 22:57:40 +01007170 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7171 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007172 return -EOPNOTSUPP;
7173
Johannes Berg2a0e0472013-01-23 22:57:40 +01007174 if (rdev->wowlan && rdev->wowlan->tcp) {
7175 /* adjust size to have room for all the data */
7176 size += rdev->wowlan->tcp->tokens_size +
7177 rdev->wowlan->tcp->payload_len +
7178 rdev->wowlan->tcp->wake_len +
7179 rdev->wowlan->tcp->wake_len / 8;
7180 }
7181
7182 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007183 if (!msg)
7184 return -ENOMEM;
7185
Eric W. Biederman15e47302012-09-07 20:12:54 +00007186 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007187 NL80211_CMD_GET_WOWLAN);
7188 if (!hdr)
7189 goto nla_put_failure;
7190
7191 if (rdev->wowlan) {
7192 struct nlattr *nl_wowlan;
7193
7194 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7195 if (!nl_wowlan)
7196 goto nla_put_failure;
7197
David S. Miller9360ffd2012-03-29 04:41:26 -04007198 if ((rdev->wowlan->any &&
7199 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7200 (rdev->wowlan->disconnect &&
7201 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7202 (rdev->wowlan->magic_pkt &&
7203 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7204 (rdev->wowlan->gtk_rekey_failure &&
7205 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7206 (rdev->wowlan->eap_identity_req &&
7207 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7208 (rdev->wowlan->four_way_handshake &&
7209 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7210 (rdev->wowlan->rfkill_release &&
7211 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7212 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007213
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007214 if (nl80211_send_wowlan_patterns(msg, rdev))
7215 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007216
7217 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7218 goto nla_put_failure;
7219
Johannes Bergff1b6e62011-05-04 15:37:28 +02007220 nla_nest_end(msg, nl_wowlan);
7221 }
7222
7223 genlmsg_end(msg, hdr);
7224 return genlmsg_reply(msg, info);
7225
7226nla_put_failure:
7227 nlmsg_free(msg);
7228 return -ENOBUFS;
7229}
7230
Johannes Berg2a0e0472013-01-23 22:57:40 +01007231static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7232 struct nlattr *attr,
7233 struct cfg80211_wowlan *trig)
7234{
7235 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7236 struct cfg80211_wowlan_tcp *cfg;
7237 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7238 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7239 u32 size;
7240 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7241 int err, port;
7242
7243 if (!rdev->wiphy.wowlan.tcp)
7244 return -EINVAL;
7245
7246 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7247 nla_data(attr), nla_len(attr),
7248 nl80211_wowlan_tcp_policy);
7249 if (err)
7250 return err;
7251
7252 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7253 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7254 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7255 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7256 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7257 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7258 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7259 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7260 return -EINVAL;
7261
7262 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7263 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7264 return -EINVAL;
7265
7266 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7267 rdev->wiphy.wowlan.tcp->data_interval_max)
7268 return -EINVAL;
7269
7270 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7271 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7272 return -EINVAL;
7273
7274 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7275 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7276 return -EINVAL;
7277
7278 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7279 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7280
7281 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7282 tokens_size = tokln - sizeof(*tok);
7283
7284 if (!tok->len || tokens_size % tok->len)
7285 return -EINVAL;
7286 if (!rdev->wiphy.wowlan.tcp->tok)
7287 return -EINVAL;
7288 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7289 return -EINVAL;
7290 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7291 return -EINVAL;
7292 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7293 return -EINVAL;
7294 if (tok->offset + tok->len > data_size)
7295 return -EINVAL;
7296 }
7297
7298 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7299 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7300 if (!rdev->wiphy.wowlan.tcp->seq)
7301 return -EINVAL;
7302 if (seq->len == 0 || seq->len > 4)
7303 return -EINVAL;
7304 if (seq->len + seq->offset > data_size)
7305 return -EINVAL;
7306 }
7307
7308 size = sizeof(*cfg);
7309 size += data_size;
7310 size += wake_size + wake_mask_size;
7311 size += tokens_size;
7312
7313 cfg = kzalloc(size, GFP_KERNEL);
7314 if (!cfg)
7315 return -ENOMEM;
7316 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7317 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7318 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7319 ETH_ALEN);
7320 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7321 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7322 else
7323 port = 0;
7324#ifdef CONFIG_INET
7325 /* allocate a socket and port for it and use it */
7326 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7327 IPPROTO_TCP, &cfg->sock, 1);
7328 if (err) {
7329 kfree(cfg);
7330 return err;
7331 }
7332 if (inet_csk_get_port(cfg->sock->sk, port)) {
7333 sock_release(cfg->sock);
7334 kfree(cfg);
7335 return -EADDRINUSE;
7336 }
7337 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7338#else
7339 if (!port) {
7340 kfree(cfg);
7341 return -EINVAL;
7342 }
7343 cfg->src_port = port;
7344#endif
7345
7346 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7347 cfg->payload_len = data_size;
7348 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7349 memcpy((void *)cfg->payload,
7350 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7351 data_size);
7352 if (seq)
7353 cfg->payload_seq = *seq;
7354 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7355 cfg->wake_len = wake_size;
7356 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7357 memcpy((void *)cfg->wake_data,
7358 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7359 wake_size);
7360 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7361 data_size + wake_size;
7362 memcpy((void *)cfg->wake_mask,
7363 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7364 wake_mask_size);
7365 if (tok) {
7366 cfg->tokens_size = tokens_size;
7367 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7368 }
7369
7370 trig->tcp = cfg;
7371
7372 return 0;
7373}
7374
Johannes Bergff1b6e62011-05-04 15:37:28 +02007375static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7376{
7377 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7378 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007379 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007380 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007381 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7382 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007383 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007384
Johannes Berg2a0e0472013-01-23 22:57:40 +01007385 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7386 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007387 return -EOPNOTSUPP;
7388
Johannes Bergae33bd82012-07-12 16:25:02 +02007389 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7390 cfg80211_rdev_free_wowlan(rdev);
7391 rdev->wowlan = NULL;
7392 goto set_wakeup;
7393 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007394
7395 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7396 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7397 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7398 nl80211_wowlan_policy);
7399 if (err)
7400 return err;
7401
7402 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7403 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7404 return -EINVAL;
7405 new_triggers.any = true;
7406 }
7407
7408 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7409 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7410 return -EINVAL;
7411 new_triggers.disconnect = true;
7412 }
7413
7414 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7415 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7416 return -EINVAL;
7417 new_triggers.magic_pkt = true;
7418 }
7419
Johannes Berg77dbbb12011-07-13 10:48:55 +02007420 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7421 return -EINVAL;
7422
7423 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7424 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7425 return -EINVAL;
7426 new_triggers.gtk_rekey_failure = true;
7427 }
7428
7429 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7430 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7431 return -EINVAL;
7432 new_triggers.eap_identity_req = true;
7433 }
7434
7435 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7436 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7437 return -EINVAL;
7438 new_triggers.four_way_handshake = true;
7439 }
7440
7441 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7442 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7443 return -EINVAL;
7444 new_triggers.rfkill_release = true;
7445 }
7446
Johannes Bergff1b6e62011-05-04 15:37:28 +02007447 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7448 struct nlattr *pat;
7449 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007450 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007451 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7452
7453 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7454 rem)
7455 n_patterns++;
7456 if (n_patterns > wowlan->n_patterns)
7457 return -EINVAL;
7458
7459 new_triggers.patterns = kcalloc(n_patterns,
7460 sizeof(new_triggers.patterns[0]),
7461 GFP_KERNEL);
7462 if (!new_triggers.patterns)
7463 return -ENOMEM;
7464
7465 new_triggers.n_patterns = n_patterns;
7466 i = 0;
7467
7468 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7469 rem) {
7470 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7471 nla_data(pat), nla_len(pat), NULL);
7472 err = -EINVAL;
7473 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7474 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7475 goto error;
7476 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7477 mask_len = DIV_ROUND_UP(pat_len, 8);
7478 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7479 mask_len)
7480 goto error;
7481 if (pat_len > wowlan->pattern_max_len ||
7482 pat_len < wowlan->pattern_min_len)
7483 goto error;
7484
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007485 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7486 pkt_offset = 0;
7487 else
7488 pkt_offset = nla_get_u32(
7489 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7490 if (pkt_offset > wowlan->max_pkt_offset)
7491 goto error;
7492 new_triggers.patterns[i].pkt_offset = pkt_offset;
7493
Johannes Bergff1b6e62011-05-04 15:37:28 +02007494 new_triggers.patterns[i].mask =
7495 kmalloc(mask_len + pat_len, GFP_KERNEL);
7496 if (!new_triggers.patterns[i].mask) {
7497 err = -ENOMEM;
7498 goto error;
7499 }
7500 new_triggers.patterns[i].pattern =
7501 new_triggers.patterns[i].mask + mask_len;
7502 memcpy(new_triggers.patterns[i].mask,
7503 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7504 mask_len);
7505 new_triggers.patterns[i].pattern_len = pat_len;
7506 memcpy(new_triggers.patterns[i].pattern,
7507 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7508 pat_len);
7509 i++;
7510 }
7511 }
7512
Johannes Berg2a0e0472013-01-23 22:57:40 +01007513 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7514 err = nl80211_parse_wowlan_tcp(
7515 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7516 &new_triggers);
7517 if (err)
7518 goto error;
7519 }
7520
Johannes Bergae33bd82012-07-12 16:25:02 +02007521 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7522 if (!ntrig) {
7523 err = -ENOMEM;
7524 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007525 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007526 cfg80211_rdev_free_wowlan(rdev);
7527 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007528
Johannes Bergae33bd82012-07-12 16:25:02 +02007529 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007530 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007531 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007532
Johannes Bergff1b6e62011-05-04 15:37:28 +02007533 return 0;
7534 error:
7535 for (i = 0; i < new_triggers.n_patterns; i++)
7536 kfree(new_triggers.patterns[i].mask);
7537 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007538 if (new_triggers.tcp && new_triggers.tcp->sock)
7539 sock_release(new_triggers.tcp->sock);
7540 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007541 return err;
7542}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007543#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007544
Johannes Berge5497d72011-07-05 16:35:40 +02007545static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7546{
7547 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7548 struct net_device *dev = info->user_ptr[1];
7549 struct wireless_dev *wdev = dev->ieee80211_ptr;
7550 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7551 struct cfg80211_gtk_rekey_data rekey_data;
7552 int err;
7553
7554 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7555 return -EINVAL;
7556
7557 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7558 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7559 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7560 nl80211_rekey_policy);
7561 if (err)
7562 return err;
7563
7564 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7565 return -ERANGE;
7566 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7567 return -ERANGE;
7568 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7569 return -ERANGE;
7570
7571 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7572 NL80211_KEK_LEN);
7573 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7574 NL80211_KCK_LEN);
7575 memcpy(rekey_data.replay_ctr,
7576 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7577 NL80211_REPLAY_CTR_LEN);
7578
7579 wdev_lock(wdev);
7580 if (!wdev->current_bss) {
7581 err = -ENOTCONN;
7582 goto out;
7583 }
7584
7585 if (!rdev->ops->set_rekey_data) {
7586 err = -EOPNOTSUPP;
7587 goto out;
7588 }
7589
Hila Gonene35e4d22012-06-27 17:19:42 +03007590 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02007591 out:
7592 wdev_unlock(wdev);
7593 return err;
7594}
7595
Johannes Berg28946da2011-11-04 11:18:12 +01007596static int nl80211_register_unexpected_frame(struct sk_buff *skb,
7597 struct genl_info *info)
7598{
7599 struct net_device *dev = info->user_ptr[1];
7600 struct wireless_dev *wdev = dev->ieee80211_ptr;
7601
7602 if (wdev->iftype != NL80211_IFTYPE_AP &&
7603 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7604 return -EINVAL;
7605
Eric W. Biederman15e47302012-09-07 20:12:54 +00007606 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01007607 return -EBUSY;
7608
Eric W. Biederman15e47302012-09-07 20:12:54 +00007609 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01007610 return 0;
7611}
7612
Johannes Berg7f6cf312011-11-04 11:18:15 +01007613static int nl80211_probe_client(struct sk_buff *skb,
7614 struct genl_info *info)
7615{
7616 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7617 struct net_device *dev = info->user_ptr[1];
7618 struct wireless_dev *wdev = dev->ieee80211_ptr;
7619 struct sk_buff *msg;
7620 void *hdr;
7621 const u8 *addr;
7622 u64 cookie;
7623 int err;
7624
7625 if (wdev->iftype != NL80211_IFTYPE_AP &&
7626 wdev->iftype != NL80211_IFTYPE_P2P_GO)
7627 return -EOPNOTSUPP;
7628
7629 if (!info->attrs[NL80211_ATTR_MAC])
7630 return -EINVAL;
7631
7632 if (!rdev->ops->probe_client)
7633 return -EOPNOTSUPP;
7634
7635 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7636 if (!msg)
7637 return -ENOMEM;
7638
Eric W. Biederman15e47302012-09-07 20:12:54 +00007639 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01007640 NL80211_CMD_PROBE_CLIENT);
7641
7642 if (IS_ERR(hdr)) {
7643 err = PTR_ERR(hdr);
7644 goto free_msg;
7645 }
7646
7647 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7648
Hila Gonene35e4d22012-06-27 17:19:42 +03007649 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01007650 if (err)
7651 goto free_msg;
7652
David S. Miller9360ffd2012-03-29 04:41:26 -04007653 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7654 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01007655
7656 genlmsg_end(msg, hdr);
7657
7658 return genlmsg_reply(msg, info);
7659
7660 nla_put_failure:
7661 err = -ENOBUFS;
7662 free_msg:
7663 nlmsg_free(msg);
7664 return err;
7665}
7666
Johannes Berg5e760232011-11-04 11:18:17 +01007667static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
7668{
7669 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07007670 struct cfg80211_beacon_registration *reg, *nreg;
7671 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007672
7673 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
7674 return -EOPNOTSUPP;
7675
Ben Greear37c73b52012-10-26 14:49:25 -07007676 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
7677 if (!nreg)
7678 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01007679
Ben Greear37c73b52012-10-26 14:49:25 -07007680 /* First, check if already registered. */
7681 spin_lock_bh(&rdev->beacon_registrations_lock);
7682 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
7683 if (reg->nlportid == info->snd_portid) {
7684 rv = -EALREADY;
7685 goto out_err;
7686 }
7687 }
7688 /* Add it to the list */
7689 nreg->nlportid = info->snd_portid;
7690 list_add(&nreg->list, &rdev->beacon_registrations);
7691
7692 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01007693
7694 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07007695out_err:
7696 spin_unlock_bh(&rdev->beacon_registrations_lock);
7697 kfree(nreg);
7698 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01007699}
7700
Johannes Berg98104fde2012-06-16 00:19:54 +02007701static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
7702{
7703 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7704 struct wireless_dev *wdev = info->user_ptr[1];
7705 int err;
7706
7707 if (!rdev->ops->start_p2p_device)
7708 return -EOPNOTSUPP;
7709
7710 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7711 return -EOPNOTSUPP;
7712
7713 if (wdev->p2p_started)
7714 return 0;
7715
7716 mutex_lock(&rdev->devlist_mtx);
7717 err = cfg80211_can_add_interface(rdev, wdev->iftype);
7718 mutex_unlock(&rdev->devlist_mtx);
7719 if (err)
7720 return err;
7721
Johannes Bergeeb126e2012-10-23 15:16:50 +02007722 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007723 if (err)
7724 return err;
7725
7726 wdev->p2p_started = true;
7727 mutex_lock(&rdev->devlist_mtx);
7728 rdev->opencount++;
7729 mutex_unlock(&rdev->devlist_mtx);
7730
7731 return 0;
7732}
7733
7734static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7735{
7736 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7737 struct wireless_dev *wdev = info->user_ptr[1];
7738
7739 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7740 return -EOPNOTSUPP;
7741
7742 if (!rdev->ops->stop_p2p_device)
7743 return -EOPNOTSUPP;
7744
7745 if (!wdev->p2p_started)
7746 return 0;
7747
Johannes Bergeeb126e2012-10-23 15:16:50 +02007748 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007749 wdev->p2p_started = false;
7750
7751 mutex_lock(&rdev->devlist_mtx);
7752 rdev->opencount--;
7753 mutex_unlock(&rdev->devlist_mtx);
7754
7755 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7756 rdev->scan_req->aborted = true;
7757 ___cfg80211_scan_done(rdev, true);
7758 }
7759
7760 return 0;
7761}
7762
Johannes Berg4c476992010-10-04 21:36:35 +02007763#define NL80211_FLAG_NEED_WIPHY 0x01
7764#define NL80211_FLAG_NEED_NETDEV 0x02
7765#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007766#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7767#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7768 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007769#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007770/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007771#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7772 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007773
7774static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7775 struct genl_info *info)
7776{
7777 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007778 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007779 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007780 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7781
7782 if (rtnl)
7783 rtnl_lock();
7784
7785 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007786 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007787 if (IS_ERR(rdev)) {
7788 if (rtnl)
7789 rtnl_unlock();
7790 return PTR_ERR(rdev);
7791 }
7792 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007793 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7794 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007795 mutex_lock(&cfg80211_mutex);
7796 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7797 info->attrs);
7798 if (IS_ERR(wdev)) {
7799 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007800 if (rtnl)
7801 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007802 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007803 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007804
Johannes Berg89a54e42012-06-15 14:33:17 +02007805 dev = wdev->netdev;
7806 rdev = wiphy_to_dev(wdev->wiphy);
7807
Johannes Berg1bf614e2012-06-15 15:23:36 +02007808 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7809 if (!dev) {
7810 mutex_unlock(&cfg80211_mutex);
7811 if (rtnl)
7812 rtnl_unlock();
7813 return -EINVAL;
7814 }
7815
7816 info->user_ptr[1] = dev;
7817 } else {
7818 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007819 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007820
Johannes Berg1bf614e2012-06-15 15:23:36 +02007821 if (dev) {
7822 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7823 !netif_running(dev)) {
7824 mutex_unlock(&cfg80211_mutex);
7825 if (rtnl)
7826 rtnl_unlock();
7827 return -ENETDOWN;
7828 }
7829
7830 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007831 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7832 if (!wdev->p2p_started) {
7833 mutex_unlock(&cfg80211_mutex);
7834 if (rtnl)
7835 rtnl_unlock();
7836 return -ENETDOWN;
7837 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007838 }
7839
Johannes Berg89a54e42012-06-15 14:33:17 +02007840 cfg80211_lock_rdev(rdev);
7841
7842 mutex_unlock(&cfg80211_mutex);
7843
Johannes Berg4c476992010-10-04 21:36:35 +02007844 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007845 }
7846
7847 return 0;
7848}
7849
7850static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7851 struct genl_info *info)
7852{
7853 if (info->user_ptr[0])
7854 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007855 if (info->user_ptr[1]) {
7856 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7857 struct wireless_dev *wdev = info->user_ptr[1];
7858
7859 if (wdev->netdev)
7860 dev_put(wdev->netdev);
7861 } else {
7862 dev_put(info->user_ptr[1]);
7863 }
7864 }
Johannes Berg4c476992010-10-04 21:36:35 +02007865 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7866 rtnl_unlock();
7867}
7868
Johannes Berg55682962007-09-20 13:09:35 -04007869static struct genl_ops nl80211_ops[] = {
7870 {
7871 .cmd = NL80211_CMD_GET_WIPHY,
7872 .doit = nl80211_get_wiphy,
7873 .dumpit = nl80211_dump_wiphy,
7874 .policy = nl80211_policy,
7875 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007876 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007877 },
7878 {
7879 .cmd = NL80211_CMD_SET_WIPHY,
7880 .doit = nl80211_set_wiphy,
7881 .policy = nl80211_policy,
7882 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007883 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007884 },
7885 {
7886 .cmd = NL80211_CMD_GET_INTERFACE,
7887 .doit = nl80211_get_interface,
7888 .dumpit = nl80211_dump_interface,
7889 .policy = nl80211_policy,
7890 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007891 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007892 },
7893 {
7894 .cmd = NL80211_CMD_SET_INTERFACE,
7895 .doit = nl80211_set_interface,
7896 .policy = nl80211_policy,
7897 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007898 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7899 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007900 },
7901 {
7902 .cmd = NL80211_CMD_NEW_INTERFACE,
7903 .doit = nl80211_new_interface,
7904 .policy = nl80211_policy,
7905 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007906 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7907 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007908 },
7909 {
7910 .cmd = NL80211_CMD_DEL_INTERFACE,
7911 .doit = nl80211_del_interface,
7912 .policy = nl80211_policy,
7913 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007914 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007915 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007916 },
Johannes Berg41ade002007-12-19 02:03:29 +01007917 {
7918 .cmd = NL80211_CMD_GET_KEY,
7919 .doit = nl80211_get_key,
7920 .policy = nl80211_policy,
7921 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007922 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007923 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007924 },
7925 {
7926 .cmd = NL80211_CMD_SET_KEY,
7927 .doit = nl80211_set_key,
7928 .policy = nl80211_policy,
7929 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007930 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007931 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007932 },
7933 {
7934 .cmd = NL80211_CMD_NEW_KEY,
7935 .doit = nl80211_new_key,
7936 .policy = nl80211_policy,
7937 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007938 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007939 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007940 },
7941 {
7942 .cmd = NL80211_CMD_DEL_KEY,
7943 .doit = nl80211_del_key,
7944 .policy = nl80211_policy,
7945 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007946 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007947 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007948 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007949 {
7950 .cmd = NL80211_CMD_SET_BEACON,
7951 .policy = nl80211_policy,
7952 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007953 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007954 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007955 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007956 },
7957 {
Johannes Berg88600202012-02-13 15:17:18 +01007958 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007959 .policy = nl80211_policy,
7960 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007961 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007962 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007963 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007964 },
7965 {
Johannes Berg88600202012-02-13 15:17:18 +01007966 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007967 .policy = nl80211_policy,
7968 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007969 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007970 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007971 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007972 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007973 {
7974 .cmd = NL80211_CMD_GET_STATION,
7975 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007976 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007977 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007978 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7979 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007980 },
7981 {
7982 .cmd = NL80211_CMD_SET_STATION,
7983 .doit = nl80211_set_station,
7984 .policy = nl80211_policy,
7985 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007986 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007987 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007988 },
7989 {
7990 .cmd = NL80211_CMD_NEW_STATION,
7991 .doit = nl80211_new_station,
7992 .policy = nl80211_policy,
7993 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007994 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007995 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007996 },
7997 {
7998 .cmd = NL80211_CMD_DEL_STATION,
7999 .doit = nl80211_del_station,
8000 .policy = nl80211_policy,
8001 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008002 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008003 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008004 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008005 {
8006 .cmd = NL80211_CMD_GET_MPATH,
8007 .doit = nl80211_get_mpath,
8008 .dumpit = nl80211_dump_mpath,
8009 .policy = nl80211_policy,
8010 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008011 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008012 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008013 },
8014 {
8015 .cmd = NL80211_CMD_SET_MPATH,
8016 .doit = nl80211_set_mpath,
8017 .policy = nl80211_policy,
8018 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008019 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008020 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008021 },
8022 {
8023 .cmd = NL80211_CMD_NEW_MPATH,
8024 .doit = nl80211_new_mpath,
8025 .policy = nl80211_policy,
8026 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008027 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008028 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008029 },
8030 {
8031 .cmd = NL80211_CMD_DEL_MPATH,
8032 .doit = nl80211_del_mpath,
8033 .policy = nl80211_policy,
8034 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008035 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008036 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008037 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008038 {
8039 .cmd = NL80211_CMD_SET_BSS,
8040 .doit = nl80211_set_bss,
8041 .policy = nl80211_policy,
8042 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008043 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008044 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008045 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008046 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008047 .cmd = NL80211_CMD_GET_REG,
8048 .doit = nl80211_get_reg,
8049 .policy = nl80211_policy,
8050 /* can be retrieved by unprivileged users */
8051 },
8052 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008053 .cmd = NL80211_CMD_SET_REG,
8054 .doit = nl80211_set_reg,
8055 .policy = nl80211_policy,
8056 .flags = GENL_ADMIN_PERM,
8057 },
8058 {
8059 .cmd = NL80211_CMD_REQ_SET_REG,
8060 .doit = nl80211_req_set_reg,
8061 .policy = nl80211_policy,
8062 .flags = GENL_ADMIN_PERM,
8063 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008064 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008065 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8066 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008067 .policy = nl80211_policy,
8068 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008069 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008070 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008071 },
8072 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008073 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8074 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008075 .policy = nl80211_policy,
8076 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008077 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008078 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008079 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008080 {
Johannes Berg2a519312009-02-10 21:25:55 +01008081 .cmd = NL80211_CMD_TRIGGER_SCAN,
8082 .doit = nl80211_trigger_scan,
8083 .policy = nl80211_policy,
8084 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008085 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008086 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008087 },
8088 {
8089 .cmd = NL80211_CMD_GET_SCAN,
8090 .policy = nl80211_policy,
8091 .dumpit = nl80211_dump_scan,
8092 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008093 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008094 .cmd = NL80211_CMD_START_SCHED_SCAN,
8095 .doit = nl80211_start_sched_scan,
8096 .policy = nl80211_policy,
8097 .flags = GENL_ADMIN_PERM,
8098 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8099 NL80211_FLAG_NEED_RTNL,
8100 },
8101 {
8102 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8103 .doit = nl80211_stop_sched_scan,
8104 .policy = nl80211_policy,
8105 .flags = GENL_ADMIN_PERM,
8106 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8107 NL80211_FLAG_NEED_RTNL,
8108 },
8109 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008110 .cmd = NL80211_CMD_AUTHENTICATE,
8111 .doit = nl80211_authenticate,
8112 .policy = nl80211_policy,
8113 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008114 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008115 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008116 },
8117 {
8118 .cmd = NL80211_CMD_ASSOCIATE,
8119 .doit = nl80211_associate,
8120 .policy = nl80211_policy,
8121 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008122 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008123 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008124 },
8125 {
8126 .cmd = NL80211_CMD_DEAUTHENTICATE,
8127 .doit = nl80211_deauthenticate,
8128 .policy = nl80211_policy,
8129 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008130 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008131 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008132 },
8133 {
8134 .cmd = NL80211_CMD_DISASSOCIATE,
8135 .doit = nl80211_disassociate,
8136 .policy = nl80211_policy,
8137 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008138 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008139 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008140 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008141 {
8142 .cmd = NL80211_CMD_JOIN_IBSS,
8143 .doit = nl80211_join_ibss,
8144 .policy = nl80211_policy,
8145 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008146 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008147 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008148 },
8149 {
8150 .cmd = NL80211_CMD_LEAVE_IBSS,
8151 .doit = nl80211_leave_ibss,
8152 .policy = nl80211_policy,
8153 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008154 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008155 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008156 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008157#ifdef CONFIG_NL80211_TESTMODE
8158 {
8159 .cmd = NL80211_CMD_TESTMODE,
8160 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008161 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008162 .policy = nl80211_policy,
8163 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008164 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8165 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008166 },
8167#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008168 {
8169 .cmd = NL80211_CMD_CONNECT,
8170 .doit = nl80211_connect,
8171 .policy = nl80211_policy,
8172 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008173 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008174 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008175 },
8176 {
8177 .cmd = NL80211_CMD_DISCONNECT,
8178 .doit = nl80211_disconnect,
8179 .policy = nl80211_policy,
8180 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008181 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008182 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008183 },
Johannes Berg463d0182009-07-14 00:33:35 +02008184 {
8185 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8186 .doit = nl80211_wiphy_netns,
8187 .policy = nl80211_policy,
8188 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008189 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8190 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008191 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008192 {
8193 .cmd = NL80211_CMD_GET_SURVEY,
8194 .policy = nl80211_policy,
8195 .dumpit = nl80211_dump_survey,
8196 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008197 {
8198 .cmd = NL80211_CMD_SET_PMKSA,
8199 .doit = nl80211_setdel_pmksa,
8200 .policy = nl80211_policy,
8201 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008202 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008203 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008204 },
8205 {
8206 .cmd = NL80211_CMD_DEL_PMKSA,
8207 .doit = nl80211_setdel_pmksa,
8208 .policy = nl80211_policy,
8209 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008210 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008211 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008212 },
8213 {
8214 .cmd = NL80211_CMD_FLUSH_PMKSA,
8215 .doit = nl80211_flush_pmksa,
8216 .policy = nl80211_policy,
8217 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008218 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008219 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008220 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008221 {
8222 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8223 .doit = nl80211_remain_on_channel,
8224 .policy = nl80211_policy,
8225 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008226 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008227 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008228 },
8229 {
8230 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8231 .doit = nl80211_cancel_remain_on_channel,
8232 .policy = nl80211_policy,
8233 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008234 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008235 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008236 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008237 {
8238 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8239 .doit = nl80211_set_tx_bitrate_mask,
8240 .policy = nl80211_policy,
8241 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008242 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8243 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008244 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008245 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008246 .cmd = NL80211_CMD_REGISTER_FRAME,
8247 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008248 .policy = nl80211_policy,
8249 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008250 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008251 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008252 },
8253 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008254 .cmd = NL80211_CMD_FRAME,
8255 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008256 .policy = nl80211_policy,
8257 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008258 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008259 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008260 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008261 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008262 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8263 .doit = nl80211_tx_mgmt_cancel_wait,
8264 .policy = nl80211_policy,
8265 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008266 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008267 NL80211_FLAG_NEED_RTNL,
8268 },
8269 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008270 .cmd = NL80211_CMD_SET_POWER_SAVE,
8271 .doit = nl80211_set_power_save,
8272 .policy = nl80211_policy,
8273 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008274 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8275 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008276 },
8277 {
8278 .cmd = NL80211_CMD_GET_POWER_SAVE,
8279 .doit = nl80211_get_power_save,
8280 .policy = nl80211_policy,
8281 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008282 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8283 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008284 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008285 {
8286 .cmd = NL80211_CMD_SET_CQM,
8287 .doit = nl80211_set_cqm,
8288 .policy = nl80211_policy,
8289 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008290 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8291 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008292 },
Johannes Bergf444de02010-05-05 15:25:02 +02008293 {
8294 .cmd = NL80211_CMD_SET_CHANNEL,
8295 .doit = nl80211_set_channel,
8296 .policy = nl80211_policy,
8297 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008298 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8299 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008300 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008301 {
8302 .cmd = NL80211_CMD_SET_WDS_PEER,
8303 .doit = nl80211_set_wds_peer,
8304 .policy = nl80211_policy,
8305 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008306 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8307 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008308 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008309 {
8310 .cmd = NL80211_CMD_JOIN_MESH,
8311 .doit = nl80211_join_mesh,
8312 .policy = nl80211_policy,
8313 .flags = GENL_ADMIN_PERM,
8314 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8315 NL80211_FLAG_NEED_RTNL,
8316 },
8317 {
8318 .cmd = NL80211_CMD_LEAVE_MESH,
8319 .doit = nl80211_leave_mesh,
8320 .policy = nl80211_policy,
8321 .flags = GENL_ADMIN_PERM,
8322 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8323 NL80211_FLAG_NEED_RTNL,
8324 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008325#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008326 {
8327 .cmd = NL80211_CMD_GET_WOWLAN,
8328 .doit = nl80211_get_wowlan,
8329 .policy = nl80211_policy,
8330 /* can be retrieved by unprivileged users */
8331 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8332 NL80211_FLAG_NEED_RTNL,
8333 },
8334 {
8335 .cmd = NL80211_CMD_SET_WOWLAN,
8336 .doit = nl80211_set_wowlan,
8337 .policy = nl80211_policy,
8338 .flags = GENL_ADMIN_PERM,
8339 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8340 NL80211_FLAG_NEED_RTNL,
8341 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008342#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008343 {
8344 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8345 .doit = nl80211_set_rekey_data,
8346 .policy = nl80211_policy,
8347 .flags = GENL_ADMIN_PERM,
8348 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8349 NL80211_FLAG_NEED_RTNL,
8350 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008351 {
8352 .cmd = NL80211_CMD_TDLS_MGMT,
8353 .doit = nl80211_tdls_mgmt,
8354 .policy = nl80211_policy,
8355 .flags = GENL_ADMIN_PERM,
8356 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8357 NL80211_FLAG_NEED_RTNL,
8358 },
8359 {
8360 .cmd = NL80211_CMD_TDLS_OPER,
8361 .doit = nl80211_tdls_oper,
8362 .policy = nl80211_policy,
8363 .flags = GENL_ADMIN_PERM,
8364 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8365 NL80211_FLAG_NEED_RTNL,
8366 },
Johannes Berg28946da2011-11-04 11:18:12 +01008367 {
8368 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8369 .doit = nl80211_register_unexpected_frame,
8370 .policy = nl80211_policy,
8371 .flags = GENL_ADMIN_PERM,
8372 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8373 NL80211_FLAG_NEED_RTNL,
8374 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008375 {
8376 .cmd = NL80211_CMD_PROBE_CLIENT,
8377 .doit = nl80211_probe_client,
8378 .policy = nl80211_policy,
8379 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008380 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008381 NL80211_FLAG_NEED_RTNL,
8382 },
Johannes Berg5e760232011-11-04 11:18:17 +01008383 {
8384 .cmd = NL80211_CMD_REGISTER_BEACONS,
8385 .doit = nl80211_register_beacons,
8386 .policy = nl80211_policy,
8387 .flags = GENL_ADMIN_PERM,
8388 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8389 NL80211_FLAG_NEED_RTNL,
8390 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008391 {
8392 .cmd = NL80211_CMD_SET_NOACK_MAP,
8393 .doit = nl80211_set_noack_map,
8394 .policy = nl80211_policy,
8395 .flags = GENL_ADMIN_PERM,
8396 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8397 NL80211_FLAG_NEED_RTNL,
8398 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008399 {
8400 .cmd = NL80211_CMD_START_P2P_DEVICE,
8401 .doit = nl80211_start_p2p_device,
8402 .policy = nl80211_policy,
8403 .flags = GENL_ADMIN_PERM,
8404 .internal_flags = NL80211_FLAG_NEED_WDEV |
8405 NL80211_FLAG_NEED_RTNL,
8406 },
8407 {
8408 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8409 .doit = nl80211_stop_p2p_device,
8410 .policy = nl80211_policy,
8411 .flags = GENL_ADMIN_PERM,
8412 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8413 NL80211_FLAG_NEED_RTNL,
8414 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008415 {
8416 .cmd = NL80211_CMD_SET_MCAST_RATE,
8417 .doit = nl80211_set_mcast_rate,
8418 .policy = nl80211_policy,
8419 .flags = GENL_ADMIN_PERM,
8420 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8421 NL80211_FLAG_NEED_RTNL,
8422 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308423 {
8424 .cmd = NL80211_CMD_SET_MAC_ACL,
8425 .doit = nl80211_set_mac_acl,
8426 .policy = nl80211_policy,
8427 .flags = GENL_ADMIN_PERM,
8428 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8429 NL80211_FLAG_NEED_RTNL,
8430 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008431 {
8432 .cmd = NL80211_CMD_RADAR_DETECT,
8433 .doit = nl80211_start_radar_detection,
8434 .policy = nl80211_policy,
8435 .flags = GENL_ADMIN_PERM,
8436 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8437 NL80211_FLAG_NEED_RTNL,
8438 },
Johannes Berg55682962007-09-20 13:09:35 -04008439};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008440
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008441static struct genl_multicast_group nl80211_mlme_mcgrp = {
8442 .name = "mlme",
8443};
Johannes Berg55682962007-09-20 13:09:35 -04008444
8445/* multicast groups */
8446static struct genl_multicast_group nl80211_config_mcgrp = {
8447 .name = "config",
8448};
Johannes Berg2a519312009-02-10 21:25:55 +01008449static struct genl_multicast_group nl80211_scan_mcgrp = {
8450 .name = "scan",
8451};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008452static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8453 .name = "regulatory",
8454};
Johannes Berg55682962007-09-20 13:09:35 -04008455
8456/* notification functions */
8457
8458void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8459{
8460 struct sk_buff *msg;
8461
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008462 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008463 if (!msg)
8464 return;
8465
8466 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
8467 nlmsg_free(msg);
8468 return;
8469 }
8470
Johannes Berg463d0182009-07-14 00:33:35 +02008471 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8472 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008473}
8474
Johannes Berg362a4152009-05-24 16:43:15 +02008475static int nl80211_add_scan_req(struct sk_buff *msg,
8476 struct cfg80211_registered_device *rdev)
8477{
8478 struct cfg80211_scan_request *req = rdev->scan_req;
8479 struct nlattr *nest;
8480 int i;
8481
Johannes Berg667503dd2009-07-07 03:56:11 +02008482 ASSERT_RDEV_LOCK(rdev);
8483
Johannes Berg362a4152009-05-24 16:43:15 +02008484 if (WARN_ON(!req))
8485 return 0;
8486
8487 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
8488 if (!nest)
8489 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008490 for (i = 0; i < req->n_ssids; i++) {
8491 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
8492 goto nla_put_failure;
8493 }
Johannes Berg362a4152009-05-24 16:43:15 +02008494 nla_nest_end(msg, nest);
8495
8496 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8497 if (!nest)
8498 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04008499 for (i = 0; i < req->n_channels; i++) {
8500 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
8501 goto nla_put_failure;
8502 }
Johannes Berg362a4152009-05-24 16:43:15 +02008503 nla_nest_end(msg, nest);
8504
David S. Miller9360ffd2012-03-29 04:41:26 -04008505 if (req->ie &&
8506 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
8507 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02008508
Sam Lefflered4737712012-10-11 21:03:31 -07008509 if (req->flags)
8510 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
8511
Johannes Berg362a4152009-05-24 16:43:15 +02008512 return 0;
8513 nla_put_failure:
8514 return -ENOBUFS;
8515}
8516
Johannes Berga538e2d2009-06-16 19:56:42 +02008517static int nl80211_send_scan_msg(struct sk_buff *msg,
8518 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008519 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008520 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02008521 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01008522{
8523 void *hdr;
8524
Eric W. Biederman15e47302012-09-07 20:12:54 +00008525 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01008526 if (!hdr)
8527 return -1;
8528
David S. Miller9360ffd2012-03-29 04:41:26 -04008529 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02008530 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8531 wdev->netdev->ifindex)) ||
8532 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04008533 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01008534
Johannes Berg362a4152009-05-24 16:43:15 +02008535 /* ignore errors and send incomplete event anyway */
8536 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01008537
8538 return genlmsg_end(msg, hdr);
8539
8540 nla_put_failure:
8541 genlmsg_cancel(msg, hdr);
8542 return -EMSGSIZE;
8543}
8544
Luciano Coelho807f8a82011-05-11 17:09:35 +03008545static int
8546nl80211_send_sched_scan_msg(struct sk_buff *msg,
8547 struct cfg80211_registered_device *rdev,
8548 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008549 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03008550{
8551 void *hdr;
8552
Eric W. Biederman15e47302012-09-07 20:12:54 +00008553 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008554 if (!hdr)
8555 return -1;
8556
David S. Miller9360ffd2012-03-29 04:41:26 -04008557 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8558 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8559 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03008560
8561 return genlmsg_end(msg, hdr);
8562
8563 nla_put_failure:
8564 genlmsg_cancel(msg, hdr);
8565 return -EMSGSIZE;
8566}
8567
Johannes Berga538e2d2009-06-16 19:56:42 +02008568void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008569 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02008570{
8571 struct sk_buff *msg;
8572
Thomas Graf58050fc2012-06-28 03:57:45 +00008573 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008574 if (!msg)
8575 return;
8576
Johannes Bergfd014282012-06-18 19:17:03 +02008577 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008578 NL80211_CMD_TRIGGER_SCAN) < 0) {
8579 nlmsg_free(msg);
8580 return;
8581 }
8582
Johannes Berg463d0182009-07-14 00:33:35 +02008583 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8584 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02008585}
8586
Johannes Berg2a519312009-02-10 21:25:55 +01008587void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008588 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008589{
8590 struct sk_buff *msg;
8591
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008592 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008593 if (!msg)
8594 return;
8595
Johannes Bergfd014282012-06-18 19:17:03 +02008596 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008597 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008598 nlmsg_free(msg);
8599 return;
8600 }
8601
Johannes Berg463d0182009-07-14 00:33:35 +02008602 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8603 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008604}
8605
8606void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02008607 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01008608{
8609 struct sk_buff *msg;
8610
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008611 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008612 if (!msg)
8613 return;
8614
Johannes Bergfd014282012-06-18 19:17:03 +02008615 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02008616 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01008617 nlmsg_free(msg);
8618 return;
8619 }
8620
Johannes Berg463d0182009-07-14 00:33:35 +02008621 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8622 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01008623}
8624
Luciano Coelho807f8a82011-05-11 17:09:35 +03008625void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
8626 struct net_device *netdev)
8627{
8628 struct sk_buff *msg;
8629
8630 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8631 if (!msg)
8632 return;
8633
8634 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
8635 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
8636 nlmsg_free(msg);
8637 return;
8638 }
8639
8640 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8641 nl80211_scan_mcgrp.id, GFP_KERNEL);
8642}
8643
8644void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
8645 struct net_device *netdev, u32 cmd)
8646{
8647 struct sk_buff *msg;
8648
Thomas Graf58050fc2012-06-28 03:57:45 +00008649 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03008650 if (!msg)
8651 return;
8652
8653 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
8654 nlmsg_free(msg);
8655 return;
8656 }
8657
8658 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8659 nl80211_scan_mcgrp.id, GFP_KERNEL);
8660}
8661
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008662/*
8663 * This can happen on global regulatory changes or device specific settings
8664 * based on custom world regulatory domains.
8665 */
8666void nl80211_send_reg_change_event(struct regulatory_request *request)
8667{
8668 struct sk_buff *msg;
8669 void *hdr;
8670
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008671 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008672 if (!msg)
8673 return;
8674
8675 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
8676 if (!hdr) {
8677 nlmsg_free(msg);
8678 return;
8679 }
8680
8681 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04008682 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
8683 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008684
David S. Miller9360ffd2012-03-29 04:41:26 -04008685 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
8686 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8687 NL80211_REGDOM_TYPE_WORLD))
8688 goto nla_put_failure;
8689 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
8690 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8691 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
8692 goto nla_put_failure;
8693 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
8694 request->intersect) {
8695 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8696 NL80211_REGDOM_TYPE_INTERSECTION))
8697 goto nla_put_failure;
8698 } else {
8699 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
8700 NL80211_REGDOM_TYPE_COUNTRY) ||
8701 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
8702 request->alpha2))
8703 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008704 }
8705
Johannes Bergf4173762012-12-03 18:23:37 +01008706 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008707 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
8708 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008709
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008710 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008711
Johannes Bergbc43b282009-07-25 10:54:13 +02008712 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02008713 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02008714 GFP_ATOMIC);
8715 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008716
8717 return;
8718
8719nla_put_failure:
8720 genlmsg_cancel(msg, hdr);
8721 nlmsg_free(msg);
8722}
8723
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008724static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
8725 struct net_device *netdev,
8726 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008727 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008728{
8729 struct sk_buff *msg;
8730 void *hdr;
8731
Johannes Berge6d6e342009-07-01 21:26:47 +02008732 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008733 if (!msg)
8734 return;
8735
8736 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8737 if (!hdr) {
8738 nlmsg_free(msg);
8739 return;
8740 }
8741
David S. Miller9360ffd2012-03-29 04:41:26 -04008742 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8743 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8744 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8745 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008746
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008747 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008748
Johannes Berg463d0182009-07-14 00:33:35 +02008749 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8750 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008751 return;
8752
8753 nla_put_failure:
8754 genlmsg_cancel(msg, hdr);
8755 nlmsg_free(msg);
8756}
8757
8758void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008759 struct net_device *netdev, const u8 *buf,
8760 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008761{
8762 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008763 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008764}
8765
8766void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8767 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008768 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008769{
Johannes Berge6d6e342009-07-01 21:26:47 +02008770 nl80211_send_mlme_event(rdev, netdev, buf, len,
8771 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008772}
8773
Jouni Malinen53b46b82009-03-27 20:53:56 +02008774void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008775 struct net_device *netdev, const u8 *buf,
8776 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008777{
8778 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008779 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008780}
8781
Jouni Malinen53b46b82009-03-27 20:53:56 +02008782void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8783 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008784 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008785{
8786 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008787 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008788}
8789
Jouni Malinencf4e5942010-12-16 00:52:40 +02008790void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8791 struct net_device *netdev, const u8 *buf,
8792 size_t len, gfp_t gfp)
8793{
8794 nl80211_send_mlme_event(rdev, netdev, buf, len,
8795 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8796}
8797
8798void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8799 struct net_device *netdev, const u8 *buf,
8800 size_t len, gfp_t gfp)
8801{
8802 nl80211_send_mlme_event(rdev, netdev, buf, len,
8803 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8804}
8805
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008806static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8807 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008808 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008809{
8810 struct sk_buff *msg;
8811 void *hdr;
8812
Johannes Berge6d6e342009-07-01 21:26:47 +02008813 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008814 if (!msg)
8815 return;
8816
8817 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8818 if (!hdr) {
8819 nlmsg_free(msg);
8820 return;
8821 }
8822
David S. Miller9360ffd2012-03-29 04:41:26 -04008823 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8824 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8825 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8826 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8827 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008828
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008829 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008830
Johannes Berg463d0182009-07-14 00:33:35 +02008831 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8832 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008833 return;
8834
8835 nla_put_failure:
8836 genlmsg_cancel(msg, hdr);
8837 nlmsg_free(msg);
8838}
8839
8840void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008841 struct net_device *netdev, const u8 *addr,
8842 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008843{
8844 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008845 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008846}
8847
8848void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008849 struct net_device *netdev, const u8 *addr,
8850 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008851{
Johannes Berge6d6e342009-07-01 21:26:47 +02008852 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8853 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008854}
8855
Samuel Ortizb23aa672009-07-01 21:26:54 +02008856void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8857 struct net_device *netdev, const u8 *bssid,
8858 const u8 *req_ie, size_t req_ie_len,
8859 const u8 *resp_ie, size_t resp_ie_len,
8860 u16 status, gfp_t gfp)
8861{
8862 struct sk_buff *msg;
8863 void *hdr;
8864
Thomas Graf58050fc2012-06-28 03:57:45 +00008865 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008866 if (!msg)
8867 return;
8868
8869 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8870 if (!hdr) {
8871 nlmsg_free(msg);
8872 return;
8873 }
8874
David S. Miller9360ffd2012-03-29 04:41:26 -04008875 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8876 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8877 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8878 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8879 (req_ie &&
8880 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8881 (resp_ie &&
8882 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8883 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008884
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008885 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008886
Johannes Berg463d0182009-07-14 00:33:35 +02008887 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8888 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008889 return;
8890
8891 nla_put_failure:
8892 genlmsg_cancel(msg, hdr);
8893 nlmsg_free(msg);
8894
8895}
8896
8897void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8898 struct net_device *netdev, const u8 *bssid,
8899 const u8 *req_ie, size_t req_ie_len,
8900 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8901{
8902 struct sk_buff *msg;
8903 void *hdr;
8904
Thomas Graf58050fc2012-06-28 03:57:45 +00008905 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008906 if (!msg)
8907 return;
8908
8909 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8910 if (!hdr) {
8911 nlmsg_free(msg);
8912 return;
8913 }
8914
David S. Miller9360ffd2012-03-29 04:41:26 -04008915 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8916 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8917 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8918 (req_ie &&
8919 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8920 (resp_ie &&
8921 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8922 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008923
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008924 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008925
Johannes Berg463d0182009-07-14 00:33:35 +02008926 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8927 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008928 return;
8929
8930 nla_put_failure:
8931 genlmsg_cancel(msg, hdr);
8932 nlmsg_free(msg);
8933
8934}
8935
8936void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8937 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008938 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008939{
8940 struct sk_buff *msg;
8941 void *hdr;
8942
Thomas Graf58050fc2012-06-28 03:57:45 +00008943 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008944 if (!msg)
8945 return;
8946
8947 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8948 if (!hdr) {
8949 nlmsg_free(msg);
8950 return;
8951 }
8952
David S. Miller9360ffd2012-03-29 04:41:26 -04008953 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8954 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8955 (from_ap && reason &&
8956 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8957 (from_ap &&
8958 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8959 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8960 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008961
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008962 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008963
Johannes Berg463d0182009-07-14 00:33:35 +02008964 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8965 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008966 return;
8967
8968 nla_put_failure:
8969 genlmsg_cancel(msg, hdr);
8970 nlmsg_free(msg);
8971
8972}
8973
Johannes Berg04a773a2009-04-19 21:24:32 +02008974void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8975 struct net_device *netdev, const u8 *bssid,
8976 gfp_t gfp)
8977{
8978 struct sk_buff *msg;
8979 void *hdr;
8980
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008981 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008982 if (!msg)
8983 return;
8984
8985 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8986 if (!hdr) {
8987 nlmsg_free(msg);
8988 return;
8989 }
8990
David S. Miller9360ffd2012-03-29 04:41:26 -04008991 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8992 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8993 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8994 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008995
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008996 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008997
Johannes Berg463d0182009-07-14 00:33:35 +02008998 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8999 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009000 return;
9001
9002 nla_put_failure:
9003 genlmsg_cancel(msg, hdr);
9004 nlmsg_free(msg);
9005}
9006
Javier Cardonac93b5e72011-04-07 15:08:34 -07009007void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
9008 struct net_device *netdev,
9009 const u8 *macaddr, const u8* ie, u8 ie_len,
9010 gfp_t gfp)
9011{
9012 struct sk_buff *msg;
9013 void *hdr;
9014
9015 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9016 if (!msg)
9017 return;
9018
9019 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9020 if (!hdr) {
9021 nlmsg_free(msg);
9022 return;
9023 }
9024
David S. Miller9360ffd2012-03-29 04:41:26 -04009025 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9026 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9027 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
9028 (ie_len && ie &&
9029 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9030 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009031
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009032 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009033
9034 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9035 nl80211_mlme_mcgrp.id, gfp);
9036 return;
9037
9038 nla_put_failure:
9039 genlmsg_cancel(msg, hdr);
9040 nlmsg_free(msg);
9041}
9042
Jouni Malinena3b8b052009-03-27 21:59:49 +02009043void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9044 struct net_device *netdev, const u8 *addr,
9045 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009046 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009047{
9048 struct sk_buff *msg;
9049 void *hdr;
9050
Johannes Berge6d6e342009-07-01 21:26:47 +02009051 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009052 if (!msg)
9053 return;
9054
9055 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9056 if (!hdr) {
9057 nlmsg_free(msg);
9058 return;
9059 }
9060
David S. Miller9360ffd2012-03-29 04:41:26 -04009061 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9062 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9063 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9064 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9065 (key_id != -1 &&
9066 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9067 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9068 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009069
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009070 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009071
Johannes Berg463d0182009-07-14 00:33:35 +02009072 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9073 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009074 return;
9075
9076 nla_put_failure:
9077 genlmsg_cancel(msg, hdr);
9078 nlmsg_free(msg);
9079}
9080
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009081void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9082 struct ieee80211_channel *channel_before,
9083 struct ieee80211_channel *channel_after)
9084{
9085 struct sk_buff *msg;
9086 void *hdr;
9087 struct nlattr *nl_freq;
9088
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009089 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009090 if (!msg)
9091 return;
9092
9093 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9094 if (!hdr) {
9095 nlmsg_free(msg);
9096 return;
9097 }
9098
9099 /*
9100 * Since we are applying the beacon hint to a wiphy we know its
9101 * wiphy_idx is valid
9102 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009103 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9104 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009105
9106 /* Before */
9107 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9108 if (!nl_freq)
9109 goto nla_put_failure;
9110 if (nl80211_msg_put_channel(msg, channel_before))
9111 goto nla_put_failure;
9112 nla_nest_end(msg, nl_freq);
9113
9114 /* After */
9115 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9116 if (!nl_freq)
9117 goto nla_put_failure;
9118 if (nl80211_msg_put_channel(msg, channel_after))
9119 goto nla_put_failure;
9120 nla_nest_end(msg, nl_freq);
9121
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009122 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009123
Johannes Berg463d0182009-07-14 00:33:35 +02009124 rcu_read_lock();
9125 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9126 GFP_ATOMIC);
9127 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009128
9129 return;
9130
9131nla_put_failure:
9132 genlmsg_cancel(msg, hdr);
9133 nlmsg_free(msg);
9134}
9135
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009136static void nl80211_send_remain_on_chan_event(
9137 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009138 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009139 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009140 unsigned int duration, gfp_t gfp)
9141{
9142 struct sk_buff *msg;
9143 void *hdr;
9144
9145 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9146 if (!msg)
9147 return;
9148
9149 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9150 if (!hdr) {
9151 nlmsg_free(msg);
9152 return;
9153 }
9154
David S. Miller9360ffd2012-03-29 04:41:26 -04009155 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009156 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9157 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009158 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009159 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009160 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9161 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009162 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9163 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009164
David S. Miller9360ffd2012-03-29 04:41:26 -04009165 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9166 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9167 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009168
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009169 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009170
9171 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9172 nl80211_mlme_mcgrp.id, gfp);
9173 return;
9174
9175 nla_put_failure:
9176 genlmsg_cancel(msg, hdr);
9177 nlmsg_free(msg);
9178}
9179
9180void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009181 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009182 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009183 unsigned int duration, gfp_t gfp)
9184{
9185 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009186 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009187 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009188}
9189
9190void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02009191 struct cfg80211_registered_device *rdev,
9192 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01009193 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009194{
9195 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009196 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009197}
9198
Johannes Berg98b62182009-12-23 13:15:44 +01009199void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
9200 struct net_device *dev, const u8 *mac_addr,
9201 struct station_info *sinfo, gfp_t gfp)
9202{
9203 struct sk_buff *msg;
9204
Thomas Graf58050fc2012-06-28 03:57:45 +00009205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009206 if (!msg)
9207 return;
9208
John W. Linville66266b32012-03-15 13:25:41 -04009209 if (nl80211_send_station(msg, 0, 0, 0,
9210 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009211 nlmsg_free(msg);
9212 return;
9213 }
9214
9215 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9216 nl80211_mlme_mcgrp.id, gfp);
9217}
9218
Jouni Malinenec15e682011-03-23 15:29:52 +02009219void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
9220 struct net_device *dev, const u8 *mac_addr,
9221 gfp_t gfp)
9222{
9223 struct sk_buff *msg;
9224 void *hdr;
9225
Thomas Graf58050fc2012-06-28 03:57:45 +00009226 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009227 if (!msg)
9228 return;
9229
9230 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9231 if (!hdr) {
9232 nlmsg_free(msg);
9233 return;
9234 }
9235
David S. Miller9360ffd2012-03-29 04:41:26 -04009236 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9237 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9238 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009239
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009240 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009241
9242 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9243 nl80211_mlme_mcgrp.id, gfp);
9244 return;
9245
9246 nla_put_failure:
9247 genlmsg_cancel(msg, hdr);
9248 nlmsg_free(msg);
9249}
9250
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309251void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
9252 struct net_device *dev, const u8 *mac_addr,
9253 enum nl80211_connect_failed_reason reason,
9254 gfp_t gfp)
9255{
9256 struct sk_buff *msg;
9257 void *hdr;
9258
9259 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9260 if (!msg)
9261 return;
9262
9263 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9264 if (!hdr) {
9265 nlmsg_free(msg);
9266 return;
9267 }
9268
9269 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9270 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9271 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9272 goto nla_put_failure;
9273
9274 genlmsg_end(msg, hdr);
9275
9276 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9277 nl80211_mlme_mcgrp.id, gfp);
9278 return;
9279
9280 nla_put_failure:
9281 genlmsg_cancel(msg, hdr);
9282 nlmsg_free(msg);
9283}
9284
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009285static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9286 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009287{
9288 struct wireless_dev *wdev = dev->ieee80211_ptr;
9289 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9290 struct sk_buff *msg;
9291 void *hdr;
9292 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009293 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009294
Eric W. Biederman15e47302012-09-07 20:12:54 +00009295 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009296 return false;
9297
9298 msg = nlmsg_new(100, gfp);
9299 if (!msg)
9300 return true;
9301
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009302 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009303 if (!hdr) {
9304 nlmsg_free(msg);
9305 return true;
9306 }
9307
David S. Miller9360ffd2012-03-29 04:41:26 -04009308 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9309 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9310 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9311 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009312
9313 err = genlmsg_end(msg, hdr);
9314 if (err < 0) {
9315 nlmsg_free(msg);
9316 return true;
9317 }
9318
Eric W. Biederman15e47302012-09-07 20:12:54 +00009319 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009320 return true;
9321
9322 nla_put_failure:
9323 genlmsg_cancel(msg, hdr);
9324 nlmsg_free(msg);
9325 return true;
9326}
9327
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009328bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
9329{
9330 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9331 addr, gfp);
9332}
9333
9334bool nl80211_unexpected_4addr_frame(struct net_device *dev,
9335 const u8 *addr, gfp_t gfp)
9336{
9337 return __nl80211_unexpected_frame(dev,
9338 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9339 addr, gfp);
9340}
9341
Johannes Berg2e161f72010-08-12 15:38:38 +02009342int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009343 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009344 int freq, int sig_dbm,
9345 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009346{
Johannes Berg71bbc992012-06-15 15:30:18 +02009347 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009348 struct sk_buff *msg;
9349 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009350
9351 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9352 if (!msg)
9353 return -ENOMEM;
9354
Johannes Berg2e161f72010-08-12 15:38:38 +02009355 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009356 if (!hdr) {
9357 nlmsg_free(msg);
9358 return -ENOMEM;
9359 }
9360
David S. Miller9360ffd2012-03-29 04:41:26 -04009361 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009362 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9363 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009364 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9365 (sig_dbm &&
9366 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9367 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9368 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009369
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009370 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009371
Eric W. Biederman15e47302012-09-07 20:12:54 +00009372 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009373
9374 nla_put_failure:
9375 genlmsg_cancel(msg, hdr);
9376 nlmsg_free(msg);
9377 return -ENOBUFS;
9378}
9379
Johannes Berg2e161f72010-08-12 15:38:38 +02009380void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009381 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02009382 const u8 *buf, size_t len, bool ack,
9383 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009384{
Johannes Berg71bbc992012-06-15 15:30:18 +02009385 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009386 struct sk_buff *msg;
9387 void *hdr;
9388
9389 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9390 if (!msg)
9391 return;
9392
Johannes Berg2e161f72010-08-12 15:38:38 +02009393 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009394 if (!hdr) {
9395 nlmsg_free(msg);
9396 return;
9397 }
9398
David S. Miller9360ffd2012-03-29 04:41:26 -04009399 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009400 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9401 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009402 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9403 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9404 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9405 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009406
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009407 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009408
9409 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9410 return;
9411
9412 nla_put_failure:
9413 genlmsg_cancel(msg, hdr);
9414 nlmsg_free(msg);
9415}
9416
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009417void
9418nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
9419 struct net_device *netdev,
9420 enum nl80211_cqm_rssi_threshold_event rssi_event,
9421 gfp_t gfp)
9422{
9423 struct sk_buff *msg;
9424 struct nlattr *pinfoattr;
9425 void *hdr;
9426
Thomas Graf58050fc2012-06-28 03:57:45 +00009427 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009428 if (!msg)
9429 return;
9430
9431 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9432 if (!hdr) {
9433 nlmsg_free(msg);
9434 return;
9435 }
9436
David S. Miller9360ffd2012-03-29 04:41:26 -04009437 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9438 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9439 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009440
9441 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9442 if (!pinfoattr)
9443 goto nla_put_failure;
9444
David S. Miller9360ffd2012-03-29 04:41:26 -04009445 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
9446 rssi_event))
9447 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009448
9449 nla_nest_end(msg, pinfoattr);
9450
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009451 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009452
9453 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9454 nl80211_mlme_mcgrp.id, gfp);
9455 return;
9456
9457 nla_put_failure:
9458 genlmsg_cancel(msg, hdr);
9459 nlmsg_free(msg);
9460}
9461
Johannes Berge5497d72011-07-05 16:35:40 +02009462void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
9463 struct net_device *netdev, const u8 *bssid,
9464 const u8 *replay_ctr, gfp_t gfp)
9465{
9466 struct sk_buff *msg;
9467 struct nlattr *rekey_attr;
9468 void *hdr;
9469
Thomas Graf58050fc2012-06-28 03:57:45 +00009470 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02009471 if (!msg)
9472 return;
9473
9474 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9475 if (!hdr) {
9476 nlmsg_free(msg);
9477 return;
9478 }
9479
David S. Miller9360ffd2012-03-29 04:41:26 -04009480 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9481 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9482 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9483 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009484
9485 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9486 if (!rekey_attr)
9487 goto nla_put_failure;
9488
David S. Miller9360ffd2012-03-29 04:41:26 -04009489 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
9490 NL80211_REPLAY_CTR_LEN, replay_ctr))
9491 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02009492
9493 nla_nest_end(msg, rekey_attr);
9494
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009495 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02009496
9497 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9498 nl80211_mlme_mcgrp.id, gfp);
9499 return;
9500
9501 nla_put_failure:
9502 genlmsg_cancel(msg, hdr);
9503 nlmsg_free(msg);
9504}
9505
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009506void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
9507 struct net_device *netdev, int index,
9508 const u8 *bssid, bool preauth, gfp_t gfp)
9509{
9510 struct sk_buff *msg;
9511 struct nlattr *attr;
9512 void *hdr;
9513
Thomas Graf58050fc2012-06-28 03:57:45 +00009514 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009515 if (!msg)
9516 return;
9517
9518 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
9519 if (!hdr) {
9520 nlmsg_free(msg);
9521 return;
9522 }
9523
David S. Miller9360ffd2012-03-29 04:41:26 -04009524 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9525 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9526 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009527
9528 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
9529 if (!attr)
9530 goto nla_put_failure;
9531
David S. Miller9360ffd2012-03-29 04:41:26 -04009532 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
9533 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
9534 (preauth &&
9535 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
9536 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009537
9538 nla_nest_end(msg, attr);
9539
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009540 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03009541
9542 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9543 nl80211_mlme_mcgrp.id, gfp);
9544 return;
9545
9546 nla_put_failure:
9547 genlmsg_cancel(msg, hdr);
9548 nlmsg_free(msg);
9549}
9550
Thomas Pedersen53145262012-04-06 13:35:47 -07009551void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01009552 struct net_device *netdev,
9553 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07009554{
9555 struct sk_buff *msg;
9556 void *hdr;
9557
Thomas Graf58050fc2012-06-28 03:57:45 +00009558 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07009559 if (!msg)
9560 return;
9561
9562 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
9563 if (!hdr) {
9564 nlmsg_free(msg);
9565 return;
9566 }
9567
Johannes Berg683b6d32012-11-08 21:25:48 +01009568 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9569 goto nla_put_failure;
9570
9571 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04009572 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07009573
9574 genlmsg_end(msg, hdr);
9575
9576 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9577 nl80211_mlme_mcgrp.id, gfp);
9578 return;
9579
9580 nla_put_failure:
9581 genlmsg_cancel(msg, hdr);
9582 nlmsg_free(msg);
9583}
9584
Johannes Bergc063dbf2010-11-24 08:10:05 +01009585void
Thomas Pedersen84f10702012-07-12 16:17:33 -07009586nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
9587 struct net_device *netdev, const u8 *peer,
9588 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
9589{
9590 struct sk_buff *msg;
9591 struct nlattr *pinfoattr;
9592 void *hdr;
9593
9594 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9595 if (!msg)
9596 return;
9597
9598 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9599 if (!hdr) {
9600 nlmsg_free(msg);
9601 return;
9602 }
9603
9604 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9605 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9606 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9607 goto nla_put_failure;
9608
9609 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9610 if (!pinfoattr)
9611 goto nla_put_failure;
9612
9613 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
9614 goto nla_put_failure;
9615
9616 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
9617 goto nla_put_failure;
9618
9619 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
9620 goto nla_put_failure;
9621
9622 nla_nest_end(msg, pinfoattr);
9623
9624 genlmsg_end(msg, hdr);
9625
9626 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9627 nl80211_mlme_mcgrp.id, gfp);
9628 return;
9629
9630 nla_put_failure:
9631 genlmsg_cancel(msg, hdr);
9632 nlmsg_free(msg);
9633}
9634
9635void
Simon Wunderlich04f39042013-02-08 18:16:19 +01009636nl80211_radar_notify(struct cfg80211_registered_device *rdev,
9637 struct cfg80211_chan_def *chandef,
9638 enum nl80211_radar_event event,
9639 struct net_device *netdev, gfp_t gfp)
9640{
9641 struct sk_buff *msg;
9642 void *hdr;
9643
9644 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9645 if (!msg)
9646 return;
9647
9648 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
9649 if (!hdr) {
9650 nlmsg_free(msg);
9651 return;
9652 }
9653
9654 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9655 goto nla_put_failure;
9656
9657 /* NOP and radar events don't need a netdev parameter */
9658 if (netdev) {
9659 struct wireless_dev *wdev = netdev->ieee80211_ptr;
9660
9661 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9662 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9663 goto nla_put_failure;
9664 }
9665
9666 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
9667 goto nla_put_failure;
9668
9669 if (nl80211_send_chandef(msg, chandef))
9670 goto nla_put_failure;
9671
9672 if (genlmsg_end(msg, hdr) < 0) {
9673 nlmsg_free(msg);
9674 return;
9675 }
9676
9677 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9678 nl80211_mlme_mcgrp.id, gfp);
9679 return;
9680
9681 nla_put_failure:
9682 genlmsg_cancel(msg, hdr);
9683 nlmsg_free(msg);
9684}
9685
9686void
Johannes Bergc063dbf2010-11-24 08:10:05 +01009687nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
9688 struct net_device *netdev, const u8 *peer,
9689 u32 num_packets, gfp_t gfp)
9690{
9691 struct sk_buff *msg;
9692 struct nlattr *pinfoattr;
9693 void *hdr;
9694
Thomas Graf58050fc2012-06-28 03:57:45 +00009695 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009696 if (!msg)
9697 return;
9698
9699 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
9700 if (!hdr) {
9701 nlmsg_free(msg);
9702 return;
9703 }
9704
David S. Miller9360ffd2012-03-29 04:41:26 -04009705 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9706 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9707 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
9708 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009709
9710 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
9711 if (!pinfoattr)
9712 goto nla_put_failure;
9713
David S. Miller9360ffd2012-03-29 04:41:26 -04009714 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
9715 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01009716
9717 nla_nest_end(msg, pinfoattr);
9718
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009719 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01009720
9721 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9722 nl80211_mlme_mcgrp.id, gfp);
9723 return;
9724
9725 nla_put_failure:
9726 genlmsg_cancel(msg, hdr);
9727 nlmsg_free(msg);
9728}
9729
Johannes Berg7f6cf312011-11-04 11:18:15 +01009730void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
9731 u64 cookie, bool acked, gfp_t gfp)
9732{
9733 struct wireless_dev *wdev = dev->ieee80211_ptr;
9734 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9735 struct sk_buff *msg;
9736 void *hdr;
9737 int err;
9738
Beni Lev4ee3e062012-08-27 12:49:39 +03009739 trace_cfg80211_probe_status(dev, addr, cookie, acked);
9740
Thomas Graf58050fc2012-06-28 03:57:45 +00009741 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03009742
Johannes Berg7f6cf312011-11-04 11:18:15 +01009743 if (!msg)
9744 return;
9745
9746 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
9747 if (!hdr) {
9748 nlmsg_free(msg);
9749 return;
9750 }
9751
David S. Miller9360ffd2012-03-29 04:41:26 -04009752 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9753 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9754 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9755 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9756 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
9757 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009758
9759 err = genlmsg_end(msg, hdr);
9760 if (err < 0) {
9761 nlmsg_free(msg);
9762 return;
9763 }
9764
9765 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9766 nl80211_mlme_mcgrp.id, gfp);
9767 return;
9768
9769 nla_put_failure:
9770 genlmsg_cancel(msg, hdr);
9771 nlmsg_free(msg);
9772}
9773EXPORT_SYMBOL(cfg80211_probe_status);
9774
Johannes Berg5e760232011-11-04 11:18:17 +01009775void cfg80211_report_obss_beacon(struct wiphy *wiphy,
9776 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07009777 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01009778{
9779 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9780 struct sk_buff *msg;
9781 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07009782 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01009783
Beni Lev4ee3e062012-08-27 12:49:39 +03009784 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
9785
Ben Greear37c73b52012-10-26 14:49:25 -07009786 spin_lock_bh(&rdev->beacon_registrations_lock);
9787 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9788 msg = nlmsg_new(len + 100, GFP_ATOMIC);
9789 if (!msg) {
9790 spin_unlock_bh(&rdev->beacon_registrations_lock);
9791 return;
9792 }
Johannes Berg5e760232011-11-04 11:18:17 +01009793
Ben Greear37c73b52012-10-26 14:49:25 -07009794 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9795 if (!hdr)
9796 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009797
Ben Greear37c73b52012-10-26 14:49:25 -07009798 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9799 (freq &&
9800 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9801 (sig_dbm &&
9802 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9803 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9804 goto nla_put_failure;
9805
9806 genlmsg_end(msg, hdr);
9807
9808 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009809 }
Ben Greear37c73b52012-10-26 14:49:25 -07009810 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009811 return;
9812
9813 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009814 spin_unlock_bh(&rdev->beacon_registrations_lock);
9815 if (hdr)
9816 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009817 nlmsg_free(msg);
9818}
9819EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9820
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009821#ifdef CONFIG_PM
9822void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
9823 struct cfg80211_wowlan_wakeup *wakeup,
9824 gfp_t gfp)
9825{
9826 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9827 struct sk_buff *msg;
9828 void *hdr;
9829 int err, size = 200;
9830
9831 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
9832
9833 if (wakeup)
9834 size += wakeup->packet_present_len;
9835
9836 msg = nlmsg_new(size, gfp);
9837 if (!msg)
9838 return;
9839
9840 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
9841 if (!hdr)
9842 goto free_msg;
9843
9844 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9845 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9846 goto free_msg;
9847
9848 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9849 wdev->netdev->ifindex))
9850 goto free_msg;
9851
9852 if (wakeup) {
9853 struct nlattr *reasons;
9854
9855 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9856
9857 if (wakeup->disconnect &&
9858 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
9859 goto free_msg;
9860 if (wakeup->magic_pkt &&
9861 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
9862 goto free_msg;
9863 if (wakeup->gtk_rekey_failure &&
9864 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
9865 goto free_msg;
9866 if (wakeup->eap_identity_req &&
9867 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
9868 goto free_msg;
9869 if (wakeup->four_way_handshake &&
9870 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
9871 goto free_msg;
9872 if (wakeup->rfkill_release &&
9873 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
9874 goto free_msg;
9875
9876 if (wakeup->pattern_idx >= 0 &&
9877 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
9878 wakeup->pattern_idx))
9879 goto free_msg;
9880
Johannes Berg2a0e0472013-01-23 22:57:40 +01009881 if (wakeup->tcp_match)
9882 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
9883
9884 if (wakeup->tcp_connlost)
9885 nla_put_flag(msg,
9886 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
9887
9888 if (wakeup->tcp_nomoretokens)
9889 nla_put_flag(msg,
9890 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
9891
Johannes Bergcd8f7cb2013-01-22 12:34:29 +01009892 if (wakeup->packet) {
9893 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
9894 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
9895
9896 if (!wakeup->packet_80211) {
9897 pkt_attr =
9898 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
9899 len_attr =
9900 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
9901 }
9902
9903 if (wakeup->packet_len &&
9904 nla_put_u32(msg, len_attr, wakeup->packet_len))
9905 goto free_msg;
9906
9907 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
9908 wakeup->packet))
9909 goto free_msg;
9910 }
9911
9912 nla_nest_end(msg, reasons);
9913 }
9914
9915 err = genlmsg_end(msg, hdr);
9916 if (err < 0)
9917 goto free_msg;
9918
9919 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9920 nl80211_mlme_mcgrp.id, gfp);
9921 return;
9922
9923 free_msg:
9924 nlmsg_free(msg);
9925}
9926EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
9927#endif
9928
Jouni Malinen3475b092012-11-16 22:49:57 +02009929void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9930 enum nl80211_tdls_operation oper,
9931 u16 reason_code, gfp_t gfp)
9932{
9933 struct wireless_dev *wdev = dev->ieee80211_ptr;
9934 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9935 struct sk_buff *msg;
9936 void *hdr;
9937 int err;
9938
9939 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9940 reason_code);
9941
9942 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9943 if (!msg)
9944 return;
9945
9946 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9947 if (!hdr) {
9948 nlmsg_free(msg);
9949 return;
9950 }
9951
9952 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9953 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9954 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9955 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9956 (reason_code > 0 &&
9957 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9958 goto nla_put_failure;
9959
9960 err = genlmsg_end(msg, hdr);
9961 if (err < 0) {
9962 nlmsg_free(msg);
9963 return;
9964 }
9965
9966 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9967 nl80211_mlme_mcgrp.id, gfp);
9968 return;
9969
9970 nla_put_failure:
9971 genlmsg_cancel(msg, hdr);
9972 nlmsg_free(msg);
9973}
9974EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9975
Jouni Malinen026331c2010-02-15 12:53:10 +02009976static int nl80211_netlink_notify(struct notifier_block * nb,
9977 unsigned long state,
9978 void *_notify)
9979{
9980 struct netlink_notify *notify = _notify;
9981 struct cfg80211_registered_device *rdev;
9982 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009983 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009984
9985 if (state != NETLINK_URELEASE)
9986 return NOTIFY_DONE;
9987
9988 rcu_read_lock();
9989
Johannes Berg5e760232011-11-04 11:18:17 +01009990 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009991 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009992 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009993
9994 spin_lock_bh(&rdev->beacon_registrations_lock);
9995 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9996 list) {
9997 if (reg->nlportid == notify->portid) {
9998 list_del(&reg->list);
9999 kfree(reg);
10000 break;
10001 }
10002 }
10003 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010004 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010005
10006 rcu_read_unlock();
10007
10008 return NOTIFY_DONE;
10009}
10010
10011static struct notifier_block nl80211_netlink_notifier = {
10012 .notifier_call = nl80211_netlink_notify,
10013};
10014
Johannes Berg55682962007-09-20 13:09:35 -040010015/* initialisation/exit functions */
10016
10017int nl80211_init(void)
10018{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010019 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010020
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010021 err = genl_register_family_with_ops(&nl80211_fam,
10022 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010023 if (err)
10024 return err;
10025
Johannes Berg55682962007-09-20 13:09:35 -040010026 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10027 if (err)
10028 goto err_out;
10029
Johannes Berg2a519312009-02-10 21:25:55 +010010030 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10031 if (err)
10032 goto err_out;
10033
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010034 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10035 if (err)
10036 goto err_out;
10037
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010038 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10039 if (err)
10040 goto err_out;
10041
Johannes Bergaff89a92009-07-01 21:26:51 +020010042#ifdef CONFIG_NL80211_TESTMODE
10043 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10044 if (err)
10045 goto err_out;
10046#endif
10047
Jouni Malinen026331c2010-02-15 12:53:10 +020010048 err = netlink_register_notifier(&nl80211_netlink_notifier);
10049 if (err)
10050 goto err_out;
10051
Johannes Berg55682962007-09-20 13:09:35 -040010052 return 0;
10053 err_out:
10054 genl_unregister_family(&nl80211_fam);
10055 return err;
10056}
10057
10058void nl80211_exit(void)
10059{
Jouni Malinen026331c2010-02-15 12:53:10 +020010060 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010061 genl_unregister_family(&nl80211_fam);
10062}