blob: 9216e45e53a090172c3ef2f036a9e83d802c2010 [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 Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg89a54e42012-06-15 14:33:17 +020049/* returns ERR_PTR values */
50static struct wireless_dev *
51__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040052{
Johannes Berg89a54e42012-06-15 14:33:17 +020053 struct cfg80211_registered_device *rdev;
54 struct wireless_dev *result = NULL;
55 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
56 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
57 u64 wdev_id;
58 int wiphy_idx = -1;
59 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040060
Johannes Berg89a54e42012-06-15 14:33:17 +020061 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040062
Johannes Berg89a54e42012-06-15 14:33:17 +020063 if (!have_ifidx && !have_wdev_id)
64 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040065
Johannes Berg89a54e42012-06-15 14:33:17 +020066 if (have_ifidx)
67 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
68 if (have_wdev_id) {
69 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
70 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040071 }
72
Johannes Berg89a54e42012-06-15 14:33:17 +020073 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
74 struct wireless_dev *wdev;
75
76 if (wiphy_net(&rdev->wiphy) != netns)
77 continue;
78
79 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
80 continue;
81
82 mutex_lock(&rdev->devlist_mtx);
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
88 }
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
92 }
93 }
94 mutex_unlock(&rdev->devlist_mtx);
95
96 if (result)
97 break;
98 }
99
100 if (result)
101 return result;
102 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400103}
104
Johannes Berga9455402012-06-15 13:32:49 +0200105static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200106__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200107{
Johannes Berg7fee4772012-06-15 14:09:58 +0200108 struct cfg80211_registered_device *rdev = NULL, *tmp;
109 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200110
111 assert_cfg80211_lock();
112
Johannes Berg878d9ec2012-06-15 14:18:32 +0200113 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200114 !attrs[NL80211_ATTR_IFINDEX] &&
115 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200116 return ERR_PTR(-EINVAL);
117
Johannes Berg878d9ec2012-06-15 14:18:32 +0200118 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200119 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200120 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200121
Johannes Berg89a54e42012-06-15 14:33:17 +0200122 if (attrs[NL80211_ATTR_WDEV]) {
123 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
124 struct wireless_dev *wdev;
125 bool found = false;
126
127 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
128 if (tmp) {
129 /* make sure wdev exists */
130 mutex_lock(&tmp->devlist_mtx);
131 list_for_each_entry(wdev, &tmp->wdev_list, list) {
132 if (wdev->identifier != (u32)wdev_id)
133 continue;
134 found = true;
135 break;
136 }
137 mutex_unlock(&tmp->devlist_mtx);
138
139 if (!found)
140 tmp = NULL;
141
142 if (rdev && tmp != rdev)
143 return ERR_PTR(-EINVAL);
144 rdev = tmp;
145 }
146 }
147
Johannes Berg878d9ec2012-06-15 14:18:32 +0200148 if (attrs[NL80211_ATTR_IFINDEX]) {
149 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200150 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200151 if (netdev) {
152 if (netdev->ieee80211_ptr)
153 tmp = wiphy_to_dev(
154 netdev->ieee80211_ptr->wiphy);
155 else
156 tmp = NULL;
157
158 dev_put(netdev);
159
160 /* not wireless device -- return error */
161 if (!tmp)
162 return ERR_PTR(-EINVAL);
163
164 /* mismatch -- return error */
165 if (rdev && tmp != rdev)
166 return ERR_PTR(-EINVAL);
167
168 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200169 }
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
171
Johannes Berg4f7eff12012-06-15 14:14:22 +0200172 if (!rdev)
173 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200174
Johannes Berg4f7eff12012-06-15 14:14:22 +0200175 if (netns != wiphy_net(&rdev->wiphy))
176 return ERR_PTR(-ENODEV);
177
178 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200179}
180
181/*
182 * This function returns a pointer to the driver
183 * that the genl_info item that is passed refers to.
184 * If successful, it returns non-NULL and also locks
185 * the driver's mutex!
186 *
187 * This means that you need to call cfg80211_unlock_rdev()
188 * before being allowed to acquire &cfg80211_mutex!
189 *
190 * This is necessary because we need to lock the global
191 * mutex to get an item off the list safely, and then
192 * we lock the rdev mutex so it doesn't go away under us.
193 *
194 * We don't want to keep cfg80211_mutex locked
195 * for all the time in order to allow requests on
196 * other interfaces to go through at the same time.
197 *
198 * The result of this can be a PTR_ERR and hence must
199 * be checked with IS_ERR() for errors.
200 */
201static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200202cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200203{
204 struct cfg80211_registered_device *rdev;
205
206 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200207 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208
209 /* if it is not an error we grab the lock on
210 * it to assure it won't be going away while
211 * we operate on it */
212 if (!IS_ERR(rdev))
213 mutex_lock(&rdev->mtx);
214
215 mutex_unlock(&cfg80211_mutex);
216
217 return rdev;
218}
219
Johannes Berg55682962007-09-20 13:09:35 -0400220/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000221static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400222 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700224 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200225 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200226 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530227 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200228 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
229 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
230 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
231 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100232 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400233
234 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
235 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
236 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100237
Eliad Pellere007b852011-11-24 18:13:56 +0200238 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
239 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100240
Johannes Bergb9454e82009-07-08 13:29:08 +0200241 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100242 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
243 .len = WLAN_MAX_KEY_LEN },
244 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
245 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
246 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200247 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200248 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100249
250 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
251 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
252 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
253 .len = IEEE80211_MAX_DATA_LEN },
254 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
255 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100256 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
257 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
258 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
259 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
260 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200263 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100264 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800265 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100266 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300267
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700268 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
269 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
270
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300271 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
272 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
273 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200274 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
275 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100276 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300277
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800278 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700279 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700280
Johannes Berg6c739412011-11-03 09:27:01 +0100281 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200282
283 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
284 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100286 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
287 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200288
289 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_SSID_LEN },
291 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
292 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200293 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300294 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300295 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300296 [NL80211_ATTR_STA_FLAGS2] = {
297 .len = sizeof(struct nl80211_sta_flag_update),
298 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300299 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300300 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
301 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200302 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
303 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
304 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200305 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100306 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100307 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
308 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100309 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
310 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200311 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200312 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
313 .len = IEEE80211_MAX_DATA_LEN },
314 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200315 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200316 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300317 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200318 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300319 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200321 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900322 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
323 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100324 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100325 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100326 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200327 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700328 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300329 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200330 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200331 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300332 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300333 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
334 .len = IEEE80211_MAX_DATA_LEN },
335 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
336 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530337 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300338 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530339 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300340 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
341 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
342 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
343 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
344 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100345 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200346 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
347 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700348 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800349 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
351 .len = NL80211_HT_CAPABILITY_LEN
352 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100353 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530354 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530355 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200356 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Johannes Berg55682962007-09-20 13:09:35 -0400357};
358
Johannes Berge31b8212010-10-05 19:39:30 +0200359/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000360static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200361 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200362 [NL80211_KEY_IDX] = { .type = NLA_U8 },
363 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200364 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200365 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
366 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200367 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100368 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
369};
370
371/* policy for the key default flags */
372static const struct nla_policy
373nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
374 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
375 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200376};
377
Johannes Bergff1b6e62011-05-04 15:37:28 +0200378/* policy for WoWLAN attributes */
379static const struct nla_policy
380nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
381 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
382 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
383 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
384 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200385 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
386 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
387 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
388 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200389};
390
Johannes Berge5497d72011-07-05 16:35:40 +0200391/* policy for GTK rekey offload attributes */
392static const struct nla_policy
393nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
394 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
395 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
396 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
397};
398
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300399static const struct nla_policy
400nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200401 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300402 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700403 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300404};
405
Holger Schuriga0438972009-11-11 11:30:02 +0100406/* ifidx get helper */
407static int nl80211_get_ifidx(struct netlink_callback *cb)
408{
409 int res;
410
411 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
412 nl80211_fam.attrbuf, nl80211_fam.maxattr,
413 nl80211_policy);
414 if (res)
415 return res;
416
417 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
418 return -EINVAL;
419
420 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
421 if (!res)
422 return -EINVAL;
423 return res;
424}
425
Johannes Berg67748892010-10-04 21:14:06 +0200426static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
427 struct netlink_callback *cb,
428 struct cfg80211_registered_device **rdev,
429 struct net_device **dev)
430{
431 int ifidx = cb->args[0];
432 int err;
433
434 if (!ifidx)
435 ifidx = nl80211_get_ifidx(cb);
436 if (ifidx < 0)
437 return ifidx;
438
439 cb->args[0] = ifidx;
440
441 rtnl_lock();
442
443 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
444 if (!*dev) {
445 err = -ENODEV;
446 goto out_rtnl;
447 }
448
449 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100450 if (IS_ERR(*rdev)) {
451 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200452 goto out_rtnl;
453 }
454
455 return 0;
456 out_rtnl:
457 rtnl_unlock();
458 return err;
459}
460
461static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
462{
463 cfg80211_unlock_rdev(rdev);
464 rtnl_unlock();
465}
466
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100467/* IE validation */
468static bool is_valid_ie_attr(const struct nlattr *attr)
469{
470 const u8 *pos;
471 int len;
472
473 if (!attr)
474 return true;
475
476 pos = nla_data(attr);
477 len = nla_len(attr);
478
479 while (len) {
480 u8 elemlen;
481
482 if (len < 2)
483 return false;
484 len -= 2;
485
486 elemlen = pos[1];
487 if (elemlen > len)
488 return false;
489
490 len -= elemlen;
491 pos += 2 + elemlen;
492 }
493
494 return true;
495}
496
Johannes Berg55682962007-09-20 13:09:35 -0400497/* message building helper */
498static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
499 int flags, u8 cmd)
500{
501 /* since there is no private header just add the generic one */
502 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
503}
504
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400505static int nl80211_msg_put_channel(struct sk_buff *msg,
506 struct ieee80211_channel *chan)
507{
David S. Miller9360ffd2012-03-29 04:41:26 -0400508 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
509 chan->center_freq))
510 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400511
David S. Miller9360ffd2012-03-29 04:41:26 -0400512 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
513 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
514 goto nla_put_failure;
515 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
516 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
517 goto nla_put_failure;
518 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
519 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
520 goto nla_put_failure;
521 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
522 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
523 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400524
David S. Miller9360ffd2012-03-29 04:41:26 -0400525 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
526 DBM_TO_MBM(chan->max_power)))
527 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400528
529 return 0;
530
531 nla_put_failure:
532 return -ENOBUFS;
533}
534
Johannes Berg55682962007-09-20 13:09:35 -0400535/* netlink command implementations */
536
Johannes Bergb9454e82009-07-08 13:29:08 +0200537struct key_parse {
538 struct key_params p;
539 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200540 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200541 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100542 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200543};
544
545static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
546{
547 struct nlattr *tb[NL80211_KEY_MAX + 1];
548 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
549 nl80211_key_policy);
550 if (err)
551 return err;
552
553 k->def = !!tb[NL80211_KEY_DEFAULT];
554 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
555
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100556 if (k->def) {
557 k->def_uni = true;
558 k->def_multi = true;
559 }
560 if (k->defmgmt)
561 k->def_multi = true;
562
Johannes Bergb9454e82009-07-08 13:29:08 +0200563 if (tb[NL80211_KEY_IDX])
564 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
565
566 if (tb[NL80211_KEY_DATA]) {
567 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
568 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
569 }
570
571 if (tb[NL80211_KEY_SEQ]) {
572 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
573 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
574 }
575
576 if (tb[NL80211_KEY_CIPHER])
577 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
578
Johannes Berge31b8212010-10-05 19:39:30 +0200579 if (tb[NL80211_KEY_TYPE]) {
580 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
581 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
582 return -EINVAL;
583 }
584
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100585 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
586 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100587 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
588 tb[NL80211_KEY_DEFAULT_TYPES],
589 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100590 if (err)
591 return err;
592
593 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
594 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
595 }
596
Johannes Bergb9454e82009-07-08 13:29:08 +0200597 return 0;
598}
599
600static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
601{
602 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
603 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
604 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
605 }
606
607 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
608 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
609 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
610 }
611
612 if (info->attrs[NL80211_ATTR_KEY_IDX])
613 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
614
615 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
616 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
617
618 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
619 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
620
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100621 if (k->def) {
622 k->def_uni = true;
623 k->def_multi = true;
624 }
625 if (k->defmgmt)
626 k->def_multi = true;
627
Johannes Berge31b8212010-10-05 19:39:30 +0200628 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
629 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
630 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
631 return -EINVAL;
632 }
633
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100634 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
635 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
636 int err = nla_parse_nested(
637 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
638 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
639 nl80211_key_default_policy);
640 if (err)
641 return err;
642
643 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
644 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
645 }
646
Johannes Bergb9454e82009-07-08 13:29:08 +0200647 return 0;
648}
649
650static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
651{
652 int err;
653
654 memset(k, 0, sizeof(*k));
655 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200656 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200657
658 if (info->attrs[NL80211_ATTR_KEY])
659 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
660 else
661 err = nl80211_parse_key_old(info, k);
662
663 if (err)
664 return err;
665
666 if (k->def && k->defmgmt)
667 return -EINVAL;
668
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100669 if (k->defmgmt) {
670 if (k->def_uni || !k->def_multi)
671 return -EINVAL;
672 }
673
Johannes Bergb9454e82009-07-08 13:29:08 +0200674 if (k->idx != -1) {
675 if (k->defmgmt) {
676 if (k->idx < 4 || k->idx > 5)
677 return -EINVAL;
678 } else if (k->def) {
679 if (k->idx < 0 || k->idx > 3)
680 return -EINVAL;
681 } else {
682 if (k->idx < 0 || k->idx > 5)
683 return -EINVAL;
684 }
685 }
686
687 return 0;
688}
689
Johannes Bergfffd0932009-07-08 14:22:54 +0200690static struct cfg80211_cached_keys *
691nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
692 struct nlattr *keys)
693{
694 struct key_parse parse;
695 struct nlattr *key;
696 struct cfg80211_cached_keys *result;
697 int rem, err, def = 0;
698
699 result = kzalloc(sizeof(*result), GFP_KERNEL);
700 if (!result)
701 return ERR_PTR(-ENOMEM);
702
703 result->def = -1;
704 result->defmgmt = -1;
705
706 nla_for_each_nested(key, keys, rem) {
707 memset(&parse, 0, sizeof(parse));
708 parse.idx = -1;
709
710 err = nl80211_parse_key_new(key, &parse);
711 if (err)
712 goto error;
713 err = -EINVAL;
714 if (!parse.p.key)
715 goto error;
716 if (parse.idx < 0 || parse.idx > 4)
717 goto error;
718 if (parse.def) {
719 if (def)
720 goto error;
721 def = 1;
722 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100723 if (!parse.def_uni || !parse.def_multi)
724 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200725 } else if (parse.defmgmt)
726 goto error;
727 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200728 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200729 if (err)
730 goto error;
731 result->params[parse.idx].cipher = parse.p.cipher;
732 result->params[parse.idx].key_len = parse.p.key_len;
733 result->params[parse.idx].key = result->data[parse.idx];
734 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
735 }
736
737 return result;
738 error:
739 kfree(result);
740 return ERR_PTR(err);
741}
742
743static int nl80211_key_allowed(struct wireless_dev *wdev)
744{
745 ASSERT_WDEV_LOCK(wdev);
746
Johannes Bergfffd0932009-07-08 14:22:54 +0200747 switch (wdev->iftype) {
748 case NL80211_IFTYPE_AP:
749 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200750 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700751 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 break;
753 case NL80211_IFTYPE_ADHOC:
754 if (!wdev->current_bss)
755 return -ENOLINK;
756 break;
757 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200758 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200759 if (wdev->sme_state != CFG80211_SME_CONNECTED)
760 return -ENOLINK;
761 break;
762 default:
763 return -EINVAL;
764 }
765
766 return 0;
767}
768
Johannes Berg7527a782011-05-13 10:58:57 +0200769static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
770{
771 struct nlattr *nl_modes = nla_nest_start(msg, attr);
772 int i;
773
774 if (!nl_modes)
775 goto nla_put_failure;
776
777 i = 0;
778 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400779 if ((ifmodes & 1) && nla_put_flag(msg, i))
780 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200781 ifmodes >>= 1;
782 i++;
783 }
784
785 nla_nest_end(msg, nl_modes);
786 return 0;
787
788nla_put_failure:
789 return -ENOBUFS;
790}
791
792static int nl80211_put_iface_combinations(struct wiphy *wiphy,
793 struct sk_buff *msg)
794{
795 struct nlattr *nl_combis;
796 int i, j;
797
798 nl_combis = nla_nest_start(msg,
799 NL80211_ATTR_INTERFACE_COMBINATIONS);
800 if (!nl_combis)
801 goto nla_put_failure;
802
803 for (i = 0; i < wiphy->n_iface_combinations; i++) {
804 const struct ieee80211_iface_combination *c;
805 struct nlattr *nl_combi, *nl_limits;
806
807 c = &wiphy->iface_combinations[i];
808
809 nl_combi = nla_nest_start(msg, i + 1);
810 if (!nl_combi)
811 goto nla_put_failure;
812
813 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
814 if (!nl_limits)
815 goto nla_put_failure;
816
817 for (j = 0; j < c->n_limits; j++) {
818 struct nlattr *nl_limit;
819
820 nl_limit = nla_nest_start(msg, j + 1);
821 if (!nl_limit)
822 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400823 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
824 c->limits[j].max))
825 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200826 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
827 c->limits[j].types))
828 goto nla_put_failure;
829 nla_nest_end(msg, nl_limit);
830 }
831
832 nla_nest_end(msg, nl_limits);
833
David S. Miller9360ffd2012-03-29 04:41:26 -0400834 if (c->beacon_int_infra_match &&
835 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
836 goto nla_put_failure;
837 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
838 c->num_different_channels) ||
839 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
840 c->max_interfaces))
841 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200842
843 nla_nest_end(msg, nl_combi);
844 }
845
846 nla_nest_end(msg, nl_combis);
847
848 return 0;
849nla_put_failure:
850 return -ENOBUFS;
851}
852
Johannes Berg55682962007-09-20 13:09:35 -0400853static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
854 struct cfg80211_registered_device *dev)
855{
856 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100857 struct nlattr *nl_bands, *nl_band;
858 struct nlattr *nl_freqs, *nl_freq;
859 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100860 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100861 enum ieee80211_band band;
862 struct ieee80211_channel *chan;
863 struct ieee80211_rate *rate;
864 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200865 const struct ieee80211_txrx_stypes *mgmt_stypes =
866 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400867
868 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
869 if (!hdr)
870 return -1;
871
David S. Miller9360ffd2012-03-29 04:41:26 -0400872 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
873 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
874 nla_put_u32(msg, NL80211_ATTR_GENERATION,
875 cfg80211_rdev_list_generation) ||
876 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
877 dev->wiphy.retry_short) ||
878 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
879 dev->wiphy.retry_long) ||
880 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
881 dev->wiphy.frag_threshold) ||
882 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
883 dev->wiphy.rts_threshold) ||
884 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
885 dev->wiphy.coverage_class) ||
886 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
887 dev->wiphy.max_scan_ssids) ||
888 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
889 dev->wiphy.max_sched_scan_ssids) ||
890 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
891 dev->wiphy.max_scan_ie_len) ||
892 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
893 dev->wiphy.max_sched_scan_ie_len) ||
894 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
895 dev->wiphy.max_match_sets))
896 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200897
David S. Miller9360ffd2012-03-29 04:41:26 -0400898 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
899 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
900 goto nla_put_failure;
901 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
902 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
903 goto nla_put_failure;
904 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
905 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
906 goto nla_put_failure;
907 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
908 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
909 goto nla_put_failure;
910 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
911 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
912 goto nla_put_failure;
913 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
914 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
915 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200916
David S. Miller9360ffd2012-03-29 04:41:26 -0400917 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
918 sizeof(u32) * dev->wiphy.n_cipher_suites,
919 dev->wiphy.cipher_suites))
920 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100921
David S. Miller9360ffd2012-03-29 04:41:26 -0400922 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
923 dev->wiphy.max_num_pmkids))
924 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530925
David S. Miller9360ffd2012-03-29 04:41:26 -0400926 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
927 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
928 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200929
David S. Miller9360ffd2012-03-29 04:41:26 -0400930 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
931 dev->wiphy.available_antennas_tx) ||
932 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
933 dev->wiphy.available_antennas_rx))
934 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100935
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
937 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
938 dev->wiphy.probe_resp_offload))
939 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200940
Bruno Randolf7f531e02010-12-16 11:30:22 +0900941 if ((dev->wiphy.available_antennas_tx ||
942 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900943 u32 tx_ant = 0, rx_ant = 0;
944 int res;
945 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
946 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
948 tx_ant) ||
949 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
950 rx_ant))
951 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900952 }
953 }
954
Johannes Berg7527a782011-05-13 10:58:57 +0200955 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
956 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700957 goto nla_put_failure;
958
Johannes Bergee688b002008-01-24 19:38:39 +0100959 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
960 if (!nl_bands)
961 goto nla_put_failure;
962
963 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
964 if (!dev->wiphy.bands[band])
965 continue;
966
967 nl_band = nla_nest_start(msg, band);
968 if (!nl_band)
969 goto nla_put_failure;
970
Johannes Bergd51626d2008-10-09 12:20:13 +0200971 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400972 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
973 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
974 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
975 &dev->wiphy.bands[band]->ht_cap.mcs) ||
976 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
977 dev->wiphy.bands[band]->ht_cap.cap) ||
978 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
979 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
980 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
981 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
982 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200983
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000984 /* add VHT info */
985 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
986 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
987 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
988 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
989 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
990 dev->wiphy.bands[band]->vht_cap.cap)))
991 goto nla_put_failure;
992
Johannes Bergee688b002008-01-24 19:38:39 +0100993 /* add frequencies */
994 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
995 if (!nl_freqs)
996 goto nla_put_failure;
997
998 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
999 nl_freq = nla_nest_start(msg, i);
1000 if (!nl_freq)
1001 goto nla_put_failure;
1002
1003 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001004
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001005 if (nl80211_msg_put_channel(msg, chan))
1006 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001007
Johannes Bergee688b002008-01-24 19:38:39 +01001008 nla_nest_end(msg, nl_freq);
1009 }
1010
1011 nla_nest_end(msg, nl_freqs);
1012
1013 /* add bitrates */
1014 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1015 if (!nl_rates)
1016 goto nla_put_failure;
1017
1018 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1019 nl_rate = nla_nest_start(msg, i);
1020 if (!nl_rate)
1021 goto nla_put_failure;
1022
1023 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001024 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1025 rate->bitrate))
1026 goto nla_put_failure;
1027 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1028 nla_put_flag(msg,
1029 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1030 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001031
1032 nla_nest_end(msg, nl_rate);
1033 }
1034
1035 nla_nest_end(msg, nl_rates);
1036
1037 nla_nest_end(msg, nl_band);
1038 }
1039 nla_nest_end(msg, nl_bands);
1040
Johannes Berg8fdc6212009-03-14 09:34:01 +01001041 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1042 if (!nl_cmds)
1043 goto nla_put_failure;
1044
1045 i = 0;
1046#define CMD(op, n) \
1047 do { \
1048 if (dev->ops->op) { \
1049 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001050 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1051 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001052 } \
1053 } while (0)
1054
1055 CMD(add_virtual_intf, NEW_INTERFACE);
1056 CMD(change_virtual_intf, SET_INTERFACE);
1057 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001058 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001059 CMD(add_station, NEW_STATION);
1060 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001061 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001062 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001063 CMD(auth, AUTHENTICATE);
1064 CMD(assoc, ASSOCIATE);
1065 CMD(deauth, DEAUTHENTICATE);
1066 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001067 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001068 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001069 CMD(set_pmksa, SET_PMKSA);
1070 CMD(del_pmksa, DEL_PMKSA);
1071 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001072 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1073 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001074 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001075 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001076 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001077 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001078 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001079 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1080 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001081 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001082 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001083 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001084 i++;
1085 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1086 goto nla_put_failure;
1087 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001088 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001089 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1090 CMD(tdls_mgmt, TDLS_MGMT);
1091 CMD(tdls_oper, TDLS_OPER);
1092 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001093 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1094 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001095 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001096 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001097 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1098 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001099 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1100 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001101 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001102
Kalle Valo4745fc02011-11-17 19:06:10 +02001103#ifdef CONFIG_NL80211_TESTMODE
1104 CMD(testmode_cmd, TESTMODE);
1105#endif
1106
Johannes Berg8fdc6212009-03-14 09:34:01 +01001107#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001108
Johannes Berg6829c872009-07-02 09:13:27 +02001109 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001110 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001111 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1112 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001113 }
1114
Johannes Berg6829c872009-07-02 09:13:27 +02001115 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001116 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001117 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1118 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001119 }
1120
Johannes Berg8fdc6212009-03-14 09:34:01 +01001121 nla_nest_end(msg, nl_cmds);
1122
Johannes Berg7c4ef712011-11-18 15:33:48 +01001123 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001124 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1125 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1126 dev->wiphy.max_remain_on_channel_duration))
1127 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001128
David S. Miller9360ffd2012-03-29 04:41:26 -04001129 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1130 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1131 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001132
Johannes Berg2e161f72010-08-12 15:38:38 +02001133 if (mgmt_stypes) {
1134 u16 stypes;
1135 struct nlattr *nl_ftypes, *nl_ifs;
1136 enum nl80211_iftype ift;
1137
1138 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1139 if (!nl_ifs)
1140 goto nla_put_failure;
1141
1142 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1143 nl_ftypes = nla_nest_start(msg, ift);
1144 if (!nl_ftypes)
1145 goto nla_put_failure;
1146 i = 0;
1147 stypes = mgmt_stypes[ift].tx;
1148 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001149 if ((stypes & 1) &&
1150 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1151 (i << 4) | IEEE80211_FTYPE_MGMT))
1152 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001153 stypes >>= 1;
1154 i++;
1155 }
1156 nla_nest_end(msg, nl_ftypes);
1157 }
1158
Johannes Berg74b70a42010-08-24 12:15:53 +02001159 nla_nest_end(msg, nl_ifs);
1160
Johannes Berg2e161f72010-08-12 15:38:38 +02001161 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1162 if (!nl_ifs)
1163 goto nla_put_failure;
1164
1165 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1166 nl_ftypes = nla_nest_start(msg, ift);
1167 if (!nl_ftypes)
1168 goto nla_put_failure;
1169 i = 0;
1170 stypes = mgmt_stypes[ift].rx;
1171 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001172 if ((stypes & 1) &&
1173 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1174 (i << 4) | IEEE80211_FTYPE_MGMT))
1175 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001176 stypes >>= 1;
1177 i++;
1178 }
1179 nla_nest_end(msg, nl_ftypes);
1180 }
1181 nla_nest_end(msg, nl_ifs);
1182 }
1183
Johannes Bergdfb89c52012-06-27 09:23:48 +02001184#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001185 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1186 struct nlattr *nl_wowlan;
1187
1188 nl_wowlan = nla_nest_start(msg,
1189 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1190 if (!nl_wowlan)
1191 goto nla_put_failure;
1192
David S. Miller9360ffd2012-03-29 04:41:26 -04001193 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1194 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1195 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1196 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1197 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1198 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1199 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1200 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1201 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1202 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1203 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1204 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1205 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1206 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1207 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1208 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1209 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001210 if (dev->wiphy.wowlan.n_patterns) {
1211 struct nl80211_wowlan_pattern_support pat = {
1212 .max_patterns = dev->wiphy.wowlan.n_patterns,
1213 .min_pattern_len =
1214 dev->wiphy.wowlan.pattern_min_len,
1215 .max_pattern_len =
1216 dev->wiphy.wowlan.pattern_max_len,
1217 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001218 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1219 sizeof(pat), &pat))
1220 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001221 }
1222
1223 nla_nest_end(msg, nl_wowlan);
1224 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001225#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001226
Johannes Berg7527a782011-05-13 10:58:57 +02001227 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1228 dev->wiphy.software_iftypes))
1229 goto nla_put_failure;
1230
1231 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1232 goto nla_put_failure;
1233
David S. Miller9360ffd2012-03-29 04:41:26 -04001234 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1235 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1236 dev->wiphy.ap_sme_capa))
1237 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001238
David S. Miller9360ffd2012-03-29 04:41:26 -04001239 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1240 dev->wiphy.features))
1241 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001242
David S. Miller9360ffd2012-03-29 04:41:26 -04001243 if (dev->wiphy.ht_capa_mod_mask &&
1244 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1245 sizeof(*dev->wiphy.ht_capa_mod_mask),
1246 dev->wiphy.ht_capa_mod_mask))
1247 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001248
Johannes Berg55682962007-09-20 13:09:35 -04001249 return genlmsg_end(msg, hdr);
1250
1251 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001252 genlmsg_cancel(msg, hdr);
1253 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001254}
1255
1256static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1257{
1258 int idx = 0;
1259 int start = cb->args[0];
1260 struct cfg80211_registered_device *dev;
1261
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001262 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001263 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001264 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1265 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001266 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001267 continue;
1268 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1269 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001270 dev) < 0) {
1271 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001272 break;
Julius Volzb4637272008-07-08 14:02:19 +02001273 }
Johannes Berg55682962007-09-20 13:09:35 -04001274 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001275 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001276
1277 cb->args[0] = idx;
1278
1279 return skb->len;
1280}
1281
1282static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1283{
1284 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001285 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001286
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001288 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001289 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001290
Johannes Berg4c476992010-10-04 21:36:35 +02001291 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1292 nlmsg_free(msg);
1293 return -ENOBUFS;
1294 }
Johannes Berg55682962007-09-20 13:09:35 -04001295
Johannes Berg134e6372009-07-10 09:51:34 +00001296 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001297}
1298
Jouni Malinen31888482008-10-30 16:59:24 +02001299static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1300 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1301 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1302 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1303 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1304 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1305};
1306
1307static int parse_txq_params(struct nlattr *tb[],
1308 struct ieee80211_txq_params *txq_params)
1309{
Johannes Berga3304b02012-03-28 11:04:24 +02001310 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001311 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1312 !tb[NL80211_TXQ_ATTR_AIFS])
1313 return -EINVAL;
1314
Johannes Berga3304b02012-03-28 11:04:24 +02001315 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001316 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1317 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1318 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1319 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1320
Johannes Berga3304b02012-03-28 11:04:24 +02001321 if (txq_params->ac >= NL80211_NUM_ACS)
1322 return -EINVAL;
1323
Jouni Malinen31888482008-10-30 16:59:24 +02001324 return 0;
1325}
1326
Johannes Bergf444de02010-05-05 15:25:02 +02001327static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1328{
1329 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001330 * You can only set the channel explicitly for WDS interfaces,
1331 * all others have their channel managed via their respective
1332 * "establish a connection" command (connect, join, ...)
1333 *
1334 * For AP/GO and mesh mode, the channel can be set with the
1335 * channel userspace API, but is only stored and passed to the
1336 * low-level driver when the AP starts or the mesh is joined.
1337 * This is for backward compatibility, userspace can also give
1338 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001339 *
1340 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001341 * whatever else is going on, so they have their own special
1342 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001343 */
1344 return !wdev ||
1345 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001346 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001347 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1348 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001349}
1350
Johannes Bergcd6c6592012-05-10 21:27:18 +02001351static bool nl80211_valid_channel_type(struct genl_info *info,
1352 enum nl80211_channel_type *channel_type)
1353{
1354 enum nl80211_channel_type tmp;
1355
1356 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1357 return false;
1358
1359 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1360 if (tmp != NL80211_CHAN_NO_HT &&
1361 tmp != NL80211_CHAN_HT20 &&
1362 tmp != NL80211_CHAN_HT40PLUS &&
1363 tmp != NL80211_CHAN_HT40MINUS)
1364 return false;
1365
1366 if (channel_type)
1367 *channel_type = tmp;
1368
1369 return true;
1370}
1371
Johannes Bergf444de02010-05-05 15:25:02 +02001372static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1373 struct wireless_dev *wdev,
1374 struct genl_info *info)
1375{
Johannes Bergaa430da2012-05-16 23:50:18 +02001376 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001377 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1378 u32 freq;
1379 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001380 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1381
1382 if (wdev)
1383 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001384
1385 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1386 return -EINVAL;
1387
1388 if (!nl80211_can_set_dev_channel(wdev))
1389 return -EOPNOTSUPP;
1390
Johannes Bergcd6c6592012-05-10 21:27:18 +02001391 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1392 !nl80211_valid_channel_type(info, &channel_type))
1393 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001394
1395 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1396
1397 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001398 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001399 case NL80211_IFTYPE_AP:
1400 case NL80211_IFTYPE_P2P_GO:
1401 if (wdev->beacon_interval) {
1402 result = -EBUSY;
1403 break;
1404 }
1405 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1406 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1407 channel,
1408 channel_type)) {
1409 result = -EINVAL;
1410 break;
1411 }
1412 wdev->preset_chan = channel;
1413 wdev->preset_chantype = channel_type;
1414 result = 0;
1415 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001416 case NL80211_IFTYPE_MESH_POINT:
1417 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1418 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001419 case NL80211_IFTYPE_MONITOR:
1420 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1421 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001422 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001423 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001424 }
1425 mutex_unlock(&rdev->devlist_mtx);
1426
1427 return result;
1428}
1429
1430static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1431{
Johannes Berg4c476992010-10-04 21:36:35 +02001432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1433 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001434
Johannes Berg4c476992010-10-04 21:36:35 +02001435 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001436}
1437
Bill Jordane8347eb2010-10-01 13:54:28 -04001438static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1439{
Johannes Berg43b19952010-10-07 13:10:30 +02001440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1441 struct net_device *dev = info->user_ptr[1];
1442 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001443 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001444
1445 if (!info->attrs[NL80211_ATTR_MAC])
1446 return -EINVAL;
1447
Johannes Berg43b19952010-10-07 13:10:30 +02001448 if (netif_running(dev))
1449 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001450
Johannes Berg43b19952010-10-07 13:10:30 +02001451 if (!rdev->ops->set_wds_peer)
1452 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001453
Johannes Berg43b19952010-10-07 13:10:30 +02001454 if (wdev->iftype != NL80211_IFTYPE_WDS)
1455 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001456
1457 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001458 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001459}
1460
1461
Johannes Berg55682962007-09-20 13:09:35 -04001462static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1463{
1464 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001465 struct net_device *netdev = NULL;
1466 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001467 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001468 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001469 u32 changed;
1470 u8 retry_short = 0, retry_long = 0;
1471 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001472 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001473
Johannes Bergf444de02010-05-05 15:25:02 +02001474 /*
1475 * Try to find the wiphy and netdev. Normally this
1476 * function shouldn't need the netdev, but this is
1477 * done for backward compatibility -- previously
1478 * setting the channel was done per wiphy, but now
1479 * it is per netdev. Previous userland like hostapd
1480 * also passed a netdev to set_wiphy, so that it is
1481 * possible to let that go to the right netdev!
1482 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001483 mutex_lock(&cfg80211_mutex);
1484
Johannes Bergf444de02010-05-05 15:25:02 +02001485 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1486 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1487
1488 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1489 if (netdev && netdev->ieee80211_ptr) {
1490 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1491 mutex_lock(&rdev->mtx);
1492 } else
1493 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001494 }
1495
Johannes Bergf444de02010-05-05 15:25:02 +02001496 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001497 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1498 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001499 if (IS_ERR(rdev)) {
1500 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001501 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001502 }
1503 wdev = NULL;
1504 netdev = NULL;
1505 result = 0;
1506
1507 mutex_lock(&rdev->mtx);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001508 } else if (nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
Johannes Bergf444de02010-05-05 15:25:02 +02001509 wdev = netdev->ieee80211_ptr;
1510 else
1511 wdev = NULL;
1512
1513 /*
1514 * end workaround code, by now the rdev is available
1515 * and locked, and wdev may or may not be NULL.
1516 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001517
1518 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001519 result = cfg80211_dev_rename(
1520 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001521
1522 mutex_unlock(&cfg80211_mutex);
1523
1524 if (result)
1525 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001526
Jouni Malinen31888482008-10-30 16:59:24 +02001527 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1528 struct ieee80211_txq_params txq_params;
1529 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1530
1531 if (!rdev->ops->set_txq_params) {
1532 result = -EOPNOTSUPP;
1533 goto bad_res;
1534 }
1535
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001536 if (!netdev) {
1537 result = -EINVAL;
1538 goto bad_res;
1539 }
1540
Johannes Berg133a3ff2011-11-03 14:50:13 +01001541 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1542 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1543 result = -EINVAL;
1544 goto bad_res;
1545 }
1546
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001547 if (!netif_running(netdev)) {
1548 result = -ENETDOWN;
1549 goto bad_res;
1550 }
1551
Jouni Malinen31888482008-10-30 16:59:24 +02001552 nla_for_each_nested(nl_txq_params,
1553 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1554 rem_txq_params) {
1555 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1556 nla_data(nl_txq_params),
1557 nla_len(nl_txq_params),
1558 txq_params_policy);
1559 result = parse_txq_params(tb, &txq_params);
1560 if (result)
1561 goto bad_res;
1562
1563 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001564 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001565 &txq_params);
1566 if (result)
1567 goto bad_res;
1568 }
1569 }
1570
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001571 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001572 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001573 if (result)
1574 goto bad_res;
1575 }
1576
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001577 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1578 enum nl80211_tx_power_setting type;
1579 int idx, mbm = 0;
1580
1581 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001582 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001583 goto bad_res;
1584 }
1585
1586 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1587 type = nla_get_u32(info->attrs[idx]);
1588
1589 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1590 (type != NL80211_TX_POWER_AUTOMATIC)) {
1591 result = -EINVAL;
1592 goto bad_res;
1593 }
1594
1595 if (type != NL80211_TX_POWER_AUTOMATIC) {
1596 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1597 mbm = nla_get_u32(info->attrs[idx]);
1598 }
1599
1600 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1601 if (result)
1602 goto bad_res;
1603 }
1604
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001605 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1606 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1607 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001608 if ((!rdev->wiphy.available_antennas_tx &&
1609 !rdev->wiphy.available_antennas_rx) ||
1610 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001611 result = -EOPNOTSUPP;
1612 goto bad_res;
1613 }
1614
1615 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1616 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1617
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001618 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001619 * available antenna masks, except for the "all" mask */
1620 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1621 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001622 result = -EINVAL;
1623 goto bad_res;
1624 }
1625
Bruno Randolf7f531e02010-12-16 11:30:22 +09001626 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1627 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001628
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001629 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1630 if (result)
1631 goto bad_res;
1632 }
1633
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001634 changed = 0;
1635
1636 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1637 retry_short = nla_get_u8(
1638 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1639 if (retry_short == 0) {
1640 result = -EINVAL;
1641 goto bad_res;
1642 }
1643 changed |= WIPHY_PARAM_RETRY_SHORT;
1644 }
1645
1646 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1647 retry_long = nla_get_u8(
1648 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1649 if (retry_long == 0) {
1650 result = -EINVAL;
1651 goto bad_res;
1652 }
1653 changed |= WIPHY_PARAM_RETRY_LONG;
1654 }
1655
1656 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1657 frag_threshold = nla_get_u32(
1658 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1659 if (frag_threshold < 256) {
1660 result = -EINVAL;
1661 goto bad_res;
1662 }
1663 if (frag_threshold != (u32) -1) {
1664 /*
1665 * Fragments (apart from the last one) are required to
1666 * have even length. Make the fragmentation code
1667 * simpler by stripping LSB should someone try to use
1668 * odd threshold value.
1669 */
1670 frag_threshold &= ~0x1;
1671 }
1672 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1673 }
1674
1675 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1676 rts_threshold = nla_get_u32(
1677 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1678 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1679 }
1680
Lukáš Turek81077e82009-12-21 22:50:47 +01001681 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1682 coverage_class = nla_get_u8(
1683 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1684 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1685 }
1686
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001687 if (changed) {
1688 u8 old_retry_short, old_retry_long;
1689 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001690 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001691
1692 if (!rdev->ops->set_wiphy_params) {
1693 result = -EOPNOTSUPP;
1694 goto bad_res;
1695 }
1696
1697 old_retry_short = rdev->wiphy.retry_short;
1698 old_retry_long = rdev->wiphy.retry_long;
1699 old_frag_threshold = rdev->wiphy.frag_threshold;
1700 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001701 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001702
1703 if (changed & WIPHY_PARAM_RETRY_SHORT)
1704 rdev->wiphy.retry_short = retry_short;
1705 if (changed & WIPHY_PARAM_RETRY_LONG)
1706 rdev->wiphy.retry_long = retry_long;
1707 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1708 rdev->wiphy.frag_threshold = frag_threshold;
1709 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1710 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001711 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1712 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001713
1714 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1715 if (result) {
1716 rdev->wiphy.retry_short = old_retry_short;
1717 rdev->wiphy.retry_long = old_retry_long;
1718 rdev->wiphy.frag_threshold = old_frag_threshold;
1719 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001720 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001721 }
1722 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001723
Johannes Berg306d6112008-12-08 12:39:04 +01001724 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001725 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001726 if (netdev)
1727 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001728 return result;
1729}
1730
Johannes Berg71bbc992012-06-15 15:30:18 +02001731static inline u64 wdev_id(struct wireless_dev *wdev)
1732{
1733 return (u64)wdev->identifier |
1734 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1735}
Johannes Berg55682962007-09-20 13:09:35 -04001736
1737static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001738 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001739 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001740{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001741 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001742 void *hdr;
1743
1744 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1745 if (!hdr)
1746 return -1;
1747
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001748 if (dev &&
1749 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1750 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1751 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dev->dev_addr)))
1752 goto nla_put_failure;
1753
1754 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1755 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001756 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001757 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1758 rdev->devlist_generation ^
1759 (cfg80211_rdev_list_generation << 2)))
1760 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001761
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001762 if (rdev->ops->get_channel) {
1763 struct ieee80211_channel *chan;
1764 enum nl80211_channel_type channel_type;
1765
1766 chan = rdev->ops->get_channel(&rdev->wiphy, wdev,
1767 &channel_type);
1768 if (chan &&
1769 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1770 chan->center_freq) ||
1771 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1772 channel_type)))
John W. Linville59ef43e2012-04-18 14:17:13 -04001773 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001774 }
1775
Johannes Berg55682962007-09-20 13:09:35 -04001776 return genlmsg_end(msg, hdr);
1777
1778 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001779 genlmsg_cancel(msg, hdr);
1780 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001781}
1782
1783static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1784{
1785 int wp_idx = 0;
1786 int if_idx = 0;
1787 int wp_start = cb->args[0];
1788 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001789 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001790 struct wireless_dev *wdev;
1791
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001792 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001793 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1794 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001795 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001796 if (wp_idx < wp_start) {
1797 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001798 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001799 }
Johannes Berg55682962007-09-20 13:09:35 -04001800 if_idx = 0;
1801
Johannes Bergf5ea9122009-08-07 16:17:38 +02001802 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001803 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001804 if (if_idx < if_start) {
1805 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001806 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001807 }
Johannes Berg55682962007-09-20 13:09:35 -04001808 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1809 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001810 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001811 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001812 goto out;
1813 }
1814 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001815 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001816 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001817
1818 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001819 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001820 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001821 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001822
1823 cb->args[0] = wp_idx;
1824 cb->args[1] = if_idx;
1825
1826 return skb->len;
1827}
1828
1829static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1830{
1831 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001832 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001833 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001834
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001835 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001836 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001837 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001838
Johannes Bergd7264052009-04-19 16:23:20 +02001839 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001840 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001841 nlmsg_free(msg);
1842 return -ENOBUFS;
1843 }
Johannes Berg55682962007-09-20 13:09:35 -04001844
Johannes Berg134e6372009-07-10 09:51:34 +00001845 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001846}
1847
Michael Wu66f7ac52008-01-31 19:48:22 +01001848static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1849 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1850 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1851 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1852 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1853 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1854};
1855
1856static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1857{
1858 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1859 int flag;
1860
1861 *mntrflags = 0;
1862
1863 if (!nla)
1864 return -EINVAL;
1865
1866 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1867 nla, mntr_flags_policy))
1868 return -EINVAL;
1869
1870 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1871 if (flags[flag])
1872 *mntrflags |= (1<<flag);
1873
1874 return 0;
1875}
1876
Johannes Berg9bc383d2009-11-19 11:55:19 +01001877static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001878 struct net_device *netdev, u8 use_4addr,
1879 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001880{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001881 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001882 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001883 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001884 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001885 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001886
1887 switch (iftype) {
1888 case NL80211_IFTYPE_AP_VLAN:
1889 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1890 return 0;
1891 break;
1892 case NL80211_IFTYPE_STATION:
1893 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1894 return 0;
1895 break;
1896 default:
1897 break;
1898 }
1899
1900 return -EOPNOTSUPP;
1901}
1902
Johannes Berg55682962007-09-20 13:09:35 -04001903static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1904{
Johannes Berg4c476992010-10-04 21:36:35 +02001905 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001906 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001907 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001908 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001909 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001910 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001911 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001912
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001913 memset(&params, 0, sizeof(params));
1914
Johannes Berg04a773a2009-04-19 21:24:32 +02001915 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001916
Johannes Berg723b0382008-09-16 20:22:09 +02001917 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001918 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001919 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001920 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001921 if (ntype > NL80211_IFTYPE_MAX)
1922 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001923 }
1924
Johannes Berg92ffe052008-09-16 20:39:36 +02001925 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001926 struct wireless_dev *wdev = dev->ieee80211_ptr;
1927
Johannes Berg4c476992010-10-04 21:36:35 +02001928 if (ntype != NL80211_IFTYPE_MESH_POINT)
1929 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001930 if (netif_running(dev))
1931 return -EBUSY;
1932
1933 wdev_lock(wdev);
1934 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1935 IEEE80211_MAX_MESH_ID_LEN);
1936 wdev->mesh_id_up_len =
1937 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1938 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1939 wdev->mesh_id_up_len);
1940 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001941 }
1942
Felix Fietkau8b787642009-11-10 18:53:10 +01001943 if (info->attrs[NL80211_ATTR_4ADDR]) {
1944 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1945 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001946 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001947 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001948 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001949 } else {
1950 params.use_4addr = -1;
1951 }
1952
Johannes Berg92ffe052008-09-16 20:39:36 +02001953 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001954 if (ntype != NL80211_IFTYPE_MONITOR)
1955 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001956 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1957 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001958 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001959 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001960
1961 flags = &_flags;
1962 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001963 }
Johannes Berg3b858752009-03-12 09:55:09 +01001964
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001965 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001966 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001967 else
1968 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001969
Johannes Berg9bc383d2009-11-19 11:55:19 +01001970 if (!err && params.use_4addr != -1)
1971 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1972
Johannes Berg55682962007-09-20 13:09:35 -04001973 return err;
1974}
1975
1976static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1977{
Johannes Berg4c476992010-10-04 21:36:35 +02001978 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001979 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02001980 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02001981 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04001982 int err;
1983 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001984 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001985
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001986 memset(&params, 0, sizeof(params));
1987
Johannes Berg55682962007-09-20 13:09:35 -04001988 if (!info->attrs[NL80211_ATTR_IFNAME])
1989 return -EINVAL;
1990
1991 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1992 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1993 if (type > NL80211_IFTYPE_MAX)
1994 return -EINVAL;
1995 }
1996
Johannes Berg79c97e92009-07-07 03:56:12 +02001997 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001998 !(rdev->wiphy.interface_modes & (1 << type)))
1999 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002000
Johannes Berg9bc383d2009-11-19 11:55:19 +01002001 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002002 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002003 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002004 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002005 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002006 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002007
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002008 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2009 if (!msg)
2010 return -ENOMEM;
2011
Michael Wu66f7ac52008-01-31 19:48:22 +01002012 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2013 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2014 &flags);
Johannes Berg84efbb82012-06-16 00:00:26 +02002015 wdev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01002016 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002017 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002018 if (IS_ERR(wdev)) {
2019 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002020 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002021 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002022
Johannes Berg29cbe682010-12-03 09:20:44 +01002023 if (type == NL80211_IFTYPE_MESH_POINT &&
2024 info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002025 wdev_lock(wdev);
2026 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2027 IEEE80211_MAX_MESH_ID_LEN);
2028 wdev->mesh_id_up_len =
2029 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2030 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2031 wdev->mesh_id_up_len);
2032 wdev_unlock(wdev);
2033 }
2034
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002035 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
2036 rdev, wdev) < 0) {
2037 nlmsg_free(msg);
2038 return -ENOBUFS;
2039 }
2040
2041 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002042}
2043
2044static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2045{
Johannes Berg4c476992010-10-04 21:36:35 +02002046 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002047 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002048
Johannes Berg4c476992010-10-04 21:36:35 +02002049 if (!rdev->ops->del_virtual_intf)
2050 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002051
Johannes Berg84efbb82012-06-16 00:00:26 +02002052 /*
2053 * If we remove a wireless device without a netdev then clear
2054 * user_ptr[1] so that nl80211_post_doit won't dereference it
2055 * to check if it needs to do dev_put(). Otherwise it crashes
2056 * since the wdev has been freed, unlike with a netdev where
2057 * we need the dev_put() for the netdev to really be freed.
2058 */
2059 if (!wdev->netdev)
2060 info->user_ptr[1] = NULL;
2061
2062 return rdev->ops->del_virtual_intf(&rdev->wiphy, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002063}
2064
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002065static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2066{
2067 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2068 struct net_device *dev = info->user_ptr[1];
2069 u16 noack_map;
2070
2071 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2072 return -EINVAL;
2073
2074 if (!rdev->ops->set_noack_map)
2075 return -EOPNOTSUPP;
2076
2077 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2078
2079 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
2080}
2081
Johannes Berg41ade002007-12-19 02:03:29 +01002082struct get_key_cookie {
2083 struct sk_buff *msg;
2084 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002085 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002086};
2087
2088static void get_key_callback(void *c, struct key_params *params)
2089{
Johannes Bergb9454e82009-07-08 13:29:08 +02002090 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002091 struct get_key_cookie *cookie = c;
2092
David S. Miller9360ffd2012-03-29 04:41:26 -04002093 if ((params->key &&
2094 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2095 params->key_len, params->key)) ||
2096 (params->seq &&
2097 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2098 params->seq_len, params->seq)) ||
2099 (params->cipher &&
2100 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2101 params->cipher)))
2102 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002103
Johannes Bergb9454e82009-07-08 13:29:08 +02002104 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2105 if (!key)
2106 goto nla_put_failure;
2107
David S. Miller9360ffd2012-03-29 04:41:26 -04002108 if ((params->key &&
2109 nla_put(cookie->msg, NL80211_KEY_DATA,
2110 params->key_len, params->key)) ||
2111 (params->seq &&
2112 nla_put(cookie->msg, NL80211_KEY_SEQ,
2113 params->seq_len, params->seq)) ||
2114 (params->cipher &&
2115 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2116 params->cipher)))
2117 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002118
David S. Miller9360ffd2012-03-29 04:41:26 -04002119 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2120 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002121
2122 nla_nest_end(cookie->msg, key);
2123
Johannes Berg41ade002007-12-19 02:03:29 +01002124 return;
2125 nla_put_failure:
2126 cookie->error = 1;
2127}
2128
2129static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2130{
Johannes Berg4c476992010-10-04 21:36:35 +02002131 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002132 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002133 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002134 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002135 const u8 *mac_addr = NULL;
2136 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002137 struct get_key_cookie cookie = {
2138 .error = 0,
2139 };
2140 void *hdr;
2141 struct sk_buff *msg;
2142
2143 if (info->attrs[NL80211_ATTR_KEY_IDX])
2144 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2145
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002146 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002147 return -EINVAL;
2148
2149 if (info->attrs[NL80211_ATTR_MAC])
2150 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2151
Johannes Berge31b8212010-10-05 19:39:30 +02002152 pairwise = !!mac_addr;
2153 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2154 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2155 if (kt >= NUM_NL80211_KEYTYPES)
2156 return -EINVAL;
2157 if (kt != NL80211_KEYTYPE_GROUP &&
2158 kt != NL80211_KEYTYPE_PAIRWISE)
2159 return -EINVAL;
2160 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2161 }
2162
Johannes Berg4c476992010-10-04 21:36:35 +02002163 if (!rdev->ops->get_key)
2164 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002165
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002166 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002167 if (!msg)
2168 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002169
2170 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2171 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002172 if (IS_ERR(hdr))
2173 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002174
2175 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002176 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002177
David S. Miller9360ffd2012-03-29 04:41:26 -04002178 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2179 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2180 goto nla_put_failure;
2181 if (mac_addr &&
2182 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2183 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002184
Johannes Berge31b8212010-10-05 19:39:30 +02002185 if (pairwise && mac_addr &&
2186 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2187 return -ENOENT;
2188
2189 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
2190 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002191
2192 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002193 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002194
2195 if (cookie.error)
2196 goto nla_put_failure;
2197
2198 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002199 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002200
2201 nla_put_failure:
2202 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002203 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002204 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002205 return err;
2206}
2207
2208static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2209{
Johannes Berg4c476992010-10-04 21:36:35 +02002210 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002211 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002212 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002213 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002214
Johannes Bergb9454e82009-07-08 13:29:08 +02002215 err = nl80211_parse_key(info, &key);
2216 if (err)
2217 return err;
2218
2219 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002220 return -EINVAL;
2221
Johannes Bergb9454e82009-07-08 13:29:08 +02002222 /* only support setting default key */
2223 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002224 return -EINVAL;
2225
Johannes Bergfffd0932009-07-08 14:22:54 +02002226 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002227
2228 if (key.def) {
2229 if (!rdev->ops->set_default_key) {
2230 err = -EOPNOTSUPP;
2231 goto out;
2232 }
2233
2234 err = nl80211_key_allowed(dev->ieee80211_ptr);
2235 if (err)
2236 goto out;
2237
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002238 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2239 key.def_uni, key.def_multi);
2240
2241 if (err)
2242 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002243
Johannes Berg3d23e342009-09-29 23:27:28 +02002244#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002245 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002246#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002247 } else {
2248 if (key.def_uni || !key.def_multi) {
2249 err = -EINVAL;
2250 goto out;
2251 }
2252
2253 if (!rdev->ops->set_default_mgmt_key) {
2254 err = -EOPNOTSUPP;
2255 goto out;
2256 }
2257
2258 err = nl80211_key_allowed(dev->ieee80211_ptr);
2259 if (err)
2260 goto out;
2261
2262 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2263 dev, key.idx);
2264 if (err)
2265 goto out;
2266
2267#ifdef CONFIG_CFG80211_WEXT
2268 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2269#endif
2270 }
2271
2272 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002273 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002274
Johannes Berg41ade002007-12-19 02:03:29 +01002275 return err;
2276}
2277
2278static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2279{
Johannes Berg4c476992010-10-04 21:36:35 +02002280 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002281 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002282 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002283 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002284 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002285
Johannes Bergb9454e82009-07-08 13:29:08 +02002286 err = nl80211_parse_key(info, &key);
2287 if (err)
2288 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002289
Johannes Bergb9454e82009-07-08 13:29:08 +02002290 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002291 return -EINVAL;
2292
Johannes Berg41ade002007-12-19 02:03:29 +01002293 if (info->attrs[NL80211_ATTR_MAC])
2294 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2295
Johannes Berge31b8212010-10-05 19:39:30 +02002296 if (key.type == -1) {
2297 if (mac_addr)
2298 key.type = NL80211_KEYTYPE_PAIRWISE;
2299 else
2300 key.type = NL80211_KEYTYPE_GROUP;
2301 }
2302
2303 /* for now */
2304 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2305 key.type != NL80211_KEYTYPE_GROUP)
2306 return -EINVAL;
2307
Johannes Berg4c476992010-10-04 21:36:35 +02002308 if (!rdev->ops->add_key)
2309 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002310
Johannes Berge31b8212010-10-05 19:39:30 +02002311 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2312 key.type == NL80211_KEYTYPE_PAIRWISE,
2313 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002314 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002315
2316 wdev_lock(dev->ieee80211_ptr);
2317 err = nl80211_key_allowed(dev->ieee80211_ptr);
2318 if (!err)
2319 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002320 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002321 mac_addr, &key.p);
2322 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002323
Johannes Berg41ade002007-12-19 02:03:29 +01002324 return err;
2325}
2326
2327static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2328{
Johannes Berg4c476992010-10-04 21:36:35 +02002329 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002330 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002331 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002332 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002333 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002334
Johannes Bergb9454e82009-07-08 13:29:08 +02002335 err = nl80211_parse_key(info, &key);
2336 if (err)
2337 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002338
2339 if (info->attrs[NL80211_ATTR_MAC])
2340 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2341
Johannes Berge31b8212010-10-05 19:39:30 +02002342 if (key.type == -1) {
2343 if (mac_addr)
2344 key.type = NL80211_KEYTYPE_PAIRWISE;
2345 else
2346 key.type = NL80211_KEYTYPE_GROUP;
2347 }
2348
2349 /* for now */
2350 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2351 key.type != NL80211_KEYTYPE_GROUP)
2352 return -EINVAL;
2353
Johannes Berg4c476992010-10-04 21:36:35 +02002354 if (!rdev->ops->del_key)
2355 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002356
Johannes Bergfffd0932009-07-08 14:22:54 +02002357 wdev_lock(dev->ieee80211_ptr);
2358 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002359
2360 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2361 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2362 err = -ENOENT;
2363
Johannes Bergfffd0932009-07-08 14:22:54 +02002364 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002365 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2366 key.type == NL80211_KEYTYPE_PAIRWISE,
2367 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002368
Johannes Berg3d23e342009-09-29 23:27:28 +02002369#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002370 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002371 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002372 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002373 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002374 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2375 }
2376#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002377 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002378
Johannes Berg41ade002007-12-19 02:03:29 +01002379 return err;
2380}
2381
Johannes Berg88600202012-02-13 15:17:18 +01002382static int nl80211_parse_beacon(struct genl_info *info,
2383 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002384{
Johannes Berg88600202012-02-13 15:17:18 +01002385 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002386
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002387 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2388 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2389 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2390 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002391 return -EINVAL;
2392
Johannes Berg88600202012-02-13 15:17:18 +01002393 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002394
Johannes Berged1b6cc2007-12-19 02:03:32 +01002395 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002396 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2397 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2398 if (!bcn->head_len)
2399 return -EINVAL;
2400 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002401 }
2402
2403 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002404 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2405 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002406 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002407 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002408 }
2409
Johannes Berg4c476992010-10-04 21:36:35 +02002410 if (!haveinfo)
2411 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002412
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002413 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002414 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2415 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002416 }
2417
2418 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002419 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002420 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002421 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002422 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2423 }
2424
2425 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002426 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002427 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002428 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002429 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2430 }
2431
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002432 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002433 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002434 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002435 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002436 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2437 }
2438
Johannes Berg88600202012-02-13 15:17:18 +01002439 return 0;
2440}
2441
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002442static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2443 struct cfg80211_ap_settings *params)
2444{
2445 struct wireless_dev *wdev;
2446 bool ret = false;
2447
2448 mutex_lock(&rdev->devlist_mtx);
2449
Johannes Berg89a54e42012-06-15 14:33:17 +02002450 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002451 if (wdev->iftype != NL80211_IFTYPE_AP &&
2452 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2453 continue;
2454
2455 if (!wdev->preset_chan)
2456 continue;
2457
2458 params->channel = wdev->preset_chan;
2459 params->channel_type = wdev->preset_chantype;
2460 ret = true;
2461 break;
2462 }
2463
2464 mutex_unlock(&rdev->devlist_mtx);
2465
2466 return ret;
2467}
2468
Johannes Berg88600202012-02-13 15:17:18 +01002469static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2470{
2471 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2472 struct net_device *dev = info->user_ptr[1];
2473 struct wireless_dev *wdev = dev->ieee80211_ptr;
2474 struct cfg80211_ap_settings params;
2475 int err;
2476
2477 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2478 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2479 return -EOPNOTSUPP;
2480
2481 if (!rdev->ops->start_ap)
2482 return -EOPNOTSUPP;
2483
2484 if (wdev->beacon_interval)
2485 return -EALREADY;
2486
2487 memset(&params, 0, sizeof(params));
2488
2489 /* these are required for START_AP */
2490 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2491 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2492 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2493 return -EINVAL;
2494
2495 err = nl80211_parse_beacon(info, &params.beacon);
2496 if (err)
2497 return err;
2498
2499 params.beacon_interval =
2500 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2501 params.dtim_period =
2502 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2503
2504 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2505 if (err)
2506 return err;
2507
2508 /*
2509 * In theory, some of these attributes should be required here
2510 * but since they were not used when the command was originally
2511 * added, keep them optional for old user space programs to let
2512 * them continue to work with drivers that do not need the
2513 * additional information -- drivers must check!
2514 */
2515 if (info->attrs[NL80211_ATTR_SSID]) {
2516 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2517 params.ssid_len =
2518 nla_len(info->attrs[NL80211_ATTR_SSID]);
2519 if (params.ssid_len == 0 ||
2520 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2521 return -EINVAL;
2522 }
2523
2524 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2525 params.hidden_ssid = nla_get_u32(
2526 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2527 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2528 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2529 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2530 return -EINVAL;
2531 }
2532
2533 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2534
2535 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2536 params.auth_type = nla_get_u32(
2537 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2538 if (!nl80211_valid_auth_type(params.auth_type))
2539 return -EINVAL;
2540 } else
2541 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2542
2543 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2544 NL80211_MAX_NR_CIPHER_SUITES);
2545 if (err)
2546 return err;
2547
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302548 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2549 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2550 return -EOPNOTSUPP;
2551 params.inactivity_timeout = nla_get_u16(
2552 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2553 }
2554
Johannes Bergaa430da2012-05-16 23:50:18 +02002555 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2556 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2557
2558 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2559 !nl80211_valid_channel_type(info, &channel_type))
2560 return -EINVAL;
2561
2562 params.channel = rdev_freq_to_chan(rdev,
2563 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2564 channel_type);
2565 if (!params.channel)
2566 return -EINVAL;
2567 params.channel_type = channel_type;
2568 } else if (wdev->preset_chan) {
2569 params.channel = wdev->preset_chan;
2570 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002571 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002572 return -EINVAL;
2573
2574 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2575 params.channel_type))
2576 return -EINVAL;
2577
Michal Kaziore4e32452012-06-29 12:47:08 +02002578 mutex_lock(&rdev->devlist_mtx);
2579 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2580 CHAN_MODE_SHARED);
2581 mutex_unlock(&rdev->devlist_mtx);
2582
2583 if (err)
2584 return err;
2585
Johannes Berg88600202012-02-13 15:17:18 +01002586 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002587 if (!err) {
2588 wdev->preset_chan = params.channel;
2589 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002590 wdev->beacon_interval = params.beacon_interval;
Michal Kaziorf4489eb2012-06-29 12:46:58 +02002591 wdev->channel = params.channel;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002592 }
Johannes Berg56d18932011-05-09 18:41:15 +02002593 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002594}
2595
Johannes Berg88600202012-02-13 15:17:18 +01002596static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2597{
2598 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2599 struct net_device *dev = info->user_ptr[1];
2600 struct wireless_dev *wdev = dev->ieee80211_ptr;
2601 struct cfg80211_beacon_data params;
2602 int err;
2603
2604 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2605 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2606 return -EOPNOTSUPP;
2607
2608 if (!rdev->ops->change_beacon)
2609 return -EOPNOTSUPP;
2610
2611 if (!wdev->beacon_interval)
2612 return -EINVAL;
2613
2614 err = nl80211_parse_beacon(info, &params);
2615 if (err)
2616 return err;
2617
2618 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2619}
2620
2621static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002622{
Johannes Berg4c476992010-10-04 21:36:35 +02002623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2624 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002625
Michal Kazior60771782012-06-29 12:46:56 +02002626 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002627}
2628
Johannes Berg5727ef12007-12-19 02:03:34 +01002629static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2630 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2631 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2632 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002633 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002634 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002635 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002636};
2637
Johannes Bergeccb8e82009-05-11 21:57:56 +03002638static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002639 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002640 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002641{
2642 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002643 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002644 int flag;
2645
Johannes Bergeccb8e82009-05-11 21:57:56 +03002646 /*
2647 * Try parsing the new attribute first so userspace
2648 * can specify both for older kernels.
2649 */
2650 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2651 if (nla) {
2652 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002653
Johannes Bergeccb8e82009-05-11 21:57:56 +03002654 sta_flags = nla_data(nla);
2655 params->sta_flags_mask = sta_flags->mask;
2656 params->sta_flags_set = sta_flags->set;
2657 if ((params->sta_flags_mask |
2658 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2659 return -EINVAL;
2660 return 0;
2661 }
2662
2663 /* if present, parse the old attribute */
2664
2665 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002666 if (!nla)
2667 return 0;
2668
2669 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2670 nla, sta_flags_policy))
2671 return -EINVAL;
2672
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002673 /*
2674 * Only allow certain flags for interface types so that
2675 * other attributes are silently ignored. Remember that
2676 * this is backward compatibility code with old userspace
2677 * and shouldn't be hit in other cases anyway.
2678 */
2679 switch (iftype) {
2680 case NL80211_IFTYPE_AP:
2681 case NL80211_IFTYPE_AP_VLAN:
2682 case NL80211_IFTYPE_P2P_GO:
2683 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2684 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2685 BIT(NL80211_STA_FLAG_WME) |
2686 BIT(NL80211_STA_FLAG_MFP);
2687 break;
2688 case NL80211_IFTYPE_P2P_CLIENT:
2689 case NL80211_IFTYPE_STATION:
2690 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2691 BIT(NL80211_STA_FLAG_TDLS_PEER);
2692 break;
2693 case NL80211_IFTYPE_MESH_POINT:
2694 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2695 BIT(NL80211_STA_FLAG_MFP) |
2696 BIT(NL80211_STA_FLAG_AUTHORIZED);
2697 default:
2698 return -EINVAL;
2699 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002700
Johannes Berg3383b5a2012-05-10 20:14:43 +02002701 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2702 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002703 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002704
Johannes Berg3383b5a2012-05-10 20:14:43 +02002705 /* no longer support new API additions in old API */
2706 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2707 return -EINVAL;
2708 }
2709 }
2710
Johannes Berg5727ef12007-12-19 02:03:34 +01002711 return 0;
2712}
2713
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002714static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2715 int attr)
2716{
2717 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002718 u32 bitrate;
2719 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002720
2721 rate = nla_nest_start(msg, attr);
2722 if (!rate)
2723 goto nla_put_failure;
2724
2725 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2726 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002727 /* report 16-bit bitrate only if we can */
2728 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002729 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002730 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2731 (bitrate_compat > 0 &&
2732 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002733 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2734 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2735 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2736 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2737 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2738 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2739 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002740
2741 nla_nest_end(msg, rate);
2742 return true;
2743
2744nla_put_failure:
2745 return false;
2746}
2747
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002748static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002749 int flags,
2750 struct cfg80211_registered_device *rdev,
2751 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002752 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002753{
2754 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002755 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002756
2757 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2758 if (!hdr)
2759 return -1;
2760
David S. Miller9360ffd2012-03-29 04:41:26 -04002761 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2762 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2763 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2764 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002765
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002766 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2767 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002768 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002769 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2770 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2771 sinfo->connected_time))
2772 goto nla_put_failure;
2773 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2774 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2775 sinfo->inactive_time))
2776 goto nla_put_failure;
2777 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2778 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2779 sinfo->rx_bytes))
2780 goto nla_put_failure;
2781 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2782 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2783 sinfo->tx_bytes))
2784 goto nla_put_failure;
2785 if ((sinfo->filled & STATION_INFO_LLID) &&
2786 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2787 goto nla_put_failure;
2788 if ((sinfo->filled & STATION_INFO_PLID) &&
2789 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2790 goto nla_put_failure;
2791 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2792 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2793 sinfo->plink_state))
2794 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002795 switch (rdev->wiphy.signal_type) {
2796 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002797 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2798 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2799 sinfo->signal))
2800 goto nla_put_failure;
2801 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2802 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2803 sinfo->signal_avg))
2804 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002805 break;
2806 default:
2807 break;
2808 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002809 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002810 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2811 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002812 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002813 }
2814 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2815 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2816 NL80211_STA_INFO_RX_BITRATE))
2817 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002818 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002819 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2820 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2821 sinfo->rx_packets))
2822 goto nla_put_failure;
2823 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2824 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2825 sinfo->tx_packets))
2826 goto nla_put_failure;
2827 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2828 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2829 sinfo->tx_retries))
2830 goto nla_put_failure;
2831 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2832 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2833 sinfo->tx_failed))
2834 goto nla_put_failure;
2835 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2836 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2837 sinfo->beacon_loss_count))
2838 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002839 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2840 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2841 if (!bss_param)
2842 goto nla_put_failure;
2843
David S. Miller9360ffd2012-03-29 04:41:26 -04002844 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2845 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2846 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2847 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2848 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2849 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2850 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2851 sinfo->bss_param.dtim_period) ||
2852 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2853 sinfo->bss_param.beacon_interval))
2854 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002855
2856 nla_nest_end(msg, bss_param);
2857 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002858 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2859 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2860 sizeof(struct nl80211_sta_flag_update),
2861 &sinfo->sta_flags))
2862 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002863 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2864 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2865 sinfo->t_offset))
2866 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002867 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002868
David S. Miller9360ffd2012-03-29 04:41:26 -04002869 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2870 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2871 sinfo->assoc_req_ies))
2872 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002873
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002874 return genlmsg_end(msg, hdr);
2875
2876 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002877 genlmsg_cancel(msg, hdr);
2878 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002879}
2880
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002881static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002882 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002883{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002884 struct station_info sinfo;
2885 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002886 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002887 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002888 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002889 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002890
Johannes Berg67748892010-10-04 21:14:06 +02002891 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2892 if (err)
2893 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002894
2895 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002896 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002897 goto out_err;
2898 }
2899
Johannes Bergbba95fe2008-07-29 13:22:51 +02002900 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002901 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002902 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2903 mac_addr, &sinfo);
2904 if (err == -ENOENT)
2905 break;
2906 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002907 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002908
2909 if (nl80211_send_station(skb,
2910 NETLINK_CB(cb->skb).pid,
2911 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002912 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002913 &sinfo) < 0)
2914 goto out;
2915
2916 sta_idx++;
2917 }
2918
2919
2920 out:
2921 cb->args[1] = sta_idx;
2922 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002923 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002924 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002925
2926 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002927}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002928
Johannes Berg5727ef12007-12-19 02:03:34 +01002929static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2930{
Johannes Berg4c476992010-10-04 21:36:35 +02002931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2932 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002933 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002934 struct sk_buff *msg;
2935 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002936 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002937
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002938 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002939
2940 if (!info->attrs[NL80211_ATTR_MAC])
2941 return -EINVAL;
2942
2943 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2944
Johannes Berg4c476992010-10-04 21:36:35 +02002945 if (!rdev->ops->get_station)
2946 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002947
Johannes Berg79c97e92009-07-07 03:56:12 +02002948 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002949 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002950 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002951
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002952 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002953 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002954 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002955
2956 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002957 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002958 nlmsg_free(msg);
2959 return -ENOBUFS;
2960 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002961
Johannes Berg4c476992010-10-04 21:36:35 +02002962 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002963}
2964
2965/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002966 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002967 */
Johannes Berg80b99892011-11-18 16:23:01 +01002968static struct net_device *get_vlan(struct genl_info *info,
2969 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002970{
Johannes Berg463d0182009-07-14 00:33:35 +02002971 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002972 struct net_device *v;
2973 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002974
Johannes Berg80b99892011-11-18 16:23:01 +01002975 if (!vlanattr)
2976 return NULL;
2977
2978 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2979 if (!v)
2980 return ERR_PTR(-ENODEV);
2981
2982 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2983 ret = -EINVAL;
2984 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002985 }
Johannes Berg80b99892011-11-18 16:23:01 +01002986
2987 if (!netif_running(v)) {
2988 ret = -ENETDOWN;
2989 goto error;
2990 }
2991
2992 return v;
2993 error:
2994 dev_put(v);
2995 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002996}
2997
2998static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2999{
Johannes Berg4c476992010-10-04 21:36:35 +02003000 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003001 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003002 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003003 struct station_parameters params;
3004 u8 *mac_addr = NULL;
3005
3006 memset(&params, 0, sizeof(params));
3007
3008 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003009 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003010
3011 if (info->attrs[NL80211_ATTR_STA_AID])
3012 return -EINVAL;
3013
3014 if (!info->attrs[NL80211_ATTR_MAC])
3015 return -EINVAL;
3016
3017 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3018
3019 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3020 params.supported_rates =
3021 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3022 params.supported_rates_len =
3023 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3024 }
3025
3026 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3027 params.listen_interval =
3028 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3029
Jouni Malinen36aedc92008-08-25 11:58:58 +03003030 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3031 params.ht_capa =
3032 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3033
Johannes Bergbdd90d52011-12-14 12:20:27 +01003034 if (!rdev->ops->change_station)
3035 return -EOPNOTSUPP;
3036
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003037 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003038 return -EINVAL;
3039
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003040 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3041 params.plink_action =
3042 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3043
Javier Cardona9c3990a2011-05-03 16:57:11 -07003044 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3045 params.plink_state =
3046 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3047
Johannes Berga97f4422009-06-18 17:23:43 +02003048 switch (dev->ieee80211_ptr->iftype) {
3049 case NL80211_IFTYPE_AP:
3050 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003051 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003052 /* disallow mesh-specific things */
3053 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003054 return -EINVAL;
3055
3056 /* TDLS can't be set, ... */
3057 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3058 return -EINVAL;
3059 /*
3060 * ... but don't bother the driver with it. This works around
3061 * a hostapd/wpa_supplicant issue -- it always includes the
3062 * TLDS_PEER flag in the mask even for AP mode.
3063 */
3064 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3065
3066 /* accept only the listed bits */
3067 if (params.sta_flags_mask &
3068 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3069 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3070 BIT(NL80211_STA_FLAG_WME) |
3071 BIT(NL80211_STA_FLAG_MFP)))
3072 return -EINVAL;
3073
3074 /* must be last in here for error handling */
3075 params.vlan = get_vlan(info, rdev);
3076 if (IS_ERR(params.vlan))
3077 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003078 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003079 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003080 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003081 /*
3082 * Don't allow userspace to change the TDLS_PEER flag,
3083 * but silently ignore attempts to change it since we
3084 * don't have state here to verify that it doesn't try
3085 * to change the flag.
3086 */
3087 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003088 /* fall through */
3089 case NL80211_IFTYPE_ADHOC:
3090 /* disallow things sta doesn't support */
3091 if (params.plink_action)
3092 return -EINVAL;
3093 if (params.ht_capa)
3094 return -EINVAL;
3095 if (params.listen_interval >= 0)
3096 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003097 /* reject any changes other than AUTHORIZED */
3098 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3099 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003100 break;
3101 case NL80211_IFTYPE_MESH_POINT:
3102 /* disallow things mesh doesn't support */
3103 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003104 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003105 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003106 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003107 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003108 return -EINVAL;
3109 /*
3110 * No special handling for TDLS here -- the userspace
3111 * mesh code doesn't have this bug.
3112 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003113 if (params.sta_flags_mask &
3114 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003115 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003116 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003117 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003118 break;
3119 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003120 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003121 }
3122
Johannes Bergbdd90d52011-12-14 12:20:27 +01003123 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003124
Johannes Berg79c97e92009-07-07 03:56:12 +02003125 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003126
Johannes Berg5727ef12007-12-19 02:03:34 +01003127 if (params.vlan)
3128 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003129
Johannes Berg5727ef12007-12-19 02:03:34 +01003130 return err;
3131}
3132
Eliad Pellerc75786c2011-08-23 14:37:46 +03003133static struct nla_policy
3134nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3135 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3136 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3137};
3138
Johannes Berg5727ef12007-12-19 02:03:34 +01003139static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3140{
Johannes Berg4c476992010-10-04 21:36:35 +02003141 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003142 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003143 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003144 struct station_parameters params;
3145 u8 *mac_addr = NULL;
3146
3147 memset(&params, 0, sizeof(params));
3148
3149 if (!info->attrs[NL80211_ATTR_MAC])
3150 return -EINVAL;
3151
Johannes Berg5727ef12007-12-19 02:03:34 +01003152 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3153 return -EINVAL;
3154
3155 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3156 return -EINVAL;
3157
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003158 if (!info->attrs[NL80211_ATTR_STA_AID])
3159 return -EINVAL;
3160
Johannes Berg5727ef12007-12-19 02:03:34 +01003161 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3162 params.supported_rates =
3163 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3164 params.supported_rates_len =
3165 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3166 params.listen_interval =
3167 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003168
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003169 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3170 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3171 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003172
Jouni Malinen36aedc92008-08-25 11:58:58 +03003173 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3174 params.ht_capa =
3175 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003176
Javier Cardona96b78df2011-04-07 15:08:33 -07003177 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3178 params.plink_action =
3179 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3180
Johannes Bergbdd90d52011-12-14 12:20:27 +01003181 if (!rdev->ops->add_station)
3182 return -EOPNOTSUPP;
3183
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003184 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003185 return -EINVAL;
3186
Johannes Bergbdd90d52011-12-14 12:20:27 +01003187 switch (dev->ieee80211_ptr->iftype) {
3188 case NL80211_IFTYPE_AP:
3189 case NL80211_IFTYPE_AP_VLAN:
3190 case NL80211_IFTYPE_P2P_GO:
3191 /* parse WME attributes if sta is WME capable */
3192 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3193 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3194 info->attrs[NL80211_ATTR_STA_WME]) {
3195 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3196 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003197
Johannes Bergbdd90d52011-12-14 12:20:27 +01003198 nla = info->attrs[NL80211_ATTR_STA_WME];
3199 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3200 nl80211_sta_wme_policy);
3201 if (err)
3202 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003203
Johannes Bergbdd90d52011-12-14 12:20:27 +01003204 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3205 params.uapsd_queues =
3206 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3207 if (params.uapsd_queues &
3208 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3209 return -EINVAL;
3210
3211 if (tb[NL80211_STA_WME_MAX_SP])
3212 params.max_sp =
3213 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3214
3215 if (params.max_sp &
3216 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3217 return -EINVAL;
3218
3219 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3220 }
3221 /* TDLS peers cannot be added */
3222 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003223 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003224 /* but don't bother the driver with it */
3225 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003226
Johannes Bergbdd90d52011-12-14 12:20:27 +01003227 /* must be last in here for error handling */
3228 params.vlan = get_vlan(info, rdev);
3229 if (IS_ERR(params.vlan))
3230 return PTR_ERR(params.vlan);
3231 break;
3232 case NL80211_IFTYPE_MESH_POINT:
3233 /* TDLS peers cannot be added */
3234 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003235 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003236 break;
3237 case NL80211_IFTYPE_STATION:
3238 /* Only TDLS peers can be added */
3239 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3240 return -EINVAL;
3241 /* Can only add if TDLS ... */
3242 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3243 return -EOPNOTSUPP;
3244 /* ... with external setup is supported */
3245 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3246 return -EOPNOTSUPP;
3247 break;
3248 default:
3249 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003250 }
3251
Johannes Bergbdd90d52011-12-14 12:20:27 +01003252 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003253
Johannes Berg79c97e92009-07-07 03:56:12 +02003254 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003255
Johannes Berg5727ef12007-12-19 02:03:34 +01003256 if (params.vlan)
3257 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003258 return err;
3259}
3260
3261static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3262{
Johannes Berg4c476992010-10-04 21:36:35 +02003263 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3264 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003265 u8 *mac_addr = NULL;
3266
3267 if (info->attrs[NL80211_ATTR_MAC])
3268 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3269
Johannes Berge80cf852009-05-11 14:43:13 +02003270 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003271 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003272 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003273 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3274 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003275
Johannes Berg4c476992010-10-04 21:36:35 +02003276 if (!rdev->ops->del_station)
3277 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003278
Johannes Berg4c476992010-10-04 21:36:35 +02003279 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003280}
3281
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003282static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3283 int flags, struct net_device *dev,
3284 u8 *dst, u8 *next_hop,
3285 struct mpath_info *pinfo)
3286{
3287 void *hdr;
3288 struct nlattr *pinfoattr;
3289
3290 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3291 if (!hdr)
3292 return -1;
3293
David S. Miller9360ffd2012-03-29 04:41:26 -04003294 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3295 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3296 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3297 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3298 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003299
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003300 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3301 if (!pinfoattr)
3302 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003303 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3304 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3305 pinfo->frame_qlen))
3306 goto nla_put_failure;
3307 if (((pinfo->filled & MPATH_INFO_SN) &&
3308 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3309 ((pinfo->filled & MPATH_INFO_METRIC) &&
3310 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3311 pinfo->metric)) ||
3312 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3313 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3314 pinfo->exptime)) ||
3315 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3316 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3317 pinfo->flags)) ||
3318 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3319 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3320 pinfo->discovery_timeout)) ||
3321 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3322 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3323 pinfo->discovery_retries)))
3324 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003325
3326 nla_nest_end(msg, pinfoattr);
3327
3328 return genlmsg_end(msg, hdr);
3329
3330 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003331 genlmsg_cancel(msg, hdr);
3332 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003333}
3334
3335static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003336 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003337{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003338 struct mpath_info pinfo;
3339 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003340 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003341 u8 dst[ETH_ALEN];
3342 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003343 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003344 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003345
Johannes Berg67748892010-10-04 21:14:06 +02003346 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3347 if (err)
3348 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003349
3350 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003351 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003352 goto out_err;
3353 }
3354
Jouni Malineneec60b02009-03-20 21:21:19 +02003355 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3356 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003357 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003358 }
3359
Johannes Bergbba95fe2008-07-29 13:22:51 +02003360 while (1) {
3361 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3362 dst, next_hop, &pinfo);
3363 if (err == -ENOENT)
3364 break;
3365 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003366 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003367
3368 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3369 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3370 netdev, dst, next_hop,
3371 &pinfo) < 0)
3372 goto out;
3373
3374 path_idx++;
3375 }
3376
3377
3378 out:
3379 cb->args[1] = path_idx;
3380 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003381 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003382 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003383 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003384}
3385
3386static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3387{
Johannes Berg4c476992010-10-04 21:36:35 +02003388 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003389 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003390 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003391 struct mpath_info pinfo;
3392 struct sk_buff *msg;
3393 u8 *dst = NULL;
3394 u8 next_hop[ETH_ALEN];
3395
3396 memset(&pinfo, 0, sizeof(pinfo));
3397
3398 if (!info->attrs[NL80211_ATTR_MAC])
3399 return -EINVAL;
3400
3401 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3402
Johannes Berg4c476992010-10-04 21:36:35 +02003403 if (!rdev->ops->get_mpath)
3404 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003405
Johannes Berg4c476992010-10-04 21:36:35 +02003406 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3407 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003408
Johannes Berg79c97e92009-07-07 03:56:12 +02003409 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003410 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003411 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003412
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003414 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003415 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003416
3417 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003418 dev, dst, next_hop, &pinfo) < 0) {
3419 nlmsg_free(msg);
3420 return -ENOBUFS;
3421 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003422
Johannes Berg4c476992010-10-04 21:36:35 +02003423 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003424}
3425
3426static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3427{
Johannes Berg4c476992010-10-04 21:36:35 +02003428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3429 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003430 u8 *dst = NULL;
3431 u8 *next_hop = NULL;
3432
3433 if (!info->attrs[NL80211_ATTR_MAC])
3434 return -EINVAL;
3435
3436 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3437 return -EINVAL;
3438
3439 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3440 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3441
Johannes Berg4c476992010-10-04 21:36:35 +02003442 if (!rdev->ops->change_mpath)
3443 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003444
Johannes Berg4c476992010-10-04 21:36:35 +02003445 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3446 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003447
Johannes Berg4c476992010-10-04 21:36:35 +02003448 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003449}
Johannes Berg4c476992010-10-04 21:36:35 +02003450
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003451static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3452{
Johannes Berg4c476992010-10-04 21:36:35 +02003453 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3454 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003455 u8 *dst = NULL;
3456 u8 *next_hop = NULL;
3457
3458 if (!info->attrs[NL80211_ATTR_MAC])
3459 return -EINVAL;
3460
3461 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3462 return -EINVAL;
3463
3464 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3465 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3466
Johannes Berg4c476992010-10-04 21:36:35 +02003467 if (!rdev->ops->add_mpath)
3468 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003469
Johannes Berg4c476992010-10-04 21:36:35 +02003470 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3471 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003472
Johannes Berg4c476992010-10-04 21:36:35 +02003473 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003474}
3475
3476static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3477{
Johannes Berg4c476992010-10-04 21:36:35 +02003478 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3479 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003480 u8 *dst = NULL;
3481
3482 if (info->attrs[NL80211_ATTR_MAC])
3483 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3484
Johannes Berg4c476992010-10-04 21:36:35 +02003485 if (!rdev->ops->del_mpath)
3486 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003487
Johannes Berg4c476992010-10-04 21:36:35 +02003488 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003489}
3490
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003491static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3492{
Johannes Berg4c476992010-10-04 21:36:35 +02003493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3494 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003495 struct bss_parameters params;
3496
3497 memset(&params, 0, sizeof(params));
3498 /* default to not changing parameters */
3499 params.use_cts_prot = -1;
3500 params.use_short_preamble = -1;
3501 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003502 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003503 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003504
3505 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3506 params.use_cts_prot =
3507 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3508 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3509 params.use_short_preamble =
3510 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3511 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3512 params.use_short_slot_time =
3513 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003514 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3515 params.basic_rates =
3516 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3517 params.basic_rates_len =
3518 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3519 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003520 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3521 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003522 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3523 params.ht_opmode =
3524 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003525
Johannes Berg4c476992010-10-04 21:36:35 +02003526 if (!rdev->ops->change_bss)
3527 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003528
Johannes Berg074ac8d2010-09-16 14:58:22 +02003529 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003530 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3531 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003532
Johannes Berg4c476992010-10-04 21:36:35 +02003533 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003534}
3535
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003536static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003537 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3538 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3539 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3540 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3541 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3542 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3543};
3544
3545static int parse_reg_rule(struct nlattr *tb[],
3546 struct ieee80211_reg_rule *reg_rule)
3547{
3548 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3549 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3550
3551 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3552 return -EINVAL;
3553 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3554 return -EINVAL;
3555 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3556 return -EINVAL;
3557 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3558 return -EINVAL;
3559 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3560 return -EINVAL;
3561
3562 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3563
3564 freq_range->start_freq_khz =
3565 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3566 freq_range->end_freq_khz =
3567 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3568 freq_range->max_bandwidth_khz =
3569 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3570
3571 power_rule->max_eirp =
3572 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3573
3574 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3575 power_rule->max_antenna_gain =
3576 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3577
3578 return 0;
3579}
3580
3581static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3582{
3583 int r;
3584 char *data = NULL;
3585
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003586 /*
3587 * You should only get this when cfg80211 hasn't yet initialized
3588 * completely when built-in to the kernel right between the time
3589 * window between nl80211_init() and regulatory_init(), if that is
3590 * even possible.
3591 */
3592 mutex_lock(&cfg80211_mutex);
3593 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003594 mutex_unlock(&cfg80211_mutex);
3595 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003596 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003597 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003598
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003599 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3600 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003601
3602 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3603
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003604 r = regulatory_hint_user(data);
3605
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003606 return r;
3607}
3608
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003609static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003610 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003611{
Johannes Berg4c476992010-10-04 21:36:35 +02003612 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003613 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003614 struct wireless_dev *wdev = dev->ieee80211_ptr;
3615 struct mesh_config cur_params;
3616 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003617 void *hdr;
3618 struct nlattr *pinfoattr;
3619 struct sk_buff *msg;
3620
Johannes Berg29cbe682010-12-03 09:20:44 +01003621 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3622 return -EOPNOTSUPP;
3623
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003624 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003625 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003626
Johannes Berg29cbe682010-12-03 09:20:44 +01003627 wdev_lock(wdev);
3628 /* If not connected, get default parameters */
3629 if (!wdev->mesh_id_len)
3630 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3631 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003632 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003633 &cur_params);
3634 wdev_unlock(wdev);
3635
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003636 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003637 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003638
3639 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003640 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003641 if (!msg)
3642 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003643 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003644 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003645 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003646 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003647 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003648 if (!pinfoattr)
3649 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003650 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3651 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3652 cur_params.dot11MeshRetryTimeout) ||
3653 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3654 cur_params.dot11MeshConfirmTimeout) ||
3655 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3656 cur_params.dot11MeshHoldingTimeout) ||
3657 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3658 cur_params.dot11MeshMaxPeerLinks) ||
3659 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3660 cur_params.dot11MeshMaxRetries) ||
3661 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3662 cur_params.dot11MeshTTL) ||
3663 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3664 cur_params.element_ttl) ||
3665 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3666 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003667 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3668 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003669 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3670 cur_params.dot11MeshHWMPmaxPREQretries) ||
3671 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3672 cur_params.path_refresh_time) ||
3673 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3674 cur_params.min_discovery_timeout) ||
3675 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3676 cur_params.dot11MeshHWMPactivePathTimeout) ||
3677 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3678 cur_params.dot11MeshHWMPpreqMinInterval) ||
3679 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3680 cur_params.dot11MeshHWMPperrMinInterval) ||
3681 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3682 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3683 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3684 cur_params.dot11MeshHWMPRootMode) ||
3685 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3686 cur_params.dot11MeshHWMPRannInterval) ||
3687 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3688 cur_params.dot11MeshGateAnnouncementProtocol) ||
3689 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3690 cur_params.dot11MeshForwarding) ||
3691 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003692 cur_params.rssi_threshold) ||
3693 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003694 cur_params.ht_opmode) ||
3695 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3696 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3697 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003698 cur_params.dot11MeshHWMProotInterval) ||
3699 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3700 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003701 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003702 nla_nest_end(msg, pinfoattr);
3703 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003704 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003705
Johannes Berg3b858752009-03-12 09:55:09 +01003706 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003707 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003708 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003709 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003710 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003711}
3712
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003713static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003714 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3715 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3716 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3717 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3718 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3719 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003720 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003721 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003722 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003723 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3724 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3725 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3726 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3727 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003728 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003729 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003730 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003731 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003732 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003733 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003734 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3735 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003736 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3737 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003738 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003739};
3740
Javier Cardonac80d5452010-12-16 17:37:49 -08003741static const struct nla_policy
3742 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003743 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003744 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3745 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003746 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003747 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003748 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003749 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003750};
3751
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003752static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003753 struct mesh_config *cfg,
3754 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003755{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003756 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003757 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003758
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003759#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3760do {\
3761 if (table[attr_num]) {\
3762 cfg->param = nla_fn(table[attr_num]); \
3763 mask |= (1 << (attr_num - 1)); \
3764 } \
3765} while (0);\
3766
3767
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003768 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003769 return -EINVAL;
3770 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003771 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003772 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003773 return -EINVAL;
3774
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003775 /* This makes sure that there aren't more than 32 mesh config
3776 * parameters (otherwise our bitfield scheme would not work.) */
3777 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3778
3779 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003780 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003781 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3782 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003783 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003784 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3785 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003786 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003787 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3788 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003789 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003790 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3791 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003792 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003793 mask, NL80211_MESHCONF_MAX_RETRIES,
3794 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003795 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003796 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003797 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003798 mask, NL80211_MESHCONF_ELEMENT_TTL,
3799 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003800 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003801 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3802 nla_get_u8);
3803 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3804 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3805 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003806 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003807 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3808 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003809 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003810 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3811 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003812 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003813 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3814 nla_get_u16);
3815 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3816 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3817 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003818 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003819 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3820 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003821 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003822 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3823 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003824 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003825 dot11MeshHWMPnetDiameterTraversalTime, mask,
3826 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3827 nla_get_u16);
3828 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3829 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3830 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3831 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3832 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003833 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003834 dot11MeshGateAnnouncementProtocol, mask,
3835 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3836 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003837 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003838 mask, NL80211_MESHCONF_FORWARDING,
3839 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003840 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003841 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3842 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003843 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003844 mask, NL80211_MESHCONF_HT_OPMODE,
3845 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003846 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3847 mask,
3848 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3849 nla_get_u32);
3850 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3851 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3852 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003853 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3854 dot11MeshHWMPconfirmationInterval, mask,
3855 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3856 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003857 if (mask_out)
3858 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003859
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003860 return 0;
3861
3862#undef FILL_IN_MESH_PARAM_IF_SET
3863}
3864
Javier Cardonac80d5452010-12-16 17:37:49 -08003865static int nl80211_parse_mesh_setup(struct genl_info *info,
3866 struct mesh_setup *setup)
3867{
3868 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3869
3870 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3871 return -EINVAL;
3872 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3873 info->attrs[NL80211_ATTR_MESH_SETUP],
3874 nl80211_mesh_setup_params_policy))
3875 return -EINVAL;
3876
Javier Cardonad299a1f2012-03-31 11:31:33 -07003877 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3878 setup->sync_method =
3879 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3880 IEEE80211_SYNC_METHOD_VENDOR :
3881 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3882
Javier Cardonac80d5452010-12-16 17:37:49 -08003883 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3884 setup->path_sel_proto =
3885 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3886 IEEE80211_PATH_PROTOCOL_VENDOR :
3887 IEEE80211_PATH_PROTOCOL_HWMP;
3888
3889 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3890 setup->path_metric =
3891 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3892 IEEE80211_PATH_METRIC_VENDOR :
3893 IEEE80211_PATH_METRIC_AIRTIME;
3894
Javier Cardona581a8b02011-04-07 15:08:27 -07003895
3896 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003897 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003898 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003899 if (!is_valid_ie_attr(ieattr))
3900 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003901 setup->ie = nla_data(ieattr);
3902 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003903 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003904 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3905 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003906
3907 return 0;
3908}
3909
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003910static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003911 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003912{
3913 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3914 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003915 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003916 struct mesh_config cfg;
3917 u32 mask;
3918 int err;
3919
Johannes Berg29cbe682010-12-03 09:20:44 +01003920 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3921 return -EOPNOTSUPP;
3922
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003923 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003924 return -EOPNOTSUPP;
3925
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003926 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003927 if (err)
3928 return err;
3929
Johannes Berg29cbe682010-12-03 09:20:44 +01003930 wdev_lock(wdev);
3931 if (!wdev->mesh_id_len)
3932 err = -ENOLINK;
3933
3934 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003935 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003936 mask, &cfg);
3937
3938 wdev_unlock(wdev);
3939
3940 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003941}
3942
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003943static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3944{
3945 struct sk_buff *msg;
3946 void *hdr = NULL;
3947 struct nlattr *nl_reg_rules;
3948 unsigned int i;
3949 int err = -EINVAL;
3950
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003951 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003952
3953 if (!cfg80211_regdomain)
3954 goto out;
3955
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003956 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003957 if (!msg) {
3958 err = -ENOBUFS;
3959 goto out;
3960 }
3961
3962 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3963 NL80211_CMD_GET_REG);
3964 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003965 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003966
David S. Miller9360ffd2012-03-29 04:41:26 -04003967 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3968 cfg80211_regdomain->alpha2) ||
3969 (cfg80211_regdomain->dfs_region &&
3970 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3971 cfg80211_regdomain->dfs_region)))
3972 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003973
3974 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3975 if (!nl_reg_rules)
3976 goto nla_put_failure;
3977
3978 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3979 struct nlattr *nl_reg_rule;
3980 const struct ieee80211_reg_rule *reg_rule;
3981 const struct ieee80211_freq_range *freq_range;
3982 const struct ieee80211_power_rule *power_rule;
3983
3984 reg_rule = &cfg80211_regdomain->reg_rules[i];
3985 freq_range = &reg_rule->freq_range;
3986 power_rule = &reg_rule->power_rule;
3987
3988 nl_reg_rule = nla_nest_start(msg, i);
3989 if (!nl_reg_rule)
3990 goto nla_put_failure;
3991
David S. Miller9360ffd2012-03-29 04:41:26 -04003992 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3993 reg_rule->flags) ||
3994 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3995 freq_range->start_freq_khz) ||
3996 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3997 freq_range->end_freq_khz) ||
3998 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3999 freq_range->max_bandwidth_khz) ||
4000 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4001 power_rule->max_antenna_gain) ||
4002 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4003 power_rule->max_eirp))
4004 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004005
4006 nla_nest_end(msg, nl_reg_rule);
4007 }
4008
4009 nla_nest_end(msg, nl_reg_rules);
4010
4011 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004012 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004013 goto out;
4014
4015nla_put_failure:
4016 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004017put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004018 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004019 err = -EMSGSIZE;
4020out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004021 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004022 return err;
4023}
4024
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004025static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4026{
4027 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4028 struct nlattr *nl_reg_rule;
4029 char *alpha2 = NULL;
4030 int rem_reg_rules = 0, r = 0;
4031 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004032 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004033 struct ieee80211_regdomain *rd = NULL;
4034
4035 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4036 return -EINVAL;
4037
4038 if (!info->attrs[NL80211_ATTR_REG_RULES])
4039 return -EINVAL;
4040
4041 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4042
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004043 if (info->attrs[NL80211_ATTR_DFS_REGION])
4044 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4045
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004046 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4047 rem_reg_rules) {
4048 num_rules++;
4049 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004050 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004051 }
4052
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004053 mutex_lock(&cfg80211_mutex);
4054
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004055 if (!reg_is_valid_request(alpha2)) {
4056 r = -EINVAL;
4057 goto bad_reg;
4058 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004059
4060 size_of_regd = sizeof(struct ieee80211_regdomain) +
4061 (num_rules * sizeof(struct ieee80211_reg_rule));
4062
4063 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004064 if (!rd) {
4065 r = -ENOMEM;
4066 goto bad_reg;
4067 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004068
4069 rd->n_reg_rules = num_rules;
4070 rd->alpha2[0] = alpha2[0];
4071 rd->alpha2[1] = alpha2[1];
4072
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004073 /*
4074 * Disable DFS master mode if the DFS region was
4075 * not supported or known on this kernel.
4076 */
4077 if (reg_supported_dfs_region(dfs_region))
4078 rd->dfs_region = dfs_region;
4079
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004080 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4081 rem_reg_rules) {
4082 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4083 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4084 reg_rule_policy);
4085 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4086 if (r)
4087 goto bad_reg;
4088
4089 rule_idx++;
4090
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004091 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4092 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004093 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004094 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004095 }
4096
4097 BUG_ON(rule_idx != num_rules);
4098
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004099 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004100
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004101 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004102
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004103 return r;
4104
Johannes Bergd2372b32008-10-24 20:32:20 +02004105 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004106 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004107 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004108 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004109}
4110
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004111static int validate_scan_freqs(struct nlattr *freqs)
4112{
4113 struct nlattr *attr1, *attr2;
4114 int n_channels = 0, tmp1, tmp2;
4115
4116 nla_for_each_nested(attr1, freqs, tmp1) {
4117 n_channels++;
4118 /*
4119 * Some hardware has a limited channel list for
4120 * scanning, and it is pretty much nonsensical
4121 * to scan for a channel twice, so disallow that
4122 * and don't require drivers to check that the
4123 * channel list they get isn't longer than what
4124 * they can scan, as long as they can scan all
4125 * the channels they registered at once.
4126 */
4127 nla_for_each_nested(attr2, freqs, tmp2)
4128 if (attr1 != attr2 &&
4129 nla_get_u32(attr1) == nla_get_u32(attr2))
4130 return 0;
4131 }
4132
4133 return n_channels;
4134}
4135
Johannes Berg2a519312009-02-10 21:25:55 +01004136static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4137{
Johannes Berg4c476992010-10-04 21:36:35 +02004138 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004139 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004140 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004141 struct nlattr *attr;
4142 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004143 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004144 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004145
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004146 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4147 return -EINVAL;
4148
Johannes Berg79c97e92009-07-07 03:56:12 +02004149 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004150
Johannes Berg4c476992010-10-04 21:36:35 +02004151 if (!rdev->ops->scan)
4152 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004153
Johannes Berg4c476992010-10-04 21:36:35 +02004154 if (rdev->scan_req)
4155 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004156
4157 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004158 n_channels = validate_scan_freqs(
4159 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004160 if (!n_channels)
4161 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004162 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004163 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004164 n_channels = 0;
4165
Johannes Berg2a519312009-02-10 21:25:55 +01004166 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4167 if (wiphy->bands[band])
4168 n_channels += wiphy->bands[band]->n_channels;
4169 }
4170
4171 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4172 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4173 n_ssids++;
4174
Johannes Berg4c476992010-10-04 21:36:35 +02004175 if (n_ssids > wiphy->max_scan_ssids)
4176 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004177
Jouni Malinen70692ad2009-02-16 19:39:13 +02004178 if (info->attrs[NL80211_ATTR_IE])
4179 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4180 else
4181 ie_len = 0;
4182
Johannes Berg4c476992010-10-04 21:36:35 +02004183 if (ie_len > wiphy->max_scan_ie_len)
4184 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004185
Johannes Berg2a519312009-02-10 21:25:55 +01004186 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004187 + sizeof(*request->ssids) * n_ssids
4188 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004189 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004190 if (!request)
4191 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004192
Johannes Berg2a519312009-02-10 21:25:55 +01004193 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004194 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004195 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004196 if (ie_len) {
4197 if (request->ssids)
4198 request->ie = (void *)(request->ssids + n_ssids);
4199 else
4200 request->ie = (void *)(request->channels + n_channels);
4201 }
Johannes Berg2a519312009-02-10 21:25:55 +01004202
Johannes Berg584991d2009-11-02 13:32:03 +01004203 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004204 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4205 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004206 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004207 struct ieee80211_channel *chan;
4208
4209 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4210
4211 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004212 err = -EINVAL;
4213 goto out_free;
4214 }
Johannes Berg584991d2009-11-02 13:32:03 +01004215
4216 /* ignore disabled channels */
4217 if (chan->flags & IEEE80211_CHAN_DISABLED)
4218 continue;
4219
4220 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004221 i++;
4222 }
4223 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004224 enum ieee80211_band band;
4225
Johannes Berg2a519312009-02-10 21:25:55 +01004226 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004227 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4228 int j;
4229 if (!wiphy->bands[band])
4230 continue;
4231 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004232 struct ieee80211_channel *chan;
4233
4234 chan = &wiphy->bands[band]->channels[j];
4235
4236 if (chan->flags & IEEE80211_CHAN_DISABLED)
4237 continue;
4238
4239 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004240 i++;
4241 }
4242 }
4243 }
4244
Johannes Berg584991d2009-11-02 13:32:03 +01004245 if (!i) {
4246 err = -EINVAL;
4247 goto out_free;
4248 }
4249
4250 request->n_channels = i;
4251
Johannes Berg2a519312009-02-10 21:25:55 +01004252 i = 0;
4253 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4254 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004255 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004256 err = -EINVAL;
4257 goto out_free;
4258 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004259 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004260 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004261 i++;
4262 }
4263 }
4264
Jouni Malinen70692ad2009-02-16 19:39:13 +02004265 if (info->attrs[NL80211_ATTR_IE]) {
4266 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004267 memcpy((void *)request->ie,
4268 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004269 request->ie_len);
4270 }
4271
Johannes Berg34850ab2011-07-18 18:08:35 +02004272 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004273 if (wiphy->bands[i])
4274 request->rates[i] =
4275 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004276
4277 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4278 nla_for_each_nested(attr,
4279 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4280 tmp) {
4281 enum ieee80211_band band = nla_type(attr);
4282
Dan Carpenter84404622011-07-29 11:52:18 +03004283 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004284 err = -EINVAL;
4285 goto out_free;
4286 }
4287 err = ieee80211_get_ratemask(wiphy->bands[band],
4288 nla_data(attr),
4289 nla_len(attr),
4290 &request->rates[band]);
4291 if (err)
4292 goto out_free;
4293 }
4294 }
4295
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304296 request->no_cck =
4297 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4298
Johannes Bergfd014282012-06-18 19:17:03 +02004299 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004300 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004301
Johannes Berg79c97e92009-07-07 03:56:12 +02004302 rdev->scan_req = request;
Johannes Bergfd014282012-06-18 19:17:03 +02004303 err = rdev->ops->scan(&rdev->wiphy, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004304
Johannes Berg463d0182009-07-14 00:33:35 +02004305 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004306 nl80211_send_scan_start(rdev, wdev);
4307 if (wdev->netdev)
4308 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004309 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004310 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004311 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004312 kfree(request);
4313 }
Johannes Berg3b858752009-03-12 09:55:09 +01004314
Johannes Berg2a519312009-02-10 21:25:55 +01004315 return err;
4316}
4317
Luciano Coelho807f8a82011-05-11 17:09:35 +03004318static int nl80211_start_sched_scan(struct sk_buff *skb,
4319 struct genl_info *info)
4320{
4321 struct cfg80211_sched_scan_request *request;
4322 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4323 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004324 struct nlattr *attr;
4325 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004326 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004327 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004328 enum ieee80211_band band;
4329 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004330 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004331
4332 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4333 !rdev->ops->sched_scan_start)
4334 return -EOPNOTSUPP;
4335
4336 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4337 return -EINVAL;
4338
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004339 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4340 return -EINVAL;
4341
4342 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4343 if (interval == 0)
4344 return -EINVAL;
4345
Luciano Coelho807f8a82011-05-11 17:09:35 +03004346 wiphy = &rdev->wiphy;
4347
4348 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4349 n_channels = validate_scan_freqs(
4350 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4351 if (!n_channels)
4352 return -EINVAL;
4353 } else {
4354 n_channels = 0;
4355
4356 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4357 if (wiphy->bands[band])
4358 n_channels += wiphy->bands[band]->n_channels;
4359 }
4360
4361 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4362 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4363 tmp)
4364 n_ssids++;
4365
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004366 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004367 return -EINVAL;
4368
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004369 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4370 nla_for_each_nested(attr,
4371 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4372 tmp)
4373 n_match_sets++;
4374
4375 if (n_match_sets > wiphy->max_match_sets)
4376 return -EINVAL;
4377
Luciano Coelho807f8a82011-05-11 17:09:35 +03004378 if (info->attrs[NL80211_ATTR_IE])
4379 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4380 else
4381 ie_len = 0;
4382
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004383 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004384 return -EINVAL;
4385
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004386 mutex_lock(&rdev->sched_scan_mtx);
4387
4388 if (rdev->sched_scan_req) {
4389 err = -EINPROGRESS;
4390 goto out;
4391 }
4392
Luciano Coelho807f8a82011-05-11 17:09:35 +03004393 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004394 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004395 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004396 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004397 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004398 if (!request) {
4399 err = -ENOMEM;
4400 goto out;
4401 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004402
4403 if (n_ssids)
4404 request->ssids = (void *)&request->channels[n_channels];
4405 request->n_ssids = n_ssids;
4406 if (ie_len) {
4407 if (request->ssids)
4408 request->ie = (void *)(request->ssids + n_ssids);
4409 else
4410 request->ie = (void *)(request->channels + n_channels);
4411 }
4412
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004413 if (n_match_sets) {
4414 if (request->ie)
4415 request->match_sets = (void *)(request->ie + ie_len);
4416 else if (request->ssids)
4417 request->match_sets =
4418 (void *)(request->ssids + n_ssids);
4419 else
4420 request->match_sets =
4421 (void *)(request->channels + n_channels);
4422 }
4423 request->n_match_sets = n_match_sets;
4424
Luciano Coelho807f8a82011-05-11 17:09:35 +03004425 i = 0;
4426 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4427 /* user specified, bail out if channel not found */
4428 nla_for_each_nested(attr,
4429 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4430 tmp) {
4431 struct ieee80211_channel *chan;
4432
4433 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4434
4435 if (!chan) {
4436 err = -EINVAL;
4437 goto out_free;
4438 }
4439
4440 /* ignore disabled channels */
4441 if (chan->flags & IEEE80211_CHAN_DISABLED)
4442 continue;
4443
4444 request->channels[i] = chan;
4445 i++;
4446 }
4447 } else {
4448 /* all channels */
4449 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4450 int j;
4451 if (!wiphy->bands[band])
4452 continue;
4453 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4454 struct ieee80211_channel *chan;
4455
4456 chan = &wiphy->bands[band]->channels[j];
4457
4458 if (chan->flags & IEEE80211_CHAN_DISABLED)
4459 continue;
4460
4461 request->channels[i] = chan;
4462 i++;
4463 }
4464 }
4465 }
4466
4467 if (!i) {
4468 err = -EINVAL;
4469 goto out_free;
4470 }
4471
4472 request->n_channels = i;
4473
4474 i = 0;
4475 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4476 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4477 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004478 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004479 err = -EINVAL;
4480 goto out_free;
4481 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004482 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004483 memcpy(request->ssids[i].ssid, nla_data(attr),
4484 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004485 i++;
4486 }
4487 }
4488
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004489 i = 0;
4490 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4491 nla_for_each_nested(attr,
4492 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4493 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004494 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004495
4496 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4497 nla_data(attr), nla_len(attr),
4498 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004499 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004500 if (ssid) {
4501 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4502 err = -EINVAL;
4503 goto out_free;
4504 }
4505 memcpy(request->match_sets[i].ssid.ssid,
4506 nla_data(ssid), nla_len(ssid));
4507 request->match_sets[i].ssid.ssid_len =
4508 nla_len(ssid);
4509 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004510 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4511 if (rssi)
4512 request->rssi_thold = nla_get_u32(rssi);
4513 else
4514 request->rssi_thold =
4515 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004516 i++;
4517 }
4518 }
4519
Luciano Coelho807f8a82011-05-11 17:09:35 +03004520 if (info->attrs[NL80211_ATTR_IE]) {
4521 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4522 memcpy((void *)request->ie,
4523 nla_data(info->attrs[NL80211_ATTR_IE]),
4524 request->ie_len);
4525 }
4526
4527 request->dev = dev;
4528 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004529 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004530
4531 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4532 if (!err) {
4533 rdev->sched_scan_req = request;
4534 nl80211_send_sched_scan(rdev, dev,
4535 NL80211_CMD_START_SCHED_SCAN);
4536 goto out;
4537 }
4538
4539out_free:
4540 kfree(request);
4541out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004542 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004543 return err;
4544}
4545
4546static int nl80211_stop_sched_scan(struct sk_buff *skb,
4547 struct genl_info *info)
4548{
4549 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004550 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004551
4552 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4553 !rdev->ops->sched_scan_stop)
4554 return -EOPNOTSUPP;
4555
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004556 mutex_lock(&rdev->sched_scan_mtx);
4557 err = __cfg80211_stop_sched_scan(rdev, false);
4558 mutex_unlock(&rdev->sched_scan_mtx);
4559
4560 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004561}
4562
Johannes Berg9720bb32011-06-21 09:45:33 +02004563static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4564 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004565 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004566 struct wireless_dev *wdev,
4567 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004568{
Johannes Berg48ab9052009-07-10 18:42:31 +02004569 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004570 void *hdr;
4571 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004572
4573 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004574
Johannes Berg9720bb32011-06-21 09:45:33 +02004575 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004576 NL80211_CMD_NEW_SCAN_RESULTS);
4577 if (!hdr)
4578 return -1;
4579
Johannes Berg9720bb32011-06-21 09:45:33 +02004580 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4581
David S. Miller9360ffd2012-03-29 04:41:26 -04004582 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4583 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4584 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004585
4586 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4587 if (!bss)
4588 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004589 if ((!is_zero_ether_addr(res->bssid) &&
4590 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4591 (res->information_elements && res->len_information_elements &&
4592 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4593 res->len_information_elements,
4594 res->information_elements)) ||
4595 (res->beacon_ies && res->len_beacon_ies &&
4596 res->beacon_ies != res->information_elements &&
4597 nla_put(msg, NL80211_BSS_BEACON_IES,
4598 res->len_beacon_ies, res->beacon_ies)))
4599 goto nla_put_failure;
4600 if (res->tsf &&
4601 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4602 goto nla_put_failure;
4603 if (res->beacon_interval &&
4604 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4605 goto nla_put_failure;
4606 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4607 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4608 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4609 jiffies_to_msecs(jiffies - intbss->ts)))
4610 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004611
Johannes Berg77965c92009-02-18 18:45:06 +01004612 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004613 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004614 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4615 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004616 break;
4617 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004618 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4619 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004620 break;
4621 default:
4622 break;
4623 }
4624
Johannes Berg48ab9052009-07-10 18:42:31 +02004625 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004626 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004627 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004628 if (intbss == wdev->current_bss &&
4629 nla_put_u32(msg, NL80211_BSS_STATUS,
4630 NL80211_BSS_STATUS_ASSOCIATED))
4631 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004632 break;
4633 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004634 if (intbss == wdev->current_bss &&
4635 nla_put_u32(msg, NL80211_BSS_STATUS,
4636 NL80211_BSS_STATUS_IBSS_JOINED))
4637 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004638 break;
4639 default:
4640 break;
4641 }
4642
Johannes Berg2a519312009-02-10 21:25:55 +01004643 nla_nest_end(msg, bss);
4644
4645 return genlmsg_end(msg, hdr);
4646
4647 nla_put_failure:
4648 genlmsg_cancel(msg, hdr);
4649 return -EMSGSIZE;
4650}
4651
4652static int nl80211_dump_scan(struct sk_buff *skb,
4653 struct netlink_callback *cb)
4654{
Johannes Berg48ab9052009-07-10 18:42:31 +02004655 struct cfg80211_registered_device *rdev;
4656 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004657 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004658 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004659 int start = cb->args[1], idx = 0;
4660 int err;
4661
Johannes Berg67748892010-10-04 21:14:06 +02004662 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4663 if (err)
4664 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004665
Johannes Berg48ab9052009-07-10 18:42:31 +02004666 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004667
Johannes Berg48ab9052009-07-10 18:42:31 +02004668 wdev_lock(wdev);
4669 spin_lock_bh(&rdev->bss_lock);
4670 cfg80211_bss_expire(rdev);
4671
Johannes Berg9720bb32011-06-21 09:45:33 +02004672 cb->seq = rdev->bss_generation;
4673
Johannes Berg48ab9052009-07-10 18:42:31 +02004674 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004675 if (++idx <= start)
4676 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004677 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004678 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004679 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004680 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004681 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004682 }
4683 }
4684
Johannes Berg48ab9052009-07-10 18:42:31 +02004685 spin_unlock_bh(&rdev->bss_lock);
4686 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004687
4688 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004689 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004690
Johannes Berg67748892010-10-04 21:14:06 +02004691 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004692}
4693
Holger Schurig61fa7132009-11-11 12:25:40 +01004694static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4695 int flags, struct net_device *dev,
4696 struct survey_info *survey)
4697{
4698 void *hdr;
4699 struct nlattr *infoattr;
4700
Holger Schurig61fa7132009-11-11 12:25:40 +01004701 hdr = nl80211hdr_put(msg, pid, seq, flags,
4702 NL80211_CMD_NEW_SURVEY_RESULTS);
4703 if (!hdr)
4704 return -ENOMEM;
4705
David S. Miller9360ffd2012-03-29 04:41:26 -04004706 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4707 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004708
4709 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4710 if (!infoattr)
4711 goto nla_put_failure;
4712
David S. Miller9360ffd2012-03-29 04:41:26 -04004713 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4714 survey->channel->center_freq))
4715 goto nla_put_failure;
4716
4717 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4718 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4719 goto nla_put_failure;
4720 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4721 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4722 goto nla_put_failure;
4723 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4724 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4725 survey->channel_time))
4726 goto nla_put_failure;
4727 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4728 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4729 survey->channel_time_busy))
4730 goto nla_put_failure;
4731 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4732 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4733 survey->channel_time_ext_busy))
4734 goto nla_put_failure;
4735 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4736 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4737 survey->channel_time_rx))
4738 goto nla_put_failure;
4739 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4740 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4741 survey->channel_time_tx))
4742 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004743
4744 nla_nest_end(msg, infoattr);
4745
4746 return genlmsg_end(msg, hdr);
4747
4748 nla_put_failure:
4749 genlmsg_cancel(msg, hdr);
4750 return -EMSGSIZE;
4751}
4752
4753static int nl80211_dump_survey(struct sk_buff *skb,
4754 struct netlink_callback *cb)
4755{
4756 struct survey_info survey;
4757 struct cfg80211_registered_device *dev;
4758 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004759 int survey_idx = cb->args[1];
4760 int res;
4761
Johannes Berg67748892010-10-04 21:14:06 +02004762 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4763 if (res)
4764 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004765
4766 if (!dev->ops->dump_survey) {
4767 res = -EOPNOTSUPP;
4768 goto out_err;
4769 }
4770
4771 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004772 struct ieee80211_channel *chan;
4773
Holger Schurig61fa7132009-11-11 12:25:40 +01004774 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4775 &survey);
4776 if (res == -ENOENT)
4777 break;
4778 if (res)
4779 goto out_err;
4780
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004781 /* Survey without a channel doesn't make sense */
4782 if (!survey.channel) {
4783 res = -EINVAL;
4784 goto out;
4785 }
4786
4787 chan = ieee80211_get_channel(&dev->wiphy,
4788 survey.channel->center_freq);
4789 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4790 survey_idx++;
4791 continue;
4792 }
4793
Holger Schurig61fa7132009-11-11 12:25:40 +01004794 if (nl80211_send_survey(skb,
4795 NETLINK_CB(cb->skb).pid,
4796 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4797 netdev,
4798 &survey) < 0)
4799 goto out;
4800 survey_idx++;
4801 }
4802
4803 out:
4804 cb->args[1] = survey_idx;
4805 res = skb->len;
4806 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004807 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004808 return res;
4809}
4810
Jouni Malinen255e7372009-03-20 21:21:17 +02004811static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4812{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004813 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004814}
4815
Samuel Ortizb23aa672009-07-01 21:26:54 +02004816static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4817{
4818 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4819 NL80211_WPA_VERSION_2));
4820}
4821
Jouni Malinen636a5d32009-03-19 13:39:22 +02004822static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4823{
Johannes Berg4c476992010-10-04 21:36:35 +02004824 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4825 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004826 struct ieee80211_channel *chan;
4827 const u8 *bssid, *ssid, *ie = NULL;
4828 int err, ssid_len, ie_len = 0;
4829 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004830 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004831 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004832
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004833 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4834 return -EINVAL;
4835
4836 if (!info->attrs[NL80211_ATTR_MAC])
4837 return -EINVAL;
4838
Jouni Malinen17780922009-03-27 20:52:47 +02004839 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4840 return -EINVAL;
4841
Johannes Berg19957bb2009-07-02 17:20:43 +02004842 if (!info->attrs[NL80211_ATTR_SSID])
4843 return -EINVAL;
4844
4845 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4846 return -EINVAL;
4847
Johannes Bergfffd0932009-07-08 14:22:54 +02004848 err = nl80211_parse_key(info, &key);
4849 if (err)
4850 return err;
4851
4852 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004853 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4854 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004855 if (!key.p.key || !key.p.key_len)
4856 return -EINVAL;
4857 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4858 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4859 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4860 key.p.key_len != WLAN_KEY_LEN_WEP104))
4861 return -EINVAL;
4862 if (key.idx > 4)
4863 return -EINVAL;
4864 } else {
4865 key.p.key_len = 0;
4866 key.p.key = NULL;
4867 }
4868
Johannes Bergafea0b72010-08-10 09:46:42 +02004869 if (key.idx >= 0) {
4870 int i;
4871 bool ok = false;
4872 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4873 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4874 ok = true;
4875 break;
4876 }
4877 }
Johannes Berg4c476992010-10-04 21:36:35 +02004878 if (!ok)
4879 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004880 }
4881
Johannes Berg4c476992010-10-04 21:36:35 +02004882 if (!rdev->ops->auth)
4883 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004884
Johannes Berg074ac8d2010-09-16 14:58:22 +02004885 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004886 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4887 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004888
Johannes Berg19957bb2009-07-02 17:20:43 +02004889 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004890 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004891 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004892 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4893 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004894
Johannes Berg19957bb2009-07-02 17:20:43 +02004895 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4896 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4897
4898 if (info->attrs[NL80211_ATTR_IE]) {
4899 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4900 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4901 }
4902
4903 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004904 if (!nl80211_valid_auth_type(auth_type))
4905 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004906
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004907 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4908
Johannes Berg95de8172012-01-20 13:55:25 +01004909 /*
4910 * Since we no longer track auth state, ignore
4911 * requests to only change local state.
4912 */
4913 if (local_state_change)
4914 return 0;
4915
Johannes Berg4c476992010-10-04 21:36:35 +02004916 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4917 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004918 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004919}
4920
Johannes Bergc0692b82010-08-27 14:26:53 +03004921static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4922 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004923 struct cfg80211_crypto_settings *settings,
4924 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004925{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004926 memset(settings, 0, sizeof(*settings));
4927
Samuel Ortizb23aa672009-07-01 21:26:54 +02004928 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4929
Johannes Bergc0692b82010-08-27 14:26:53 +03004930 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4931 u16 proto;
4932 proto = nla_get_u16(
4933 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4934 settings->control_port_ethertype = cpu_to_be16(proto);
4935 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4936 proto != ETH_P_PAE)
4937 return -EINVAL;
4938 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4939 settings->control_port_no_encrypt = true;
4940 } else
4941 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4942
Samuel Ortizb23aa672009-07-01 21:26:54 +02004943 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4944 void *data;
4945 int len, i;
4946
4947 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4948 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4949 settings->n_ciphers_pairwise = len / sizeof(u32);
4950
4951 if (len % sizeof(u32))
4952 return -EINVAL;
4953
Johannes Berg3dc27d22009-07-02 21:36:37 +02004954 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004955 return -EINVAL;
4956
4957 memcpy(settings->ciphers_pairwise, data, len);
4958
4959 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004960 if (!cfg80211_supported_cipher_suite(
4961 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004962 settings->ciphers_pairwise[i]))
4963 return -EINVAL;
4964 }
4965
4966 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4967 settings->cipher_group =
4968 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004969 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4970 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004971 return -EINVAL;
4972 }
4973
4974 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4975 settings->wpa_versions =
4976 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4977 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4978 return -EINVAL;
4979 }
4980
4981 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4982 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004983 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004984
4985 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4986 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4987 settings->n_akm_suites = len / sizeof(u32);
4988
4989 if (len % sizeof(u32))
4990 return -EINVAL;
4991
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004992 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4993 return -EINVAL;
4994
Samuel Ortizb23aa672009-07-01 21:26:54 +02004995 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004996 }
4997
4998 return 0;
4999}
5000
Jouni Malinen636a5d32009-03-19 13:39:22 +02005001static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5002{
Johannes Berg4c476992010-10-04 21:36:35 +02005003 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5004 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005005 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005006 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005007 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005008 int err, ssid_len, ie_len = 0;
5009 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005010 u32 flags = 0;
5011 struct ieee80211_ht_cap *ht_capa = NULL;
5012 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005013
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005014 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5015 return -EINVAL;
5016
5017 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005018 !info->attrs[NL80211_ATTR_SSID] ||
5019 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005020 return -EINVAL;
5021
Johannes Berg4c476992010-10-04 21:36:35 +02005022 if (!rdev->ops->assoc)
5023 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005024
Johannes Berg074ac8d2010-09-16 14:58:22 +02005025 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005026 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5027 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005028
Johannes Berg19957bb2009-07-02 17:20:43 +02005029 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005030
Johannes Berg19957bb2009-07-02 17:20:43 +02005031 chan = ieee80211_get_channel(&rdev->wiphy,
5032 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005033 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5034 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005035
Johannes Berg19957bb2009-07-02 17:20:43 +02005036 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5037 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005038
5039 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005040 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5041 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005042 }
5043
Jouni Malinendc6382c2009-05-06 22:09:37 +03005044 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005045 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005046 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005047 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005048 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005049 else if (mfp != NL80211_MFP_NO)
5050 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005051 }
5052
Johannes Berg3e5d7642009-07-07 14:37:26 +02005053 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5054 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5055
Ben Greear7e7c8922011-11-18 11:31:59 -08005056 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5057 flags |= ASSOC_REQ_DISABLE_HT;
5058
5059 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5060 ht_capa_mask =
5061 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5062
5063 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5064 if (!ht_capa_mask)
5065 return -EINVAL;
5066 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5067 }
5068
Johannes Bergc0692b82010-08-27 14:26:53 +03005069 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005070 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005071 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5072 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005073 &crypto, flags, ht_capa,
5074 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005075
Jouni Malinen636a5d32009-03-19 13:39:22 +02005076 return err;
5077}
5078
5079static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5080{
Johannes Berg4c476992010-10-04 21:36:35 +02005081 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5082 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005083 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005084 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005085 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005086 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005087
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005088 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5089 return -EINVAL;
5090
5091 if (!info->attrs[NL80211_ATTR_MAC])
5092 return -EINVAL;
5093
5094 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5095 return -EINVAL;
5096
Johannes Berg4c476992010-10-04 21:36:35 +02005097 if (!rdev->ops->deauth)
5098 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005099
Johannes Berg074ac8d2010-09-16 14:58:22 +02005100 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005101 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5102 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005103
Johannes Berg19957bb2009-07-02 17:20:43 +02005104 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005105
Johannes Berg19957bb2009-07-02 17:20:43 +02005106 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5107 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005108 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005109 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005110 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005111
5112 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005113 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5114 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005115 }
5116
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005117 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5118
Johannes Berg4c476992010-10-04 21:36:35 +02005119 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5120 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005121}
5122
5123static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5124{
Johannes Berg4c476992010-10-04 21:36:35 +02005125 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5126 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005127 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005128 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005129 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005130 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005131
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005132 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5133 return -EINVAL;
5134
5135 if (!info->attrs[NL80211_ATTR_MAC])
5136 return -EINVAL;
5137
5138 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5139 return -EINVAL;
5140
Johannes Berg4c476992010-10-04 21:36:35 +02005141 if (!rdev->ops->disassoc)
5142 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005143
Johannes Berg074ac8d2010-09-16 14:58:22 +02005144 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005145 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5146 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005147
Johannes Berg19957bb2009-07-02 17:20:43 +02005148 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005149
Johannes Berg19957bb2009-07-02 17:20:43 +02005150 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5151 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005152 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005153 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005154 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005155
5156 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005157 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5158 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005159 }
5160
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005161 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5162
Johannes Berg4c476992010-10-04 21:36:35 +02005163 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5164 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005165}
5166
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005167static bool
5168nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5169 int mcast_rate[IEEE80211_NUM_BANDS],
5170 int rateval)
5171{
5172 struct wiphy *wiphy = &rdev->wiphy;
5173 bool found = false;
5174 int band, i;
5175
5176 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5177 struct ieee80211_supported_band *sband;
5178
5179 sband = wiphy->bands[band];
5180 if (!sband)
5181 continue;
5182
5183 for (i = 0; i < sband->n_bitrates; i++) {
5184 if (sband->bitrates[i].bitrate == rateval) {
5185 mcast_rate[band] = i + 1;
5186 found = true;
5187 break;
5188 }
5189 }
5190 }
5191
5192 return found;
5193}
5194
Johannes Berg04a773a2009-04-19 21:24:32 +02005195static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5196{
Johannes Berg4c476992010-10-04 21:36:35 +02005197 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5198 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005199 struct cfg80211_ibss_params ibss;
5200 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005201 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005202 int err;
5203
Johannes Berg8e30bc52009-04-22 17:45:38 +02005204 memset(&ibss, 0, sizeof(ibss));
5205
Johannes Berg04a773a2009-04-19 21:24:32 +02005206 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5207 return -EINVAL;
5208
5209 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5210 !info->attrs[NL80211_ATTR_SSID] ||
5211 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5212 return -EINVAL;
5213
Johannes Berg8e30bc52009-04-22 17:45:38 +02005214 ibss.beacon_interval = 100;
5215
5216 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5217 ibss.beacon_interval =
5218 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5219 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5220 return -EINVAL;
5221 }
5222
Johannes Berg4c476992010-10-04 21:36:35 +02005223 if (!rdev->ops->join_ibss)
5224 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005225
Johannes Berg4c476992010-10-04 21:36:35 +02005226 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5227 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005228
Johannes Berg79c97e92009-07-07 03:56:12 +02005229 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005230
Johannes Berg39193492011-09-16 13:45:25 +02005231 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005232 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005233
5234 if (!is_valid_ether_addr(ibss.bssid))
5235 return -EINVAL;
5236 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005237 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5238 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5239
5240 if (info->attrs[NL80211_ATTR_IE]) {
5241 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5242 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5243 }
5244
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005245 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5246 enum nl80211_channel_type channel_type;
5247
Johannes Bergcd6c6592012-05-10 21:27:18 +02005248 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005249 return -EINVAL;
5250
5251 if (channel_type != NL80211_CHAN_NO_HT &&
5252 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5253 return -EINVAL;
5254
5255 ibss.channel_type = channel_type;
5256 } else {
5257 ibss.channel_type = NL80211_CHAN_NO_HT;
5258 }
5259
5260 ibss.channel = rdev_freq_to_chan(rdev,
5261 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5262 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005263 if (!ibss.channel ||
5264 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005265 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5266 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005267
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005268 /* Both channels should be able to initiate communication */
5269 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5270 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5271 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5272 ibss.channel_type))
5273 return -EINVAL;
5274
Johannes Berg04a773a2009-04-19 21:24:32 +02005275 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005276 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005277
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005278 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5279 u8 *rates =
5280 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5281 int n_rates =
5282 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5283 struct ieee80211_supported_band *sband =
5284 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005285
Johannes Berg34850ab2011-07-18 18:08:35 +02005286 err = ieee80211_get_ratemask(sband, rates, n_rates,
5287 &ibss.basic_rates);
5288 if (err)
5289 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005290 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005291
5292 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5293 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5294 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5295 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005296
Johannes Berg4c476992010-10-04 21:36:35 +02005297 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5298 connkeys = nl80211_parse_connkeys(rdev,
5299 info->attrs[NL80211_ATTR_KEYS]);
5300 if (IS_ERR(connkeys))
5301 return PTR_ERR(connkeys);
5302 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005303
Antonio Quartulli267335d2012-01-31 20:25:47 +01005304 ibss.control_port =
5305 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5306
Johannes Berg4c476992010-10-04 21:36:35 +02005307 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005308 if (err)
5309 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005310 return err;
5311}
5312
5313static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5314{
Johannes Berg4c476992010-10-04 21:36:35 +02005315 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5316 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005317
Johannes Berg4c476992010-10-04 21:36:35 +02005318 if (!rdev->ops->leave_ibss)
5319 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005320
Johannes Berg4c476992010-10-04 21:36:35 +02005321 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5322 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005323
Johannes Berg4c476992010-10-04 21:36:35 +02005324 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005325}
5326
Johannes Bergaff89a92009-07-01 21:26:51 +02005327#ifdef CONFIG_NL80211_TESTMODE
5328static struct genl_multicast_group nl80211_testmode_mcgrp = {
5329 .name = "testmode",
5330};
5331
5332static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5333{
Johannes Berg4c476992010-10-04 21:36:35 +02005334 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005335 int err;
5336
5337 if (!info->attrs[NL80211_ATTR_TESTDATA])
5338 return -EINVAL;
5339
Johannes Bergaff89a92009-07-01 21:26:51 +02005340 err = -EOPNOTSUPP;
5341 if (rdev->ops->testmode_cmd) {
5342 rdev->testmode_info = info;
5343 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5344 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5345 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5346 rdev->testmode_info = NULL;
5347 }
5348
Johannes Bergaff89a92009-07-01 21:26:51 +02005349 return err;
5350}
5351
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005352static int nl80211_testmode_dump(struct sk_buff *skb,
5353 struct netlink_callback *cb)
5354{
Johannes Berg00918d32011-12-13 17:22:05 +01005355 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005356 int err;
5357 long phy_idx;
5358 void *data = NULL;
5359 int data_len = 0;
5360
5361 if (cb->args[0]) {
5362 /*
5363 * 0 is a valid index, but not valid for args[0],
5364 * so we need to offset by 1.
5365 */
5366 phy_idx = cb->args[0] - 1;
5367 } else {
5368 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5369 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5370 nl80211_policy);
5371 if (err)
5372 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005373
Johannes Berg2bd7e352012-06-15 14:23:16 +02005374 mutex_lock(&cfg80211_mutex);
5375 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5376 nl80211_fam.attrbuf);
5377 if (IS_ERR(rdev)) {
5378 mutex_unlock(&cfg80211_mutex);
5379 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005380 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005381 phy_idx = rdev->wiphy_idx;
5382 rdev = NULL;
5383 mutex_unlock(&cfg80211_mutex);
5384
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005385 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5386 cb->args[1] =
5387 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5388 }
5389
5390 if (cb->args[1]) {
5391 data = nla_data((void *)cb->args[1]);
5392 data_len = nla_len((void *)cb->args[1]);
5393 }
5394
5395 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005396 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5397 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005398 mutex_unlock(&cfg80211_mutex);
5399 return -ENOENT;
5400 }
Johannes Berg00918d32011-12-13 17:22:05 +01005401 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005402 mutex_unlock(&cfg80211_mutex);
5403
Johannes Berg00918d32011-12-13 17:22:05 +01005404 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005405 err = -EOPNOTSUPP;
5406 goto out_err;
5407 }
5408
5409 while (1) {
5410 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5411 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5412 NL80211_CMD_TESTMODE);
5413 struct nlattr *tmdata;
5414
David S. Miller9360ffd2012-03-29 04:41:26 -04005415 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005416 genlmsg_cancel(skb, hdr);
5417 break;
5418 }
5419
5420 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5421 if (!tmdata) {
5422 genlmsg_cancel(skb, hdr);
5423 break;
5424 }
Johannes Berg00918d32011-12-13 17:22:05 +01005425 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5426 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005427 nla_nest_end(skb, tmdata);
5428
5429 if (err == -ENOBUFS || err == -ENOENT) {
5430 genlmsg_cancel(skb, hdr);
5431 break;
5432 } else if (err) {
5433 genlmsg_cancel(skb, hdr);
5434 goto out_err;
5435 }
5436
5437 genlmsg_end(skb, hdr);
5438 }
5439
5440 err = skb->len;
5441 /* see above */
5442 cb->args[0] = phy_idx + 1;
5443 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005444 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005445 return err;
5446}
5447
Johannes Bergaff89a92009-07-01 21:26:51 +02005448static struct sk_buff *
5449__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5450 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5451{
5452 struct sk_buff *skb;
5453 void *hdr;
5454 struct nlattr *data;
5455
5456 skb = nlmsg_new(approxlen + 100, gfp);
5457 if (!skb)
5458 return NULL;
5459
5460 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5461 if (!hdr) {
5462 kfree_skb(skb);
5463 return NULL;
5464 }
5465
David S. Miller9360ffd2012-03-29 04:41:26 -04005466 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5467 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005468 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5469
5470 ((void **)skb->cb)[0] = rdev;
5471 ((void **)skb->cb)[1] = hdr;
5472 ((void **)skb->cb)[2] = data;
5473
5474 return skb;
5475
5476 nla_put_failure:
5477 kfree_skb(skb);
5478 return NULL;
5479}
5480
5481struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5482 int approxlen)
5483{
5484 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5485
5486 if (WARN_ON(!rdev->testmode_info))
5487 return NULL;
5488
5489 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5490 rdev->testmode_info->snd_pid,
5491 rdev->testmode_info->snd_seq,
5492 GFP_KERNEL);
5493}
5494EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5495
5496int cfg80211_testmode_reply(struct sk_buff *skb)
5497{
5498 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5499 void *hdr = ((void **)skb->cb)[1];
5500 struct nlattr *data = ((void **)skb->cb)[2];
5501
5502 if (WARN_ON(!rdev->testmode_info)) {
5503 kfree_skb(skb);
5504 return -EINVAL;
5505 }
5506
5507 nla_nest_end(skb, data);
5508 genlmsg_end(skb, hdr);
5509 return genlmsg_reply(skb, rdev->testmode_info);
5510}
5511EXPORT_SYMBOL(cfg80211_testmode_reply);
5512
5513struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5514 int approxlen, gfp_t gfp)
5515{
5516 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5517
5518 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5519}
5520EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5521
5522void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5523{
5524 void *hdr = ((void **)skb->cb)[1];
5525 struct nlattr *data = ((void **)skb->cb)[2];
5526
5527 nla_nest_end(skb, data);
5528 genlmsg_end(skb, hdr);
5529 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5530}
5531EXPORT_SYMBOL(cfg80211_testmode_event);
5532#endif
5533
Samuel Ortizb23aa672009-07-01 21:26:54 +02005534static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5535{
Johannes Berg4c476992010-10-04 21:36:35 +02005536 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5537 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005538 struct cfg80211_connect_params connect;
5539 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005540 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005541 int err;
5542
5543 memset(&connect, 0, sizeof(connect));
5544
5545 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5546 return -EINVAL;
5547
5548 if (!info->attrs[NL80211_ATTR_SSID] ||
5549 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5550 return -EINVAL;
5551
5552 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5553 connect.auth_type =
5554 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5555 if (!nl80211_valid_auth_type(connect.auth_type))
5556 return -EINVAL;
5557 } else
5558 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5559
5560 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5561
Johannes Bergc0692b82010-08-27 14:26:53 +03005562 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005563 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005564 if (err)
5565 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005566
Johannes Berg074ac8d2010-09-16 14:58:22 +02005567 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005568 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5569 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005570
Johannes Berg79c97e92009-07-07 03:56:12 +02005571 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005572
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305573 connect.bg_scan_period = -1;
5574 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5575 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5576 connect.bg_scan_period =
5577 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5578 }
5579
Samuel Ortizb23aa672009-07-01 21:26:54 +02005580 if (info->attrs[NL80211_ATTR_MAC])
5581 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5582 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5583 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5584
5585 if (info->attrs[NL80211_ATTR_IE]) {
5586 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5587 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5588 }
5589
5590 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5591 connect.channel =
5592 ieee80211_get_channel(wiphy,
5593 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5594 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005595 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5596 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005597 }
5598
Johannes Bergfffd0932009-07-08 14:22:54 +02005599 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5600 connkeys = nl80211_parse_connkeys(rdev,
5601 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005602 if (IS_ERR(connkeys))
5603 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005604 }
5605
Ben Greear7e7c8922011-11-18 11:31:59 -08005606 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5607 connect.flags |= ASSOC_REQ_DISABLE_HT;
5608
5609 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5610 memcpy(&connect.ht_capa_mask,
5611 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5612 sizeof(connect.ht_capa_mask));
5613
5614 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5615 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5616 return -EINVAL;
5617 memcpy(&connect.ht_capa,
5618 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5619 sizeof(connect.ht_capa));
5620 }
5621
Johannes Bergfffd0932009-07-08 14:22:54 +02005622 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005623 if (err)
5624 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005625 return err;
5626}
5627
5628static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5629{
Johannes Berg4c476992010-10-04 21:36:35 +02005630 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5631 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005632 u16 reason;
5633
5634 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5635 reason = WLAN_REASON_DEAUTH_LEAVING;
5636 else
5637 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5638
5639 if (reason == 0)
5640 return -EINVAL;
5641
Johannes Berg074ac8d2010-09-16 14:58:22 +02005642 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005643 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5644 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005645
Johannes Berg4c476992010-10-04 21:36:35 +02005646 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005647}
5648
Johannes Berg463d0182009-07-14 00:33:35 +02005649static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5650{
Johannes Berg4c476992010-10-04 21:36:35 +02005651 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005652 struct net *net;
5653 int err;
5654 u32 pid;
5655
5656 if (!info->attrs[NL80211_ATTR_PID])
5657 return -EINVAL;
5658
5659 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5660
Johannes Berg463d0182009-07-14 00:33:35 +02005661 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005662 if (IS_ERR(net))
5663 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005664
5665 err = 0;
5666
5667 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005668 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5669 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005670
Johannes Berg463d0182009-07-14 00:33:35 +02005671 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005672 return err;
5673}
5674
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005675static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5676{
Johannes Berg4c476992010-10-04 21:36:35 +02005677 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005678 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5679 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005680 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005681 struct cfg80211_pmksa pmksa;
5682
5683 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5684
5685 if (!info->attrs[NL80211_ATTR_MAC])
5686 return -EINVAL;
5687
5688 if (!info->attrs[NL80211_ATTR_PMKID])
5689 return -EINVAL;
5690
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005691 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5692 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5693
Johannes Berg074ac8d2010-09-16 14:58:22 +02005694 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005695 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5696 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005697
5698 switch (info->genlhdr->cmd) {
5699 case NL80211_CMD_SET_PMKSA:
5700 rdev_ops = rdev->ops->set_pmksa;
5701 break;
5702 case NL80211_CMD_DEL_PMKSA:
5703 rdev_ops = rdev->ops->del_pmksa;
5704 break;
5705 default:
5706 WARN_ON(1);
5707 break;
5708 }
5709
Johannes Berg4c476992010-10-04 21:36:35 +02005710 if (!rdev_ops)
5711 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005712
Johannes Berg4c476992010-10-04 21:36:35 +02005713 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005714}
5715
5716static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5717{
Johannes Berg4c476992010-10-04 21:36:35 +02005718 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5719 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005720
Johannes Berg074ac8d2010-09-16 14:58:22 +02005721 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005722 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5723 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005724
Johannes Berg4c476992010-10-04 21:36:35 +02005725 if (!rdev->ops->flush_pmksa)
5726 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005727
Johannes Berg4c476992010-10-04 21:36:35 +02005728 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005729}
5730
Arik Nemtsov109086c2011-09-28 14:12:50 +03005731static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5732{
5733 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5734 struct net_device *dev = info->user_ptr[1];
5735 u8 action_code, dialog_token;
5736 u16 status_code;
5737 u8 *peer;
5738
5739 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5740 !rdev->ops->tdls_mgmt)
5741 return -EOPNOTSUPP;
5742
5743 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5744 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5745 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5746 !info->attrs[NL80211_ATTR_IE] ||
5747 !info->attrs[NL80211_ATTR_MAC])
5748 return -EINVAL;
5749
5750 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5751 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5752 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5753 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5754
5755 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5756 dialog_token, status_code,
5757 nla_data(info->attrs[NL80211_ATTR_IE]),
5758 nla_len(info->attrs[NL80211_ATTR_IE]));
5759}
5760
5761static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5762{
5763 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5764 struct net_device *dev = info->user_ptr[1];
5765 enum nl80211_tdls_operation operation;
5766 u8 *peer;
5767
5768 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5769 !rdev->ops->tdls_oper)
5770 return -EOPNOTSUPP;
5771
5772 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5773 !info->attrs[NL80211_ATTR_MAC])
5774 return -EINVAL;
5775
5776 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5777 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5778
5779 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5780}
5781
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005782static int nl80211_remain_on_channel(struct sk_buff *skb,
5783 struct genl_info *info)
5784{
Johannes Berg4c476992010-10-04 21:36:35 +02005785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005786 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005787 struct ieee80211_channel *chan;
5788 struct sk_buff *msg;
5789 void *hdr;
5790 u64 cookie;
5791 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5792 u32 freq, duration;
5793 int err;
5794
5795 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5796 !info->attrs[NL80211_ATTR_DURATION])
5797 return -EINVAL;
5798
5799 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5800
Johannes Berg7c4ef712011-11-18 15:33:48 +01005801 if (!rdev->ops->remain_on_channel ||
5802 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005803 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005804
Johannes Bergebf348f2012-06-01 12:50:54 +02005805 /*
5806 * We should be on that channel for at least a minimum amount of
5807 * time (10ms) but no longer than the driver supports.
5808 */
5809 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5810 duration > rdev->wiphy.max_remain_on_channel_duration)
5811 return -EINVAL;
5812
Johannes Bergcd6c6592012-05-10 21:27:18 +02005813 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5814 !nl80211_valid_channel_type(info, &channel_type))
5815 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005816
5817 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5818 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005819 if (chan == NULL)
5820 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005821
5822 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005823 if (!msg)
5824 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005825
5826 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5827 NL80211_CMD_REMAIN_ON_CHANNEL);
5828
5829 if (IS_ERR(hdr)) {
5830 err = PTR_ERR(hdr);
5831 goto free_msg;
5832 }
5833
Johannes Berg71bbc992012-06-15 15:30:18 +02005834 err = rdev->ops->remain_on_channel(&rdev->wiphy, wdev, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005835 channel_type, duration, &cookie);
5836
5837 if (err)
5838 goto free_msg;
5839
David S. Miller9360ffd2012-03-29 04:41:26 -04005840 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5841 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005842
5843 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005844
5845 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005846
5847 nla_put_failure:
5848 err = -ENOBUFS;
5849 free_msg:
5850 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005851 return err;
5852}
5853
5854static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5855 struct genl_info *info)
5856{
Johannes Berg4c476992010-10-04 21:36:35 +02005857 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005858 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005859 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005860
5861 if (!info->attrs[NL80211_ATTR_COOKIE])
5862 return -EINVAL;
5863
Johannes Berg4c476992010-10-04 21:36:35 +02005864 if (!rdev->ops->cancel_remain_on_channel)
5865 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005866
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005867 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5868
Johannes Berg71bbc992012-06-15 15:30:18 +02005869 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005870}
5871
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005872static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5873 u8 *rates, u8 rates_len)
5874{
5875 u8 i;
5876 u32 mask = 0;
5877
5878 for (i = 0; i < rates_len; i++) {
5879 int rate = (rates[i] & 0x7f) * 5;
5880 int ridx;
5881 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5882 struct ieee80211_rate *srate =
5883 &sband->bitrates[ridx];
5884 if (rate == srate->bitrate) {
5885 mask |= 1 << ridx;
5886 break;
5887 }
5888 }
5889 if (ridx == sband->n_bitrates)
5890 return 0; /* rate not found */
5891 }
5892
5893 return mask;
5894}
5895
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005896static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5897 u8 *rates, u8 rates_len,
5898 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5899{
5900 u8 i;
5901
5902 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5903
5904 for (i = 0; i < rates_len; i++) {
5905 int ridx, rbit;
5906
5907 ridx = rates[i] / 8;
5908 rbit = BIT(rates[i] % 8);
5909
5910 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005911 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005912 return false;
5913
5914 /* check availability */
5915 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5916 mcs[ridx] |= rbit;
5917 else
5918 return false;
5919 }
5920
5921 return true;
5922}
5923
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005924static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005925 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5926 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005927 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5928 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005929};
5930
5931static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5932 struct genl_info *info)
5933{
5934 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005935 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005936 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005937 int rem, i;
5938 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005939 struct nlattr *tx_rates;
5940 struct ieee80211_supported_band *sband;
5941
5942 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5943 return -EINVAL;
5944
Johannes Berg4c476992010-10-04 21:36:35 +02005945 if (!rdev->ops->set_bitrate_mask)
5946 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005947
5948 memset(&mask, 0, sizeof(mask));
5949 /* Default to all rates enabled */
5950 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5951 sband = rdev->wiphy.bands[i];
5952 mask.control[i].legacy =
5953 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005954 if (sband)
5955 memcpy(mask.control[i].mcs,
5956 sband->ht_cap.mcs.rx_mask,
5957 sizeof(mask.control[i].mcs));
5958 else
5959 memset(mask.control[i].mcs, 0,
5960 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005961 }
5962
5963 /*
5964 * The nested attribute uses enum nl80211_band as the index. This maps
5965 * directly to the enum ieee80211_band values used in cfg80211.
5966 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005967 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005968 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5969 {
5970 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005971 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5972 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005973 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005974 if (sband == NULL)
5975 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005976 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5977 nla_len(tx_rates), nl80211_txattr_policy);
5978 if (tb[NL80211_TXRATE_LEGACY]) {
5979 mask.control[band].legacy = rateset_to_mask(
5980 sband,
5981 nla_data(tb[NL80211_TXRATE_LEGACY]),
5982 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305983 if ((mask.control[band].legacy == 0) &&
5984 nla_len(tb[NL80211_TXRATE_LEGACY]))
5985 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005986 }
5987 if (tb[NL80211_TXRATE_MCS]) {
5988 if (!ht_rateset_to_mask(
5989 sband,
5990 nla_data(tb[NL80211_TXRATE_MCS]),
5991 nla_len(tb[NL80211_TXRATE_MCS]),
5992 mask.control[band].mcs))
5993 return -EINVAL;
5994 }
5995
5996 if (mask.control[band].legacy == 0) {
5997 /* don't allow empty legacy rates if HT
5998 * is not even supported. */
5999 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6000 return -EINVAL;
6001
6002 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6003 if (mask.control[band].mcs[i])
6004 break;
6005
6006 /* legacy and mcs rates may not be both empty */
6007 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006008 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006009 }
6010 }
6011
Johannes Berg4c476992010-10-04 21:36:35 +02006012 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006013}
6014
Johannes Berg2e161f72010-08-12 15:38:38 +02006015static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006016{
Johannes Berg4c476992010-10-04 21:36:35 +02006017 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006018 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006019 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006020
6021 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6022 return -EINVAL;
6023
Johannes Berg2e161f72010-08-12 15:38:38 +02006024 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6025 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006026
Johannes Berg71bbc992012-06-15 15:30:18 +02006027 switch (wdev->iftype) {
6028 case NL80211_IFTYPE_STATION:
6029 case NL80211_IFTYPE_ADHOC:
6030 case NL80211_IFTYPE_P2P_CLIENT:
6031 case NL80211_IFTYPE_AP:
6032 case NL80211_IFTYPE_AP_VLAN:
6033 case NL80211_IFTYPE_MESH_POINT:
6034 case NL80211_IFTYPE_P2P_GO:
6035 break;
6036 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006037 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006038 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006039
6040 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006041 if (!rdev->ops->mgmt_tx)
6042 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006043
Johannes Berg71bbc992012-06-15 15:30:18 +02006044 return cfg80211_mlme_register_mgmt(wdev, info->snd_pid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006045 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6046 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006047}
6048
Johannes Berg2e161f72010-08-12 15:38:38 +02006049static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006050{
Johannes Berg4c476992010-10-04 21:36:35 +02006051 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006052 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006053 struct ieee80211_channel *chan;
6054 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02006055 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02006056 u32 freq;
6057 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006058 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006059 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006060 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006061 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006062 bool offchan, no_cck, dont_wait_for_ack;
6063
6064 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006065
6066 if (!info->attrs[NL80211_ATTR_FRAME] ||
6067 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6068 return -EINVAL;
6069
Johannes Berg4c476992010-10-04 21:36:35 +02006070 if (!rdev->ops->mgmt_tx)
6071 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006072
Johannes Berg71bbc992012-06-15 15:30:18 +02006073 switch (wdev->iftype) {
6074 case NL80211_IFTYPE_STATION:
6075 case NL80211_IFTYPE_ADHOC:
6076 case NL80211_IFTYPE_P2P_CLIENT:
6077 case NL80211_IFTYPE_AP:
6078 case NL80211_IFTYPE_AP_VLAN:
6079 case NL80211_IFTYPE_MESH_POINT:
6080 case NL80211_IFTYPE_P2P_GO:
6081 break;
6082 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006083 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006084 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006085
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006086 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006087 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006088 return -EINVAL;
6089 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006090
6091 /*
6092 * We should wait on the channel for at least a minimum amount
6093 * of time (10ms) but no longer than the driver supports.
6094 */
6095 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6096 wait > rdev->wiphy.max_remain_on_channel_duration)
6097 return -EINVAL;
6098
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006099 }
6100
Jouni Malinen026331c2010-02-15 12:53:10 +02006101 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02006102 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02006103 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02006104 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02006105 }
6106
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006107 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6108
Johannes Berg7c4ef712011-11-18 15:33:48 +01006109 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6110 return -EINVAL;
6111
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306112 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6113
Jouni Malinen026331c2010-02-15 12:53:10 +02006114 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6115 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006116 if (chan == NULL)
6117 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006118
Johannes Berge247bd902011-11-04 11:18:21 +01006119 if (!dont_wait_for_ack) {
6120 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6121 if (!msg)
6122 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006123
Johannes Berge247bd902011-11-04 11:18:21 +01006124 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6125 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006126
Johannes Berge247bd902011-11-04 11:18:21 +01006127 if (IS_ERR(hdr)) {
6128 err = PTR_ERR(hdr);
6129 goto free_msg;
6130 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006131 }
Johannes Berge247bd902011-11-04 11:18:21 +01006132
Johannes Berg71bbc992012-06-15 15:30:18 +02006133 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, channel_type,
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006134 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006135 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6136 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006137 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006138 if (err)
6139 goto free_msg;
6140
Johannes Berge247bd902011-11-04 11:18:21 +01006141 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006142 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6143 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006144
Johannes Berge247bd902011-11-04 11:18:21 +01006145 genlmsg_end(msg, hdr);
6146 return genlmsg_reply(msg, info);
6147 }
6148
6149 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006150
6151 nla_put_failure:
6152 err = -ENOBUFS;
6153 free_msg:
6154 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006155 return err;
6156}
6157
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006158static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6159{
6160 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006161 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006162 u64 cookie;
6163
6164 if (!info->attrs[NL80211_ATTR_COOKIE])
6165 return -EINVAL;
6166
6167 if (!rdev->ops->mgmt_tx_cancel_wait)
6168 return -EOPNOTSUPP;
6169
Johannes Berg71bbc992012-06-15 15:30:18 +02006170 switch (wdev->iftype) {
6171 case NL80211_IFTYPE_STATION:
6172 case NL80211_IFTYPE_ADHOC:
6173 case NL80211_IFTYPE_P2P_CLIENT:
6174 case NL80211_IFTYPE_AP:
6175 case NL80211_IFTYPE_AP_VLAN:
6176 case NL80211_IFTYPE_P2P_GO:
6177 break;
6178 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006179 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006180 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006181
6182 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6183
Johannes Berg71bbc992012-06-15 15:30:18 +02006184 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006185}
6186
Kalle Valoffb9eb32010-02-17 17:58:10 +02006187static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6188{
Johannes Berg4c476992010-10-04 21:36:35 +02006189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006190 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006191 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006192 u8 ps_state;
6193 bool state;
6194 int err;
6195
Johannes Berg4c476992010-10-04 21:36:35 +02006196 if (!info->attrs[NL80211_ATTR_PS_STATE])
6197 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006198
6199 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6200
Johannes Berg4c476992010-10-04 21:36:35 +02006201 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6202 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006203
6204 wdev = dev->ieee80211_ptr;
6205
Johannes Berg4c476992010-10-04 21:36:35 +02006206 if (!rdev->ops->set_power_mgmt)
6207 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006208
6209 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6210
6211 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006212 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006213
Johannes Berg4c476992010-10-04 21:36:35 +02006214 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
6215 wdev->ps_timeout);
6216 if (!err)
6217 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006218 return err;
6219}
6220
6221static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6222{
Johannes Berg4c476992010-10-04 21:36:35 +02006223 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006224 enum nl80211_ps_state ps_state;
6225 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006226 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006227 struct sk_buff *msg;
6228 void *hdr;
6229 int err;
6230
Kalle Valoffb9eb32010-02-17 17:58:10 +02006231 wdev = dev->ieee80211_ptr;
6232
Johannes Berg4c476992010-10-04 21:36:35 +02006233 if (!rdev->ops->set_power_mgmt)
6234 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006235
6236 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006237 if (!msg)
6238 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006239
6240 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6241 NL80211_CMD_GET_POWER_SAVE);
6242 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006243 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006244 goto free_msg;
6245 }
6246
6247 if (wdev->ps)
6248 ps_state = NL80211_PS_ENABLED;
6249 else
6250 ps_state = NL80211_PS_DISABLED;
6251
David S. Miller9360ffd2012-03-29 04:41:26 -04006252 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6253 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006254
6255 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006256 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006257
Johannes Berg4c476992010-10-04 21:36:35 +02006258 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006259 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006260 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006261 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006262 return err;
6263}
6264
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006265static struct nla_policy
6266nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6267 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6268 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6269 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006270 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6271 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6272 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006273};
6274
Thomas Pedersen84f10702012-07-12 16:17:33 -07006275static int nl80211_set_cqm_txe(struct genl_info *info,
6276 u32 rate, u32 pkts, u32 intvl)
6277{
6278 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6279 struct wireless_dev *wdev;
6280 struct net_device *dev = info->user_ptr[1];
6281
6282 if ((rate < 0 || rate > 100) ||
6283 (intvl < 0 || intvl > NL80211_CQM_TXE_MAX_INTVL))
6284 return -EINVAL;
6285
6286 wdev = dev->ieee80211_ptr;
6287
6288 if (!rdev->ops->set_cqm_txe_config)
6289 return -EOPNOTSUPP;
6290
6291 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6292 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6293 return -EOPNOTSUPP;
6294
6295 return rdev->ops->set_cqm_txe_config(wdev->wiphy, dev,
6296 rate, pkts, intvl);
6297}
6298
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006299static int nl80211_set_cqm_rssi(struct genl_info *info,
6300 s32 threshold, u32 hysteresis)
6301{
Johannes Berg4c476992010-10-04 21:36:35 +02006302 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006303 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006304 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006305
6306 if (threshold > 0)
6307 return -EINVAL;
6308
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006309 wdev = dev->ieee80211_ptr;
6310
Johannes Berg4c476992010-10-04 21:36:35 +02006311 if (!rdev->ops->set_cqm_rssi_config)
6312 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006313
Johannes Berg074ac8d2010-09-16 14:58:22 +02006314 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006315 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6316 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006317
Johannes Berg4c476992010-10-04 21:36:35 +02006318 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6319 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006320}
6321
6322static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6323{
6324 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6325 struct nlattr *cqm;
6326 int err;
6327
6328 cqm = info->attrs[NL80211_ATTR_CQM];
6329 if (!cqm) {
6330 err = -EINVAL;
6331 goto out;
6332 }
6333
6334 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6335 nl80211_attr_cqm_policy);
6336 if (err)
6337 goto out;
6338
6339 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6340 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6341 s32 threshold;
6342 u32 hysteresis;
6343 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6344 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6345 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006346 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6347 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6348 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6349 u32 rate, pkts, intvl;
6350 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6351 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6352 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6353 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006354 } else
6355 err = -EINVAL;
6356
6357out:
6358 return err;
6359}
6360
Johannes Berg29cbe682010-12-03 09:20:44 +01006361static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6362{
6363 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6364 struct net_device *dev = info->user_ptr[1];
6365 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006366 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006367 int err;
6368
6369 /* start with default */
6370 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006371 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006372
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006373 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006374 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006375 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006376 if (err)
6377 return err;
6378 }
6379
6380 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6381 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6382 return -EINVAL;
6383
Javier Cardonac80d5452010-12-16 17:37:49 -08006384 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6385 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6386
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006387 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6388 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6389 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6390 return -EINVAL;
6391
Javier Cardonac80d5452010-12-16 17:37:49 -08006392 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6393 /* parse additional setup parameters if given */
6394 err = nl80211_parse_mesh_setup(info, &setup);
6395 if (err)
6396 return err;
6397 }
6398
Johannes Bergcc1d2802012-05-16 23:50:20 +02006399 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6400 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6401
6402 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6403 !nl80211_valid_channel_type(info, &channel_type))
6404 return -EINVAL;
6405
6406 setup.channel = rdev_freq_to_chan(rdev,
6407 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6408 channel_type);
6409 if (!setup.channel)
6410 return -EINVAL;
6411 setup.channel_type = channel_type;
6412 } else {
6413 /* cfg80211_join_mesh() will sort it out */
6414 setup.channel = NULL;
6415 }
6416
Javier Cardonac80d5452010-12-16 17:37:49 -08006417 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006418}
6419
6420static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6421{
6422 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6423 struct net_device *dev = info->user_ptr[1];
6424
6425 return cfg80211_leave_mesh(rdev, dev);
6426}
6427
Johannes Bergdfb89c52012-06-27 09:23:48 +02006428#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006429static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6430{
6431 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6432 struct sk_buff *msg;
6433 void *hdr;
6434
6435 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6436 return -EOPNOTSUPP;
6437
6438 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6439 if (!msg)
6440 return -ENOMEM;
6441
6442 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6443 NL80211_CMD_GET_WOWLAN);
6444 if (!hdr)
6445 goto nla_put_failure;
6446
6447 if (rdev->wowlan) {
6448 struct nlattr *nl_wowlan;
6449
6450 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6451 if (!nl_wowlan)
6452 goto nla_put_failure;
6453
David S. Miller9360ffd2012-03-29 04:41:26 -04006454 if ((rdev->wowlan->any &&
6455 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6456 (rdev->wowlan->disconnect &&
6457 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6458 (rdev->wowlan->magic_pkt &&
6459 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6460 (rdev->wowlan->gtk_rekey_failure &&
6461 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6462 (rdev->wowlan->eap_identity_req &&
6463 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6464 (rdev->wowlan->four_way_handshake &&
6465 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6466 (rdev->wowlan->rfkill_release &&
6467 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6468 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006469 if (rdev->wowlan->n_patterns) {
6470 struct nlattr *nl_pats, *nl_pat;
6471 int i, pat_len;
6472
6473 nl_pats = nla_nest_start(msg,
6474 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6475 if (!nl_pats)
6476 goto nla_put_failure;
6477
6478 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6479 nl_pat = nla_nest_start(msg, i + 1);
6480 if (!nl_pat)
6481 goto nla_put_failure;
6482 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006483 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6484 DIV_ROUND_UP(pat_len, 8),
6485 rdev->wowlan->patterns[i].mask) ||
6486 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6487 pat_len,
6488 rdev->wowlan->patterns[i].pattern))
6489 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006490 nla_nest_end(msg, nl_pat);
6491 }
6492 nla_nest_end(msg, nl_pats);
6493 }
6494
6495 nla_nest_end(msg, nl_wowlan);
6496 }
6497
6498 genlmsg_end(msg, hdr);
6499 return genlmsg_reply(msg, info);
6500
6501nla_put_failure:
6502 nlmsg_free(msg);
6503 return -ENOBUFS;
6504}
6505
6506static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6507{
6508 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6509 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006510 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006511 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006512 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6513 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006514 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006515
6516 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6517 return -EOPNOTSUPP;
6518
Johannes Bergae33bd82012-07-12 16:25:02 +02006519 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6520 cfg80211_rdev_free_wowlan(rdev);
6521 rdev->wowlan = NULL;
6522 goto set_wakeup;
6523 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006524
6525 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6526 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6527 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6528 nl80211_wowlan_policy);
6529 if (err)
6530 return err;
6531
6532 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6533 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6534 return -EINVAL;
6535 new_triggers.any = true;
6536 }
6537
6538 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6539 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6540 return -EINVAL;
6541 new_triggers.disconnect = true;
6542 }
6543
6544 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6545 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6546 return -EINVAL;
6547 new_triggers.magic_pkt = true;
6548 }
6549
Johannes Berg77dbbb12011-07-13 10:48:55 +02006550 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6551 return -EINVAL;
6552
6553 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6554 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6555 return -EINVAL;
6556 new_triggers.gtk_rekey_failure = true;
6557 }
6558
6559 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6560 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6561 return -EINVAL;
6562 new_triggers.eap_identity_req = true;
6563 }
6564
6565 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6566 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6567 return -EINVAL;
6568 new_triggers.four_way_handshake = true;
6569 }
6570
6571 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6572 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6573 return -EINVAL;
6574 new_triggers.rfkill_release = true;
6575 }
6576
Johannes Bergff1b6e62011-05-04 15:37:28 +02006577 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6578 struct nlattr *pat;
6579 int n_patterns = 0;
6580 int rem, pat_len, mask_len;
6581 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6582
6583 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6584 rem)
6585 n_patterns++;
6586 if (n_patterns > wowlan->n_patterns)
6587 return -EINVAL;
6588
6589 new_triggers.patterns = kcalloc(n_patterns,
6590 sizeof(new_triggers.patterns[0]),
6591 GFP_KERNEL);
6592 if (!new_triggers.patterns)
6593 return -ENOMEM;
6594
6595 new_triggers.n_patterns = n_patterns;
6596 i = 0;
6597
6598 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6599 rem) {
6600 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6601 nla_data(pat), nla_len(pat), NULL);
6602 err = -EINVAL;
6603 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6604 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6605 goto error;
6606 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6607 mask_len = DIV_ROUND_UP(pat_len, 8);
6608 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6609 mask_len)
6610 goto error;
6611 if (pat_len > wowlan->pattern_max_len ||
6612 pat_len < wowlan->pattern_min_len)
6613 goto error;
6614
6615 new_triggers.patterns[i].mask =
6616 kmalloc(mask_len + pat_len, GFP_KERNEL);
6617 if (!new_triggers.patterns[i].mask) {
6618 err = -ENOMEM;
6619 goto error;
6620 }
6621 new_triggers.patterns[i].pattern =
6622 new_triggers.patterns[i].mask + mask_len;
6623 memcpy(new_triggers.patterns[i].mask,
6624 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6625 mask_len);
6626 new_triggers.patterns[i].pattern_len = pat_len;
6627 memcpy(new_triggers.patterns[i].pattern,
6628 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6629 pat_len);
6630 i++;
6631 }
6632 }
6633
Johannes Bergae33bd82012-07-12 16:25:02 +02006634 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6635 if (!ntrig) {
6636 err = -ENOMEM;
6637 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006638 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006639 cfg80211_rdev_free_wowlan(rdev);
6640 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006641
Johannes Bergae33bd82012-07-12 16:25:02 +02006642 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006643 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6644 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6645
Johannes Bergff1b6e62011-05-04 15:37:28 +02006646 return 0;
6647 error:
6648 for (i = 0; i < new_triggers.n_patterns; i++)
6649 kfree(new_triggers.patterns[i].mask);
6650 kfree(new_triggers.patterns);
6651 return err;
6652}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006653#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006654
Johannes Berge5497d72011-07-05 16:35:40 +02006655static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6656{
6657 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6658 struct net_device *dev = info->user_ptr[1];
6659 struct wireless_dev *wdev = dev->ieee80211_ptr;
6660 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6661 struct cfg80211_gtk_rekey_data rekey_data;
6662 int err;
6663
6664 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6665 return -EINVAL;
6666
6667 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6668 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6669 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6670 nl80211_rekey_policy);
6671 if (err)
6672 return err;
6673
6674 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6675 return -ERANGE;
6676 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6677 return -ERANGE;
6678 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6679 return -ERANGE;
6680
6681 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6682 NL80211_KEK_LEN);
6683 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6684 NL80211_KCK_LEN);
6685 memcpy(rekey_data.replay_ctr,
6686 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6687 NL80211_REPLAY_CTR_LEN);
6688
6689 wdev_lock(wdev);
6690 if (!wdev->current_bss) {
6691 err = -ENOTCONN;
6692 goto out;
6693 }
6694
6695 if (!rdev->ops->set_rekey_data) {
6696 err = -EOPNOTSUPP;
6697 goto out;
6698 }
6699
6700 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6701 out:
6702 wdev_unlock(wdev);
6703 return err;
6704}
6705
Johannes Berg28946da2011-11-04 11:18:12 +01006706static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6707 struct genl_info *info)
6708{
6709 struct net_device *dev = info->user_ptr[1];
6710 struct wireless_dev *wdev = dev->ieee80211_ptr;
6711
6712 if (wdev->iftype != NL80211_IFTYPE_AP &&
6713 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6714 return -EINVAL;
6715
6716 if (wdev->ap_unexpected_nlpid)
6717 return -EBUSY;
6718
6719 wdev->ap_unexpected_nlpid = info->snd_pid;
6720 return 0;
6721}
6722
Johannes Berg7f6cf312011-11-04 11:18:15 +01006723static int nl80211_probe_client(struct sk_buff *skb,
6724 struct genl_info *info)
6725{
6726 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6727 struct net_device *dev = info->user_ptr[1];
6728 struct wireless_dev *wdev = dev->ieee80211_ptr;
6729 struct sk_buff *msg;
6730 void *hdr;
6731 const u8 *addr;
6732 u64 cookie;
6733 int err;
6734
6735 if (wdev->iftype != NL80211_IFTYPE_AP &&
6736 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6737 return -EOPNOTSUPP;
6738
6739 if (!info->attrs[NL80211_ATTR_MAC])
6740 return -EINVAL;
6741
6742 if (!rdev->ops->probe_client)
6743 return -EOPNOTSUPP;
6744
6745 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6746 if (!msg)
6747 return -ENOMEM;
6748
6749 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6750 NL80211_CMD_PROBE_CLIENT);
6751
6752 if (IS_ERR(hdr)) {
6753 err = PTR_ERR(hdr);
6754 goto free_msg;
6755 }
6756
6757 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6758
6759 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6760 if (err)
6761 goto free_msg;
6762
David S. Miller9360ffd2012-03-29 04:41:26 -04006763 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6764 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006765
6766 genlmsg_end(msg, hdr);
6767
6768 return genlmsg_reply(msg, info);
6769
6770 nla_put_failure:
6771 err = -ENOBUFS;
6772 free_msg:
6773 nlmsg_free(msg);
6774 return err;
6775}
6776
Johannes Berg5e760232011-11-04 11:18:17 +01006777static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6778{
6779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6780
6781 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6782 return -EOPNOTSUPP;
6783
6784 if (rdev->ap_beacons_nlpid)
6785 return -EBUSY;
6786
6787 rdev->ap_beacons_nlpid = info->snd_pid;
6788
6789 return 0;
6790}
6791
Johannes Berg4c476992010-10-04 21:36:35 +02006792#define NL80211_FLAG_NEED_WIPHY 0x01
6793#define NL80211_FLAG_NEED_NETDEV 0x02
6794#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006795#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6796#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6797 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02006798#define NL80211_FLAG_NEED_WDEV 0x10
6799/* If a netdev is associated, it must be UP */
6800#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
6801 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006802
6803static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6804 struct genl_info *info)
6805{
6806 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02006807 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006808 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02006809 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6810
6811 if (rtnl)
6812 rtnl_lock();
6813
6814 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02006815 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02006816 if (IS_ERR(rdev)) {
6817 if (rtnl)
6818 rtnl_unlock();
6819 return PTR_ERR(rdev);
6820 }
6821 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02006822 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
6823 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02006824 mutex_lock(&cfg80211_mutex);
6825 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
6826 info->attrs);
6827 if (IS_ERR(wdev)) {
6828 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02006829 if (rtnl)
6830 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02006831 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02006832 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006833
Johannes Berg89a54e42012-06-15 14:33:17 +02006834 dev = wdev->netdev;
6835 rdev = wiphy_to_dev(wdev->wiphy);
6836
Johannes Berg1bf614e2012-06-15 15:23:36 +02006837 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6838 if (!dev) {
6839 mutex_unlock(&cfg80211_mutex);
6840 if (rtnl)
6841 rtnl_unlock();
6842 return -EINVAL;
6843 }
6844
6845 info->user_ptr[1] = dev;
6846 } else {
6847 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02006848 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006849
Johannes Berg1bf614e2012-06-15 15:23:36 +02006850 if (dev) {
6851 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6852 !netif_running(dev)) {
6853 mutex_unlock(&cfg80211_mutex);
6854 if (rtnl)
6855 rtnl_unlock();
6856 return -ENETDOWN;
6857 }
6858
6859 dev_hold(dev);
6860 }
6861
Johannes Berg89a54e42012-06-15 14:33:17 +02006862 cfg80211_lock_rdev(rdev);
6863
6864 mutex_unlock(&cfg80211_mutex);
6865
Johannes Berg4c476992010-10-04 21:36:35 +02006866 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006867 }
6868
6869 return 0;
6870}
6871
6872static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6873 struct genl_info *info)
6874{
6875 if (info->user_ptr[0])
6876 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02006877 if (info->user_ptr[1]) {
6878 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
6879 struct wireless_dev *wdev = info->user_ptr[1];
6880
6881 if (wdev->netdev)
6882 dev_put(wdev->netdev);
6883 } else {
6884 dev_put(info->user_ptr[1]);
6885 }
6886 }
Johannes Berg4c476992010-10-04 21:36:35 +02006887 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6888 rtnl_unlock();
6889}
6890
Johannes Berg55682962007-09-20 13:09:35 -04006891static struct genl_ops nl80211_ops[] = {
6892 {
6893 .cmd = NL80211_CMD_GET_WIPHY,
6894 .doit = nl80211_get_wiphy,
6895 .dumpit = nl80211_dump_wiphy,
6896 .policy = nl80211_policy,
6897 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006898 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006899 },
6900 {
6901 .cmd = NL80211_CMD_SET_WIPHY,
6902 .doit = nl80211_set_wiphy,
6903 .policy = nl80211_policy,
6904 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006905 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006906 },
6907 {
6908 .cmd = NL80211_CMD_GET_INTERFACE,
6909 .doit = nl80211_get_interface,
6910 .dumpit = nl80211_dump_interface,
6911 .policy = nl80211_policy,
6912 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02006913 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006914 },
6915 {
6916 .cmd = NL80211_CMD_SET_INTERFACE,
6917 .doit = nl80211_set_interface,
6918 .policy = nl80211_policy,
6919 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006920 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6921 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006922 },
6923 {
6924 .cmd = NL80211_CMD_NEW_INTERFACE,
6925 .doit = nl80211_new_interface,
6926 .policy = nl80211_policy,
6927 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006928 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6929 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006930 },
6931 {
6932 .cmd = NL80211_CMD_DEL_INTERFACE,
6933 .doit = nl80211_del_interface,
6934 .policy = nl80211_policy,
6935 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02006936 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02006937 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006938 },
Johannes Berg41ade002007-12-19 02:03:29 +01006939 {
6940 .cmd = NL80211_CMD_GET_KEY,
6941 .doit = nl80211_get_key,
6942 .policy = nl80211_policy,
6943 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006944 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006945 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006946 },
6947 {
6948 .cmd = NL80211_CMD_SET_KEY,
6949 .doit = nl80211_set_key,
6950 .policy = nl80211_policy,
6951 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006952 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006953 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006954 },
6955 {
6956 .cmd = NL80211_CMD_NEW_KEY,
6957 .doit = nl80211_new_key,
6958 .policy = nl80211_policy,
6959 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006960 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006961 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006962 },
6963 {
6964 .cmd = NL80211_CMD_DEL_KEY,
6965 .doit = nl80211_del_key,
6966 .policy = nl80211_policy,
6967 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006968 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006969 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006970 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006971 {
6972 .cmd = NL80211_CMD_SET_BEACON,
6973 .policy = nl80211_policy,
6974 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006975 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006976 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006977 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006978 },
6979 {
Johannes Berg88600202012-02-13 15:17:18 +01006980 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006981 .policy = nl80211_policy,
6982 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006983 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006984 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006985 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006986 },
6987 {
Johannes Berg88600202012-02-13 15:17:18 +01006988 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006989 .policy = nl80211_policy,
6990 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006991 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006992 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006993 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006994 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006995 {
6996 .cmd = NL80211_CMD_GET_STATION,
6997 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006998 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006999 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007000 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7001 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007002 },
7003 {
7004 .cmd = NL80211_CMD_SET_STATION,
7005 .doit = nl80211_set_station,
7006 .policy = nl80211_policy,
7007 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007008 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007009 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007010 },
7011 {
7012 .cmd = NL80211_CMD_NEW_STATION,
7013 .doit = nl80211_new_station,
7014 .policy = nl80211_policy,
7015 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007016 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007017 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007018 },
7019 {
7020 .cmd = NL80211_CMD_DEL_STATION,
7021 .doit = nl80211_del_station,
7022 .policy = nl80211_policy,
7023 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007024 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007025 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007026 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007027 {
7028 .cmd = NL80211_CMD_GET_MPATH,
7029 .doit = nl80211_get_mpath,
7030 .dumpit = nl80211_dump_mpath,
7031 .policy = nl80211_policy,
7032 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007033 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007034 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007035 },
7036 {
7037 .cmd = NL80211_CMD_SET_MPATH,
7038 .doit = nl80211_set_mpath,
7039 .policy = nl80211_policy,
7040 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007041 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007042 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007043 },
7044 {
7045 .cmd = NL80211_CMD_NEW_MPATH,
7046 .doit = nl80211_new_mpath,
7047 .policy = nl80211_policy,
7048 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007049 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007050 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007051 },
7052 {
7053 .cmd = NL80211_CMD_DEL_MPATH,
7054 .doit = nl80211_del_mpath,
7055 .policy = nl80211_policy,
7056 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007057 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007058 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007059 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007060 {
7061 .cmd = NL80211_CMD_SET_BSS,
7062 .doit = nl80211_set_bss,
7063 .policy = nl80211_policy,
7064 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007065 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007066 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007067 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007068 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007069 .cmd = NL80211_CMD_GET_REG,
7070 .doit = nl80211_get_reg,
7071 .policy = nl80211_policy,
7072 /* can be retrieved by unprivileged users */
7073 },
7074 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007075 .cmd = NL80211_CMD_SET_REG,
7076 .doit = nl80211_set_reg,
7077 .policy = nl80211_policy,
7078 .flags = GENL_ADMIN_PERM,
7079 },
7080 {
7081 .cmd = NL80211_CMD_REQ_SET_REG,
7082 .doit = nl80211_req_set_reg,
7083 .policy = nl80211_policy,
7084 .flags = GENL_ADMIN_PERM,
7085 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007086 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007087 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7088 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007089 .policy = nl80211_policy,
7090 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007091 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007092 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007093 },
7094 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007095 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7096 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007097 .policy = nl80211_policy,
7098 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007099 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007100 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007101 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007102 {
Johannes Berg2a519312009-02-10 21:25:55 +01007103 .cmd = NL80211_CMD_TRIGGER_SCAN,
7104 .doit = nl80211_trigger_scan,
7105 .policy = nl80211_policy,
7106 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007107 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007108 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007109 },
7110 {
7111 .cmd = NL80211_CMD_GET_SCAN,
7112 .policy = nl80211_policy,
7113 .dumpit = nl80211_dump_scan,
7114 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007115 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007116 .cmd = NL80211_CMD_START_SCHED_SCAN,
7117 .doit = nl80211_start_sched_scan,
7118 .policy = nl80211_policy,
7119 .flags = GENL_ADMIN_PERM,
7120 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7121 NL80211_FLAG_NEED_RTNL,
7122 },
7123 {
7124 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7125 .doit = nl80211_stop_sched_scan,
7126 .policy = nl80211_policy,
7127 .flags = GENL_ADMIN_PERM,
7128 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7129 NL80211_FLAG_NEED_RTNL,
7130 },
7131 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007132 .cmd = NL80211_CMD_AUTHENTICATE,
7133 .doit = nl80211_authenticate,
7134 .policy = nl80211_policy,
7135 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007136 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007137 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007138 },
7139 {
7140 .cmd = NL80211_CMD_ASSOCIATE,
7141 .doit = nl80211_associate,
7142 .policy = nl80211_policy,
7143 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007144 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007145 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007146 },
7147 {
7148 .cmd = NL80211_CMD_DEAUTHENTICATE,
7149 .doit = nl80211_deauthenticate,
7150 .policy = nl80211_policy,
7151 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007152 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007153 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007154 },
7155 {
7156 .cmd = NL80211_CMD_DISASSOCIATE,
7157 .doit = nl80211_disassociate,
7158 .policy = nl80211_policy,
7159 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007160 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007161 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007162 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007163 {
7164 .cmd = NL80211_CMD_JOIN_IBSS,
7165 .doit = nl80211_join_ibss,
7166 .policy = nl80211_policy,
7167 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007168 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007169 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007170 },
7171 {
7172 .cmd = NL80211_CMD_LEAVE_IBSS,
7173 .doit = nl80211_leave_ibss,
7174 .policy = nl80211_policy,
7175 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007176 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007177 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007178 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007179#ifdef CONFIG_NL80211_TESTMODE
7180 {
7181 .cmd = NL80211_CMD_TESTMODE,
7182 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007183 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007184 .policy = nl80211_policy,
7185 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007186 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7187 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007188 },
7189#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007190 {
7191 .cmd = NL80211_CMD_CONNECT,
7192 .doit = nl80211_connect,
7193 .policy = nl80211_policy,
7194 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007195 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007196 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007197 },
7198 {
7199 .cmd = NL80211_CMD_DISCONNECT,
7200 .doit = nl80211_disconnect,
7201 .policy = nl80211_policy,
7202 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007203 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007204 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007205 },
Johannes Berg463d0182009-07-14 00:33:35 +02007206 {
7207 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7208 .doit = nl80211_wiphy_netns,
7209 .policy = nl80211_policy,
7210 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007211 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7212 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007213 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007214 {
7215 .cmd = NL80211_CMD_GET_SURVEY,
7216 .policy = nl80211_policy,
7217 .dumpit = nl80211_dump_survey,
7218 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007219 {
7220 .cmd = NL80211_CMD_SET_PMKSA,
7221 .doit = nl80211_setdel_pmksa,
7222 .policy = nl80211_policy,
7223 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007224 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007225 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007226 },
7227 {
7228 .cmd = NL80211_CMD_DEL_PMKSA,
7229 .doit = nl80211_setdel_pmksa,
7230 .policy = nl80211_policy,
7231 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007232 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007233 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007234 },
7235 {
7236 .cmd = NL80211_CMD_FLUSH_PMKSA,
7237 .doit = nl80211_flush_pmksa,
7238 .policy = nl80211_policy,
7239 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007240 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007241 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007242 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007243 {
7244 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7245 .doit = nl80211_remain_on_channel,
7246 .policy = nl80211_policy,
7247 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007248 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007249 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007250 },
7251 {
7252 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7253 .doit = nl80211_cancel_remain_on_channel,
7254 .policy = nl80211_policy,
7255 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007256 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007257 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007258 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007259 {
7260 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7261 .doit = nl80211_set_tx_bitrate_mask,
7262 .policy = nl80211_policy,
7263 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007264 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7265 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007266 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007267 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007268 .cmd = NL80211_CMD_REGISTER_FRAME,
7269 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007270 .policy = nl80211_policy,
7271 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007272 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007273 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007274 },
7275 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007276 .cmd = NL80211_CMD_FRAME,
7277 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007278 .policy = nl80211_policy,
7279 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007280 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007281 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007282 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007283 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007284 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7285 .doit = nl80211_tx_mgmt_cancel_wait,
7286 .policy = nl80211_policy,
7287 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007288 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007289 NL80211_FLAG_NEED_RTNL,
7290 },
7291 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007292 .cmd = NL80211_CMD_SET_POWER_SAVE,
7293 .doit = nl80211_set_power_save,
7294 .policy = nl80211_policy,
7295 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007296 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7297 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007298 },
7299 {
7300 .cmd = NL80211_CMD_GET_POWER_SAVE,
7301 .doit = nl80211_get_power_save,
7302 .policy = nl80211_policy,
7303 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007304 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7305 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007306 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007307 {
7308 .cmd = NL80211_CMD_SET_CQM,
7309 .doit = nl80211_set_cqm,
7310 .policy = nl80211_policy,
7311 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007312 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7313 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007314 },
Johannes Bergf444de02010-05-05 15:25:02 +02007315 {
7316 .cmd = NL80211_CMD_SET_CHANNEL,
7317 .doit = nl80211_set_channel,
7318 .policy = nl80211_policy,
7319 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007320 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7321 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007322 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007323 {
7324 .cmd = NL80211_CMD_SET_WDS_PEER,
7325 .doit = nl80211_set_wds_peer,
7326 .policy = nl80211_policy,
7327 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007328 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7329 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007330 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007331 {
7332 .cmd = NL80211_CMD_JOIN_MESH,
7333 .doit = nl80211_join_mesh,
7334 .policy = nl80211_policy,
7335 .flags = GENL_ADMIN_PERM,
7336 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7337 NL80211_FLAG_NEED_RTNL,
7338 },
7339 {
7340 .cmd = NL80211_CMD_LEAVE_MESH,
7341 .doit = nl80211_leave_mesh,
7342 .policy = nl80211_policy,
7343 .flags = GENL_ADMIN_PERM,
7344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7345 NL80211_FLAG_NEED_RTNL,
7346 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007347#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007348 {
7349 .cmd = NL80211_CMD_GET_WOWLAN,
7350 .doit = nl80211_get_wowlan,
7351 .policy = nl80211_policy,
7352 /* can be retrieved by unprivileged users */
7353 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7354 NL80211_FLAG_NEED_RTNL,
7355 },
7356 {
7357 .cmd = NL80211_CMD_SET_WOWLAN,
7358 .doit = nl80211_set_wowlan,
7359 .policy = nl80211_policy,
7360 .flags = GENL_ADMIN_PERM,
7361 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7362 NL80211_FLAG_NEED_RTNL,
7363 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007364#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007365 {
7366 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7367 .doit = nl80211_set_rekey_data,
7368 .policy = nl80211_policy,
7369 .flags = GENL_ADMIN_PERM,
7370 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7371 NL80211_FLAG_NEED_RTNL,
7372 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007373 {
7374 .cmd = NL80211_CMD_TDLS_MGMT,
7375 .doit = nl80211_tdls_mgmt,
7376 .policy = nl80211_policy,
7377 .flags = GENL_ADMIN_PERM,
7378 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7379 NL80211_FLAG_NEED_RTNL,
7380 },
7381 {
7382 .cmd = NL80211_CMD_TDLS_OPER,
7383 .doit = nl80211_tdls_oper,
7384 .policy = nl80211_policy,
7385 .flags = GENL_ADMIN_PERM,
7386 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7387 NL80211_FLAG_NEED_RTNL,
7388 },
Johannes Berg28946da2011-11-04 11:18:12 +01007389 {
7390 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7391 .doit = nl80211_register_unexpected_frame,
7392 .policy = nl80211_policy,
7393 .flags = GENL_ADMIN_PERM,
7394 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7395 NL80211_FLAG_NEED_RTNL,
7396 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007397 {
7398 .cmd = NL80211_CMD_PROBE_CLIENT,
7399 .doit = nl80211_probe_client,
7400 .policy = nl80211_policy,
7401 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007402 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007403 NL80211_FLAG_NEED_RTNL,
7404 },
Johannes Berg5e760232011-11-04 11:18:17 +01007405 {
7406 .cmd = NL80211_CMD_REGISTER_BEACONS,
7407 .doit = nl80211_register_beacons,
7408 .policy = nl80211_policy,
7409 .flags = GENL_ADMIN_PERM,
7410 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7411 NL80211_FLAG_NEED_RTNL,
7412 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007413 {
7414 .cmd = NL80211_CMD_SET_NOACK_MAP,
7415 .doit = nl80211_set_noack_map,
7416 .policy = nl80211_policy,
7417 .flags = GENL_ADMIN_PERM,
7418 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7419 NL80211_FLAG_NEED_RTNL,
7420 },
7421
Johannes Berg55682962007-09-20 13:09:35 -04007422};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007423
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007424static struct genl_multicast_group nl80211_mlme_mcgrp = {
7425 .name = "mlme",
7426};
Johannes Berg55682962007-09-20 13:09:35 -04007427
7428/* multicast groups */
7429static struct genl_multicast_group nl80211_config_mcgrp = {
7430 .name = "config",
7431};
Johannes Berg2a519312009-02-10 21:25:55 +01007432static struct genl_multicast_group nl80211_scan_mcgrp = {
7433 .name = "scan",
7434};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007435static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7436 .name = "regulatory",
7437};
Johannes Berg55682962007-09-20 13:09:35 -04007438
7439/* notification functions */
7440
7441void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7442{
7443 struct sk_buff *msg;
7444
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007445 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007446 if (!msg)
7447 return;
7448
7449 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7450 nlmsg_free(msg);
7451 return;
7452 }
7453
Johannes Berg463d0182009-07-14 00:33:35 +02007454 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7455 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007456}
7457
Johannes Berg362a4152009-05-24 16:43:15 +02007458static int nl80211_add_scan_req(struct sk_buff *msg,
7459 struct cfg80211_registered_device *rdev)
7460{
7461 struct cfg80211_scan_request *req = rdev->scan_req;
7462 struct nlattr *nest;
7463 int i;
7464
Johannes Berg667503dd2009-07-07 03:56:11 +02007465 ASSERT_RDEV_LOCK(rdev);
7466
Johannes Berg362a4152009-05-24 16:43:15 +02007467 if (WARN_ON(!req))
7468 return 0;
7469
7470 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7471 if (!nest)
7472 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007473 for (i = 0; i < req->n_ssids; i++) {
7474 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7475 goto nla_put_failure;
7476 }
Johannes Berg362a4152009-05-24 16:43:15 +02007477 nla_nest_end(msg, nest);
7478
7479 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7480 if (!nest)
7481 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007482 for (i = 0; i < req->n_channels; i++) {
7483 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7484 goto nla_put_failure;
7485 }
Johannes Berg362a4152009-05-24 16:43:15 +02007486 nla_nest_end(msg, nest);
7487
David S. Miller9360ffd2012-03-29 04:41:26 -04007488 if (req->ie &&
7489 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7490 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007491
7492 return 0;
7493 nla_put_failure:
7494 return -ENOBUFS;
7495}
7496
Johannes Berga538e2d2009-06-16 19:56:42 +02007497static int nl80211_send_scan_msg(struct sk_buff *msg,
7498 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007499 struct wireless_dev *wdev,
Johannes Berga538e2d2009-06-16 19:56:42 +02007500 u32 pid, u32 seq, int flags,
7501 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007502{
7503 void *hdr;
7504
7505 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7506 if (!hdr)
7507 return -1;
7508
David S. Miller9360ffd2012-03-29 04:41:26 -04007509 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007510 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7511 wdev->netdev->ifindex)) ||
7512 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007513 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007514
Johannes Berg362a4152009-05-24 16:43:15 +02007515 /* ignore errors and send incomplete event anyway */
7516 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007517
7518 return genlmsg_end(msg, hdr);
7519
7520 nla_put_failure:
7521 genlmsg_cancel(msg, hdr);
7522 return -EMSGSIZE;
7523}
7524
Luciano Coelho807f8a82011-05-11 17:09:35 +03007525static int
7526nl80211_send_sched_scan_msg(struct sk_buff *msg,
7527 struct cfg80211_registered_device *rdev,
7528 struct net_device *netdev,
7529 u32 pid, u32 seq, int flags, u32 cmd)
7530{
7531 void *hdr;
7532
7533 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7534 if (!hdr)
7535 return -1;
7536
David S. Miller9360ffd2012-03-29 04:41:26 -04007537 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7538 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7539 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007540
7541 return genlmsg_end(msg, hdr);
7542
7543 nla_put_failure:
7544 genlmsg_cancel(msg, hdr);
7545 return -EMSGSIZE;
7546}
7547
Johannes Berga538e2d2009-06-16 19:56:42 +02007548void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007549 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007550{
7551 struct sk_buff *msg;
7552
7553 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7554 if (!msg)
7555 return;
7556
Johannes Bergfd014282012-06-18 19:17:03 +02007557 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007558 NL80211_CMD_TRIGGER_SCAN) < 0) {
7559 nlmsg_free(msg);
7560 return;
7561 }
7562
Johannes Berg463d0182009-07-14 00:33:35 +02007563 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7564 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007565}
7566
Johannes Berg2a519312009-02-10 21:25:55 +01007567void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007568 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007569{
7570 struct sk_buff *msg;
7571
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007572 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007573 if (!msg)
7574 return;
7575
Johannes Bergfd014282012-06-18 19:17:03 +02007576 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007577 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007578 nlmsg_free(msg);
7579 return;
7580 }
7581
Johannes Berg463d0182009-07-14 00:33:35 +02007582 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7583 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007584}
7585
7586void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007587 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007588{
7589 struct sk_buff *msg;
7590
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007591 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007592 if (!msg)
7593 return;
7594
Johannes Bergfd014282012-06-18 19:17:03 +02007595 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007596 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007597 nlmsg_free(msg);
7598 return;
7599 }
7600
Johannes Berg463d0182009-07-14 00:33:35 +02007601 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7602 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007603}
7604
Luciano Coelho807f8a82011-05-11 17:09:35 +03007605void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7606 struct net_device *netdev)
7607{
7608 struct sk_buff *msg;
7609
7610 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7611 if (!msg)
7612 return;
7613
7614 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7615 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7616 nlmsg_free(msg);
7617 return;
7618 }
7619
7620 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7621 nl80211_scan_mcgrp.id, GFP_KERNEL);
7622}
7623
7624void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7625 struct net_device *netdev, u32 cmd)
7626{
7627 struct sk_buff *msg;
7628
7629 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7630 if (!msg)
7631 return;
7632
7633 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7634 nlmsg_free(msg);
7635 return;
7636 }
7637
7638 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7639 nl80211_scan_mcgrp.id, GFP_KERNEL);
7640}
7641
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007642/*
7643 * This can happen on global regulatory changes or device specific settings
7644 * based on custom world regulatory domains.
7645 */
7646void nl80211_send_reg_change_event(struct regulatory_request *request)
7647{
7648 struct sk_buff *msg;
7649 void *hdr;
7650
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007651 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007652 if (!msg)
7653 return;
7654
7655 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7656 if (!hdr) {
7657 nlmsg_free(msg);
7658 return;
7659 }
7660
7661 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007662 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7663 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007664
David S. Miller9360ffd2012-03-29 04:41:26 -04007665 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7666 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7667 NL80211_REGDOM_TYPE_WORLD))
7668 goto nla_put_failure;
7669 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7670 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7671 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7672 goto nla_put_failure;
7673 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7674 request->intersect) {
7675 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7676 NL80211_REGDOM_TYPE_INTERSECTION))
7677 goto nla_put_failure;
7678 } else {
7679 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7680 NL80211_REGDOM_TYPE_COUNTRY) ||
7681 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7682 request->alpha2))
7683 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007684 }
7685
David S. Miller9360ffd2012-03-29 04:41:26 -04007686 if (wiphy_idx_valid(request->wiphy_idx) &&
7687 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7688 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007689
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007690 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007691
Johannes Bergbc43b282009-07-25 10:54:13 +02007692 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007693 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007694 GFP_ATOMIC);
7695 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007696
7697 return;
7698
7699nla_put_failure:
7700 genlmsg_cancel(msg, hdr);
7701 nlmsg_free(msg);
7702}
7703
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007704static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7705 struct net_device *netdev,
7706 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007707 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007708{
7709 struct sk_buff *msg;
7710 void *hdr;
7711
Johannes Berge6d6e342009-07-01 21:26:47 +02007712 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007713 if (!msg)
7714 return;
7715
7716 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7717 if (!hdr) {
7718 nlmsg_free(msg);
7719 return;
7720 }
7721
David S. Miller9360ffd2012-03-29 04:41:26 -04007722 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7723 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7724 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7725 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007726
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007727 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007728
Johannes Berg463d0182009-07-14 00:33:35 +02007729 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7730 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007731 return;
7732
7733 nla_put_failure:
7734 genlmsg_cancel(msg, hdr);
7735 nlmsg_free(msg);
7736}
7737
7738void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007739 struct net_device *netdev, const u8 *buf,
7740 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007741{
7742 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007743 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007744}
7745
7746void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7747 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007748 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007749{
Johannes Berge6d6e342009-07-01 21:26:47 +02007750 nl80211_send_mlme_event(rdev, netdev, buf, len,
7751 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007752}
7753
Jouni Malinen53b46b82009-03-27 20:53:56 +02007754void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007755 struct net_device *netdev, const u8 *buf,
7756 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007757{
7758 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007759 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007760}
7761
Jouni Malinen53b46b82009-03-27 20:53:56 +02007762void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7763 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007764 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007765{
7766 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007767 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007768}
7769
Jouni Malinencf4e5942010-12-16 00:52:40 +02007770void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7771 struct net_device *netdev, const u8 *buf,
7772 size_t len, gfp_t gfp)
7773{
7774 nl80211_send_mlme_event(rdev, netdev, buf, len,
7775 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7776}
7777
7778void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7779 struct net_device *netdev, const u8 *buf,
7780 size_t len, gfp_t gfp)
7781{
7782 nl80211_send_mlme_event(rdev, netdev, buf, len,
7783 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7784}
7785
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007786static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7787 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007788 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007789{
7790 struct sk_buff *msg;
7791 void *hdr;
7792
Johannes Berge6d6e342009-07-01 21:26:47 +02007793 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007794 if (!msg)
7795 return;
7796
7797 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7798 if (!hdr) {
7799 nlmsg_free(msg);
7800 return;
7801 }
7802
David S. Miller9360ffd2012-03-29 04:41:26 -04007803 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7804 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7805 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7806 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7807 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007808
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007809 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007810
Johannes Berg463d0182009-07-14 00:33:35 +02007811 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7812 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007813 return;
7814
7815 nla_put_failure:
7816 genlmsg_cancel(msg, hdr);
7817 nlmsg_free(msg);
7818}
7819
7820void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007821 struct net_device *netdev, const u8 *addr,
7822 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007823{
7824 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007825 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007826}
7827
7828void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007829 struct net_device *netdev, const u8 *addr,
7830 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007831{
Johannes Berge6d6e342009-07-01 21:26:47 +02007832 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7833 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007834}
7835
Samuel Ortizb23aa672009-07-01 21:26:54 +02007836void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7837 struct net_device *netdev, const u8 *bssid,
7838 const u8 *req_ie, size_t req_ie_len,
7839 const u8 *resp_ie, size_t resp_ie_len,
7840 u16 status, gfp_t gfp)
7841{
7842 struct sk_buff *msg;
7843 void *hdr;
7844
7845 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7846 if (!msg)
7847 return;
7848
7849 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7850 if (!hdr) {
7851 nlmsg_free(msg);
7852 return;
7853 }
7854
David S. Miller9360ffd2012-03-29 04:41:26 -04007855 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7856 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7857 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7858 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7859 (req_ie &&
7860 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7861 (resp_ie &&
7862 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7863 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007864
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007865 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007866
Johannes Berg463d0182009-07-14 00:33:35 +02007867 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7868 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007869 return;
7870
7871 nla_put_failure:
7872 genlmsg_cancel(msg, hdr);
7873 nlmsg_free(msg);
7874
7875}
7876
7877void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7878 struct net_device *netdev, const u8 *bssid,
7879 const u8 *req_ie, size_t req_ie_len,
7880 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7881{
7882 struct sk_buff *msg;
7883 void *hdr;
7884
7885 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7886 if (!msg)
7887 return;
7888
7889 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7890 if (!hdr) {
7891 nlmsg_free(msg);
7892 return;
7893 }
7894
David S. Miller9360ffd2012-03-29 04:41:26 -04007895 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7896 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7897 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7898 (req_ie &&
7899 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7900 (resp_ie &&
7901 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7902 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007903
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007904 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007905
Johannes Berg463d0182009-07-14 00:33:35 +02007906 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7907 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007908 return;
7909
7910 nla_put_failure:
7911 genlmsg_cancel(msg, hdr);
7912 nlmsg_free(msg);
7913
7914}
7915
7916void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7917 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007918 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007919{
7920 struct sk_buff *msg;
7921 void *hdr;
7922
Johannes Berg667503dd2009-07-07 03:56:11 +02007923 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007924 if (!msg)
7925 return;
7926
7927 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7928 if (!hdr) {
7929 nlmsg_free(msg);
7930 return;
7931 }
7932
David S. Miller9360ffd2012-03-29 04:41:26 -04007933 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7934 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7935 (from_ap && reason &&
7936 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7937 (from_ap &&
7938 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7939 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7940 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007941
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007942 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007943
Johannes Berg463d0182009-07-14 00:33:35 +02007944 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7945 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007946 return;
7947
7948 nla_put_failure:
7949 genlmsg_cancel(msg, hdr);
7950 nlmsg_free(msg);
7951
7952}
7953
Johannes Berg04a773a2009-04-19 21:24:32 +02007954void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7955 struct net_device *netdev, const u8 *bssid,
7956 gfp_t gfp)
7957{
7958 struct sk_buff *msg;
7959 void *hdr;
7960
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007961 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007962 if (!msg)
7963 return;
7964
7965 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7966 if (!hdr) {
7967 nlmsg_free(msg);
7968 return;
7969 }
7970
David S. Miller9360ffd2012-03-29 04:41:26 -04007971 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7972 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7973 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7974 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007975
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007976 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007977
Johannes Berg463d0182009-07-14 00:33:35 +02007978 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7979 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007980 return;
7981
7982 nla_put_failure:
7983 genlmsg_cancel(msg, hdr);
7984 nlmsg_free(msg);
7985}
7986
Javier Cardonac93b5e72011-04-07 15:08:34 -07007987void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7988 struct net_device *netdev,
7989 const u8 *macaddr, const u8* ie, u8 ie_len,
7990 gfp_t gfp)
7991{
7992 struct sk_buff *msg;
7993 void *hdr;
7994
7995 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7996 if (!msg)
7997 return;
7998
7999 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8000 if (!hdr) {
8001 nlmsg_free(msg);
8002 return;
8003 }
8004
David S. Miller9360ffd2012-03-29 04:41:26 -04008005 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8006 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8007 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8008 (ie_len && ie &&
8009 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8010 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008011
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008012 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008013
8014 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8015 nl80211_mlme_mcgrp.id, gfp);
8016 return;
8017
8018 nla_put_failure:
8019 genlmsg_cancel(msg, hdr);
8020 nlmsg_free(msg);
8021}
8022
Jouni Malinena3b8b052009-03-27 21:59:49 +02008023void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8024 struct net_device *netdev, const u8 *addr,
8025 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008026 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008027{
8028 struct sk_buff *msg;
8029 void *hdr;
8030
Johannes Berge6d6e342009-07-01 21:26:47 +02008031 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008032 if (!msg)
8033 return;
8034
8035 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8036 if (!hdr) {
8037 nlmsg_free(msg);
8038 return;
8039 }
8040
David S. Miller9360ffd2012-03-29 04:41:26 -04008041 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8042 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8043 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8044 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8045 (key_id != -1 &&
8046 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8047 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8048 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008049
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008050 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008051
Johannes Berg463d0182009-07-14 00:33:35 +02008052 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8053 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008054 return;
8055
8056 nla_put_failure:
8057 genlmsg_cancel(msg, hdr);
8058 nlmsg_free(msg);
8059}
8060
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008061void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8062 struct ieee80211_channel *channel_before,
8063 struct ieee80211_channel *channel_after)
8064{
8065 struct sk_buff *msg;
8066 void *hdr;
8067 struct nlattr *nl_freq;
8068
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008069 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008070 if (!msg)
8071 return;
8072
8073 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8074 if (!hdr) {
8075 nlmsg_free(msg);
8076 return;
8077 }
8078
8079 /*
8080 * Since we are applying the beacon hint to a wiphy we know its
8081 * wiphy_idx is valid
8082 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008083 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8084 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008085
8086 /* Before */
8087 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8088 if (!nl_freq)
8089 goto nla_put_failure;
8090 if (nl80211_msg_put_channel(msg, channel_before))
8091 goto nla_put_failure;
8092 nla_nest_end(msg, nl_freq);
8093
8094 /* After */
8095 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8096 if (!nl_freq)
8097 goto nla_put_failure;
8098 if (nl80211_msg_put_channel(msg, channel_after))
8099 goto nla_put_failure;
8100 nla_nest_end(msg, nl_freq);
8101
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008102 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008103
Johannes Berg463d0182009-07-14 00:33:35 +02008104 rcu_read_lock();
8105 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8106 GFP_ATOMIC);
8107 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008108
8109 return;
8110
8111nla_put_failure:
8112 genlmsg_cancel(msg, hdr);
8113 nlmsg_free(msg);
8114}
8115
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008116static void nl80211_send_remain_on_chan_event(
8117 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008118 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008119 struct ieee80211_channel *chan,
8120 enum nl80211_channel_type channel_type,
8121 unsigned int duration, gfp_t gfp)
8122{
8123 struct sk_buff *msg;
8124 void *hdr;
8125
8126 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8127 if (!msg)
8128 return;
8129
8130 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8131 if (!hdr) {
8132 nlmsg_free(msg);
8133 return;
8134 }
8135
David S. Miller9360ffd2012-03-29 04:41:26 -04008136 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008137 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8138 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008139 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008140 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8141 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8142 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8143 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008144
David S. Miller9360ffd2012-03-29 04:41:26 -04008145 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8146 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8147 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008148
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008149 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008150
8151 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8152 nl80211_mlme_mcgrp.id, gfp);
8153 return;
8154
8155 nla_put_failure:
8156 genlmsg_cancel(msg, hdr);
8157 nlmsg_free(msg);
8158}
8159
8160void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008161 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008162 struct ieee80211_channel *chan,
8163 enum nl80211_channel_type channel_type,
8164 unsigned int duration, gfp_t gfp)
8165{
8166 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008167 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008168 channel_type, duration, gfp);
8169}
8170
8171void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008172 struct cfg80211_registered_device *rdev,
8173 struct wireless_dev *wdev,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008174 u64 cookie, struct ieee80211_channel *chan,
8175 enum nl80211_channel_type channel_type, gfp_t gfp)
8176{
8177 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008178 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008179 channel_type, 0, gfp);
8180}
8181
Johannes Berg98b62182009-12-23 13:15:44 +01008182void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8183 struct net_device *dev, const u8 *mac_addr,
8184 struct station_info *sinfo, gfp_t gfp)
8185{
8186 struct sk_buff *msg;
8187
8188 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8189 if (!msg)
8190 return;
8191
John W. Linville66266b32012-03-15 13:25:41 -04008192 if (nl80211_send_station(msg, 0, 0, 0,
8193 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008194 nlmsg_free(msg);
8195 return;
8196 }
8197
8198 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8199 nl80211_mlme_mcgrp.id, gfp);
8200}
8201
Jouni Malinenec15e682011-03-23 15:29:52 +02008202void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8203 struct net_device *dev, const u8 *mac_addr,
8204 gfp_t gfp)
8205{
8206 struct sk_buff *msg;
8207 void *hdr;
8208
8209 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8210 if (!msg)
8211 return;
8212
8213 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8214 if (!hdr) {
8215 nlmsg_free(msg);
8216 return;
8217 }
8218
David S. Miller9360ffd2012-03-29 04:41:26 -04008219 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8220 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8221 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008222
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008223 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008224
8225 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8226 nl80211_mlme_mcgrp.id, gfp);
8227 return;
8228
8229 nla_put_failure:
8230 genlmsg_cancel(msg, hdr);
8231 nlmsg_free(msg);
8232}
8233
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008234static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8235 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008236{
8237 struct wireless_dev *wdev = dev->ieee80211_ptr;
8238 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8239 struct sk_buff *msg;
8240 void *hdr;
8241 int err;
8242 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
8243
8244 if (!nlpid)
8245 return false;
8246
8247 msg = nlmsg_new(100, gfp);
8248 if (!msg)
8249 return true;
8250
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008251 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008252 if (!hdr) {
8253 nlmsg_free(msg);
8254 return true;
8255 }
8256
David S. Miller9360ffd2012-03-29 04:41:26 -04008257 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8258 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8259 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8260 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008261
8262 err = genlmsg_end(msg, hdr);
8263 if (err < 0) {
8264 nlmsg_free(msg);
8265 return true;
8266 }
8267
8268 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8269 return true;
8270
8271 nla_put_failure:
8272 genlmsg_cancel(msg, hdr);
8273 nlmsg_free(msg);
8274 return true;
8275}
8276
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008277bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8278{
8279 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8280 addr, gfp);
8281}
8282
8283bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8284 const u8 *addr, gfp_t gfp)
8285{
8286 return __nl80211_unexpected_frame(dev,
8287 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8288 addr, gfp);
8289}
8290
Johannes Berg2e161f72010-08-12 15:38:38 +02008291int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008292 struct wireless_dev *wdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01008293 int freq, int sig_dbm,
8294 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008295{
Johannes Berg71bbc992012-06-15 15:30:18 +02008296 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008297 struct sk_buff *msg;
8298 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008299
8300 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8301 if (!msg)
8302 return -ENOMEM;
8303
Johannes Berg2e161f72010-08-12 15:38:38 +02008304 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008305 if (!hdr) {
8306 nlmsg_free(msg);
8307 return -ENOMEM;
8308 }
8309
David S. Miller9360ffd2012-03-29 04:41:26 -04008310 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008311 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8312 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008313 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8314 (sig_dbm &&
8315 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8316 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8317 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008318
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008319 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008320
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008321 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008322
8323 nla_put_failure:
8324 genlmsg_cancel(msg, hdr);
8325 nlmsg_free(msg);
8326 return -ENOBUFS;
8327}
8328
Johannes Berg2e161f72010-08-12 15:38:38 +02008329void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008330 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008331 const u8 *buf, size_t len, bool ack,
8332 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008333{
Johannes Berg71bbc992012-06-15 15:30:18 +02008334 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008335 struct sk_buff *msg;
8336 void *hdr;
8337
8338 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8339 if (!msg)
8340 return;
8341
Johannes Berg2e161f72010-08-12 15:38:38 +02008342 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008343 if (!hdr) {
8344 nlmsg_free(msg);
8345 return;
8346 }
8347
David S. Miller9360ffd2012-03-29 04:41:26 -04008348 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008349 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8350 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008351 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8352 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8353 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8354 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008355
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008356 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008357
8358 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8359 return;
8360
8361 nla_put_failure:
8362 genlmsg_cancel(msg, hdr);
8363 nlmsg_free(msg);
8364}
8365
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008366void
8367nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8368 struct net_device *netdev,
8369 enum nl80211_cqm_rssi_threshold_event rssi_event,
8370 gfp_t gfp)
8371{
8372 struct sk_buff *msg;
8373 struct nlattr *pinfoattr;
8374 void *hdr;
8375
8376 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8377 if (!msg)
8378 return;
8379
8380 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8381 if (!hdr) {
8382 nlmsg_free(msg);
8383 return;
8384 }
8385
David S. Miller9360ffd2012-03-29 04:41:26 -04008386 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8387 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8388 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008389
8390 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8391 if (!pinfoattr)
8392 goto nla_put_failure;
8393
David S. Miller9360ffd2012-03-29 04:41:26 -04008394 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8395 rssi_event))
8396 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008397
8398 nla_nest_end(msg, pinfoattr);
8399
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008400 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008401
8402 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8403 nl80211_mlme_mcgrp.id, gfp);
8404 return;
8405
8406 nla_put_failure:
8407 genlmsg_cancel(msg, hdr);
8408 nlmsg_free(msg);
8409}
8410
Johannes Berge5497d72011-07-05 16:35:40 +02008411void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8412 struct net_device *netdev, const u8 *bssid,
8413 const u8 *replay_ctr, gfp_t gfp)
8414{
8415 struct sk_buff *msg;
8416 struct nlattr *rekey_attr;
8417 void *hdr;
8418
8419 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8420 if (!msg)
8421 return;
8422
8423 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8424 if (!hdr) {
8425 nlmsg_free(msg);
8426 return;
8427 }
8428
David S. Miller9360ffd2012-03-29 04:41:26 -04008429 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8430 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8431 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8432 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008433
8434 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8435 if (!rekey_attr)
8436 goto nla_put_failure;
8437
David S. Miller9360ffd2012-03-29 04:41:26 -04008438 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8439 NL80211_REPLAY_CTR_LEN, replay_ctr))
8440 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008441
8442 nla_nest_end(msg, rekey_attr);
8443
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008444 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008445
8446 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8447 nl80211_mlme_mcgrp.id, gfp);
8448 return;
8449
8450 nla_put_failure:
8451 genlmsg_cancel(msg, hdr);
8452 nlmsg_free(msg);
8453}
8454
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008455void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8456 struct net_device *netdev, int index,
8457 const u8 *bssid, bool preauth, gfp_t gfp)
8458{
8459 struct sk_buff *msg;
8460 struct nlattr *attr;
8461 void *hdr;
8462
8463 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8464 if (!msg)
8465 return;
8466
8467 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8468 if (!hdr) {
8469 nlmsg_free(msg);
8470 return;
8471 }
8472
David S. Miller9360ffd2012-03-29 04:41:26 -04008473 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8474 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8475 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008476
8477 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8478 if (!attr)
8479 goto nla_put_failure;
8480
David S. Miller9360ffd2012-03-29 04:41:26 -04008481 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8482 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8483 (preauth &&
8484 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8485 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008486
8487 nla_nest_end(msg, attr);
8488
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008489 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008490
8491 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8492 nl80211_mlme_mcgrp.id, gfp);
8493 return;
8494
8495 nla_put_failure:
8496 genlmsg_cancel(msg, hdr);
8497 nlmsg_free(msg);
8498}
8499
Thomas Pedersen53145262012-04-06 13:35:47 -07008500void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8501 struct net_device *netdev, int freq,
8502 enum nl80211_channel_type type, gfp_t gfp)
8503{
8504 struct sk_buff *msg;
8505 void *hdr;
8506
8507 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8508 if (!msg)
8509 return;
8510
8511 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8512 if (!hdr) {
8513 nlmsg_free(msg);
8514 return;
8515 }
8516
John W. Linville7eab0f62012-04-12 14:25:14 -04008517 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8518 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8519 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8520 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008521
8522 genlmsg_end(msg, hdr);
8523
8524 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8525 nl80211_mlme_mcgrp.id, gfp);
8526 return;
8527
8528 nla_put_failure:
8529 genlmsg_cancel(msg, hdr);
8530 nlmsg_free(msg);
8531}
8532
Johannes Bergc063dbf2010-11-24 08:10:05 +01008533void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008534nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8535 struct net_device *netdev, const u8 *peer,
8536 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8537{
8538 struct sk_buff *msg;
8539 struct nlattr *pinfoattr;
8540 void *hdr;
8541
8542 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8543 if (!msg)
8544 return;
8545
8546 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8547 if (!hdr) {
8548 nlmsg_free(msg);
8549 return;
8550 }
8551
8552 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8553 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8554 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8555 goto nla_put_failure;
8556
8557 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8558 if (!pinfoattr)
8559 goto nla_put_failure;
8560
8561 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8562 goto nla_put_failure;
8563
8564 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8565 goto nla_put_failure;
8566
8567 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8568 goto nla_put_failure;
8569
8570 nla_nest_end(msg, pinfoattr);
8571
8572 genlmsg_end(msg, hdr);
8573
8574 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8575 nl80211_mlme_mcgrp.id, gfp);
8576 return;
8577
8578 nla_put_failure:
8579 genlmsg_cancel(msg, hdr);
8580 nlmsg_free(msg);
8581}
8582
8583void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008584nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8585 struct net_device *netdev, const u8 *peer,
8586 u32 num_packets, gfp_t gfp)
8587{
8588 struct sk_buff *msg;
8589 struct nlattr *pinfoattr;
8590 void *hdr;
8591
8592 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8593 if (!msg)
8594 return;
8595
8596 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8597 if (!hdr) {
8598 nlmsg_free(msg);
8599 return;
8600 }
8601
David S. Miller9360ffd2012-03-29 04:41:26 -04008602 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8603 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8604 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8605 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008606
8607 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8608 if (!pinfoattr)
8609 goto nla_put_failure;
8610
David S. Miller9360ffd2012-03-29 04:41:26 -04008611 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8612 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008613
8614 nla_nest_end(msg, pinfoattr);
8615
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008616 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008617
8618 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8619 nl80211_mlme_mcgrp.id, gfp);
8620 return;
8621
8622 nla_put_failure:
8623 genlmsg_cancel(msg, hdr);
8624 nlmsg_free(msg);
8625}
8626
Johannes Berg7f6cf312011-11-04 11:18:15 +01008627void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8628 u64 cookie, bool acked, gfp_t gfp)
8629{
8630 struct wireless_dev *wdev = dev->ieee80211_ptr;
8631 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8632 struct sk_buff *msg;
8633 void *hdr;
8634 int err;
8635
8636 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8637 if (!msg)
8638 return;
8639
8640 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8641 if (!hdr) {
8642 nlmsg_free(msg);
8643 return;
8644 }
8645
David S. Miller9360ffd2012-03-29 04:41:26 -04008646 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8647 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8648 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8649 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8650 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8651 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008652
8653 err = genlmsg_end(msg, hdr);
8654 if (err < 0) {
8655 nlmsg_free(msg);
8656 return;
8657 }
8658
8659 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8660 nl80211_mlme_mcgrp.id, gfp);
8661 return;
8662
8663 nla_put_failure:
8664 genlmsg_cancel(msg, hdr);
8665 nlmsg_free(msg);
8666}
8667EXPORT_SYMBOL(cfg80211_probe_status);
8668
Johannes Berg5e760232011-11-04 11:18:17 +01008669void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8670 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008671 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008672{
8673 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8674 struct sk_buff *msg;
8675 void *hdr;
8676 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8677
8678 if (!nlpid)
8679 return;
8680
8681 msg = nlmsg_new(len + 100, gfp);
8682 if (!msg)
8683 return;
8684
8685 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8686 if (!hdr) {
8687 nlmsg_free(msg);
8688 return;
8689 }
8690
David S. Miller9360ffd2012-03-29 04:41:26 -04008691 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8692 (freq &&
8693 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8694 (sig_dbm &&
8695 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8696 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8697 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008698
8699 genlmsg_end(msg, hdr);
8700
8701 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8702 return;
8703
8704 nla_put_failure:
8705 genlmsg_cancel(msg, hdr);
8706 nlmsg_free(msg);
8707}
8708EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8709
Jouni Malinen026331c2010-02-15 12:53:10 +02008710static int nl80211_netlink_notify(struct notifier_block * nb,
8711 unsigned long state,
8712 void *_notify)
8713{
8714 struct netlink_notify *notify = _notify;
8715 struct cfg80211_registered_device *rdev;
8716 struct wireless_dev *wdev;
8717
8718 if (state != NETLINK_URELEASE)
8719 return NOTIFY_DONE;
8720
8721 rcu_read_lock();
8722
Johannes Berg5e760232011-11-04 11:18:17 +01008723 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008724 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008725 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008726 if (rdev->ap_beacons_nlpid == notify->pid)
8727 rdev->ap_beacons_nlpid = 0;
8728 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008729
8730 rcu_read_unlock();
8731
8732 return NOTIFY_DONE;
8733}
8734
8735static struct notifier_block nl80211_netlink_notifier = {
8736 .notifier_call = nl80211_netlink_notify,
8737};
8738
Johannes Berg55682962007-09-20 13:09:35 -04008739/* initialisation/exit functions */
8740
8741int nl80211_init(void)
8742{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008743 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008744
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008745 err = genl_register_family_with_ops(&nl80211_fam,
8746 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008747 if (err)
8748 return err;
8749
Johannes Berg55682962007-09-20 13:09:35 -04008750 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8751 if (err)
8752 goto err_out;
8753
Johannes Berg2a519312009-02-10 21:25:55 +01008754 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8755 if (err)
8756 goto err_out;
8757
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008758 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8759 if (err)
8760 goto err_out;
8761
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008762 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8763 if (err)
8764 goto err_out;
8765
Johannes Bergaff89a92009-07-01 21:26:51 +02008766#ifdef CONFIG_NL80211_TESTMODE
8767 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8768 if (err)
8769 goto err_out;
8770#endif
8771
Jouni Malinen026331c2010-02-15 12:53:10 +02008772 err = netlink_register_notifier(&nl80211_netlink_notifier);
8773 if (err)
8774 goto err_out;
8775
Johannes Berg55682962007-09-20 13:09:35 -04008776 return 0;
8777 err_out:
8778 genl_unregister_family(&nl80211_fam);
8779 return err;
8780}
8781
8782void nl80211_exit(void)
8783{
Jouni Malinen026331c2010-02-15 12:53:10 +02008784 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008785 genl_unregister_family(&nl80211_fam);
8786}