blob: e880f4494950eb329675a9f74952770e1301b629 [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"
Hila Gonene35e4d22012-06-27 17:19:42 +030025#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040026
Jouni Malinen5fb628e2011-08-10 23:54:35 +030027static 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 Malinendc6382ce2009-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 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700357 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300358 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000359 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700360 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400361};
362
Johannes Berge31b8212010-10-05 19:39:30 +0200363/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000364static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200365 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200366 [NL80211_KEY_IDX] = { .type = NLA_U8 },
367 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200368 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200369 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
370 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200371 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100372 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
373};
374
375/* policy for the key default flags */
376static const struct nla_policy
377nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
378 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
379 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200380};
381
Johannes Bergff1b6e62011-05-04 15:37:28 +0200382/* policy for WoWLAN attributes */
383static const struct nla_policy
384nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
385 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
386 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
387 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
388 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200389 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
390 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
391 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
392 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200393};
394
Johannes Berge5497d72011-07-05 16:35:40 +0200395/* policy for GTK rekey offload attributes */
396static const struct nla_policy
397nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
398 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
399 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
400 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
401};
402
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300403static const struct nla_policy
404nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200405 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300406 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700407 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300408};
409
Holger Schuriga0438972009-11-11 11:30:02 +0100410/* ifidx get helper */
411static int nl80211_get_ifidx(struct netlink_callback *cb)
412{
413 int res;
414
415 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
416 nl80211_fam.attrbuf, nl80211_fam.maxattr,
417 nl80211_policy);
418 if (res)
419 return res;
420
421 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
422 return -EINVAL;
423
424 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
425 if (!res)
426 return -EINVAL;
427 return res;
428}
429
Johannes Berg67748892010-10-04 21:14:06 +0200430static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
431 struct netlink_callback *cb,
432 struct cfg80211_registered_device **rdev,
433 struct net_device **dev)
434{
435 int ifidx = cb->args[0];
436 int err;
437
438 if (!ifidx)
439 ifidx = nl80211_get_ifidx(cb);
440 if (ifidx < 0)
441 return ifidx;
442
443 cb->args[0] = ifidx;
444
445 rtnl_lock();
446
447 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
448 if (!*dev) {
449 err = -ENODEV;
450 goto out_rtnl;
451 }
452
453 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100454 if (IS_ERR(*rdev)) {
455 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200456 goto out_rtnl;
457 }
458
459 return 0;
460 out_rtnl:
461 rtnl_unlock();
462 return err;
463}
464
465static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
466{
467 cfg80211_unlock_rdev(rdev);
468 rtnl_unlock();
469}
470
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100471/* IE validation */
472static bool is_valid_ie_attr(const struct nlattr *attr)
473{
474 const u8 *pos;
475 int len;
476
477 if (!attr)
478 return true;
479
480 pos = nla_data(attr);
481 len = nla_len(attr);
482
483 while (len) {
484 u8 elemlen;
485
486 if (len < 2)
487 return false;
488 len -= 2;
489
490 elemlen = pos[1];
491 if (elemlen > len)
492 return false;
493
494 len -= elemlen;
495 pos += 2 + elemlen;
496 }
497
498 return true;
499}
500
Johannes Berg55682962007-09-20 13:09:35 -0400501/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000502static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400503 int flags, u8 cmd)
504{
505 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000506 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400507}
508
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400509static int nl80211_msg_put_channel(struct sk_buff *msg,
510 struct ieee80211_channel *chan)
511{
David S. Miller9360ffd2012-03-29 04:41:26 -0400512 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
513 chan->center_freq))
514 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400515
David S. Miller9360ffd2012-03-29 04:41:26 -0400516 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
517 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
518 goto nla_put_failure;
519 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
520 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
521 goto nla_put_failure;
522 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
523 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
524 goto nla_put_failure;
525 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
526 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
527 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400528
David S. Miller9360ffd2012-03-29 04:41:26 -0400529 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
530 DBM_TO_MBM(chan->max_power)))
531 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400532
533 return 0;
534
535 nla_put_failure:
536 return -ENOBUFS;
537}
538
Johannes Berg55682962007-09-20 13:09:35 -0400539/* netlink command implementations */
540
Johannes Bergb9454e82009-07-08 13:29:08 +0200541struct key_parse {
542 struct key_params p;
543 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200544 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200545 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100546 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200547};
548
549static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
550{
551 struct nlattr *tb[NL80211_KEY_MAX + 1];
552 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
553 nl80211_key_policy);
554 if (err)
555 return err;
556
557 k->def = !!tb[NL80211_KEY_DEFAULT];
558 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
559
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100560 if (k->def) {
561 k->def_uni = true;
562 k->def_multi = true;
563 }
564 if (k->defmgmt)
565 k->def_multi = true;
566
Johannes Bergb9454e82009-07-08 13:29:08 +0200567 if (tb[NL80211_KEY_IDX])
568 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
569
570 if (tb[NL80211_KEY_DATA]) {
571 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
572 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
573 }
574
575 if (tb[NL80211_KEY_SEQ]) {
576 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
577 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
578 }
579
580 if (tb[NL80211_KEY_CIPHER])
581 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
582
Johannes Berge31b8212010-10-05 19:39:30 +0200583 if (tb[NL80211_KEY_TYPE]) {
584 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
585 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
586 return -EINVAL;
587 }
588
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100589 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
590 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100591 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
592 tb[NL80211_KEY_DEFAULT_TYPES],
593 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100594 if (err)
595 return err;
596
597 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
598 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
599 }
600
Johannes Bergb9454e82009-07-08 13:29:08 +0200601 return 0;
602}
603
604static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
605{
606 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
607 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
608 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
609 }
610
611 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
612 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
613 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
614 }
615
616 if (info->attrs[NL80211_ATTR_KEY_IDX])
617 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
618
619 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
620 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
621
622 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
623 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
624
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100625 if (k->def) {
626 k->def_uni = true;
627 k->def_multi = true;
628 }
629 if (k->defmgmt)
630 k->def_multi = true;
631
Johannes Berge31b8212010-10-05 19:39:30 +0200632 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
633 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
634 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
635 return -EINVAL;
636 }
637
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100638 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
639 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
640 int err = nla_parse_nested(
641 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
642 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
643 nl80211_key_default_policy);
644 if (err)
645 return err;
646
647 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
648 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
649 }
650
Johannes Bergb9454e82009-07-08 13:29:08 +0200651 return 0;
652}
653
654static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
655{
656 int err;
657
658 memset(k, 0, sizeof(*k));
659 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200660 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200661
662 if (info->attrs[NL80211_ATTR_KEY])
663 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
664 else
665 err = nl80211_parse_key_old(info, k);
666
667 if (err)
668 return err;
669
670 if (k->def && k->defmgmt)
671 return -EINVAL;
672
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100673 if (k->defmgmt) {
674 if (k->def_uni || !k->def_multi)
675 return -EINVAL;
676 }
677
Johannes Bergb9454e82009-07-08 13:29:08 +0200678 if (k->idx != -1) {
679 if (k->defmgmt) {
680 if (k->idx < 4 || k->idx > 5)
681 return -EINVAL;
682 } else if (k->def) {
683 if (k->idx < 0 || k->idx > 3)
684 return -EINVAL;
685 } else {
686 if (k->idx < 0 || k->idx > 5)
687 return -EINVAL;
688 }
689 }
690
691 return 0;
692}
693
Johannes Bergfffd0932009-07-08 14:22:54 +0200694static struct cfg80211_cached_keys *
695nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530696 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200697{
698 struct key_parse parse;
699 struct nlattr *key;
700 struct cfg80211_cached_keys *result;
701 int rem, err, def = 0;
702
703 result = kzalloc(sizeof(*result), GFP_KERNEL);
704 if (!result)
705 return ERR_PTR(-ENOMEM);
706
707 result->def = -1;
708 result->defmgmt = -1;
709
710 nla_for_each_nested(key, keys, rem) {
711 memset(&parse, 0, sizeof(parse));
712 parse.idx = -1;
713
714 err = nl80211_parse_key_new(key, &parse);
715 if (err)
716 goto error;
717 err = -EINVAL;
718 if (!parse.p.key)
719 goto error;
720 if (parse.idx < 0 || parse.idx > 4)
721 goto error;
722 if (parse.def) {
723 if (def)
724 goto error;
725 def = 1;
726 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100727 if (!parse.def_uni || !parse.def_multi)
728 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200729 } else if (parse.defmgmt)
730 goto error;
731 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200732 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200733 if (err)
734 goto error;
735 result->params[parse.idx].cipher = parse.p.cipher;
736 result->params[parse.idx].key_len = parse.p.key_len;
737 result->params[parse.idx].key = result->data[parse.idx];
738 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530739
740 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
741 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
742 if (no_ht)
743 *no_ht = true;
744 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200745 }
746
747 return result;
748 error:
749 kfree(result);
750 return ERR_PTR(err);
751}
752
753static int nl80211_key_allowed(struct wireless_dev *wdev)
754{
755 ASSERT_WDEV_LOCK(wdev);
756
Johannes Bergfffd0932009-07-08 14:22:54 +0200757 switch (wdev->iftype) {
758 case NL80211_IFTYPE_AP:
759 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200760 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700761 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200762 break;
763 case NL80211_IFTYPE_ADHOC:
764 if (!wdev->current_bss)
765 return -ENOLINK;
766 break;
767 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200768 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200769 if (wdev->sme_state != CFG80211_SME_CONNECTED)
770 return -ENOLINK;
771 break;
772 default:
773 return -EINVAL;
774 }
775
776 return 0;
777}
778
Johannes Berg7527a782011-05-13 10:58:57 +0200779static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
780{
781 struct nlattr *nl_modes = nla_nest_start(msg, attr);
782 int i;
783
784 if (!nl_modes)
785 goto nla_put_failure;
786
787 i = 0;
788 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400789 if ((ifmodes & 1) && nla_put_flag(msg, i))
790 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200791 ifmodes >>= 1;
792 i++;
793 }
794
795 nla_nest_end(msg, nl_modes);
796 return 0;
797
798nla_put_failure:
799 return -ENOBUFS;
800}
801
802static int nl80211_put_iface_combinations(struct wiphy *wiphy,
803 struct sk_buff *msg)
804{
805 struct nlattr *nl_combis;
806 int i, j;
807
808 nl_combis = nla_nest_start(msg,
809 NL80211_ATTR_INTERFACE_COMBINATIONS);
810 if (!nl_combis)
811 goto nla_put_failure;
812
813 for (i = 0; i < wiphy->n_iface_combinations; i++) {
814 const struct ieee80211_iface_combination *c;
815 struct nlattr *nl_combi, *nl_limits;
816
817 c = &wiphy->iface_combinations[i];
818
819 nl_combi = nla_nest_start(msg, i + 1);
820 if (!nl_combi)
821 goto nla_put_failure;
822
823 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
824 if (!nl_limits)
825 goto nla_put_failure;
826
827 for (j = 0; j < c->n_limits; j++) {
828 struct nlattr *nl_limit;
829
830 nl_limit = nla_nest_start(msg, j + 1);
831 if (!nl_limit)
832 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400833 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
834 c->limits[j].max))
835 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200836 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
837 c->limits[j].types))
838 goto nla_put_failure;
839 nla_nest_end(msg, nl_limit);
840 }
841
842 nla_nest_end(msg, nl_limits);
843
David S. Miller9360ffd2012-03-29 04:41:26 -0400844 if (c->beacon_int_infra_match &&
845 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
846 goto nla_put_failure;
847 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
848 c->num_different_channels) ||
849 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
850 c->max_interfaces))
851 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200852
853 nla_nest_end(msg, nl_combi);
854 }
855
856 nla_nest_end(msg, nl_combis);
857
858 return 0;
859nla_put_failure:
860 return -ENOBUFS;
861}
862
Eric W. Biederman15e47302012-09-07 20:12:54 +0000863static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400864 struct cfg80211_registered_device *dev)
865{
866 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100867 struct nlattr *nl_bands, *nl_band;
868 struct nlattr *nl_freqs, *nl_freq;
869 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100870 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100871 enum ieee80211_band band;
872 struct ieee80211_channel *chan;
873 struct ieee80211_rate *rate;
874 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200875 const struct ieee80211_txrx_stypes *mgmt_stypes =
876 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400877
Eric W. Biederman15e47302012-09-07 20:12:54 +0000878 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400879 if (!hdr)
880 return -1;
881
David S. Miller9360ffd2012-03-29 04:41:26 -0400882 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
883 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
884 nla_put_u32(msg, NL80211_ATTR_GENERATION,
885 cfg80211_rdev_list_generation) ||
886 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
887 dev->wiphy.retry_short) ||
888 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
889 dev->wiphy.retry_long) ||
890 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
891 dev->wiphy.frag_threshold) ||
892 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
893 dev->wiphy.rts_threshold) ||
894 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
895 dev->wiphy.coverage_class) ||
896 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
897 dev->wiphy.max_scan_ssids) ||
898 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
899 dev->wiphy.max_sched_scan_ssids) ||
900 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
901 dev->wiphy.max_scan_ie_len) ||
902 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
903 dev->wiphy.max_sched_scan_ie_len) ||
904 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
905 dev->wiphy.max_match_sets))
906 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200907
David S. Miller9360ffd2012-03-29 04:41:26 -0400908 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
909 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
910 goto nla_put_failure;
911 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
912 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
913 goto nla_put_failure;
914 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
915 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
916 goto nla_put_failure;
917 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
918 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
919 goto nla_put_failure;
920 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
921 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
922 goto nla_put_failure;
923 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
924 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
925 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200926
David S. Miller9360ffd2012-03-29 04:41:26 -0400927 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
928 sizeof(u32) * dev->wiphy.n_cipher_suites,
929 dev->wiphy.cipher_suites))
930 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100931
David S. Miller9360ffd2012-03-29 04:41:26 -0400932 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
933 dev->wiphy.max_num_pmkids))
934 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530935
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
937 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
938 goto nla_put_failure;
Johannes Berg25e47c182009-04-02 20:14:06 +0200939
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
941 dev->wiphy.available_antennas_tx) ||
942 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
943 dev->wiphy.available_antennas_rx))
944 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100945
David S. Miller9360ffd2012-03-29 04:41:26 -0400946 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
947 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
948 dev->wiphy.probe_resp_offload))
949 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200950
Bruno Randolf7f531e02010-12-16 11:30:22 +0900951 if ((dev->wiphy.available_antennas_tx ||
952 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900953 u32 tx_ant = 0, rx_ant = 0;
954 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300955 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900956 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400957 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
958 tx_ant) ||
959 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
960 rx_ant))
961 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900962 }
963 }
964
Johannes Berg7527a782011-05-13 10:58:57 +0200965 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
966 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700967 goto nla_put_failure;
968
Johannes Bergee688b002008-01-24 19:38:39 +0100969 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
970 if (!nl_bands)
971 goto nla_put_failure;
972
973 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
974 if (!dev->wiphy.bands[band])
975 continue;
976
977 nl_band = nla_nest_start(msg, band);
978 if (!nl_band)
979 goto nla_put_failure;
980
Johannes Bergd51626d2008-10-09 12:20:13 +0200981 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400982 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
983 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
984 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
985 &dev->wiphy.bands[band]->ht_cap.mcs) ||
986 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
987 dev->wiphy.bands[band]->ht_cap.cap) ||
988 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
989 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
990 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
991 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
992 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200993
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000994 /* add VHT info */
995 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
996 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
997 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
998 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
999 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1000 dev->wiphy.bands[band]->vht_cap.cap)))
1001 goto nla_put_failure;
1002
Johannes Bergee688b002008-01-24 19:38:39 +01001003 /* add frequencies */
1004 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1005 if (!nl_freqs)
1006 goto nla_put_failure;
1007
1008 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1009 nl_freq = nla_nest_start(msg, i);
1010 if (!nl_freq)
1011 goto nla_put_failure;
1012
1013 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001014
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001015 if (nl80211_msg_put_channel(msg, chan))
1016 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001017
Johannes Bergee688b002008-01-24 19:38:39 +01001018 nla_nest_end(msg, nl_freq);
1019 }
1020
1021 nla_nest_end(msg, nl_freqs);
1022
1023 /* add bitrates */
1024 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1025 if (!nl_rates)
1026 goto nla_put_failure;
1027
1028 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1029 nl_rate = nla_nest_start(msg, i);
1030 if (!nl_rate)
1031 goto nla_put_failure;
1032
1033 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001034 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1035 rate->bitrate))
1036 goto nla_put_failure;
1037 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1038 nla_put_flag(msg,
1039 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1040 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001041
1042 nla_nest_end(msg, nl_rate);
1043 }
1044
1045 nla_nest_end(msg, nl_rates);
1046
1047 nla_nest_end(msg, nl_band);
1048 }
1049 nla_nest_end(msg, nl_bands);
1050
Johannes Berg8fdc6212009-03-14 09:34:01 +01001051 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1052 if (!nl_cmds)
1053 goto nla_put_failure;
1054
1055 i = 0;
1056#define CMD(op, n) \
1057 do { \
1058 if (dev->ops->op) { \
1059 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001060 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1061 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001062 } \
1063 } while (0)
1064
1065 CMD(add_virtual_intf, NEW_INTERFACE);
1066 CMD(change_virtual_intf, SET_INTERFACE);
1067 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001068 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001069 CMD(add_station, NEW_STATION);
1070 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001071 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001072 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001073 CMD(auth, AUTHENTICATE);
1074 CMD(assoc, ASSOCIATE);
1075 CMD(deauth, DEAUTHENTICATE);
1076 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001077 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001078 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001079 CMD(set_pmksa, SET_PMKSA);
1080 CMD(del_pmksa, DEL_PMKSA);
1081 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001082 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1083 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001084 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001085 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001086 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001087 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001088 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001089 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1090 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001091 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001092 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001093 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001094 i++;
1095 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1096 goto nla_put_failure;
1097 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001098 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001099 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1100 CMD(tdls_mgmt, TDLS_MGMT);
1101 CMD(tdls_oper, TDLS_OPER);
1102 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001103 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1104 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001105 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001106 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001107 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1108 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001109 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1110 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001111 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001112 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001113 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001114
Kalle Valo4745fc02011-11-17 19:06:10 +02001115#ifdef CONFIG_NL80211_TESTMODE
1116 CMD(testmode_cmd, TESTMODE);
1117#endif
1118
Johannes Berg8fdc6212009-03-14 09:34:01 +01001119#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001120
Johannes Berg6829c8782009-07-02 09:13:27 +02001121 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001122 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001123 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1124 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001125 }
1126
Johannes Berg6829c8782009-07-02 09:13:27 +02001127 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001128 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001129 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1130 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001131 }
1132
Johannes Berg8fdc6212009-03-14 09:34:01 +01001133 nla_nest_end(msg, nl_cmds);
1134
Johannes Berg7c4ef712011-11-18 15:33:48 +01001135 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001136 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1137 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1138 dev->wiphy.max_remain_on_channel_duration))
1139 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001140
David S. Miller9360ffd2012-03-29 04:41:26 -04001141 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1142 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1143 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001144
Johannes Berg2e161f72010-08-12 15:38:38 +02001145 if (mgmt_stypes) {
1146 u16 stypes;
1147 struct nlattr *nl_ftypes, *nl_ifs;
1148 enum nl80211_iftype ift;
1149
1150 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1151 if (!nl_ifs)
1152 goto nla_put_failure;
1153
1154 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1155 nl_ftypes = nla_nest_start(msg, ift);
1156 if (!nl_ftypes)
1157 goto nla_put_failure;
1158 i = 0;
1159 stypes = mgmt_stypes[ift].tx;
1160 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001161 if ((stypes & 1) &&
1162 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1163 (i << 4) | IEEE80211_FTYPE_MGMT))
1164 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001165 stypes >>= 1;
1166 i++;
1167 }
1168 nla_nest_end(msg, nl_ftypes);
1169 }
1170
Johannes Berg74b70a42010-08-24 12:15:53 +02001171 nla_nest_end(msg, nl_ifs);
1172
Johannes Berg2e161f72010-08-12 15:38:38 +02001173 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1174 if (!nl_ifs)
1175 goto nla_put_failure;
1176
1177 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1178 nl_ftypes = nla_nest_start(msg, ift);
1179 if (!nl_ftypes)
1180 goto nla_put_failure;
1181 i = 0;
1182 stypes = mgmt_stypes[ift].rx;
1183 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001184 if ((stypes & 1) &&
1185 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1186 (i << 4) | IEEE80211_FTYPE_MGMT))
1187 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001188 stypes >>= 1;
1189 i++;
1190 }
1191 nla_nest_end(msg, nl_ftypes);
1192 }
1193 nla_nest_end(msg, nl_ifs);
1194 }
1195
Johannes Bergdfb89c52012-06-27 09:23:48 +02001196#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001197 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1198 struct nlattr *nl_wowlan;
1199
1200 nl_wowlan = nla_nest_start(msg,
1201 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1202 if (!nl_wowlan)
1203 goto nla_put_failure;
1204
David S. Miller9360ffd2012-03-29 04:41:26 -04001205 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1206 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1207 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1208 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1209 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1210 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1211 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1212 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1213 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1214 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1215 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1216 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1217 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1218 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1219 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1220 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1221 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001222 if (dev->wiphy.wowlan.n_patterns) {
1223 struct nl80211_wowlan_pattern_support pat = {
1224 .max_patterns = dev->wiphy.wowlan.n_patterns,
1225 .min_pattern_len =
1226 dev->wiphy.wowlan.pattern_min_len,
1227 .max_pattern_len =
1228 dev->wiphy.wowlan.pattern_max_len,
1229 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001230 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1231 sizeof(pat), &pat))
1232 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001233 }
1234
1235 nla_nest_end(msg, nl_wowlan);
1236 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001237#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001238
Johannes Berg7527a782011-05-13 10:58:57 +02001239 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1240 dev->wiphy.software_iftypes))
1241 goto nla_put_failure;
1242
1243 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1244 goto nla_put_failure;
1245
David S. Miller9360ffd2012-03-29 04:41:26 -04001246 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1247 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1248 dev->wiphy.ap_sme_capa))
1249 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001250
David S. Miller9360ffd2012-03-29 04:41:26 -04001251 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1252 dev->wiphy.features))
1253 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001254
David S. Miller9360ffd2012-03-29 04:41:26 -04001255 if (dev->wiphy.ht_capa_mod_mask &&
1256 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1257 sizeof(*dev->wiphy.ht_capa_mod_mask),
1258 dev->wiphy.ht_capa_mod_mask))
1259 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001260
Johannes Berg55682962007-09-20 13:09:35 -04001261 return genlmsg_end(msg, hdr);
1262
1263 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001264 genlmsg_cancel(msg, hdr);
1265 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001266}
1267
1268static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1269{
1270 int idx = 0;
1271 int start = cb->args[0];
1272 struct cfg80211_registered_device *dev;
1273
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001274 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001275 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001276 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1277 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001278 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001279 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001280 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001281 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001282 dev) < 0) {
1283 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001284 break;
Julius Volzb4637272008-07-08 14:02:19 +02001285 }
Johannes Berg55682962007-09-20 13:09:35 -04001286 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001287 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001288
1289 cb->args[0] = idx;
1290
1291 return skb->len;
1292}
1293
1294static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1295{
1296 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001297 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001298
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001299 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001300 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001301 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001302
Eric W. Biederman15e47302012-09-07 20:12:54 +00001303 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001304 nlmsg_free(msg);
1305 return -ENOBUFS;
1306 }
Johannes Berg55682962007-09-20 13:09:35 -04001307
Johannes Berg134e6372009-07-10 09:51:34 +00001308 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001309}
1310
Jouni Malinen31888482008-10-30 16:59:24 +02001311static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1312 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1313 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1314 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1315 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1316 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1317};
1318
1319static int parse_txq_params(struct nlattr *tb[],
1320 struct ieee80211_txq_params *txq_params)
1321{
Johannes Berga3304b02012-03-28 11:04:24 +02001322 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001323 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1324 !tb[NL80211_TXQ_ATTR_AIFS])
1325 return -EINVAL;
1326
Johannes Berga3304b02012-03-28 11:04:24 +02001327 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001328 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1329 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1330 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1331 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1332
Johannes Berga3304b02012-03-28 11:04:24 +02001333 if (txq_params->ac >= NL80211_NUM_ACS)
1334 return -EINVAL;
1335
Jouni Malinen31888482008-10-30 16:59:24 +02001336 return 0;
1337}
1338
Johannes Bergf444de02010-05-05 15:25:02 +02001339static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1340{
1341 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001342 * You can only set the channel explicitly for WDS interfaces,
1343 * all others have their channel managed via their respective
1344 * "establish a connection" command (connect, join, ...)
1345 *
1346 * For AP/GO and mesh mode, the channel can be set with the
1347 * channel userspace API, but is only stored and passed to the
1348 * low-level driver when the AP starts or the mesh is joined.
1349 * This is for backward compatibility, userspace can also give
1350 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001351 *
1352 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001353 * whatever else is going on, so they have their own special
1354 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001355 */
1356 return !wdev ||
1357 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001358 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001359 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1360 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001361}
1362
Johannes Bergcd6c6592012-05-10 21:27:18 +02001363static bool nl80211_valid_channel_type(struct genl_info *info,
1364 enum nl80211_channel_type *channel_type)
1365{
1366 enum nl80211_channel_type tmp;
1367
1368 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1369 return false;
1370
1371 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1372 if (tmp != NL80211_CHAN_NO_HT &&
1373 tmp != NL80211_CHAN_HT20 &&
1374 tmp != NL80211_CHAN_HT40PLUS &&
1375 tmp != NL80211_CHAN_HT40MINUS)
1376 return false;
1377
1378 if (channel_type)
1379 *channel_type = tmp;
1380
1381 return true;
1382}
1383
Johannes Bergf444de02010-05-05 15:25:02 +02001384static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1385 struct wireless_dev *wdev,
1386 struct genl_info *info)
1387{
Johannes Bergaa430da2012-05-16 23:50:18 +02001388 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001389 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1390 u32 freq;
1391 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001392 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1393
1394 if (wdev)
1395 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001396
1397 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1398 return -EINVAL;
1399
1400 if (!nl80211_can_set_dev_channel(wdev))
1401 return -EOPNOTSUPP;
1402
Johannes Bergcd6c6592012-05-10 21:27:18 +02001403 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1404 !nl80211_valid_channel_type(info, &channel_type))
1405 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001406
1407 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1408
1409 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001410 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001411 case NL80211_IFTYPE_AP:
1412 case NL80211_IFTYPE_P2P_GO:
1413 if (wdev->beacon_interval) {
1414 result = -EBUSY;
1415 break;
1416 }
1417 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1418 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1419 channel,
1420 channel_type)) {
1421 result = -EINVAL;
1422 break;
1423 }
1424 wdev->preset_chan = channel;
1425 wdev->preset_chantype = channel_type;
1426 result = 0;
1427 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001428 case NL80211_IFTYPE_MESH_POINT:
1429 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1430 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001431 case NL80211_IFTYPE_MONITOR:
1432 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1433 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001434 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001435 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001436 }
1437 mutex_unlock(&rdev->devlist_mtx);
1438
1439 return result;
1440}
1441
1442static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1443{
Johannes Berg4c476992010-10-04 21:36:35 +02001444 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1445 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001446
Johannes Berg4c476992010-10-04 21:36:35 +02001447 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001448}
1449
Bill Jordane8347eb2010-10-01 13:54:28 -04001450static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1451{
Johannes Berg43b19952010-10-07 13:10:30 +02001452 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1453 struct net_device *dev = info->user_ptr[1];
1454 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001455 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001456
1457 if (!info->attrs[NL80211_ATTR_MAC])
1458 return -EINVAL;
1459
Johannes Berg43b19952010-10-07 13:10:30 +02001460 if (netif_running(dev))
1461 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001462
Johannes Berg43b19952010-10-07 13:10:30 +02001463 if (!rdev->ops->set_wds_peer)
1464 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001465
Johannes Berg43b19952010-10-07 13:10:30 +02001466 if (wdev->iftype != NL80211_IFTYPE_WDS)
1467 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001468
1469 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001470 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001471}
1472
1473
Johannes Berg55682962007-09-20 13:09:35 -04001474static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1475{
1476 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001477 struct net_device *netdev = NULL;
1478 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001479 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001480 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001481 u32 changed;
1482 u8 retry_short = 0, retry_long = 0;
1483 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001484 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001485
Johannes Bergf444de02010-05-05 15:25:02 +02001486 /*
1487 * Try to find the wiphy and netdev. Normally this
1488 * function shouldn't need the netdev, but this is
1489 * done for backward compatibility -- previously
1490 * setting the channel was done per wiphy, but now
1491 * it is per netdev. Previous userland like hostapd
1492 * also passed a netdev to set_wiphy, so that it is
1493 * possible to let that go to the right netdev!
1494 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001495 mutex_lock(&cfg80211_mutex);
1496
Johannes Bergf444de02010-05-05 15:25:02 +02001497 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1498 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1499
1500 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1501 if (netdev && netdev->ieee80211_ptr) {
1502 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1503 mutex_lock(&rdev->mtx);
1504 } else
1505 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001506 }
1507
Johannes Bergf444de02010-05-05 15:25:02 +02001508 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001509 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1510 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001511 if (IS_ERR(rdev)) {
1512 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001513 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001514 }
1515 wdev = NULL;
1516 netdev = NULL;
1517 result = 0;
1518
1519 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001520 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001521 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001522
1523 /*
1524 * end workaround code, by now the rdev is available
1525 * and locked, and wdev may or may not be NULL.
1526 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001527
1528 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001529 result = cfg80211_dev_rename(
1530 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001531
1532 mutex_unlock(&cfg80211_mutex);
1533
1534 if (result)
1535 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001536
Jouni Malinen31888482008-10-30 16:59:24 +02001537 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1538 struct ieee80211_txq_params txq_params;
1539 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1540
1541 if (!rdev->ops->set_txq_params) {
1542 result = -EOPNOTSUPP;
1543 goto bad_res;
1544 }
1545
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001546 if (!netdev) {
1547 result = -EINVAL;
1548 goto bad_res;
1549 }
1550
Johannes Berg133a3ff2011-11-03 14:50:13 +01001551 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1552 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1553 result = -EINVAL;
1554 goto bad_res;
1555 }
1556
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001557 if (!netif_running(netdev)) {
1558 result = -ENETDOWN;
1559 goto bad_res;
1560 }
1561
Jouni Malinen31888482008-10-30 16:59:24 +02001562 nla_for_each_nested(nl_txq_params,
1563 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1564 rem_txq_params) {
1565 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1566 nla_data(nl_txq_params),
1567 nla_len(nl_txq_params),
1568 txq_params_policy);
1569 result = parse_txq_params(tb, &txq_params);
1570 if (result)
1571 goto bad_res;
1572
Hila Gonene35e4d22012-06-27 17:19:42 +03001573 result = rdev_set_txq_params(rdev, netdev,
1574 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001575 if (result)
1576 goto bad_res;
1577 }
1578 }
1579
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001580 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001581 result = __nl80211_set_channel(rdev,
1582 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1583 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001584 if (result)
1585 goto bad_res;
1586 }
1587
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001588 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001589 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001590 enum nl80211_tx_power_setting type;
1591 int idx, mbm = 0;
1592
Johannes Bergc8442112012-10-24 10:17:18 +02001593 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1594 txp_wdev = NULL;
1595
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001596 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001597 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001598 goto bad_res;
1599 }
1600
1601 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1602 type = nla_get_u32(info->attrs[idx]);
1603
1604 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1605 (type != NL80211_TX_POWER_AUTOMATIC)) {
1606 result = -EINVAL;
1607 goto bad_res;
1608 }
1609
1610 if (type != NL80211_TX_POWER_AUTOMATIC) {
1611 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1612 mbm = nla_get_u32(info->attrs[idx]);
1613 }
1614
Johannes Bergc8442112012-10-24 10:17:18 +02001615 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001616 if (result)
1617 goto bad_res;
1618 }
1619
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001620 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1621 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1622 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001623 if ((!rdev->wiphy.available_antennas_tx &&
1624 !rdev->wiphy.available_antennas_rx) ||
1625 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001626 result = -EOPNOTSUPP;
1627 goto bad_res;
1628 }
1629
1630 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1631 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1632
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001633 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001634 * available antenna masks, except for the "all" mask */
1635 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1636 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001637 result = -EINVAL;
1638 goto bad_res;
1639 }
1640
Bruno Randolf7f531e02010-12-16 11:30:22 +09001641 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1642 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001643
Hila Gonene35e4d22012-06-27 17:19:42 +03001644 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001645 if (result)
1646 goto bad_res;
1647 }
1648
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001649 changed = 0;
1650
1651 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1652 retry_short = nla_get_u8(
1653 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1654 if (retry_short == 0) {
1655 result = -EINVAL;
1656 goto bad_res;
1657 }
1658 changed |= WIPHY_PARAM_RETRY_SHORT;
1659 }
1660
1661 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1662 retry_long = nla_get_u8(
1663 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1664 if (retry_long == 0) {
1665 result = -EINVAL;
1666 goto bad_res;
1667 }
1668 changed |= WIPHY_PARAM_RETRY_LONG;
1669 }
1670
1671 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1672 frag_threshold = nla_get_u32(
1673 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1674 if (frag_threshold < 256) {
1675 result = -EINVAL;
1676 goto bad_res;
1677 }
1678 if (frag_threshold != (u32) -1) {
1679 /*
1680 * Fragments (apart from the last one) are required to
1681 * have even length. Make the fragmentation code
1682 * simpler by stripping LSB should someone try to use
1683 * odd threshold value.
1684 */
1685 frag_threshold &= ~0x1;
1686 }
1687 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1688 }
1689
1690 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1691 rts_threshold = nla_get_u32(
1692 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1693 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1694 }
1695
Lukáš Turek81077e82009-12-21 22:50:47 +01001696 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1697 coverage_class = nla_get_u8(
1698 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1699 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1700 }
1701
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001702 if (changed) {
1703 u8 old_retry_short, old_retry_long;
1704 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001705 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001706
1707 if (!rdev->ops->set_wiphy_params) {
1708 result = -EOPNOTSUPP;
1709 goto bad_res;
1710 }
1711
1712 old_retry_short = rdev->wiphy.retry_short;
1713 old_retry_long = rdev->wiphy.retry_long;
1714 old_frag_threshold = rdev->wiphy.frag_threshold;
1715 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001716 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001717
1718 if (changed & WIPHY_PARAM_RETRY_SHORT)
1719 rdev->wiphy.retry_short = retry_short;
1720 if (changed & WIPHY_PARAM_RETRY_LONG)
1721 rdev->wiphy.retry_long = retry_long;
1722 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1723 rdev->wiphy.frag_threshold = frag_threshold;
1724 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1725 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001726 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1727 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001728
Hila Gonene35e4d22012-06-27 17:19:42 +03001729 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001730 if (result) {
1731 rdev->wiphy.retry_short = old_retry_short;
1732 rdev->wiphy.retry_long = old_retry_long;
1733 rdev->wiphy.frag_threshold = old_frag_threshold;
1734 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001735 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001736 }
1737 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001738
Johannes Berg306d6112008-12-08 12:39:04 +01001739 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001740 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001741 if (netdev)
1742 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001743 return result;
1744}
1745
Johannes Berg71bbc992012-06-15 15:30:18 +02001746static inline u64 wdev_id(struct wireless_dev *wdev)
1747{
1748 return (u64)wdev->identifier |
1749 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1750}
Johannes Berg55682962007-09-20 13:09:35 -04001751
Eric W. Biederman15e47302012-09-07 20:12:54 +00001752static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001753 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001754 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001755{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001756 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001757 void *hdr;
1758
Eric W. Biederman15e47302012-09-07 20:12:54 +00001759 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001760 if (!hdr)
1761 return -1;
1762
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001763 if (dev &&
1764 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001765 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001766 goto nla_put_failure;
1767
1768 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1769 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001770 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001771 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001772 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1773 rdev->devlist_generation ^
1774 (cfg80211_rdev_list_generation << 2)))
1775 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001776
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001777 if (rdev->ops->get_channel) {
1778 struct ieee80211_channel *chan;
1779 enum nl80211_channel_type channel_type;
1780
Hila Gonene35e4d22012-06-27 17:19:42 +03001781 chan = rdev_get_channel(rdev, wdev, &channel_type);
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001782 if (chan &&
1783 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1784 chan->center_freq) ||
1785 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1786 channel_type)))
John W. Linville59ef43e2012-04-18 14:17:13 -04001787 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001788 }
1789
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001790 if (wdev->ssid_len) {
1791 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1792 goto nla_put_failure;
1793 }
1794
Johannes Berg55682962007-09-20 13:09:35 -04001795 return genlmsg_end(msg, hdr);
1796
1797 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001798 genlmsg_cancel(msg, hdr);
1799 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001800}
1801
1802static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1803{
1804 int wp_idx = 0;
1805 int if_idx = 0;
1806 int wp_start = cb->args[0];
1807 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001808 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001809 struct wireless_dev *wdev;
1810
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001811 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001812 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1813 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001814 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001815 if (wp_idx < wp_start) {
1816 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001817 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001818 }
Johannes Berg55682962007-09-20 13:09:35 -04001819 if_idx = 0;
1820
Johannes Bergf5ea9122009-08-07 16:17:38 +02001821 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001822 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001823 if (if_idx < if_start) {
1824 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001825 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001826 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001827 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001828 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001829 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001830 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001831 goto out;
1832 }
1833 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001834 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001835 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001836
1837 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001838 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001839 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001840 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001841
1842 cb->args[0] = wp_idx;
1843 cb->args[1] = if_idx;
1844
1845 return skb->len;
1846}
1847
1848static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1849{
1850 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001851 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001852 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001853
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001854 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001855 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001856 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001857
Eric W. Biederman15e47302012-09-07 20:12:54 +00001858 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001859 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001860 nlmsg_free(msg);
1861 return -ENOBUFS;
1862 }
Johannes Berg55682962007-09-20 13:09:35 -04001863
Johannes Berg134e6372009-07-10 09:51:34 +00001864 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001865}
1866
Michael Wu66f7ac52008-01-31 19:48:22 +01001867static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1868 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1869 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1870 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1871 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1872 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1873};
1874
1875static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1876{
1877 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1878 int flag;
1879
1880 *mntrflags = 0;
1881
1882 if (!nla)
1883 return -EINVAL;
1884
1885 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1886 nla, mntr_flags_policy))
1887 return -EINVAL;
1888
1889 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1890 if (flags[flag])
1891 *mntrflags |= (1<<flag);
1892
1893 return 0;
1894}
1895
Johannes Berg9bc383d2009-11-19 11:55:19 +01001896static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001897 struct net_device *netdev, u8 use_4addr,
1898 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001899{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001900 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001901 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001902 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001903 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001904 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001905
1906 switch (iftype) {
1907 case NL80211_IFTYPE_AP_VLAN:
1908 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1909 return 0;
1910 break;
1911 case NL80211_IFTYPE_STATION:
1912 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1913 return 0;
1914 break;
1915 default:
1916 break;
1917 }
1918
1919 return -EOPNOTSUPP;
1920}
1921
Johannes Berg55682962007-09-20 13:09:35 -04001922static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1923{
Johannes Berg4c476992010-10-04 21:36:35 +02001924 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001925 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001926 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001927 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001928 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001929 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001930 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001931
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001932 memset(&params, 0, sizeof(params));
1933
Johannes Berg04a773a2009-04-19 21:24:32 +02001934 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001935
Johannes Berg723b0382008-09-16 20:22:09 +02001936 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001937 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001938 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001939 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001940 if (ntype > NL80211_IFTYPE_MAX)
1941 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001942 }
1943
Johannes Berg92ffe052008-09-16 20:39:36 +02001944 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001945 struct wireless_dev *wdev = dev->ieee80211_ptr;
1946
Johannes Berg4c476992010-10-04 21:36:35 +02001947 if (ntype != NL80211_IFTYPE_MESH_POINT)
1948 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001949 if (netif_running(dev))
1950 return -EBUSY;
1951
1952 wdev_lock(wdev);
1953 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1954 IEEE80211_MAX_MESH_ID_LEN);
1955 wdev->mesh_id_up_len =
1956 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1957 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1958 wdev->mesh_id_up_len);
1959 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001960 }
1961
Felix Fietkau8b787642009-11-10 18:53:10 +01001962 if (info->attrs[NL80211_ATTR_4ADDR]) {
1963 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1964 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001965 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001966 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001967 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001968 } else {
1969 params.use_4addr = -1;
1970 }
1971
Johannes Berg92ffe052008-09-16 20:39:36 +02001972 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001973 if (ntype != NL80211_IFTYPE_MONITOR)
1974 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001975 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1976 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001977 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001978 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001979
1980 flags = &_flags;
1981 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001982 }
Johannes Berg3b858752009-03-12 09:55:09 +01001983
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001984 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001985 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001986 else
1987 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001988
Johannes Berg9bc383d2009-11-19 11:55:19 +01001989 if (!err && params.use_4addr != -1)
1990 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1991
Johannes Berg55682962007-09-20 13:09:35 -04001992 return err;
1993}
1994
1995static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1996{
Johannes Berg4c476992010-10-04 21:36:35 +02001997 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001998 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02001999 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002000 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002001 int err;
2002 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002003 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002004
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002005 memset(&params, 0, sizeof(params));
2006
Johannes Berg55682962007-09-20 13:09:35 -04002007 if (!info->attrs[NL80211_ATTR_IFNAME])
2008 return -EINVAL;
2009
2010 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2011 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2012 if (type > NL80211_IFTYPE_MAX)
2013 return -EINVAL;
2014 }
2015
Johannes Berg79c97e92009-07-07 03:56:12 +02002016 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002017 !(rdev->wiphy.interface_modes & (1 << type)))
2018 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002019
Johannes Berg9bc383d2009-11-19 11:55:19 +01002020 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002021 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002022 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002023 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002024 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002025 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002026
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002027 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2028 if (!msg)
2029 return -ENOMEM;
2030
Michael Wu66f7ac52008-01-31 19:48:22 +01002031 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2032 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2033 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002034 wdev = rdev_add_virtual_intf(rdev,
2035 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2036 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002037 if (IS_ERR(wdev)) {
2038 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002039 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002040 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002041
Johannes Berg98104fde2012-06-16 00:19:54 +02002042 switch (type) {
2043 case NL80211_IFTYPE_MESH_POINT:
2044 if (!info->attrs[NL80211_ATTR_MESH_ID])
2045 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002046 wdev_lock(wdev);
2047 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2048 IEEE80211_MAX_MESH_ID_LEN);
2049 wdev->mesh_id_up_len =
2050 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2051 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2052 wdev->mesh_id_up_len);
2053 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002054 break;
2055 case NL80211_IFTYPE_P2P_DEVICE:
2056 /*
2057 * P2P Device doesn't have a netdev, so doesn't go
2058 * through the netdev notifier and must be added here
2059 */
2060 mutex_init(&wdev->mtx);
2061 INIT_LIST_HEAD(&wdev->event_list);
2062 spin_lock_init(&wdev->event_lock);
2063 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2064 spin_lock_init(&wdev->mgmt_registrations_lock);
2065
2066 mutex_lock(&rdev->devlist_mtx);
2067 wdev->identifier = ++rdev->wdev_id;
2068 list_add_rcu(&wdev->list, &rdev->wdev_list);
2069 rdev->devlist_generation++;
2070 mutex_unlock(&rdev->devlist_mtx);
2071 break;
2072 default:
2073 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002074 }
2075
Eric W. Biederman15e47302012-09-07 20:12:54 +00002076 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002077 rdev, wdev) < 0) {
2078 nlmsg_free(msg);
2079 return -ENOBUFS;
2080 }
2081
2082 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002083}
2084
2085static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2086{
Johannes Berg4c476992010-10-04 21:36:35 +02002087 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002088 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002089
Johannes Berg4c476992010-10-04 21:36:35 +02002090 if (!rdev->ops->del_virtual_intf)
2091 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002092
Johannes Berg84efbb82012-06-16 00:00:26 +02002093 /*
2094 * If we remove a wireless device without a netdev then clear
2095 * user_ptr[1] so that nl80211_post_doit won't dereference it
2096 * to check if it needs to do dev_put(). Otherwise it crashes
2097 * since the wdev has been freed, unlike with a netdev where
2098 * we need the dev_put() for the netdev to really be freed.
2099 */
2100 if (!wdev->netdev)
2101 info->user_ptr[1] = NULL;
2102
Hila Gonene35e4d22012-06-27 17:19:42 +03002103 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002104}
2105
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002106static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2107{
2108 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2109 struct net_device *dev = info->user_ptr[1];
2110 u16 noack_map;
2111
2112 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2113 return -EINVAL;
2114
2115 if (!rdev->ops->set_noack_map)
2116 return -EOPNOTSUPP;
2117
2118 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2119
Hila Gonene35e4d22012-06-27 17:19:42 +03002120 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002121}
2122
Johannes Berg41ade002007-12-19 02:03:29 +01002123struct get_key_cookie {
2124 struct sk_buff *msg;
2125 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002126 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002127};
2128
2129static void get_key_callback(void *c, struct key_params *params)
2130{
Johannes Bergb9454e82009-07-08 13:29:08 +02002131 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002132 struct get_key_cookie *cookie = c;
2133
David S. Miller9360ffd2012-03-29 04:41:26 -04002134 if ((params->key &&
2135 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2136 params->key_len, params->key)) ||
2137 (params->seq &&
2138 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2139 params->seq_len, params->seq)) ||
2140 (params->cipher &&
2141 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2142 params->cipher)))
2143 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002144
Johannes Bergb9454e82009-07-08 13:29:08 +02002145 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2146 if (!key)
2147 goto nla_put_failure;
2148
David S. Miller9360ffd2012-03-29 04:41:26 -04002149 if ((params->key &&
2150 nla_put(cookie->msg, NL80211_KEY_DATA,
2151 params->key_len, params->key)) ||
2152 (params->seq &&
2153 nla_put(cookie->msg, NL80211_KEY_SEQ,
2154 params->seq_len, params->seq)) ||
2155 (params->cipher &&
2156 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2157 params->cipher)))
2158 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002159
David S. Miller9360ffd2012-03-29 04:41:26 -04002160 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2161 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002162
2163 nla_nest_end(cookie->msg, key);
2164
Johannes Berg41ade002007-12-19 02:03:29 +01002165 return;
2166 nla_put_failure:
2167 cookie->error = 1;
2168}
2169
2170static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2171{
Johannes Berg4c476992010-10-04 21:36:35 +02002172 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002173 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002174 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002175 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002176 const u8 *mac_addr = NULL;
2177 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002178 struct get_key_cookie cookie = {
2179 .error = 0,
2180 };
2181 void *hdr;
2182 struct sk_buff *msg;
2183
2184 if (info->attrs[NL80211_ATTR_KEY_IDX])
2185 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2186
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002187 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002188 return -EINVAL;
2189
2190 if (info->attrs[NL80211_ATTR_MAC])
2191 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2192
Johannes Berge31b8212010-10-05 19:39:30 +02002193 pairwise = !!mac_addr;
2194 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2195 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2196 if (kt >= NUM_NL80211_KEYTYPES)
2197 return -EINVAL;
2198 if (kt != NL80211_KEYTYPE_GROUP &&
2199 kt != NL80211_KEYTYPE_PAIRWISE)
2200 return -EINVAL;
2201 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2202 }
2203
Johannes Berg4c476992010-10-04 21:36:35 +02002204 if (!rdev->ops->get_key)
2205 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002206
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002207 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002208 if (!msg)
2209 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002210
Eric W. Biederman15e47302012-09-07 20:12:54 +00002211 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002212 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002213 if (IS_ERR(hdr))
2214 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002215
2216 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002217 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002218
David S. Miller9360ffd2012-03-29 04:41:26 -04002219 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2220 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2221 goto nla_put_failure;
2222 if (mac_addr &&
2223 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2224 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002225
Johannes Berge31b8212010-10-05 19:39:30 +02002226 if (pairwise && mac_addr &&
2227 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2228 return -ENOENT;
2229
Hila Gonene35e4d22012-06-27 17:19:42 +03002230 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2231 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002232
2233 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002234 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002235
2236 if (cookie.error)
2237 goto nla_put_failure;
2238
2239 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002240 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002241
2242 nla_put_failure:
2243 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002244 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002245 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002246 return err;
2247}
2248
2249static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2250{
Johannes Berg4c476992010-10-04 21:36:35 +02002251 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002252 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002253 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002254 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002255
Johannes Bergb9454e82009-07-08 13:29:08 +02002256 err = nl80211_parse_key(info, &key);
2257 if (err)
2258 return err;
2259
2260 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002261 return -EINVAL;
2262
Johannes Bergb9454e82009-07-08 13:29:08 +02002263 /* only support setting default key */
2264 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002265 return -EINVAL;
2266
Johannes Bergfffd0932009-07-08 14:22:54 +02002267 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002268
2269 if (key.def) {
2270 if (!rdev->ops->set_default_key) {
2271 err = -EOPNOTSUPP;
2272 goto out;
2273 }
2274
2275 err = nl80211_key_allowed(dev->ieee80211_ptr);
2276 if (err)
2277 goto out;
2278
Hila Gonene35e4d22012-06-27 17:19:42 +03002279 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002280 key.def_uni, key.def_multi);
2281
2282 if (err)
2283 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002284
Johannes Berg3d23e342009-09-29 23:27:28 +02002285#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002286 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002287#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002288 } else {
2289 if (key.def_uni || !key.def_multi) {
2290 err = -EINVAL;
2291 goto out;
2292 }
2293
2294 if (!rdev->ops->set_default_mgmt_key) {
2295 err = -EOPNOTSUPP;
2296 goto out;
2297 }
2298
2299 err = nl80211_key_allowed(dev->ieee80211_ptr);
2300 if (err)
2301 goto out;
2302
Hila Gonene35e4d22012-06-27 17:19:42 +03002303 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002304 if (err)
2305 goto out;
2306
2307#ifdef CONFIG_CFG80211_WEXT
2308 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2309#endif
2310 }
2311
2312 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002313 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002314
Johannes Berg41ade002007-12-19 02:03:29 +01002315 return err;
2316}
2317
2318static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2319{
Johannes Berg4c476992010-10-04 21:36:35 +02002320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002321 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002322 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002323 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002324 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002325
Johannes Bergb9454e82009-07-08 13:29:08 +02002326 err = nl80211_parse_key(info, &key);
2327 if (err)
2328 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002329
Johannes Bergb9454e82009-07-08 13:29:08 +02002330 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002331 return -EINVAL;
2332
Johannes Berg41ade002007-12-19 02:03:29 +01002333 if (info->attrs[NL80211_ATTR_MAC])
2334 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2335
Johannes Berge31b8212010-10-05 19:39:30 +02002336 if (key.type == -1) {
2337 if (mac_addr)
2338 key.type = NL80211_KEYTYPE_PAIRWISE;
2339 else
2340 key.type = NL80211_KEYTYPE_GROUP;
2341 }
2342
2343 /* for now */
2344 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2345 key.type != NL80211_KEYTYPE_GROUP)
2346 return -EINVAL;
2347
Johannes Berg4c476992010-10-04 21:36:35 +02002348 if (!rdev->ops->add_key)
2349 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002350
Johannes Berge31b8212010-10-05 19:39:30 +02002351 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2352 key.type == NL80211_KEYTYPE_PAIRWISE,
2353 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002354 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002355
2356 wdev_lock(dev->ieee80211_ptr);
2357 err = nl80211_key_allowed(dev->ieee80211_ptr);
2358 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002359 err = rdev_add_key(rdev, dev, key.idx,
2360 key.type == NL80211_KEYTYPE_PAIRWISE,
2361 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002362 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002363
Johannes Berg41ade002007-12-19 02:03:29 +01002364 return err;
2365}
2366
2367static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2368{
Johannes Berg4c476992010-10-04 21:36:35 +02002369 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002370 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002371 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002372 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002373 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002374
Johannes Bergb9454e82009-07-08 13:29:08 +02002375 err = nl80211_parse_key(info, &key);
2376 if (err)
2377 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002378
2379 if (info->attrs[NL80211_ATTR_MAC])
2380 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2381
Johannes Berge31b8212010-10-05 19:39:30 +02002382 if (key.type == -1) {
2383 if (mac_addr)
2384 key.type = NL80211_KEYTYPE_PAIRWISE;
2385 else
2386 key.type = NL80211_KEYTYPE_GROUP;
2387 }
2388
2389 /* for now */
2390 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2391 key.type != NL80211_KEYTYPE_GROUP)
2392 return -EINVAL;
2393
Johannes Berg4c476992010-10-04 21:36:35 +02002394 if (!rdev->ops->del_key)
2395 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002396
Johannes Bergfffd0932009-07-08 14:22:54 +02002397 wdev_lock(dev->ieee80211_ptr);
2398 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002399
2400 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2401 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2402 err = -ENOENT;
2403
Johannes Bergfffd0932009-07-08 14:22:54 +02002404 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002405 err = rdev_del_key(rdev, dev, key.idx,
2406 key.type == NL80211_KEYTYPE_PAIRWISE,
2407 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002408
Johannes Berg3d23e342009-09-29 23:27:28 +02002409#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002410 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002411 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002412 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002413 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002414 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2415 }
2416#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002417 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002418
Johannes Berg41ade002007-12-19 02:03:29 +01002419 return err;
2420}
2421
Johannes Berg88600202012-02-13 15:17:18 +01002422static int nl80211_parse_beacon(struct genl_info *info,
2423 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002424{
Johannes Berg88600202012-02-13 15:17:18 +01002425 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002426
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002427 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2428 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2429 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2430 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002431 return -EINVAL;
2432
Johannes Berg88600202012-02-13 15:17:18 +01002433 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002434
Johannes Berged1b6cc2007-12-19 02:03:32 +01002435 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002436 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2437 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2438 if (!bcn->head_len)
2439 return -EINVAL;
2440 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002441 }
2442
2443 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002444 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2445 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002446 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002447 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002448 }
2449
Johannes Berg4c476992010-10-04 21:36:35 +02002450 if (!haveinfo)
2451 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002452
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002453 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002454 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2455 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002456 }
2457
2458 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002459 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002460 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002461 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002462 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2463 }
2464
2465 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002466 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002467 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002468 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002469 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2470 }
2471
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002472 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002473 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002474 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002475 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002476 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2477 }
2478
Johannes Berg88600202012-02-13 15:17:18 +01002479 return 0;
2480}
2481
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002482static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2483 struct cfg80211_ap_settings *params)
2484{
2485 struct wireless_dev *wdev;
2486 bool ret = false;
2487
2488 mutex_lock(&rdev->devlist_mtx);
2489
Johannes Berg89a54e42012-06-15 14:33:17 +02002490 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002491 if (wdev->iftype != NL80211_IFTYPE_AP &&
2492 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2493 continue;
2494
2495 if (!wdev->preset_chan)
2496 continue;
2497
2498 params->channel = wdev->preset_chan;
2499 params->channel_type = wdev->preset_chantype;
2500 ret = true;
2501 break;
2502 }
2503
2504 mutex_unlock(&rdev->devlist_mtx);
2505
2506 return ret;
2507}
2508
Jouni Malinene39e5b52012-09-30 19:29:39 +03002509static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2510 enum nl80211_auth_type auth_type,
2511 enum nl80211_commands cmd)
2512{
2513 if (auth_type > NL80211_AUTHTYPE_MAX)
2514 return false;
2515
2516 switch (cmd) {
2517 case NL80211_CMD_AUTHENTICATE:
2518 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2519 auth_type == NL80211_AUTHTYPE_SAE)
2520 return false;
2521 return true;
2522 case NL80211_CMD_CONNECT:
2523 case NL80211_CMD_START_AP:
2524 /* SAE not supported yet */
2525 if (auth_type == NL80211_AUTHTYPE_SAE)
2526 return false;
2527 return true;
2528 default:
2529 return false;
2530 }
2531}
2532
Johannes Berg88600202012-02-13 15:17:18 +01002533static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2534{
2535 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2536 struct net_device *dev = info->user_ptr[1];
2537 struct wireless_dev *wdev = dev->ieee80211_ptr;
2538 struct cfg80211_ap_settings params;
2539 int err;
2540
2541 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2542 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2543 return -EOPNOTSUPP;
2544
2545 if (!rdev->ops->start_ap)
2546 return -EOPNOTSUPP;
2547
2548 if (wdev->beacon_interval)
2549 return -EALREADY;
2550
2551 memset(&params, 0, sizeof(params));
2552
2553 /* these are required for START_AP */
2554 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2555 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2556 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2557 return -EINVAL;
2558
2559 err = nl80211_parse_beacon(info, &params.beacon);
2560 if (err)
2561 return err;
2562
2563 params.beacon_interval =
2564 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2565 params.dtim_period =
2566 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2567
2568 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2569 if (err)
2570 return err;
2571
2572 /*
2573 * In theory, some of these attributes should be required here
2574 * but since they were not used when the command was originally
2575 * added, keep them optional for old user space programs to let
2576 * them continue to work with drivers that do not need the
2577 * additional information -- drivers must check!
2578 */
2579 if (info->attrs[NL80211_ATTR_SSID]) {
2580 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2581 params.ssid_len =
2582 nla_len(info->attrs[NL80211_ATTR_SSID]);
2583 if (params.ssid_len == 0 ||
2584 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2585 return -EINVAL;
2586 }
2587
2588 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2589 params.hidden_ssid = nla_get_u32(
2590 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2591 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2592 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2593 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2594 return -EINVAL;
2595 }
2596
2597 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2598
2599 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2600 params.auth_type = nla_get_u32(
2601 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002602 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2603 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002604 return -EINVAL;
2605 } else
2606 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2607
2608 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2609 NL80211_MAX_NR_CIPHER_SUITES);
2610 if (err)
2611 return err;
2612
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302613 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2614 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2615 return -EOPNOTSUPP;
2616 params.inactivity_timeout = nla_get_u16(
2617 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2618 }
2619
Johannes Bergaa430da2012-05-16 23:50:18 +02002620 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2621 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2622
2623 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2624 !nl80211_valid_channel_type(info, &channel_type))
2625 return -EINVAL;
2626
2627 params.channel = rdev_freq_to_chan(rdev,
2628 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2629 channel_type);
2630 if (!params.channel)
2631 return -EINVAL;
2632 params.channel_type = channel_type;
2633 } else if (wdev->preset_chan) {
2634 params.channel = wdev->preset_chan;
2635 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002636 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002637 return -EINVAL;
2638
2639 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2640 params.channel_type))
2641 return -EINVAL;
2642
Michal Kaziore4e32452012-06-29 12:47:08 +02002643 mutex_lock(&rdev->devlist_mtx);
2644 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2645 CHAN_MODE_SHARED);
2646 mutex_unlock(&rdev->devlist_mtx);
2647
2648 if (err)
2649 return err;
2650
Hila Gonene35e4d22012-06-27 17:19:42 +03002651 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002652 if (!err) {
2653 wdev->preset_chan = params.channel;
2654 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002655 wdev->beacon_interval = params.beacon_interval;
Michal Kaziorf4489eb2012-06-29 12:46:58 +02002656 wdev->channel = params.channel;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002657 wdev->ssid_len = params.ssid_len;
2658 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002659 }
Johannes Berg56d18932011-05-09 18:41:15 +02002660 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002661}
2662
Johannes Berg88600202012-02-13 15:17:18 +01002663static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2664{
2665 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2666 struct net_device *dev = info->user_ptr[1];
2667 struct wireless_dev *wdev = dev->ieee80211_ptr;
2668 struct cfg80211_beacon_data params;
2669 int err;
2670
2671 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2672 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2673 return -EOPNOTSUPP;
2674
2675 if (!rdev->ops->change_beacon)
2676 return -EOPNOTSUPP;
2677
2678 if (!wdev->beacon_interval)
2679 return -EINVAL;
2680
2681 err = nl80211_parse_beacon(info, &params);
2682 if (err)
2683 return err;
2684
Hila Gonene35e4d22012-06-27 17:19:42 +03002685 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002686}
2687
2688static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002689{
Johannes Berg4c476992010-10-04 21:36:35 +02002690 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2691 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002692
Michal Kazior60771782012-06-29 12:46:56 +02002693 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002694}
2695
Johannes Berg5727ef12007-12-19 02:03:34 +01002696static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2697 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2698 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2699 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002700 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002701 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002702 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002703};
2704
Johannes Bergeccb8e82009-05-11 21:57:56 +03002705static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002706 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002707 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002708{
2709 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002710 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002711 int flag;
2712
Johannes Bergeccb8e82009-05-11 21:57:56 +03002713 /*
2714 * Try parsing the new attribute first so userspace
2715 * can specify both for older kernels.
2716 */
2717 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2718 if (nla) {
2719 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002720
Johannes Bergeccb8e82009-05-11 21:57:56 +03002721 sta_flags = nla_data(nla);
2722 params->sta_flags_mask = sta_flags->mask;
2723 params->sta_flags_set = sta_flags->set;
2724 if ((params->sta_flags_mask |
2725 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2726 return -EINVAL;
2727 return 0;
2728 }
2729
2730 /* if present, parse the old attribute */
2731
2732 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002733 if (!nla)
2734 return 0;
2735
2736 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2737 nla, sta_flags_policy))
2738 return -EINVAL;
2739
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002740 /*
2741 * Only allow certain flags for interface types so that
2742 * other attributes are silently ignored. Remember that
2743 * this is backward compatibility code with old userspace
2744 * and shouldn't be hit in other cases anyway.
2745 */
2746 switch (iftype) {
2747 case NL80211_IFTYPE_AP:
2748 case NL80211_IFTYPE_AP_VLAN:
2749 case NL80211_IFTYPE_P2P_GO:
2750 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2751 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2752 BIT(NL80211_STA_FLAG_WME) |
2753 BIT(NL80211_STA_FLAG_MFP);
2754 break;
2755 case NL80211_IFTYPE_P2P_CLIENT:
2756 case NL80211_IFTYPE_STATION:
2757 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2758 BIT(NL80211_STA_FLAG_TDLS_PEER);
2759 break;
2760 case NL80211_IFTYPE_MESH_POINT:
2761 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2762 BIT(NL80211_STA_FLAG_MFP) |
2763 BIT(NL80211_STA_FLAG_AUTHORIZED);
2764 default:
2765 return -EINVAL;
2766 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002767
Johannes Berg3383b5a2012-05-10 20:14:43 +02002768 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2769 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002770 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002771
Johannes Berg3383b5a2012-05-10 20:14:43 +02002772 /* no longer support new API additions in old API */
2773 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2774 return -EINVAL;
2775 }
2776 }
2777
Johannes Berg5727ef12007-12-19 02:03:34 +01002778 return 0;
2779}
2780
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002781static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2782 int attr)
2783{
2784 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002785 u32 bitrate;
2786 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002787
2788 rate = nla_nest_start(msg, attr);
2789 if (!rate)
2790 goto nla_put_failure;
2791
2792 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2793 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002794 /* report 16-bit bitrate only if we can */
2795 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002796 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002797 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2798 (bitrate_compat > 0 &&
2799 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002800 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2801 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2802 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2803 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2804 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2805 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2806 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002807
2808 nla_nest_end(msg, rate);
2809 return true;
2810
2811nla_put_failure:
2812 return false;
2813}
2814
Eric W. Biederman15e47302012-09-07 20:12:54 +00002815static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002816 int flags,
2817 struct cfg80211_registered_device *rdev,
2818 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002819 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002820{
2821 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002822 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002823
Eric W. Biederman15e47302012-09-07 20:12:54 +00002824 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002825 if (!hdr)
2826 return -1;
2827
David S. Miller9360ffd2012-03-29 04:41:26 -04002828 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2829 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2830 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2831 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002832
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002833 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2834 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002835 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002836 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2837 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2838 sinfo->connected_time))
2839 goto nla_put_failure;
2840 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2841 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2842 sinfo->inactive_time))
2843 goto nla_put_failure;
2844 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2845 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2846 sinfo->rx_bytes))
2847 goto nla_put_failure;
2848 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2849 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2850 sinfo->tx_bytes))
2851 goto nla_put_failure;
2852 if ((sinfo->filled & STATION_INFO_LLID) &&
2853 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2854 goto nla_put_failure;
2855 if ((sinfo->filled & STATION_INFO_PLID) &&
2856 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2857 goto nla_put_failure;
2858 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2859 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2860 sinfo->plink_state))
2861 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002862 switch (rdev->wiphy.signal_type) {
2863 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002864 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2865 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2866 sinfo->signal))
2867 goto nla_put_failure;
2868 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2869 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2870 sinfo->signal_avg))
2871 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002872 break;
2873 default:
2874 break;
2875 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002876 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002877 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2878 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002879 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002880 }
2881 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2882 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2883 NL80211_STA_INFO_RX_BITRATE))
2884 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002885 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002886 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2887 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2888 sinfo->rx_packets))
2889 goto nla_put_failure;
2890 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2891 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2892 sinfo->tx_packets))
2893 goto nla_put_failure;
2894 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2895 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2896 sinfo->tx_retries))
2897 goto nla_put_failure;
2898 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2899 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2900 sinfo->tx_failed))
2901 goto nla_put_failure;
2902 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2903 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2904 sinfo->beacon_loss_count))
2905 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002906 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2907 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2908 if (!bss_param)
2909 goto nla_put_failure;
2910
David S. Miller9360ffd2012-03-29 04:41:26 -04002911 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2912 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2913 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2914 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2915 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2916 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2917 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2918 sinfo->bss_param.dtim_period) ||
2919 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2920 sinfo->bss_param.beacon_interval))
2921 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002922
2923 nla_nest_end(msg, bss_param);
2924 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002925 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2926 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2927 sizeof(struct nl80211_sta_flag_update),
2928 &sinfo->sta_flags))
2929 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002930 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2931 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2932 sinfo->t_offset))
2933 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002934 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002935
David S. Miller9360ffd2012-03-29 04:41:26 -04002936 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2937 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2938 sinfo->assoc_req_ies))
2939 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002940
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002941 return genlmsg_end(msg, hdr);
2942
2943 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002944 genlmsg_cancel(msg, hdr);
2945 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002946}
2947
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002948static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002949 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002950{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002951 struct station_info sinfo;
2952 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002953 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002954 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002955 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002956 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002957
Johannes Berg67748892010-10-04 21:14:06 +02002958 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2959 if (err)
2960 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002961
2962 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002963 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002964 goto out_err;
2965 }
2966
Johannes Bergbba95fe2008-07-29 13:22:51 +02002967 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002968 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03002969 err = rdev_dump_station(dev, netdev, sta_idx,
2970 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002971 if (err == -ENOENT)
2972 break;
2973 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002974 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002975
2976 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002977 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002978 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002979 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002980 &sinfo) < 0)
2981 goto out;
2982
2983 sta_idx++;
2984 }
2985
2986
2987 out:
2988 cb->args[1] = sta_idx;
2989 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002990 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002991 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002992
2993 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002994}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002995
Johannes Berg5727ef12007-12-19 02:03:34 +01002996static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2997{
Johannes Berg4c476992010-10-04 21:36:35 +02002998 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2999 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003000 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003001 struct sk_buff *msg;
3002 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003003 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003004
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003005 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003006
3007 if (!info->attrs[NL80211_ATTR_MAC])
3008 return -EINVAL;
3009
3010 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3011
Johannes Berg4c476992010-10-04 21:36:35 +02003012 if (!rdev->ops->get_station)
3013 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003014
Hila Gonene35e4d22012-06-27 17:19:42 +03003015 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003016 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003017 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003018
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003019 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003020 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003021 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003022
Eric W. Biederman15e47302012-09-07 20:12:54 +00003023 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003024 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003025 nlmsg_free(msg);
3026 return -ENOBUFS;
3027 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003028
Johannes Berg4c476992010-10-04 21:36:35 +02003029 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003030}
3031
3032/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003033 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003034 */
Johannes Berg80b99892011-11-18 16:23:01 +01003035static struct net_device *get_vlan(struct genl_info *info,
3036 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003037{
Johannes Berg463d0182009-07-14 00:33:35 +02003038 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003039 struct net_device *v;
3040 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003041
Johannes Berg80b99892011-11-18 16:23:01 +01003042 if (!vlanattr)
3043 return NULL;
3044
3045 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3046 if (!v)
3047 return ERR_PTR(-ENODEV);
3048
3049 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3050 ret = -EINVAL;
3051 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003052 }
Johannes Berg80b99892011-11-18 16:23:01 +01003053
3054 if (!netif_running(v)) {
3055 ret = -ENETDOWN;
3056 goto error;
3057 }
3058
3059 return v;
3060 error:
3061 dev_put(v);
3062 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003063}
3064
3065static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3066{
Johannes Berg4c476992010-10-04 21:36:35 +02003067 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003068 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003069 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003070 struct station_parameters params;
3071 u8 *mac_addr = NULL;
3072
3073 memset(&params, 0, sizeof(params));
3074
3075 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003076 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003077
3078 if (info->attrs[NL80211_ATTR_STA_AID])
3079 return -EINVAL;
3080
3081 if (!info->attrs[NL80211_ATTR_MAC])
3082 return -EINVAL;
3083
3084 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3085
3086 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3087 params.supported_rates =
3088 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3089 params.supported_rates_len =
3090 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3091 }
3092
3093 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3094 params.listen_interval =
3095 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3096
Jouni Malinen36aedc92008-08-25 11:58:58 +03003097 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3098 params.ht_capa =
3099 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3100
Johannes Bergbdd90d52011-12-14 12:20:27 +01003101 if (!rdev->ops->change_station)
3102 return -EOPNOTSUPP;
3103
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003104 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003105 return -EINVAL;
3106
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003107 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3108 params.plink_action =
3109 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3110
Javier Cardona9c3990a2011-05-03 16:57:11 -07003111 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3112 params.plink_state =
3113 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3114
Johannes Berga97f4422009-06-18 17:23:43 +02003115 switch (dev->ieee80211_ptr->iftype) {
3116 case NL80211_IFTYPE_AP:
3117 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003118 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003119 /* disallow mesh-specific things */
3120 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003121 return -EINVAL;
3122
3123 /* TDLS can't be set, ... */
3124 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3125 return -EINVAL;
3126 /*
3127 * ... but don't bother the driver with it. This works around
3128 * a hostapd/wpa_supplicant issue -- it always includes the
3129 * TLDS_PEER flag in the mask even for AP mode.
3130 */
3131 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3132
3133 /* accept only the listed bits */
3134 if (params.sta_flags_mask &
3135 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3136 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3137 BIT(NL80211_STA_FLAG_WME) |
3138 BIT(NL80211_STA_FLAG_MFP)))
3139 return -EINVAL;
3140
3141 /* must be last in here for error handling */
3142 params.vlan = get_vlan(info, rdev);
3143 if (IS_ERR(params.vlan))
3144 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003145 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003146 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003147 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003148 /*
3149 * Don't allow userspace to change the TDLS_PEER flag,
3150 * but silently ignore attempts to change it since we
3151 * don't have state here to verify that it doesn't try
3152 * to change the flag.
3153 */
3154 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003155 /* fall through */
3156 case NL80211_IFTYPE_ADHOC:
3157 /* disallow things sta doesn't support */
3158 if (params.plink_action)
3159 return -EINVAL;
3160 if (params.ht_capa)
3161 return -EINVAL;
3162 if (params.listen_interval >= 0)
3163 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003164 /* reject any changes other than AUTHORIZED */
3165 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3166 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003167 break;
3168 case NL80211_IFTYPE_MESH_POINT:
3169 /* disallow things mesh doesn't support */
3170 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003171 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003172 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003173 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003174 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003175 return -EINVAL;
3176 /*
3177 * No special handling for TDLS here -- the userspace
3178 * mesh code doesn't have this bug.
3179 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003180 if (params.sta_flags_mask &
3181 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003182 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003183 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003184 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003185 break;
3186 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003187 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003188 }
3189
Johannes Bergbdd90d52011-12-14 12:20:27 +01003190 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003191
Hila Gonene35e4d22012-06-27 17:19:42 +03003192 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003193
Johannes Berg5727ef12007-12-19 02:03:34 +01003194 if (params.vlan)
3195 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003196
Johannes Berg5727ef12007-12-19 02:03:34 +01003197 return err;
3198}
3199
Eliad Pellerc75786c2011-08-23 14:37:46 +03003200static struct nla_policy
3201nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3202 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3203 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3204};
3205
Johannes Berg5727ef12007-12-19 02:03:34 +01003206static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3207{
Johannes Berg4c476992010-10-04 21:36:35 +02003208 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003209 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003210 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003211 struct station_parameters params;
3212 u8 *mac_addr = NULL;
3213
3214 memset(&params, 0, sizeof(params));
3215
3216 if (!info->attrs[NL80211_ATTR_MAC])
3217 return -EINVAL;
3218
Johannes Berg5727ef12007-12-19 02:03:34 +01003219 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3220 return -EINVAL;
3221
3222 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3223 return -EINVAL;
3224
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003225 if (!info->attrs[NL80211_ATTR_STA_AID])
3226 return -EINVAL;
3227
Johannes Berg5727ef12007-12-19 02:03:34 +01003228 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3229 params.supported_rates =
3230 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3231 params.supported_rates_len =
3232 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3233 params.listen_interval =
3234 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003235
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003236 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3237 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3238 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003239
Jouni Malinen36aedc92008-08-25 11:58:58 +03003240 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3241 params.ht_capa =
3242 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003243
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003244 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3245 params.vht_capa =
3246 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3247
Javier Cardona96b78df2011-04-07 15:08:33 -07003248 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3249 params.plink_action =
3250 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3251
Johannes Bergbdd90d52011-12-14 12:20:27 +01003252 if (!rdev->ops->add_station)
3253 return -EOPNOTSUPP;
3254
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003255 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003256 return -EINVAL;
3257
Johannes Bergbdd90d52011-12-14 12:20:27 +01003258 switch (dev->ieee80211_ptr->iftype) {
3259 case NL80211_IFTYPE_AP:
3260 case NL80211_IFTYPE_AP_VLAN:
3261 case NL80211_IFTYPE_P2P_GO:
3262 /* parse WME attributes if sta is WME capable */
3263 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3264 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3265 info->attrs[NL80211_ATTR_STA_WME]) {
3266 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3267 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003268
Johannes Bergbdd90d52011-12-14 12:20:27 +01003269 nla = info->attrs[NL80211_ATTR_STA_WME];
3270 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3271 nl80211_sta_wme_policy);
3272 if (err)
3273 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003274
Johannes Bergbdd90d52011-12-14 12:20:27 +01003275 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3276 params.uapsd_queues =
3277 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3278 if (params.uapsd_queues &
3279 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3280 return -EINVAL;
3281
3282 if (tb[NL80211_STA_WME_MAX_SP])
3283 params.max_sp =
3284 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3285
3286 if (params.max_sp &
3287 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3288 return -EINVAL;
3289
3290 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3291 }
3292 /* TDLS peers cannot be added */
3293 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003294 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003295 /* but don't bother the driver with it */
3296 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003297
Johannes Bergbdd90d52011-12-14 12:20:27 +01003298 /* must be last in here for error handling */
3299 params.vlan = get_vlan(info, rdev);
3300 if (IS_ERR(params.vlan))
3301 return PTR_ERR(params.vlan);
3302 break;
3303 case NL80211_IFTYPE_MESH_POINT:
3304 /* TDLS peers cannot be added */
3305 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003306 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003307 break;
3308 case NL80211_IFTYPE_STATION:
3309 /* Only TDLS peers can be added */
3310 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3311 return -EINVAL;
3312 /* Can only add if TDLS ... */
3313 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3314 return -EOPNOTSUPP;
3315 /* ... with external setup is supported */
3316 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3317 return -EOPNOTSUPP;
3318 break;
3319 default:
3320 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003321 }
3322
Johannes Bergbdd90d52011-12-14 12:20:27 +01003323 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003324
Hila Gonene35e4d22012-06-27 17:19:42 +03003325 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003326
Johannes Berg5727ef12007-12-19 02:03:34 +01003327 if (params.vlan)
3328 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003329 return err;
3330}
3331
3332static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3333{
Johannes Berg4c476992010-10-04 21:36:35 +02003334 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3335 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003336 u8 *mac_addr = NULL;
3337
3338 if (info->attrs[NL80211_ATTR_MAC])
3339 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3340
Johannes Berge80cf852009-05-11 14:43:13 +02003341 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003342 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003343 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003344 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3345 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003346
Johannes Berg4c476992010-10-04 21:36:35 +02003347 if (!rdev->ops->del_station)
3348 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003349
Hila Gonene35e4d22012-06-27 17:19:42 +03003350 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003351}
3352
Eric W. Biederman15e47302012-09-07 20:12:54 +00003353static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003354 int flags, struct net_device *dev,
3355 u8 *dst, u8 *next_hop,
3356 struct mpath_info *pinfo)
3357{
3358 void *hdr;
3359 struct nlattr *pinfoattr;
3360
Eric W. Biederman15e47302012-09-07 20:12:54 +00003361 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003362 if (!hdr)
3363 return -1;
3364
David S. Miller9360ffd2012-03-29 04:41:26 -04003365 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3366 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3367 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3368 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3369 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003370
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003371 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3372 if (!pinfoattr)
3373 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003374 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3375 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3376 pinfo->frame_qlen))
3377 goto nla_put_failure;
3378 if (((pinfo->filled & MPATH_INFO_SN) &&
3379 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3380 ((pinfo->filled & MPATH_INFO_METRIC) &&
3381 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3382 pinfo->metric)) ||
3383 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3384 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3385 pinfo->exptime)) ||
3386 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3387 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3388 pinfo->flags)) ||
3389 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3390 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3391 pinfo->discovery_timeout)) ||
3392 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3393 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3394 pinfo->discovery_retries)))
3395 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003396
3397 nla_nest_end(msg, pinfoattr);
3398
3399 return genlmsg_end(msg, hdr);
3400
3401 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003402 genlmsg_cancel(msg, hdr);
3403 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003404}
3405
3406static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003407 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003408{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003409 struct mpath_info pinfo;
3410 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003411 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003412 u8 dst[ETH_ALEN];
3413 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003414 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003415 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003416
Johannes Berg67748892010-10-04 21:14:06 +02003417 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3418 if (err)
3419 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003420
3421 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003422 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003423 goto out_err;
3424 }
3425
Jouni Malineneec60b02009-03-20 21:21:19 +02003426 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3427 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003428 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003429 }
3430
Johannes Bergbba95fe2008-07-29 13:22:51 +02003431 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003432 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3433 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003434 if (err == -ENOENT)
3435 break;
3436 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003437 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003438
Eric W. Biederman15e47302012-09-07 20:12:54 +00003439 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003440 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3441 netdev, dst, next_hop,
3442 &pinfo) < 0)
3443 goto out;
3444
3445 path_idx++;
3446 }
3447
3448
3449 out:
3450 cb->args[1] = path_idx;
3451 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003452 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003453 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003454 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003455}
3456
3457static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3458{
Johannes Berg4c476992010-10-04 21:36:35 +02003459 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003460 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003461 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003462 struct mpath_info pinfo;
3463 struct sk_buff *msg;
3464 u8 *dst = NULL;
3465 u8 next_hop[ETH_ALEN];
3466
3467 memset(&pinfo, 0, sizeof(pinfo));
3468
3469 if (!info->attrs[NL80211_ATTR_MAC])
3470 return -EINVAL;
3471
3472 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3473
Johannes Berg4c476992010-10-04 21:36:35 +02003474 if (!rdev->ops->get_mpath)
3475 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003476
Johannes Berg4c476992010-10-04 21:36:35 +02003477 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3478 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003479
Hila Gonene35e4d22012-06-27 17:19:42 +03003480 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003481 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003482 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003483
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003484 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003485 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003486 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003487
Eric W. Biederman15e47302012-09-07 20:12:54 +00003488 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003489 dev, dst, next_hop, &pinfo) < 0) {
3490 nlmsg_free(msg);
3491 return -ENOBUFS;
3492 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003493
Johannes Berg4c476992010-10-04 21:36:35 +02003494 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003495}
3496
3497static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3498{
Johannes Berg4c476992010-10-04 21:36:35 +02003499 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3500 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003501 u8 *dst = NULL;
3502 u8 *next_hop = NULL;
3503
3504 if (!info->attrs[NL80211_ATTR_MAC])
3505 return -EINVAL;
3506
3507 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3508 return -EINVAL;
3509
3510 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3511 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3512
Johannes Berg4c476992010-10-04 21:36:35 +02003513 if (!rdev->ops->change_mpath)
3514 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003515
Johannes Berg4c476992010-10-04 21:36:35 +02003516 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3517 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003518
Hila Gonene35e4d22012-06-27 17:19:42 +03003519 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003520}
Johannes Berg4c476992010-10-04 21:36:35 +02003521
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003522static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3523{
Johannes Berg4c476992010-10-04 21:36:35 +02003524 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3525 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003526 u8 *dst = NULL;
3527 u8 *next_hop = NULL;
3528
3529 if (!info->attrs[NL80211_ATTR_MAC])
3530 return -EINVAL;
3531
3532 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3533 return -EINVAL;
3534
3535 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3536 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3537
Johannes Berg4c476992010-10-04 21:36:35 +02003538 if (!rdev->ops->add_mpath)
3539 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003540
Johannes Berg4c476992010-10-04 21:36:35 +02003541 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3542 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003543
Hila Gonene35e4d22012-06-27 17:19:42 +03003544 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003545}
3546
3547static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3548{
Johannes Berg4c476992010-10-04 21:36:35 +02003549 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3550 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003551 u8 *dst = NULL;
3552
3553 if (info->attrs[NL80211_ATTR_MAC])
3554 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3555
Johannes Berg4c476992010-10-04 21:36:35 +02003556 if (!rdev->ops->del_mpath)
3557 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003558
Hila Gonene35e4d22012-06-27 17:19:42 +03003559 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003560}
3561
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003562static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3563{
Johannes Berg4c476992010-10-04 21:36:35 +02003564 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3565 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003566 struct bss_parameters params;
3567
3568 memset(&params, 0, sizeof(params));
3569 /* default to not changing parameters */
3570 params.use_cts_prot = -1;
3571 params.use_short_preamble = -1;
3572 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003573 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003574 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003575
3576 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3577 params.use_cts_prot =
3578 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3579 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3580 params.use_short_preamble =
3581 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3582 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3583 params.use_short_slot_time =
3584 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003585 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3586 params.basic_rates =
3587 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3588 params.basic_rates_len =
3589 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3590 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003591 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3592 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003593 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3594 params.ht_opmode =
3595 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003596
Johannes Berg4c476992010-10-04 21:36:35 +02003597 if (!rdev->ops->change_bss)
3598 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003599
Johannes Berg074ac8d2010-09-16 14:58:22 +02003600 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003601 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3602 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003603
Hila Gonene35e4d22012-06-27 17:19:42 +03003604 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003605}
3606
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003607static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003608 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3609 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3610 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3611 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3612 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3613 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3614};
3615
3616static int parse_reg_rule(struct nlattr *tb[],
3617 struct ieee80211_reg_rule *reg_rule)
3618{
3619 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3620 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3621
3622 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3623 return -EINVAL;
3624 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3625 return -EINVAL;
3626 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3627 return -EINVAL;
3628 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3629 return -EINVAL;
3630 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3631 return -EINVAL;
3632
3633 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3634
3635 freq_range->start_freq_khz =
3636 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3637 freq_range->end_freq_khz =
3638 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3639 freq_range->max_bandwidth_khz =
3640 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3641
3642 power_rule->max_eirp =
3643 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3644
3645 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3646 power_rule->max_antenna_gain =
3647 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3648
3649 return 0;
3650}
3651
3652static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3653{
3654 int r;
3655 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003656 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003657
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003658 /*
3659 * You should only get this when cfg80211 hasn't yet initialized
3660 * completely when built-in to the kernel right between the time
3661 * window between nl80211_init() and regulatory_init(), if that is
3662 * even possible.
3663 */
3664 mutex_lock(&cfg80211_mutex);
3665 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003666 mutex_unlock(&cfg80211_mutex);
3667 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003668 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003669 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003670
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003671 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3672 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003673
3674 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3675
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003676 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3677 user_reg_hint_type =
3678 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3679 else
3680 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3681
3682 switch (user_reg_hint_type) {
3683 case NL80211_USER_REG_HINT_USER:
3684 case NL80211_USER_REG_HINT_CELL_BASE:
3685 break;
3686 default:
3687 return -EINVAL;
3688 }
3689
3690 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003691
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003692 return r;
3693}
3694
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003695static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003696 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003697{
Johannes Berg4c476992010-10-04 21:36:35 +02003698 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003699 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003700 struct wireless_dev *wdev = dev->ieee80211_ptr;
3701 struct mesh_config cur_params;
3702 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003703 void *hdr;
3704 struct nlattr *pinfoattr;
3705 struct sk_buff *msg;
3706
Johannes Berg29cbe682010-12-03 09:20:44 +01003707 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3708 return -EOPNOTSUPP;
3709
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003710 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003711 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003712
Johannes Berg29cbe682010-12-03 09:20:44 +01003713 wdev_lock(wdev);
3714 /* If not connected, get default parameters */
3715 if (!wdev->mesh_id_len)
3716 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3717 else
Hila Gonene35e4d22012-06-27 17:19:42 +03003718 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01003719 wdev_unlock(wdev);
3720
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003721 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003722 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003723
3724 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003725 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003726 if (!msg)
3727 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003728 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003729 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003730 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003731 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003732 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003733 if (!pinfoattr)
3734 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003735 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3736 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3737 cur_params.dot11MeshRetryTimeout) ||
3738 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3739 cur_params.dot11MeshConfirmTimeout) ||
3740 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3741 cur_params.dot11MeshHoldingTimeout) ||
3742 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3743 cur_params.dot11MeshMaxPeerLinks) ||
3744 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3745 cur_params.dot11MeshMaxRetries) ||
3746 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3747 cur_params.dot11MeshTTL) ||
3748 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3749 cur_params.element_ttl) ||
3750 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3751 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003752 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3753 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003754 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3755 cur_params.dot11MeshHWMPmaxPREQretries) ||
3756 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3757 cur_params.path_refresh_time) ||
3758 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3759 cur_params.min_discovery_timeout) ||
3760 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3761 cur_params.dot11MeshHWMPactivePathTimeout) ||
3762 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3763 cur_params.dot11MeshHWMPpreqMinInterval) ||
3764 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3765 cur_params.dot11MeshHWMPperrMinInterval) ||
3766 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3767 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3768 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3769 cur_params.dot11MeshHWMPRootMode) ||
3770 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3771 cur_params.dot11MeshHWMPRannInterval) ||
3772 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3773 cur_params.dot11MeshGateAnnouncementProtocol) ||
3774 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3775 cur_params.dot11MeshForwarding) ||
3776 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003777 cur_params.rssi_threshold) ||
3778 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003779 cur_params.ht_opmode) ||
3780 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3781 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3782 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003783 cur_params.dot11MeshHWMProotInterval) ||
3784 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3785 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003786 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003787 nla_nest_end(msg, pinfoattr);
3788 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003789 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003790
Johannes Berg3b858752009-03-12 09:55:09 +01003791 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003792 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003793 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003794 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003795 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003796}
3797
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003798static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003799 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3800 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3801 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3802 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3803 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3804 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003805 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003806 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003807 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003808 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3809 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3810 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3811 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3812 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003813 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003814 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003815 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003816 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003817 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003818 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003819 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3820 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003821 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3822 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003823 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003824};
3825
Javier Cardonac80d5452010-12-16 17:37:49 -08003826static const struct nla_policy
3827 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003828 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003829 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3830 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003831 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003832 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003833 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003834 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003835};
3836
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003837static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003838 struct mesh_config *cfg,
3839 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003840{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003841 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003842 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003843
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003844#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3845do {\
3846 if (table[attr_num]) {\
3847 cfg->param = nla_fn(table[attr_num]); \
3848 mask |= (1 << (attr_num - 1)); \
3849 } \
3850} while (0);\
3851
3852
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003853 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003854 return -EINVAL;
3855 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003856 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003857 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003858 return -EINVAL;
3859
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003860 /* This makes sure that there aren't more than 32 mesh config
3861 * parameters (otherwise our bitfield scheme would not work.) */
3862 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3863
3864 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003865 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003866 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3867 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003868 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003869 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3870 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003871 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003872 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3873 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003874 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003875 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3876 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003877 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003878 mask, NL80211_MESHCONF_MAX_RETRIES,
3879 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003880 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003881 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003882 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003883 mask, NL80211_MESHCONF_ELEMENT_TTL,
3884 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003885 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003886 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3887 nla_get_u8);
3888 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3889 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3890 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003891 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003892 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3893 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003894 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003895 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3896 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003897 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003898 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3899 nla_get_u16);
3900 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3901 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3902 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003903 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003904 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3905 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003906 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003907 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3908 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003909 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003910 dot11MeshHWMPnetDiameterTraversalTime, mask,
3911 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3912 nla_get_u16);
3913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3914 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3915 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3916 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3917 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003918 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003919 dot11MeshGateAnnouncementProtocol, mask,
3920 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3921 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003922 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003923 mask, NL80211_MESHCONF_FORWARDING,
3924 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003925 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003926 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3927 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003928 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003929 mask, NL80211_MESHCONF_HT_OPMODE,
3930 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003931 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3932 mask,
3933 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3934 nla_get_u32);
3935 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3936 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3937 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003938 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3939 dot11MeshHWMPconfirmationInterval, mask,
3940 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3941 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003942 if (mask_out)
3943 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003944
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003945 return 0;
3946
3947#undef FILL_IN_MESH_PARAM_IF_SET
3948}
3949
Javier Cardonac80d5452010-12-16 17:37:49 -08003950static int nl80211_parse_mesh_setup(struct genl_info *info,
3951 struct mesh_setup *setup)
3952{
3953 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3954
3955 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3956 return -EINVAL;
3957 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3958 info->attrs[NL80211_ATTR_MESH_SETUP],
3959 nl80211_mesh_setup_params_policy))
3960 return -EINVAL;
3961
Javier Cardonad299a1f2012-03-31 11:31:33 -07003962 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3963 setup->sync_method =
3964 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3965 IEEE80211_SYNC_METHOD_VENDOR :
3966 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3967
Javier Cardonac80d5452010-12-16 17:37:49 -08003968 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3969 setup->path_sel_proto =
3970 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3971 IEEE80211_PATH_PROTOCOL_VENDOR :
3972 IEEE80211_PATH_PROTOCOL_HWMP;
3973
3974 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3975 setup->path_metric =
3976 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3977 IEEE80211_PATH_METRIC_VENDOR :
3978 IEEE80211_PATH_METRIC_AIRTIME;
3979
Javier Cardona581a8b02011-04-07 15:08:27 -07003980
3981 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003982 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003983 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003984 if (!is_valid_ie_attr(ieattr))
3985 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003986 setup->ie = nla_data(ieattr);
3987 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003988 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003989 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3990 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003991
3992 return 0;
3993}
3994
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003995static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003996 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003997{
3998 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3999 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004000 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004001 struct mesh_config cfg;
4002 u32 mask;
4003 int err;
4004
Johannes Berg29cbe682010-12-03 09:20:44 +01004005 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4006 return -EOPNOTSUPP;
4007
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004008 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004009 return -EOPNOTSUPP;
4010
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004011 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004012 if (err)
4013 return err;
4014
Johannes Berg29cbe682010-12-03 09:20:44 +01004015 wdev_lock(wdev);
4016 if (!wdev->mesh_id_len)
4017 err = -ENOLINK;
4018
4019 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004020 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004021
4022 wdev_unlock(wdev);
4023
4024 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004025}
4026
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004027static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4028{
4029 struct sk_buff *msg;
4030 void *hdr = NULL;
4031 struct nlattr *nl_reg_rules;
4032 unsigned int i;
4033 int err = -EINVAL;
4034
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004035 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004036
4037 if (!cfg80211_regdomain)
4038 goto out;
4039
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004040 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004041 if (!msg) {
4042 err = -ENOBUFS;
4043 goto out;
4044 }
4045
Eric W. Biederman15e47302012-09-07 20:12:54 +00004046 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004047 NL80211_CMD_GET_REG);
4048 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004049 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004050
David S. Miller9360ffd2012-03-29 04:41:26 -04004051 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4052 cfg80211_regdomain->alpha2) ||
4053 (cfg80211_regdomain->dfs_region &&
4054 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4055 cfg80211_regdomain->dfs_region)))
4056 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004057
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004058 if (reg_last_request_cell_base() &&
4059 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4060 NL80211_USER_REG_HINT_CELL_BASE))
4061 goto nla_put_failure;
4062
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004063 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4064 if (!nl_reg_rules)
4065 goto nla_put_failure;
4066
4067 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4068 struct nlattr *nl_reg_rule;
4069 const struct ieee80211_reg_rule *reg_rule;
4070 const struct ieee80211_freq_range *freq_range;
4071 const struct ieee80211_power_rule *power_rule;
4072
4073 reg_rule = &cfg80211_regdomain->reg_rules[i];
4074 freq_range = &reg_rule->freq_range;
4075 power_rule = &reg_rule->power_rule;
4076
4077 nl_reg_rule = nla_nest_start(msg, i);
4078 if (!nl_reg_rule)
4079 goto nla_put_failure;
4080
David S. Miller9360ffd2012-03-29 04:41:26 -04004081 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4082 reg_rule->flags) ||
4083 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4084 freq_range->start_freq_khz) ||
4085 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4086 freq_range->end_freq_khz) ||
4087 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4088 freq_range->max_bandwidth_khz) ||
4089 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4090 power_rule->max_antenna_gain) ||
4091 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4092 power_rule->max_eirp))
4093 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004094
4095 nla_nest_end(msg, nl_reg_rule);
4096 }
4097
4098 nla_nest_end(msg, nl_reg_rules);
4099
4100 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004101 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004102 goto out;
4103
4104nla_put_failure:
4105 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004106put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004107 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004108 err = -EMSGSIZE;
4109out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004110 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004111 return err;
4112}
4113
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004114static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4115{
4116 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4117 struct nlattr *nl_reg_rule;
4118 char *alpha2 = NULL;
4119 int rem_reg_rules = 0, r = 0;
4120 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004121 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004122 struct ieee80211_regdomain *rd = NULL;
4123
4124 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4125 return -EINVAL;
4126
4127 if (!info->attrs[NL80211_ATTR_REG_RULES])
4128 return -EINVAL;
4129
4130 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4131
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004132 if (info->attrs[NL80211_ATTR_DFS_REGION])
4133 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4134
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004135 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4136 rem_reg_rules) {
4137 num_rules++;
4138 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004139 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004140 }
4141
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004142 mutex_lock(&cfg80211_mutex);
4143
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004144 if (!reg_is_valid_request(alpha2)) {
4145 r = -EINVAL;
4146 goto bad_reg;
4147 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004148
4149 size_of_regd = sizeof(struct ieee80211_regdomain) +
4150 (num_rules * sizeof(struct ieee80211_reg_rule));
4151
4152 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004153 if (!rd) {
4154 r = -ENOMEM;
4155 goto bad_reg;
4156 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004157
4158 rd->n_reg_rules = num_rules;
4159 rd->alpha2[0] = alpha2[0];
4160 rd->alpha2[1] = alpha2[1];
4161
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004162 /*
4163 * Disable DFS master mode if the DFS region was
4164 * not supported or known on this kernel.
4165 */
4166 if (reg_supported_dfs_region(dfs_region))
4167 rd->dfs_region = dfs_region;
4168
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004169 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4170 rem_reg_rules) {
4171 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4172 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4173 reg_rule_policy);
4174 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4175 if (r)
4176 goto bad_reg;
4177
4178 rule_idx++;
4179
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004180 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4181 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004182 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004183 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004184 }
4185
4186 BUG_ON(rule_idx != num_rules);
4187
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004188 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004189
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004190 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004191
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004192 return r;
4193
Johannes Bergd2372b32008-10-24 20:32:20 +02004194 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004195 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004196 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004197 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004198}
4199
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004200static int validate_scan_freqs(struct nlattr *freqs)
4201{
4202 struct nlattr *attr1, *attr2;
4203 int n_channels = 0, tmp1, tmp2;
4204
4205 nla_for_each_nested(attr1, freqs, tmp1) {
4206 n_channels++;
4207 /*
4208 * Some hardware has a limited channel list for
4209 * scanning, and it is pretty much nonsensical
4210 * to scan for a channel twice, so disallow that
4211 * and don't require drivers to check that the
4212 * channel list they get isn't longer than what
4213 * they can scan, as long as they can scan all
4214 * the channels they registered at once.
4215 */
4216 nla_for_each_nested(attr2, freqs, tmp2)
4217 if (attr1 != attr2 &&
4218 nla_get_u32(attr1) == nla_get_u32(attr2))
4219 return 0;
4220 }
4221
4222 return n_channels;
4223}
4224
Johannes Berg2a519312009-02-10 21:25:55 +01004225static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4226{
Johannes Berg4c476992010-10-04 21:36:35 +02004227 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004228 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004229 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004230 struct nlattr *attr;
4231 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004232 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004233 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004234
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004235 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4236 return -EINVAL;
4237
Johannes Berg79c97e92009-07-07 03:56:12 +02004238 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004239
Johannes Berg4c476992010-10-04 21:36:35 +02004240 if (!rdev->ops->scan)
4241 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004242
Johannes Berg4c476992010-10-04 21:36:35 +02004243 if (rdev->scan_req)
4244 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004245
4246 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004247 n_channels = validate_scan_freqs(
4248 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004249 if (!n_channels)
4250 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004251 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004252 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004253 n_channels = 0;
4254
Johannes Berg2a519312009-02-10 21:25:55 +01004255 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4256 if (wiphy->bands[band])
4257 n_channels += wiphy->bands[band]->n_channels;
4258 }
4259
4260 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4261 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4262 n_ssids++;
4263
Johannes Berg4c476992010-10-04 21:36:35 +02004264 if (n_ssids > wiphy->max_scan_ssids)
4265 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004266
Jouni Malinen70692ad2009-02-16 19:39:13 +02004267 if (info->attrs[NL80211_ATTR_IE])
4268 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4269 else
4270 ie_len = 0;
4271
Johannes Berg4c476992010-10-04 21:36:35 +02004272 if (ie_len > wiphy->max_scan_ie_len)
4273 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004274
Johannes Berg2a519312009-02-10 21:25:55 +01004275 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004276 + sizeof(*request->ssids) * n_ssids
4277 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004278 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004279 if (!request)
4280 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004281
Johannes Berg2a519312009-02-10 21:25:55 +01004282 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004283 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004284 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004285 if (ie_len) {
4286 if (request->ssids)
4287 request->ie = (void *)(request->ssids + n_ssids);
4288 else
4289 request->ie = (void *)(request->channels + n_channels);
4290 }
Johannes Berg2a519312009-02-10 21:25:55 +01004291
Johannes Berg584991d2009-11-02 13:32:03 +01004292 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004293 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4294 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004295 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004296 struct ieee80211_channel *chan;
4297
4298 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4299
4300 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004301 err = -EINVAL;
4302 goto out_free;
4303 }
Johannes Berg584991d2009-11-02 13:32:03 +01004304
4305 /* ignore disabled channels */
4306 if (chan->flags & IEEE80211_CHAN_DISABLED)
4307 continue;
4308
4309 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004310 i++;
4311 }
4312 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004313 enum ieee80211_band band;
4314
Johannes Berg2a519312009-02-10 21:25:55 +01004315 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004316 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4317 int j;
4318 if (!wiphy->bands[band])
4319 continue;
4320 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004321 struct ieee80211_channel *chan;
4322
4323 chan = &wiphy->bands[band]->channels[j];
4324
4325 if (chan->flags & IEEE80211_CHAN_DISABLED)
4326 continue;
4327
4328 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004329 i++;
4330 }
4331 }
4332 }
4333
Johannes Berg584991d2009-11-02 13:32:03 +01004334 if (!i) {
4335 err = -EINVAL;
4336 goto out_free;
4337 }
4338
4339 request->n_channels = i;
4340
Johannes Berg2a519312009-02-10 21:25:55 +01004341 i = 0;
4342 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4343 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004344 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004345 err = -EINVAL;
4346 goto out_free;
4347 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004348 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004349 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004350 i++;
4351 }
4352 }
4353
Jouni Malinen70692ad2009-02-16 19:39:13 +02004354 if (info->attrs[NL80211_ATTR_IE]) {
4355 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004356 memcpy((void *)request->ie,
4357 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004358 request->ie_len);
4359 }
4360
Johannes Berg34850ab2011-07-18 18:08:35 +02004361 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004362 if (wiphy->bands[i])
4363 request->rates[i] =
4364 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004365
4366 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4367 nla_for_each_nested(attr,
4368 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4369 tmp) {
4370 enum ieee80211_band band = nla_type(attr);
4371
Dan Carpenter84404622011-07-29 11:52:18 +03004372 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004373 err = -EINVAL;
4374 goto out_free;
4375 }
4376 err = ieee80211_get_ratemask(wiphy->bands[band],
4377 nla_data(attr),
4378 nla_len(attr),
4379 &request->rates[band]);
4380 if (err)
4381 goto out_free;
4382 }
4383 }
4384
Sam Leffler46856bb2012-10-11 21:03:32 -07004385 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004386 request->flags = nla_get_u32(
4387 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004388 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4389 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4390 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4391 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004392 err = -EOPNOTSUPP;
4393 goto out_free;
4394 }
4395 }
Sam Lefflered4737712012-10-11 21:03:31 -07004396
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304397 request->no_cck =
4398 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4399
Johannes Bergfd014282012-06-18 19:17:03 +02004400 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004401 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004402 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004403
Johannes Berg79c97e92009-07-07 03:56:12 +02004404 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004405 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004406
Johannes Berg463d0182009-07-14 00:33:35 +02004407 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004408 nl80211_send_scan_start(rdev, wdev);
4409 if (wdev->netdev)
4410 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004411 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004412 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004413 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004414 kfree(request);
4415 }
Johannes Berg3b858752009-03-12 09:55:09 +01004416
Johannes Berg2a519312009-02-10 21:25:55 +01004417 return err;
4418}
4419
Luciano Coelho807f8a82011-05-11 17:09:35 +03004420static int nl80211_start_sched_scan(struct sk_buff *skb,
4421 struct genl_info *info)
4422{
4423 struct cfg80211_sched_scan_request *request;
4424 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4425 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004426 struct nlattr *attr;
4427 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004428 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004429 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004430 enum ieee80211_band band;
4431 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004432 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004433
4434 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4435 !rdev->ops->sched_scan_start)
4436 return -EOPNOTSUPP;
4437
4438 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4439 return -EINVAL;
4440
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004441 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4442 return -EINVAL;
4443
4444 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4445 if (interval == 0)
4446 return -EINVAL;
4447
Luciano Coelho807f8a82011-05-11 17:09:35 +03004448 wiphy = &rdev->wiphy;
4449
4450 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4451 n_channels = validate_scan_freqs(
4452 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4453 if (!n_channels)
4454 return -EINVAL;
4455 } else {
4456 n_channels = 0;
4457
4458 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4459 if (wiphy->bands[band])
4460 n_channels += wiphy->bands[band]->n_channels;
4461 }
4462
4463 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4464 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4465 tmp)
4466 n_ssids++;
4467
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004468 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004469 return -EINVAL;
4470
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004471 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4472 nla_for_each_nested(attr,
4473 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4474 tmp)
4475 n_match_sets++;
4476
4477 if (n_match_sets > wiphy->max_match_sets)
4478 return -EINVAL;
4479
Luciano Coelho807f8a82011-05-11 17:09:35 +03004480 if (info->attrs[NL80211_ATTR_IE])
4481 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4482 else
4483 ie_len = 0;
4484
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004485 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004486 return -EINVAL;
4487
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004488 mutex_lock(&rdev->sched_scan_mtx);
4489
4490 if (rdev->sched_scan_req) {
4491 err = -EINPROGRESS;
4492 goto out;
4493 }
4494
Luciano Coelho807f8a82011-05-11 17:09:35 +03004495 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004496 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004497 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004498 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004499 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004500 if (!request) {
4501 err = -ENOMEM;
4502 goto out;
4503 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004504
4505 if (n_ssids)
4506 request->ssids = (void *)&request->channels[n_channels];
4507 request->n_ssids = n_ssids;
4508 if (ie_len) {
4509 if (request->ssids)
4510 request->ie = (void *)(request->ssids + n_ssids);
4511 else
4512 request->ie = (void *)(request->channels + n_channels);
4513 }
4514
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004515 if (n_match_sets) {
4516 if (request->ie)
4517 request->match_sets = (void *)(request->ie + ie_len);
4518 else if (request->ssids)
4519 request->match_sets =
4520 (void *)(request->ssids + n_ssids);
4521 else
4522 request->match_sets =
4523 (void *)(request->channels + n_channels);
4524 }
4525 request->n_match_sets = n_match_sets;
4526
Luciano Coelho807f8a82011-05-11 17:09:35 +03004527 i = 0;
4528 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4529 /* user specified, bail out if channel not found */
4530 nla_for_each_nested(attr,
4531 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4532 tmp) {
4533 struct ieee80211_channel *chan;
4534
4535 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4536
4537 if (!chan) {
4538 err = -EINVAL;
4539 goto out_free;
4540 }
4541
4542 /* ignore disabled channels */
4543 if (chan->flags & IEEE80211_CHAN_DISABLED)
4544 continue;
4545
4546 request->channels[i] = chan;
4547 i++;
4548 }
4549 } else {
4550 /* all channels */
4551 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4552 int j;
4553 if (!wiphy->bands[band])
4554 continue;
4555 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4556 struct ieee80211_channel *chan;
4557
4558 chan = &wiphy->bands[band]->channels[j];
4559
4560 if (chan->flags & IEEE80211_CHAN_DISABLED)
4561 continue;
4562
4563 request->channels[i] = chan;
4564 i++;
4565 }
4566 }
4567 }
4568
4569 if (!i) {
4570 err = -EINVAL;
4571 goto out_free;
4572 }
4573
4574 request->n_channels = i;
4575
4576 i = 0;
4577 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4578 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4579 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004580 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004581 err = -EINVAL;
4582 goto out_free;
4583 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004584 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004585 memcpy(request->ssids[i].ssid, nla_data(attr),
4586 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004587 i++;
4588 }
4589 }
4590
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004591 i = 0;
4592 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4593 nla_for_each_nested(attr,
4594 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4595 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004596 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004597
4598 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4599 nla_data(attr), nla_len(attr),
4600 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004601 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004602 if (ssid) {
4603 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4604 err = -EINVAL;
4605 goto out_free;
4606 }
4607 memcpy(request->match_sets[i].ssid.ssid,
4608 nla_data(ssid), nla_len(ssid));
4609 request->match_sets[i].ssid.ssid_len =
4610 nla_len(ssid);
4611 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004612 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4613 if (rssi)
4614 request->rssi_thold = nla_get_u32(rssi);
4615 else
4616 request->rssi_thold =
4617 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004618 i++;
4619 }
4620 }
4621
Luciano Coelho807f8a82011-05-11 17:09:35 +03004622 if (info->attrs[NL80211_ATTR_IE]) {
4623 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4624 memcpy((void *)request->ie,
4625 nla_data(info->attrs[NL80211_ATTR_IE]),
4626 request->ie_len);
4627 }
4628
Sam Leffler46856bb2012-10-11 21:03:32 -07004629 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004630 request->flags = nla_get_u32(
4631 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004632 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4633 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4634 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4635 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004636 err = -EOPNOTSUPP;
4637 goto out_free;
4638 }
4639 }
Sam Lefflered4737712012-10-11 21:03:31 -07004640
Luciano Coelho807f8a82011-05-11 17:09:35 +03004641 request->dev = dev;
4642 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004643 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004644 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004645
Hila Gonene35e4d22012-06-27 17:19:42 +03004646 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004647 if (!err) {
4648 rdev->sched_scan_req = request;
4649 nl80211_send_sched_scan(rdev, dev,
4650 NL80211_CMD_START_SCHED_SCAN);
4651 goto out;
4652 }
4653
4654out_free:
4655 kfree(request);
4656out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004657 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004658 return err;
4659}
4660
4661static int nl80211_stop_sched_scan(struct sk_buff *skb,
4662 struct genl_info *info)
4663{
4664 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004665 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004666
4667 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4668 !rdev->ops->sched_scan_stop)
4669 return -EOPNOTSUPP;
4670
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004671 mutex_lock(&rdev->sched_scan_mtx);
4672 err = __cfg80211_stop_sched_scan(rdev, false);
4673 mutex_unlock(&rdev->sched_scan_mtx);
4674
4675 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004676}
4677
Johannes Berg9720bb32011-06-21 09:45:33 +02004678static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4679 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004680 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004681 struct wireless_dev *wdev,
4682 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004683{
Johannes Berg48ab9052009-07-10 18:42:31 +02004684 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004685 void *hdr;
4686 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004687
4688 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004689
Eric W. Biederman15e47302012-09-07 20:12:54 +00004690 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004691 NL80211_CMD_NEW_SCAN_RESULTS);
4692 if (!hdr)
4693 return -1;
4694
Johannes Berg9720bb32011-06-21 09:45:33 +02004695 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4696
David S. Miller9360ffd2012-03-29 04:41:26 -04004697 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4698 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4699 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004700
4701 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4702 if (!bss)
4703 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004704 if ((!is_zero_ether_addr(res->bssid) &&
4705 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4706 (res->information_elements && res->len_information_elements &&
4707 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4708 res->len_information_elements,
4709 res->information_elements)) ||
4710 (res->beacon_ies && res->len_beacon_ies &&
4711 res->beacon_ies != res->information_elements &&
4712 nla_put(msg, NL80211_BSS_BEACON_IES,
4713 res->len_beacon_ies, res->beacon_ies)))
4714 goto nla_put_failure;
4715 if (res->tsf &&
4716 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4717 goto nla_put_failure;
4718 if (res->beacon_interval &&
4719 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4720 goto nla_put_failure;
4721 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4722 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4723 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4724 jiffies_to_msecs(jiffies - intbss->ts)))
4725 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004726
Johannes Berg77965c92009-02-18 18:45:06 +01004727 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004728 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004729 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4730 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004731 break;
4732 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004733 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4734 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004735 break;
4736 default:
4737 break;
4738 }
4739
Johannes Berg48ab9052009-07-10 18:42:31 +02004740 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004741 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004742 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004743 if (intbss == wdev->current_bss &&
4744 nla_put_u32(msg, NL80211_BSS_STATUS,
4745 NL80211_BSS_STATUS_ASSOCIATED))
4746 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004747 break;
4748 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004749 if (intbss == wdev->current_bss &&
4750 nla_put_u32(msg, NL80211_BSS_STATUS,
4751 NL80211_BSS_STATUS_IBSS_JOINED))
4752 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004753 break;
4754 default:
4755 break;
4756 }
4757
Johannes Berg2a519312009-02-10 21:25:55 +01004758 nla_nest_end(msg, bss);
4759
4760 return genlmsg_end(msg, hdr);
4761
4762 nla_put_failure:
4763 genlmsg_cancel(msg, hdr);
4764 return -EMSGSIZE;
4765}
4766
4767static int nl80211_dump_scan(struct sk_buff *skb,
4768 struct netlink_callback *cb)
4769{
Johannes Berg48ab9052009-07-10 18:42:31 +02004770 struct cfg80211_registered_device *rdev;
4771 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004772 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004773 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004774 int start = cb->args[1], idx = 0;
4775 int err;
4776
Johannes Berg67748892010-10-04 21:14:06 +02004777 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4778 if (err)
4779 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004780
Johannes Berg48ab9052009-07-10 18:42:31 +02004781 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004782
Johannes Berg48ab9052009-07-10 18:42:31 +02004783 wdev_lock(wdev);
4784 spin_lock_bh(&rdev->bss_lock);
4785 cfg80211_bss_expire(rdev);
4786
Johannes Berg9720bb32011-06-21 09:45:33 +02004787 cb->seq = rdev->bss_generation;
4788
Johannes Berg48ab9052009-07-10 18:42:31 +02004789 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004790 if (++idx <= start)
4791 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004792 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004793 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004794 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004795 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004796 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004797 }
4798 }
4799
Johannes Berg48ab9052009-07-10 18:42:31 +02004800 spin_unlock_bh(&rdev->bss_lock);
4801 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004802
4803 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004804 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004805
Johannes Berg67748892010-10-04 21:14:06 +02004806 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004807}
4808
Eric W. Biederman15e47302012-09-07 20:12:54 +00004809static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004810 int flags, struct net_device *dev,
4811 struct survey_info *survey)
4812{
4813 void *hdr;
4814 struct nlattr *infoattr;
4815
Eric W. Biederman15e47302012-09-07 20:12:54 +00004816 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004817 NL80211_CMD_NEW_SURVEY_RESULTS);
4818 if (!hdr)
4819 return -ENOMEM;
4820
David S. Miller9360ffd2012-03-29 04:41:26 -04004821 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4822 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004823
4824 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4825 if (!infoattr)
4826 goto nla_put_failure;
4827
David S. Miller9360ffd2012-03-29 04:41:26 -04004828 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4829 survey->channel->center_freq))
4830 goto nla_put_failure;
4831
4832 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4833 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4834 goto nla_put_failure;
4835 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4836 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4837 goto nla_put_failure;
4838 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4839 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4840 survey->channel_time))
4841 goto nla_put_failure;
4842 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4843 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4844 survey->channel_time_busy))
4845 goto nla_put_failure;
4846 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4847 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4848 survey->channel_time_ext_busy))
4849 goto nla_put_failure;
4850 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4851 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4852 survey->channel_time_rx))
4853 goto nla_put_failure;
4854 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4855 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4856 survey->channel_time_tx))
4857 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004858
4859 nla_nest_end(msg, infoattr);
4860
4861 return genlmsg_end(msg, hdr);
4862
4863 nla_put_failure:
4864 genlmsg_cancel(msg, hdr);
4865 return -EMSGSIZE;
4866}
4867
4868static int nl80211_dump_survey(struct sk_buff *skb,
4869 struct netlink_callback *cb)
4870{
4871 struct survey_info survey;
4872 struct cfg80211_registered_device *dev;
4873 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004874 int survey_idx = cb->args[1];
4875 int res;
4876
Johannes Berg67748892010-10-04 21:14:06 +02004877 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4878 if (res)
4879 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004880
4881 if (!dev->ops->dump_survey) {
4882 res = -EOPNOTSUPP;
4883 goto out_err;
4884 }
4885
4886 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004887 struct ieee80211_channel *chan;
4888
Hila Gonene35e4d22012-06-27 17:19:42 +03004889 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01004890 if (res == -ENOENT)
4891 break;
4892 if (res)
4893 goto out_err;
4894
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004895 /* Survey without a channel doesn't make sense */
4896 if (!survey.channel) {
4897 res = -EINVAL;
4898 goto out;
4899 }
4900
4901 chan = ieee80211_get_channel(&dev->wiphy,
4902 survey.channel->center_freq);
4903 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4904 survey_idx++;
4905 continue;
4906 }
4907
Holger Schurig61fa7132009-11-11 12:25:40 +01004908 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004909 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01004910 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4911 netdev,
4912 &survey) < 0)
4913 goto out;
4914 survey_idx++;
4915 }
4916
4917 out:
4918 cb->args[1] = survey_idx;
4919 res = skb->len;
4920 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004921 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004922 return res;
4923}
4924
Samuel Ortizb23aa672009-07-01 21:26:54 +02004925static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4926{
4927 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4928 NL80211_WPA_VERSION_2));
4929}
4930
Jouni Malinen636a5d32009-03-19 13:39:22 +02004931static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4932{
Johannes Berg4c476992010-10-04 21:36:35 +02004933 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4934 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004935 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03004936 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
4937 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004938 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004939 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004940 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004941
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004942 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4943 return -EINVAL;
4944
4945 if (!info->attrs[NL80211_ATTR_MAC])
4946 return -EINVAL;
4947
Jouni Malinen17780922009-03-27 20:52:47 +02004948 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4949 return -EINVAL;
4950
Johannes Berg19957bb2009-07-02 17:20:43 +02004951 if (!info->attrs[NL80211_ATTR_SSID])
4952 return -EINVAL;
4953
4954 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4955 return -EINVAL;
4956
Johannes Bergfffd0932009-07-08 14:22:54 +02004957 err = nl80211_parse_key(info, &key);
4958 if (err)
4959 return err;
4960
4961 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004962 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4963 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004964 if (!key.p.key || !key.p.key_len)
4965 return -EINVAL;
4966 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4967 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4968 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4969 key.p.key_len != WLAN_KEY_LEN_WEP104))
4970 return -EINVAL;
4971 if (key.idx > 4)
4972 return -EINVAL;
4973 } else {
4974 key.p.key_len = 0;
4975 key.p.key = NULL;
4976 }
4977
Johannes Bergafea0b72010-08-10 09:46:42 +02004978 if (key.idx >= 0) {
4979 int i;
4980 bool ok = false;
4981 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4982 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4983 ok = true;
4984 break;
4985 }
4986 }
Johannes Berg4c476992010-10-04 21:36:35 +02004987 if (!ok)
4988 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004989 }
4990
Johannes Berg4c476992010-10-04 21:36:35 +02004991 if (!rdev->ops->auth)
4992 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004993
Johannes Berg074ac8d2010-09-16 14:58:22 +02004994 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004995 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4996 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004997
Johannes Berg19957bb2009-07-02 17:20:43 +02004998 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004999 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005000 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005001 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5002 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005003
Johannes Berg19957bb2009-07-02 17:20:43 +02005004 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5005 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5006
5007 if (info->attrs[NL80211_ATTR_IE]) {
5008 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5009 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5010 }
5011
5012 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005013 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005014 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005015
Jouni Malinene39e5b52012-09-30 19:29:39 +03005016 if (auth_type == NL80211_AUTHTYPE_SAE &&
5017 !info->attrs[NL80211_ATTR_SAE_DATA])
5018 return -EINVAL;
5019
5020 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5021 if (auth_type != NL80211_AUTHTYPE_SAE)
5022 return -EINVAL;
5023 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5024 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5025 /* need to include at least Auth Transaction and Status Code */
5026 if (sae_data_len < 4)
5027 return -EINVAL;
5028 }
5029
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005030 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5031
Johannes Berg95de8172012-01-20 13:55:25 +01005032 /*
5033 * Since we no longer track auth state, ignore
5034 * requests to only change local state.
5035 */
5036 if (local_state_change)
5037 return 0;
5038
Johannes Berg4c476992010-10-04 21:36:35 +02005039 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5040 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005041 key.p.key, key.p.key_len, key.idx,
5042 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005043}
5044
Johannes Bergc0692b82010-08-27 14:26:53 +03005045static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5046 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005047 struct cfg80211_crypto_settings *settings,
5048 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005049{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005050 memset(settings, 0, sizeof(*settings));
5051
Samuel Ortizb23aa672009-07-01 21:26:54 +02005052 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5053
Johannes Bergc0692b82010-08-27 14:26:53 +03005054 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5055 u16 proto;
5056 proto = nla_get_u16(
5057 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5058 settings->control_port_ethertype = cpu_to_be16(proto);
5059 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5060 proto != ETH_P_PAE)
5061 return -EINVAL;
5062 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5063 settings->control_port_no_encrypt = true;
5064 } else
5065 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5066
Samuel Ortizb23aa672009-07-01 21:26:54 +02005067 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5068 void *data;
5069 int len, i;
5070
5071 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5072 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5073 settings->n_ciphers_pairwise = len / sizeof(u32);
5074
5075 if (len % sizeof(u32))
5076 return -EINVAL;
5077
Johannes Berg3dc27d22009-07-02 21:36:37 +02005078 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005079 return -EINVAL;
5080
5081 memcpy(settings->ciphers_pairwise, data, len);
5082
5083 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005084 if (!cfg80211_supported_cipher_suite(
5085 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005086 settings->ciphers_pairwise[i]))
5087 return -EINVAL;
5088 }
5089
5090 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5091 settings->cipher_group =
5092 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005093 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5094 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005095 return -EINVAL;
5096 }
5097
5098 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5099 settings->wpa_versions =
5100 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5101 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5102 return -EINVAL;
5103 }
5104
5105 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5106 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005107 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005108
5109 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5110 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5111 settings->n_akm_suites = len / sizeof(u32);
5112
5113 if (len % sizeof(u32))
5114 return -EINVAL;
5115
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005116 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5117 return -EINVAL;
5118
Samuel Ortizb23aa672009-07-01 21:26:54 +02005119 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005120 }
5121
5122 return 0;
5123}
5124
Jouni Malinen636a5d32009-03-19 13:39:22 +02005125static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5126{
Johannes Berg4c476992010-10-04 21:36:35 +02005127 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5128 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005129 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005130 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005131 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005132 int err, ssid_len, ie_len = 0;
5133 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005134 u32 flags = 0;
5135 struct ieee80211_ht_cap *ht_capa = NULL;
5136 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005137
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005138 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5139 return -EINVAL;
5140
5141 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005142 !info->attrs[NL80211_ATTR_SSID] ||
5143 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005144 return -EINVAL;
5145
Johannes Berg4c476992010-10-04 21:36:35 +02005146 if (!rdev->ops->assoc)
5147 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005148
Johannes Berg074ac8d2010-09-16 14:58:22 +02005149 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005150 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5151 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005152
Johannes Berg19957bb2009-07-02 17:20:43 +02005153 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005154
Johannes Berg19957bb2009-07-02 17:20:43 +02005155 chan = ieee80211_get_channel(&rdev->wiphy,
5156 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005157 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5158 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005159
Johannes Berg19957bb2009-07-02 17:20:43 +02005160 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5161 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005162
5163 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005164 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5165 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005166 }
5167
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005168 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005169 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005170 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005171 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005172 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005173 else if (mfp != NL80211_MFP_NO)
5174 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005175 }
5176
Johannes Berg3e5d7642009-07-07 14:37:26 +02005177 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5178 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5179
Ben Greear7e7c8922011-11-18 11:31:59 -08005180 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5181 flags |= ASSOC_REQ_DISABLE_HT;
5182
5183 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5184 ht_capa_mask =
5185 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5186
5187 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5188 if (!ht_capa_mask)
5189 return -EINVAL;
5190 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5191 }
5192
Johannes Bergc0692b82010-08-27 14:26:53 +03005193 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005194 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005195 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5196 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005197 &crypto, flags, ht_capa,
5198 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005199
Jouni Malinen636a5d32009-03-19 13:39:22 +02005200 return err;
5201}
5202
5203static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5204{
Johannes Berg4c476992010-10-04 21:36:35 +02005205 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5206 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005207 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005208 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005209 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005210 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005211
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005212 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5213 return -EINVAL;
5214
5215 if (!info->attrs[NL80211_ATTR_MAC])
5216 return -EINVAL;
5217
5218 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5219 return -EINVAL;
5220
Johannes Berg4c476992010-10-04 21:36:35 +02005221 if (!rdev->ops->deauth)
5222 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005223
Johannes Berg074ac8d2010-09-16 14:58:22 +02005224 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005225 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5226 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005227
Johannes Berg19957bb2009-07-02 17:20:43 +02005228 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005229
Johannes Berg19957bb2009-07-02 17:20:43 +02005230 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5231 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005232 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005233 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005234 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005235
5236 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005237 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5238 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005239 }
5240
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005241 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5242
Johannes Berg4c476992010-10-04 21:36:35 +02005243 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5244 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005245}
5246
5247static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5248{
Johannes Berg4c476992010-10-04 21:36:35 +02005249 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5250 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005251 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005252 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005253 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005254 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005255
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005256 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5257 return -EINVAL;
5258
5259 if (!info->attrs[NL80211_ATTR_MAC])
5260 return -EINVAL;
5261
5262 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5263 return -EINVAL;
5264
Johannes Berg4c476992010-10-04 21:36:35 +02005265 if (!rdev->ops->disassoc)
5266 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005267
Johannes Berg074ac8d2010-09-16 14:58:22 +02005268 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005269 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5270 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005271
Johannes Berg19957bb2009-07-02 17:20:43 +02005272 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005273
Johannes Berg19957bb2009-07-02 17:20:43 +02005274 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5275 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005276 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005277 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005278 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005279
5280 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005281 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5282 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005283 }
5284
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005285 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5286
Johannes Berg4c476992010-10-04 21:36:35 +02005287 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5288 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005289}
5290
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005291static bool
5292nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5293 int mcast_rate[IEEE80211_NUM_BANDS],
5294 int rateval)
5295{
5296 struct wiphy *wiphy = &rdev->wiphy;
5297 bool found = false;
5298 int band, i;
5299
5300 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5301 struct ieee80211_supported_band *sband;
5302
5303 sband = wiphy->bands[band];
5304 if (!sband)
5305 continue;
5306
5307 for (i = 0; i < sband->n_bitrates; i++) {
5308 if (sband->bitrates[i].bitrate == rateval) {
5309 mcast_rate[band] = i + 1;
5310 found = true;
5311 break;
5312 }
5313 }
5314 }
5315
5316 return found;
5317}
5318
Johannes Berg04a773a2009-04-19 21:24:32 +02005319static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5320{
Johannes Berg4c476992010-10-04 21:36:35 +02005321 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5322 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005323 struct cfg80211_ibss_params ibss;
5324 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005325 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005326 int err;
5327
Johannes Berg8e30bc52009-04-22 17:45:38 +02005328 memset(&ibss, 0, sizeof(ibss));
5329
Johannes Berg04a773a2009-04-19 21:24:32 +02005330 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5331 return -EINVAL;
5332
5333 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5334 !info->attrs[NL80211_ATTR_SSID] ||
5335 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5336 return -EINVAL;
5337
Johannes Berg8e30bc52009-04-22 17:45:38 +02005338 ibss.beacon_interval = 100;
5339
5340 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5341 ibss.beacon_interval =
5342 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5343 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5344 return -EINVAL;
5345 }
5346
Johannes Berg4c476992010-10-04 21:36:35 +02005347 if (!rdev->ops->join_ibss)
5348 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005349
Johannes Berg4c476992010-10-04 21:36:35 +02005350 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5351 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005352
Johannes Berg79c97e92009-07-07 03:56:12 +02005353 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005354
Johannes Berg39193492011-09-16 13:45:25 +02005355 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005356 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005357
5358 if (!is_valid_ether_addr(ibss.bssid))
5359 return -EINVAL;
5360 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005361 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5362 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5363
5364 if (info->attrs[NL80211_ATTR_IE]) {
5365 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5366 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5367 }
5368
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005369 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5370 enum nl80211_channel_type channel_type;
5371
Johannes Bergcd6c6592012-05-10 21:27:18 +02005372 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005373 return -EINVAL;
5374
5375 if (channel_type != NL80211_CHAN_NO_HT &&
5376 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5377 return -EINVAL;
5378
5379 ibss.channel_type = channel_type;
5380 } else {
5381 ibss.channel_type = NL80211_CHAN_NO_HT;
5382 }
5383
5384 ibss.channel = rdev_freq_to_chan(rdev,
5385 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5386 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005387 if (!ibss.channel ||
5388 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005389 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5390 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005391
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005392 /* Both channels should be able to initiate communication */
5393 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5394 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5395 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5396 ibss.channel_type))
5397 return -EINVAL;
5398
Johannes Berg04a773a2009-04-19 21:24:32 +02005399 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005400 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005401
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005402 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5403 u8 *rates =
5404 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5405 int n_rates =
5406 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5407 struct ieee80211_supported_band *sband =
5408 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005409
Johannes Berg34850ab2011-07-18 18:08:35 +02005410 err = ieee80211_get_ratemask(sband, rates, n_rates,
5411 &ibss.basic_rates);
5412 if (err)
5413 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005414 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005415
5416 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5417 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5418 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5419 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005420
Johannes Berg4c476992010-10-04 21:36:35 +02005421 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305422 bool no_ht = false;
5423
Johannes Berg4c476992010-10-04 21:36:35 +02005424 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305425 info->attrs[NL80211_ATTR_KEYS],
5426 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005427 if (IS_ERR(connkeys))
5428 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305429
5430 if ((ibss.channel_type != NL80211_CHAN_NO_HT) && no_ht) {
5431 kfree(connkeys);
5432 return -EINVAL;
5433 }
Johannes Berg4c476992010-10-04 21:36:35 +02005434 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005435
Antonio Quartulli267335d2012-01-31 20:25:47 +01005436 ibss.control_port =
5437 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5438
Johannes Berg4c476992010-10-04 21:36:35 +02005439 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005440 if (err)
5441 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005442 return err;
5443}
5444
5445static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5446{
Johannes Berg4c476992010-10-04 21:36:35 +02005447 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5448 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005449
Johannes Berg4c476992010-10-04 21:36:35 +02005450 if (!rdev->ops->leave_ibss)
5451 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005452
Johannes Berg4c476992010-10-04 21:36:35 +02005453 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5454 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005455
Johannes Berg4c476992010-10-04 21:36:35 +02005456 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005457}
5458
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005459static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5460{
5461 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5462 struct net_device *dev = info->user_ptr[1];
5463 int mcast_rate[IEEE80211_NUM_BANDS];
5464 u32 nla_rate;
5465 int err;
5466
5467 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5468 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5469 return -EOPNOTSUPP;
5470
5471 if (!rdev->ops->set_mcast_rate)
5472 return -EOPNOTSUPP;
5473
5474 memset(mcast_rate, 0, sizeof(mcast_rate));
5475
5476 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5477 return -EINVAL;
5478
5479 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5480 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5481 return -EINVAL;
5482
5483 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5484
5485 return err;
5486}
5487
5488
Johannes Bergaff89a92009-07-01 21:26:51 +02005489#ifdef CONFIG_NL80211_TESTMODE
5490static struct genl_multicast_group nl80211_testmode_mcgrp = {
5491 .name = "testmode",
5492};
5493
5494static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5495{
Johannes Berg4c476992010-10-04 21:36:35 +02005496 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005497 int err;
5498
5499 if (!info->attrs[NL80211_ATTR_TESTDATA])
5500 return -EINVAL;
5501
Johannes Bergaff89a92009-07-01 21:26:51 +02005502 err = -EOPNOTSUPP;
5503 if (rdev->ops->testmode_cmd) {
5504 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005505 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005506 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5507 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5508 rdev->testmode_info = NULL;
5509 }
5510
Johannes Bergaff89a92009-07-01 21:26:51 +02005511 return err;
5512}
5513
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005514static int nl80211_testmode_dump(struct sk_buff *skb,
5515 struct netlink_callback *cb)
5516{
Johannes Berg00918d32011-12-13 17:22:05 +01005517 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005518 int err;
5519 long phy_idx;
5520 void *data = NULL;
5521 int data_len = 0;
5522
5523 if (cb->args[0]) {
5524 /*
5525 * 0 is a valid index, but not valid for args[0],
5526 * so we need to offset by 1.
5527 */
5528 phy_idx = cb->args[0] - 1;
5529 } else {
5530 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5531 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5532 nl80211_policy);
5533 if (err)
5534 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005535
Johannes Berg2bd7e352012-06-15 14:23:16 +02005536 mutex_lock(&cfg80211_mutex);
5537 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5538 nl80211_fam.attrbuf);
5539 if (IS_ERR(rdev)) {
5540 mutex_unlock(&cfg80211_mutex);
5541 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005542 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005543 phy_idx = rdev->wiphy_idx;
5544 rdev = NULL;
5545 mutex_unlock(&cfg80211_mutex);
5546
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005547 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5548 cb->args[1] =
5549 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5550 }
5551
5552 if (cb->args[1]) {
5553 data = nla_data((void *)cb->args[1]);
5554 data_len = nla_len((void *)cb->args[1]);
5555 }
5556
5557 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005558 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5559 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005560 mutex_unlock(&cfg80211_mutex);
5561 return -ENOENT;
5562 }
Johannes Berg00918d32011-12-13 17:22:05 +01005563 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005564 mutex_unlock(&cfg80211_mutex);
5565
Johannes Berg00918d32011-12-13 17:22:05 +01005566 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005567 err = -EOPNOTSUPP;
5568 goto out_err;
5569 }
5570
5571 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005572 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005573 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5574 NL80211_CMD_TESTMODE);
5575 struct nlattr *tmdata;
5576
David S. Miller9360ffd2012-03-29 04:41:26 -04005577 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005578 genlmsg_cancel(skb, hdr);
5579 break;
5580 }
5581
5582 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5583 if (!tmdata) {
5584 genlmsg_cancel(skb, hdr);
5585 break;
5586 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005587 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005588 nla_nest_end(skb, tmdata);
5589
5590 if (err == -ENOBUFS || err == -ENOENT) {
5591 genlmsg_cancel(skb, hdr);
5592 break;
5593 } else if (err) {
5594 genlmsg_cancel(skb, hdr);
5595 goto out_err;
5596 }
5597
5598 genlmsg_end(skb, hdr);
5599 }
5600
5601 err = skb->len;
5602 /* see above */
5603 cb->args[0] = phy_idx + 1;
5604 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005605 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005606 return err;
5607}
5608
Johannes Bergaff89a92009-07-01 21:26:51 +02005609static struct sk_buff *
5610__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005611 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005612{
5613 struct sk_buff *skb;
5614 void *hdr;
5615 struct nlattr *data;
5616
5617 skb = nlmsg_new(approxlen + 100, gfp);
5618 if (!skb)
5619 return NULL;
5620
Eric W. Biederman15e47302012-09-07 20:12:54 +00005621 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005622 if (!hdr) {
5623 kfree_skb(skb);
5624 return NULL;
5625 }
5626
David S. Miller9360ffd2012-03-29 04:41:26 -04005627 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5628 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005629 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5630
5631 ((void **)skb->cb)[0] = rdev;
5632 ((void **)skb->cb)[1] = hdr;
5633 ((void **)skb->cb)[2] = data;
5634
5635 return skb;
5636
5637 nla_put_failure:
5638 kfree_skb(skb);
5639 return NULL;
5640}
5641
5642struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5643 int approxlen)
5644{
5645 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5646
5647 if (WARN_ON(!rdev->testmode_info))
5648 return NULL;
5649
5650 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005651 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005652 rdev->testmode_info->snd_seq,
5653 GFP_KERNEL);
5654}
5655EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5656
5657int cfg80211_testmode_reply(struct sk_buff *skb)
5658{
5659 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5660 void *hdr = ((void **)skb->cb)[1];
5661 struct nlattr *data = ((void **)skb->cb)[2];
5662
5663 if (WARN_ON(!rdev->testmode_info)) {
5664 kfree_skb(skb);
5665 return -EINVAL;
5666 }
5667
5668 nla_nest_end(skb, data);
5669 genlmsg_end(skb, hdr);
5670 return genlmsg_reply(skb, rdev->testmode_info);
5671}
5672EXPORT_SYMBOL(cfg80211_testmode_reply);
5673
5674struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5675 int approxlen, gfp_t gfp)
5676{
5677 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5678
5679 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5680}
5681EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5682
5683void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5684{
5685 void *hdr = ((void **)skb->cb)[1];
5686 struct nlattr *data = ((void **)skb->cb)[2];
5687
5688 nla_nest_end(skb, data);
5689 genlmsg_end(skb, hdr);
5690 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5691}
5692EXPORT_SYMBOL(cfg80211_testmode_event);
5693#endif
5694
Samuel Ortizb23aa672009-07-01 21:26:54 +02005695static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5696{
Johannes Berg4c476992010-10-04 21:36:35 +02005697 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5698 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005699 struct cfg80211_connect_params connect;
5700 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005701 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005702 int err;
5703
5704 memset(&connect, 0, sizeof(connect));
5705
5706 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5707 return -EINVAL;
5708
5709 if (!info->attrs[NL80211_ATTR_SSID] ||
5710 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5711 return -EINVAL;
5712
5713 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5714 connect.auth_type =
5715 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005716 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5717 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005718 return -EINVAL;
5719 } else
5720 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5721
5722 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5723
Johannes Bergc0692b82010-08-27 14:26:53 +03005724 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005725 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005726 if (err)
5727 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005728
Johannes Berg074ac8d2010-09-16 14:58:22 +02005729 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005730 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5731 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005732
Johannes Berg79c97e92009-07-07 03:56:12 +02005733 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005734
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305735 connect.bg_scan_period = -1;
5736 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5737 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5738 connect.bg_scan_period =
5739 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5740 }
5741
Samuel Ortizb23aa672009-07-01 21:26:54 +02005742 if (info->attrs[NL80211_ATTR_MAC])
5743 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5744 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5745 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5746
5747 if (info->attrs[NL80211_ATTR_IE]) {
5748 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5749 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5750 }
5751
5752 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5753 connect.channel =
5754 ieee80211_get_channel(wiphy,
5755 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5756 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005757 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5758 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005759 }
5760
Johannes Bergfffd0932009-07-08 14:22:54 +02005761 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5762 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305763 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005764 if (IS_ERR(connkeys))
5765 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005766 }
5767
Ben Greear7e7c8922011-11-18 11:31:59 -08005768 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5769 connect.flags |= ASSOC_REQ_DISABLE_HT;
5770
5771 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5772 memcpy(&connect.ht_capa_mask,
5773 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5774 sizeof(connect.ht_capa_mask));
5775
5776 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005777 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5778 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005779 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005780 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005781 memcpy(&connect.ht_capa,
5782 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5783 sizeof(connect.ht_capa));
5784 }
5785
Johannes Bergfffd0932009-07-08 14:22:54 +02005786 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005787 if (err)
5788 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005789 return err;
5790}
5791
5792static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5793{
Johannes Berg4c476992010-10-04 21:36:35 +02005794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5795 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005796 u16 reason;
5797
5798 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5799 reason = WLAN_REASON_DEAUTH_LEAVING;
5800 else
5801 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5802
5803 if (reason == 0)
5804 return -EINVAL;
5805
Johannes Berg074ac8d2010-09-16 14:58:22 +02005806 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005807 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5808 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005809
Johannes Berg4c476992010-10-04 21:36:35 +02005810 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005811}
5812
Johannes Berg463d0182009-07-14 00:33:35 +02005813static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5814{
Johannes Berg4c476992010-10-04 21:36:35 +02005815 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005816 struct net *net;
5817 int err;
5818 u32 pid;
5819
5820 if (!info->attrs[NL80211_ATTR_PID])
5821 return -EINVAL;
5822
5823 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5824
Johannes Berg463d0182009-07-14 00:33:35 +02005825 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005826 if (IS_ERR(net))
5827 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005828
5829 err = 0;
5830
5831 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005832 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5833 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005834
Johannes Berg463d0182009-07-14 00:33:35 +02005835 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005836 return err;
5837}
5838
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005839static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5840{
Johannes Berg4c476992010-10-04 21:36:35 +02005841 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005842 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5843 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005844 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005845 struct cfg80211_pmksa pmksa;
5846
5847 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5848
5849 if (!info->attrs[NL80211_ATTR_MAC])
5850 return -EINVAL;
5851
5852 if (!info->attrs[NL80211_ATTR_PMKID])
5853 return -EINVAL;
5854
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005855 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5856 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5857
Johannes Berg074ac8d2010-09-16 14:58:22 +02005858 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005859 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5860 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005861
5862 switch (info->genlhdr->cmd) {
5863 case NL80211_CMD_SET_PMKSA:
5864 rdev_ops = rdev->ops->set_pmksa;
5865 break;
5866 case NL80211_CMD_DEL_PMKSA:
5867 rdev_ops = rdev->ops->del_pmksa;
5868 break;
5869 default:
5870 WARN_ON(1);
5871 break;
5872 }
5873
Johannes Berg4c476992010-10-04 21:36:35 +02005874 if (!rdev_ops)
5875 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005876
Johannes Berg4c476992010-10-04 21:36:35 +02005877 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005878}
5879
5880static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5881{
Johannes Berg4c476992010-10-04 21:36:35 +02005882 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5883 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005884
Johannes Berg074ac8d2010-09-16 14:58:22 +02005885 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005886 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5887 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005888
Johannes Berg4c476992010-10-04 21:36:35 +02005889 if (!rdev->ops->flush_pmksa)
5890 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005891
Hila Gonene35e4d22012-06-27 17:19:42 +03005892 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005893}
5894
Arik Nemtsov109086c2011-09-28 14:12:50 +03005895static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5896{
5897 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5898 struct net_device *dev = info->user_ptr[1];
5899 u8 action_code, dialog_token;
5900 u16 status_code;
5901 u8 *peer;
5902
5903 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5904 !rdev->ops->tdls_mgmt)
5905 return -EOPNOTSUPP;
5906
5907 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5908 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5909 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5910 !info->attrs[NL80211_ATTR_IE] ||
5911 !info->attrs[NL80211_ATTR_MAC])
5912 return -EINVAL;
5913
5914 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5915 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5916 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5917 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5918
Hila Gonene35e4d22012-06-27 17:19:42 +03005919 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
5920 dialog_token, status_code,
5921 nla_data(info->attrs[NL80211_ATTR_IE]),
5922 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03005923}
5924
5925static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5926{
5927 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5928 struct net_device *dev = info->user_ptr[1];
5929 enum nl80211_tdls_operation operation;
5930 u8 *peer;
5931
5932 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5933 !rdev->ops->tdls_oper)
5934 return -EOPNOTSUPP;
5935
5936 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5937 !info->attrs[NL80211_ATTR_MAC])
5938 return -EINVAL;
5939
5940 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5941 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5942
Hila Gonene35e4d22012-06-27 17:19:42 +03005943 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03005944}
5945
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005946static int nl80211_remain_on_channel(struct sk_buff *skb,
5947 struct genl_info *info)
5948{
Johannes Berg4c476992010-10-04 21:36:35 +02005949 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005950 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005951 struct ieee80211_channel *chan;
5952 struct sk_buff *msg;
5953 void *hdr;
5954 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005955 u32 freq, duration;
5956 int err;
5957
5958 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5959 !info->attrs[NL80211_ATTR_DURATION])
5960 return -EINVAL;
5961
5962 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5963
Johannes Berg7c4ef712011-11-18 15:33:48 +01005964 if (!rdev->ops->remain_on_channel ||
5965 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005966 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005967
Johannes Bergebf348f2012-06-01 12:50:54 +02005968 /*
5969 * We should be on that channel for at least a minimum amount of
5970 * time (10ms) but no longer than the driver supports.
5971 */
5972 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5973 duration > rdev->wiphy.max_remain_on_channel_duration)
5974 return -EINVAL;
5975
Johannes Bergcd6c6592012-05-10 21:27:18 +02005976 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
Johannes Berg42d97a52012-11-08 18:31:02 +01005977 !nl80211_valid_channel_type(info, NULL))
Johannes Bergcd6c6592012-05-10 21:27:18 +02005978 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005979
5980 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
Johannes Berg42d97a52012-11-08 18:31:02 +01005981 chan = rdev_freq_to_chan(rdev, freq, NL80211_CHAN_NO_HT);
Johannes Berg4c476992010-10-04 21:36:35 +02005982 if (chan == NULL)
5983 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005984
5985 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005986 if (!msg)
5987 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005988
Eric W. Biederman15e47302012-09-07 20:12:54 +00005989 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005990 NL80211_CMD_REMAIN_ON_CHANNEL);
5991
5992 if (IS_ERR(hdr)) {
5993 err = PTR_ERR(hdr);
5994 goto free_msg;
5995 }
5996
Johannes Berg42d97a52012-11-08 18:31:02 +01005997 err = rdev_remain_on_channel(rdev, wdev, chan, duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005998
5999 if (err)
6000 goto free_msg;
6001
David S. Miller9360ffd2012-03-29 04:41:26 -04006002 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6003 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006004
6005 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006006
6007 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006008
6009 nla_put_failure:
6010 err = -ENOBUFS;
6011 free_msg:
6012 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006013 return err;
6014}
6015
6016static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6017 struct genl_info *info)
6018{
Johannes Berg4c476992010-10-04 21:36:35 +02006019 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006020 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006021 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006022
6023 if (!info->attrs[NL80211_ATTR_COOKIE])
6024 return -EINVAL;
6025
Johannes Berg4c476992010-10-04 21:36:35 +02006026 if (!rdev->ops->cancel_remain_on_channel)
6027 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006028
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006029 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6030
Hila Gonene35e4d22012-06-27 17:19:42 +03006031 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006032}
6033
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006034static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6035 u8 *rates, u8 rates_len)
6036{
6037 u8 i;
6038 u32 mask = 0;
6039
6040 for (i = 0; i < rates_len; i++) {
6041 int rate = (rates[i] & 0x7f) * 5;
6042 int ridx;
6043 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6044 struct ieee80211_rate *srate =
6045 &sband->bitrates[ridx];
6046 if (rate == srate->bitrate) {
6047 mask |= 1 << ridx;
6048 break;
6049 }
6050 }
6051 if (ridx == sband->n_bitrates)
6052 return 0; /* rate not found */
6053 }
6054
6055 return mask;
6056}
6057
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006058static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6059 u8 *rates, u8 rates_len,
6060 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6061{
6062 u8 i;
6063
6064 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6065
6066 for (i = 0; i < rates_len; i++) {
6067 int ridx, rbit;
6068
6069 ridx = rates[i] / 8;
6070 rbit = BIT(rates[i] % 8);
6071
6072 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006073 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006074 return false;
6075
6076 /* check availability */
6077 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6078 mcs[ridx] |= rbit;
6079 else
6080 return false;
6081 }
6082
6083 return true;
6084}
6085
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006086static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006087 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6088 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006089 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6090 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006091};
6092
6093static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6094 struct genl_info *info)
6095{
6096 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006097 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006098 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006099 int rem, i;
6100 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006101 struct nlattr *tx_rates;
6102 struct ieee80211_supported_band *sband;
6103
6104 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6105 return -EINVAL;
6106
Johannes Berg4c476992010-10-04 21:36:35 +02006107 if (!rdev->ops->set_bitrate_mask)
6108 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006109
6110 memset(&mask, 0, sizeof(mask));
6111 /* Default to all rates enabled */
6112 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6113 sband = rdev->wiphy.bands[i];
6114 mask.control[i].legacy =
6115 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006116 if (sband)
6117 memcpy(mask.control[i].mcs,
6118 sband->ht_cap.mcs.rx_mask,
6119 sizeof(mask.control[i].mcs));
6120 else
6121 memset(mask.control[i].mcs, 0,
6122 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006123 }
6124
6125 /*
6126 * The nested attribute uses enum nl80211_band as the index. This maps
6127 * directly to the enum ieee80211_band values used in cfg80211.
6128 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006129 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006130 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6131 {
6132 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006133 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6134 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006135 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006136 if (sband == NULL)
6137 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006138 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6139 nla_len(tx_rates), nl80211_txattr_policy);
6140 if (tb[NL80211_TXRATE_LEGACY]) {
6141 mask.control[band].legacy = rateset_to_mask(
6142 sband,
6143 nla_data(tb[NL80211_TXRATE_LEGACY]),
6144 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306145 if ((mask.control[band].legacy == 0) &&
6146 nla_len(tb[NL80211_TXRATE_LEGACY]))
6147 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006148 }
6149 if (tb[NL80211_TXRATE_MCS]) {
6150 if (!ht_rateset_to_mask(
6151 sband,
6152 nla_data(tb[NL80211_TXRATE_MCS]),
6153 nla_len(tb[NL80211_TXRATE_MCS]),
6154 mask.control[band].mcs))
6155 return -EINVAL;
6156 }
6157
6158 if (mask.control[band].legacy == 0) {
6159 /* don't allow empty legacy rates if HT
6160 * is not even supported. */
6161 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6162 return -EINVAL;
6163
6164 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6165 if (mask.control[band].mcs[i])
6166 break;
6167
6168 /* legacy and mcs rates may not be both empty */
6169 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006170 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006171 }
6172 }
6173
Hila Gonene35e4d22012-06-27 17:19:42 +03006174 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006175}
6176
Johannes Berg2e161f72010-08-12 15:38:38 +02006177static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006178{
Johannes Berg4c476992010-10-04 21:36:35 +02006179 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006180 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006181 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006182
6183 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6184 return -EINVAL;
6185
Johannes Berg2e161f72010-08-12 15:38:38 +02006186 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6187 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006188
Johannes Berg71bbc992012-06-15 15:30:18 +02006189 switch (wdev->iftype) {
6190 case NL80211_IFTYPE_STATION:
6191 case NL80211_IFTYPE_ADHOC:
6192 case NL80211_IFTYPE_P2P_CLIENT:
6193 case NL80211_IFTYPE_AP:
6194 case NL80211_IFTYPE_AP_VLAN:
6195 case NL80211_IFTYPE_MESH_POINT:
6196 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006197 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006198 break;
6199 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006200 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006201 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006202
6203 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006204 if (!rdev->ops->mgmt_tx)
6205 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006206
Eric W. Biederman15e47302012-09-07 20:12:54 +00006207 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006208 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6209 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006210}
6211
Johannes Berg2e161f72010-08-12 15:38:38 +02006212static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006213{
Johannes Berg4c476992010-10-04 21:36:35 +02006214 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006215 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006216 struct ieee80211_channel *chan;
Jouni Malinen026331c2010-02-15 12:53:10 +02006217 u32 freq;
6218 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006219 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006220 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006221 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006222 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006223 bool offchan, no_cck, dont_wait_for_ack;
6224
6225 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006226
6227 if (!info->attrs[NL80211_ATTR_FRAME] ||
6228 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6229 return -EINVAL;
6230
Johannes Berg4c476992010-10-04 21:36:35 +02006231 if (!rdev->ops->mgmt_tx)
6232 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006233
Johannes Berg71bbc992012-06-15 15:30:18 +02006234 switch (wdev->iftype) {
6235 case NL80211_IFTYPE_STATION:
6236 case NL80211_IFTYPE_ADHOC:
6237 case NL80211_IFTYPE_P2P_CLIENT:
6238 case NL80211_IFTYPE_AP:
6239 case NL80211_IFTYPE_AP_VLAN:
6240 case NL80211_IFTYPE_MESH_POINT:
6241 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006242 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006243 break;
6244 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006245 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006246 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006247
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006248 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006249 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006250 return -EINVAL;
6251 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006252
6253 /*
6254 * We should wait on the channel for at least a minimum amount
6255 * of time (10ms) but no longer than the driver supports.
6256 */
6257 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6258 wait > rdev->wiphy.max_remain_on_channel_duration)
6259 return -EINVAL;
6260
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006261 }
6262
Johannes Berg42d97a52012-11-08 18:31:02 +01006263 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6264 !nl80211_valid_channel_type(info, NULL))
6265 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006266
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006267 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6268
Johannes Berg7c4ef712011-11-18 15:33:48 +01006269 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6270 return -EINVAL;
6271
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306272 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6273
Jouni Malinen026331c2010-02-15 12:53:10 +02006274 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
Johannes Berg42d97a52012-11-08 18:31:02 +01006275 chan = rdev_freq_to_chan(rdev, freq, NL80211_CHAN_NO_HT);
Johannes Berg4c476992010-10-04 21:36:35 +02006276 if (chan == NULL)
6277 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006278
Johannes Berge247bd902011-11-04 11:18:21 +01006279 if (!dont_wait_for_ack) {
6280 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6281 if (!msg)
6282 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006283
Eric W. Biederman15e47302012-09-07 20:12:54 +00006284 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006285 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006286
Johannes Berge247bd902011-11-04 11:18:21 +01006287 if (IS_ERR(hdr)) {
6288 err = PTR_ERR(hdr);
6289 goto free_msg;
6290 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006291 }
Johannes Berge247bd902011-11-04 11:18:21 +01006292
Johannes Berg42d97a52012-11-08 18:31:02 +01006293 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006294 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6295 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006296 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006297 if (err)
6298 goto free_msg;
6299
Johannes Berge247bd902011-11-04 11:18:21 +01006300 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006301 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6302 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006303
Johannes Berge247bd902011-11-04 11:18:21 +01006304 genlmsg_end(msg, hdr);
6305 return genlmsg_reply(msg, info);
6306 }
6307
6308 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006309
6310 nla_put_failure:
6311 err = -ENOBUFS;
6312 free_msg:
6313 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006314 return err;
6315}
6316
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006317static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6318{
6319 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006320 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006321 u64 cookie;
6322
6323 if (!info->attrs[NL80211_ATTR_COOKIE])
6324 return -EINVAL;
6325
6326 if (!rdev->ops->mgmt_tx_cancel_wait)
6327 return -EOPNOTSUPP;
6328
Johannes Berg71bbc992012-06-15 15:30:18 +02006329 switch (wdev->iftype) {
6330 case NL80211_IFTYPE_STATION:
6331 case NL80211_IFTYPE_ADHOC:
6332 case NL80211_IFTYPE_P2P_CLIENT:
6333 case NL80211_IFTYPE_AP:
6334 case NL80211_IFTYPE_AP_VLAN:
6335 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006336 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006337 break;
6338 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006339 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006340 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006341
6342 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6343
Hila Gonene35e4d22012-06-27 17:19:42 +03006344 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006345}
6346
Kalle Valoffb9eb32010-02-17 17:58:10 +02006347static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6348{
Johannes Berg4c476992010-10-04 21:36:35 +02006349 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006350 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006351 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006352 u8 ps_state;
6353 bool state;
6354 int err;
6355
Johannes Berg4c476992010-10-04 21:36:35 +02006356 if (!info->attrs[NL80211_ATTR_PS_STATE])
6357 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006358
6359 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6360
Johannes Berg4c476992010-10-04 21:36:35 +02006361 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6362 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006363
6364 wdev = dev->ieee80211_ptr;
6365
Johannes Berg4c476992010-10-04 21:36:35 +02006366 if (!rdev->ops->set_power_mgmt)
6367 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006368
6369 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6370
6371 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006372 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006373
Hila Gonene35e4d22012-06-27 17:19:42 +03006374 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006375 if (!err)
6376 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006377 return err;
6378}
6379
6380static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6381{
Johannes Berg4c476992010-10-04 21:36:35 +02006382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006383 enum nl80211_ps_state ps_state;
6384 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006385 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006386 struct sk_buff *msg;
6387 void *hdr;
6388 int err;
6389
Kalle Valoffb9eb32010-02-17 17:58:10 +02006390 wdev = dev->ieee80211_ptr;
6391
Johannes Berg4c476992010-10-04 21:36:35 +02006392 if (!rdev->ops->set_power_mgmt)
6393 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006394
6395 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006396 if (!msg)
6397 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006398
Eric W. Biederman15e47302012-09-07 20:12:54 +00006399 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006400 NL80211_CMD_GET_POWER_SAVE);
6401 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006402 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006403 goto free_msg;
6404 }
6405
6406 if (wdev->ps)
6407 ps_state = NL80211_PS_ENABLED;
6408 else
6409 ps_state = NL80211_PS_DISABLED;
6410
David S. Miller9360ffd2012-03-29 04:41:26 -04006411 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6412 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006413
6414 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006415 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006416
Johannes Berg4c476992010-10-04 21:36:35 +02006417 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006418 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006419 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006420 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006421 return err;
6422}
6423
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006424static struct nla_policy
6425nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6426 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6427 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6428 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006429 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6430 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6431 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006432};
6433
Thomas Pedersen84f10702012-07-12 16:17:33 -07006434static int nl80211_set_cqm_txe(struct genl_info *info,
6435 u32 rate, u32 pkts, u32 intvl)
6436{
6437 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6438 struct wireless_dev *wdev;
6439 struct net_device *dev = info->user_ptr[1];
6440
6441 if ((rate < 0 || rate > 100) ||
6442 (intvl < 0 || intvl > NL80211_CQM_TXE_MAX_INTVL))
6443 return -EINVAL;
6444
6445 wdev = dev->ieee80211_ptr;
6446
6447 if (!rdev->ops->set_cqm_txe_config)
6448 return -EOPNOTSUPP;
6449
6450 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6451 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6452 return -EOPNOTSUPP;
6453
Hila Gonene35e4d22012-06-27 17:19:42 +03006454 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006455}
6456
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006457static int nl80211_set_cqm_rssi(struct genl_info *info,
6458 s32 threshold, u32 hysteresis)
6459{
Johannes Berg4c476992010-10-04 21:36:35 +02006460 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006461 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006462 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006463
6464 if (threshold > 0)
6465 return -EINVAL;
6466
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006467 wdev = dev->ieee80211_ptr;
6468
Johannes Berg4c476992010-10-04 21:36:35 +02006469 if (!rdev->ops->set_cqm_rssi_config)
6470 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006471
Johannes Berg074ac8d2010-09-16 14:58:22 +02006472 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006473 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6474 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006475
Hila Gonene35e4d22012-06-27 17:19:42 +03006476 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006477}
6478
6479static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6480{
6481 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6482 struct nlattr *cqm;
6483 int err;
6484
6485 cqm = info->attrs[NL80211_ATTR_CQM];
6486 if (!cqm) {
6487 err = -EINVAL;
6488 goto out;
6489 }
6490
6491 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6492 nl80211_attr_cqm_policy);
6493 if (err)
6494 goto out;
6495
6496 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6497 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6498 s32 threshold;
6499 u32 hysteresis;
6500 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6501 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6502 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006503 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6504 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6505 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6506 u32 rate, pkts, intvl;
6507 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6508 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6509 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6510 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006511 } else
6512 err = -EINVAL;
6513
6514out:
6515 return err;
6516}
6517
Johannes Berg29cbe682010-12-03 09:20:44 +01006518static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6519{
6520 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6521 struct net_device *dev = info->user_ptr[1];
6522 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006523 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006524 int err;
6525
6526 /* start with default */
6527 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006528 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006529
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006530 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006531 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006532 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006533 if (err)
6534 return err;
6535 }
6536
6537 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6538 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6539 return -EINVAL;
6540
Javier Cardonac80d5452010-12-16 17:37:49 -08006541 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6542 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6543
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006544 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6545 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6546 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6547 return -EINVAL;
6548
Javier Cardonac80d5452010-12-16 17:37:49 -08006549 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6550 /* parse additional setup parameters if given */
6551 err = nl80211_parse_mesh_setup(info, &setup);
6552 if (err)
6553 return err;
6554 }
6555
Johannes Bergcc1d2802012-05-16 23:50:20 +02006556 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6557 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6558
6559 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6560 !nl80211_valid_channel_type(info, &channel_type))
6561 return -EINVAL;
6562
6563 setup.channel = rdev_freq_to_chan(rdev,
6564 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6565 channel_type);
6566 if (!setup.channel)
6567 return -EINVAL;
6568 setup.channel_type = channel_type;
6569 } else {
6570 /* cfg80211_join_mesh() will sort it out */
6571 setup.channel = NULL;
6572 }
6573
Javier Cardonac80d5452010-12-16 17:37:49 -08006574 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006575}
6576
6577static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6578{
6579 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6580 struct net_device *dev = info->user_ptr[1];
6581
6582 return cfg80211_leave_mesh(rdev, dev);
6583}
6584
Johannes Bergdfb89c52012-06-27 09:23:48 +02006585#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006586static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6587{
6588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6589 struct sk_buff *msg;
6590 void *hdr;
6591
6592 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6593 return -EOPNOTSUPP;
6594
6595 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6596 if (!msg)
6597 return -ENOMEM;
6598
Eric W. Biederman15e47302012-09-07 20:12:54 +00006599 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006600 NL80211_CMD_GET_WOWLAN);
6601 if (!hdr)
6602 goto nla_put_failure;
6603
6604 if (rdev->wowlan) {
6605 struct nlattr *nl_wowlan;
6606
6607 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6608 if (!nl_wowlan)
6609 goto nla_put_failure;
6610
David S. Miller9360ffd2012-03-29 04:41:26 -04006611 if ((rdev->wowlan->any &&
6612 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6613 (rdev->wowlan->disconnect &&
6614 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6615 (rdev->wowlan->magic_pkt &&
6616 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6617 (rdev->wowlan->gtk_rekey_failure &&
6618 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6619 (rdev->wowlan->eap_identity_req &&
6620 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6621 (rdev->wowlan->four_way_handshake &&
6622 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6623 (rdev->wowlan->rfkill_release &&
6624 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6625 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006626 if (rdev->wowlan->n_patterns) {
6627 struct nlattr *nl_pats, *nl_pat;
6628 int i, pat_len;
6629
6630 nl_pats = nla_nest_start(msg,
6631 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6632 if (!nl_pats)
6633 goto nla_put_failure;
6634
6635 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6636 nl_pat = nla_nest_start(msg, i + 1);
6637 if (!nl_pat)
6638 goto nla_put_failure;
6639 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006640 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6641 DIV_ROUND_UP(pat_len, 8),
6642 rdev->wowlan->patterns[i].mask) ||
6643 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6644 pat_len,
6645 rdev->wowlan->patterns[i].pattern))
6646 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006647 nla_nest_end(msg, nl_pat);
6648 }
6649 nla_nest_end(msg, nl_pats);
6650 }
6651
6652 nla_nest_end(msg, nl_wowlan);
6653 }
6654
6655 genlmsg_end(msg, hdr);
6656 return genlmsg_reply(msg, info);
6657
6658nla_put_failure:
6659 nlmsg_free(msg);
6660 return -ENOBUFS;
6661}
6662
6663static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6664{
6665 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6666 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006667 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006668 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006669 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6670 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006671 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006672
6673 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6674 return -EOPNOTSUPP;
6675
Johannes Bergae33bd82012-07-12 16:25:02 +02006676 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6677 cfg80211_rdev_free_wowlan(rdev);
6678 rdev->wowlan = NULL;
6679 goto set_wakeup;
6680 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006681
6682 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6683 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6684 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6685 nl80211_wowlan_policy);
6686 if (err)
6687 return err;
6688
6689 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6690 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6691 return -EINVAL;
6692 new_triggers.any = true;
6693 }
6694
6695 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6696 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6697 return -EINVAL;
6698 new_triggers.disconnect = true;
6699 }
6700
6701 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6702 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6703 return -EINVAL;
6704 new_triggers.magic_pkt = true;
6705 }
6706
Johannes Berg77dbbb12011-07-13 10:48:55 +02006707 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6708 return -EINVAL;
6709
6710 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6711 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6712 return -EINVAL;
6713 new_triggers.gtk_rekey_failure = true;
6714 }
6715
6716 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6717 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6718 return -EINVAL;
6719 new_triggers.eap_identity_req = true;
6720 }
6721
6722 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6723 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6724 return -EINVAL;
6725 new_triggers.four_way_handshake = true;
6726 }
6727
6728 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6729 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6730 return -EINVAL;
6731 new_triggers.rfkill_release = true;
6732 }
6733
Johannes Bergff1b6e62011-05-04 15:37:28 +02006734 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6735 struct nlattr *pat;
6736 int n_patterns = 0;
6737 int rem, pat_len, mask_len;
6738 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6739
6740 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6741 rem)
6742 n_patterns++;
6743 if (n_patterns > wowlan->n_patterns)
6744 return -EINVAL;
6745
6746 new_triggers.patterns = kcalloc(n_patterns,
6747 sizeof(new_triggers.patterns[0]),
6748 GFP_KERNEL);
6749 if (!new_triggers.patterns)
6750 return -ENOMEM;
6751
6752 new_triggers.n_patterns = n_patterns;
6753 i = 0;
6754
6755 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6756 rem) {
6757 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6758 nla_data(pat), nla_len(pat), NULL);
6759 err = -EINVAL;
6760 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6761 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6762 goto error;
6763 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6764 mask_len = DIV_ROUND_UP(pat_len, 8);
6765 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6766 mask_len)
6767 goto error;
6768 if (pat_len > wowlan->pattern_max_len ||
6769 pat_len < wowlan->pattern_min_len)
6770 goto error;
6771
6772 new_triggers.patterns[i].mask =
6773 kmalloc(mask_len + pat_len, GFP_KERNEL);
6774 if (!new_triggers.patterns[i].mask) {
6775 err = -ENOMEM;
6776 goto error;
6777 }
6778 new_triggers.patterns[i].pattern =
6779 new_triggers.patterns[i].mask + mask_len;
6780 memcpy(new_triggers.patterns[i].mask,
6781 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6782 mask_len);
6783 new_triggers.patterns[i].pattern_len = pat_len;
6784 memcpy(new_triggers.patterns[i].pattern,
6785 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6786 pat_len);
6787 i++;
6788 }
6789 }
6790
Johannes Bergae33bd82012-07-12 16:25:02 +02006791 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6792 if (!ntrig) {
6793 err = -ENOMEM;
6794 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006795 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006796 cfg80211_rdev_free_wowlan(rdev);
6797 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006798
Johannes Bergae33bd82012-07-12 16:25:02 +02006799 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006800 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03006801 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02006802
Johannes Bergff1b6e62011-05-04 15:37:28 +02006803 return 0;
6804 error:
6805 for (i = 0; i < new_triggers.n_patterns; i++)
6806 kfree(new_triggers.patterns[i].mask);
6807 kfree(new_triggers.patterns);
6808 return err;
6809}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006810#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006811
Johannes Berge5497d72011-07-05 16:35:40 +02006812static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6813{
6814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6815 struct net_device *dev = info->user_ptr[1];
6816 struct wireless_dev *wdev = dev->ieee80211_ptr;
6817 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6818 struct cfg80211_gtk_rekey_data rekey_data;
6819 int err;
6820
6821 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6822 return -EINVAL;
6823
6824 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6825 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6826 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6827 nl80211_rekey_policy);
6828 if (err)
6829 return err;
6830
6831 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6832 return -ERANGE;
6833 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6834 return -ERANGE;
6835 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6836 return -ERANGE;
6837
6838 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6839 NL80211_KEK_LEN);
6840 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6841 NL80211_KCK_LEN);
6842 memcpy(rekey_data.replay_ctr,
6843 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6844 NL80211_REPLAY_CTR_LEN);
6845
6846 wdev_lock(wdev);
6847 if (!wdev->current_bss) {
6848 err = -ENOTCONN;
6849 goto out;
6850 }
6851
6852 if (!rdev->ops->set_rekey_data) {
6853 err = -EOPNOTSUPP;
6854 goto out;
6855 }
6856
Hila Gonene35e4d22012-06-27 17:19:42 +03006857 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02006858 out:
6859 wdev_unlock(wdev);
6860 return err;
6861}
6862
Johannes Berg28946da2011-11-04 11:18:12 +01006863static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6864 struct genl_info *info)
6865{
6866 struct net_device *dev = info->user_ptr[1];
6867 struct wireless_dev *wdev = dev->ieee80211_ptr;
6868
6869 if (wdev->iftype != NL80211_IFTYPE_AP &&
6870 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6871 return -EINVAL;
6872
Eric W. Biederman15e47302012-09-07 20:12:54 +00006873 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006874 return -EBUSY;
6875
Eric W. Biederman15e47302012-09-07 20:12:54 +00006876 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006877 return 0;
6878}
6879
Johannes Berg7f6cf312011-11-04 11:18:15 +01006880static int nl80211_probe_client(struct sk_buff *skb,
6881 struct genl_info *info)
6882{
6883 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6884 struct net_device *dev = info->user_ptr[1];
6885 struct wireless_dev *wdev = dev->ieee80211_ptr;
6886 struct sk_buff *msg;
6887 void *hdr;
6888 const u8 *addr;
6889 u64 cookie;
6890 int err;
6891
6892 if (wdev->iftype != NL80211_IFTYPE_AP &&
6893 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6894 return -EOPNOTSUPP;
6895
6896 if (!info->attrs[NL80211_ATTR_MAC])
6897 return -EINVAL;
6898
6899 if (!rdev->ops->probe_client)
6900 return -EOPNOTSUPP;
6901
6902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6903 if (!msg)
6904 return -ENOMEM;
6905
Eric W. Biederman15e47302012-09-07 20:12:54 +00006906 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01006907 NL80211_CMD_PROBE_CLIENT);
6908
6909 if (IS_ERR(hdr)) {
6910 err = PTR_ERR(hdr);
6911 goto free_msg;
6912 }
6913
6914 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6915
Hila Gonene35e4d22012-06-27 17:19:42 +03006916 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01006917 if (err)
6918 goto free_msg;
6919
David S. Miller9360ffd2012-03-29 04:41:26 -04006920 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6921 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006922
6923 genlmsg_end(msg, hdr);
6924
6925 return genlmsg_reply(msg, info);
6926
6927 nla_put_failure:
6928 err = -ENOBUFS;
6929 free_msg:
6930 nlmsg_free(msg);
6931 return err;
6932}
6933
Johannes Berg5e760232011-11-04 11:18:17 +01006934static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6935{
6936 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07006937 struct cfg80211_beacon_registration *reg, *nreg;
6938 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01006939
6940 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6941 return -EOPNOTSUPP;
6942
Ben Greear37c73b52012-10-26 14:49:25 -07006943 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
6944 if (!nreg)
6945 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01006946
Ben Greear37c73b52012-10-26 14:49:25 -07006947 /* First, check if already registered. */
6948 spin_lock_bh(&rdev->beacon_registrations_lock);
6949 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
6950 if (reg->nlportid == info->snd_portid) {
6951 rv = -EALREADY;
6952 goto out_err;
6953 }
6954 }
6955 /* Add it to the list */
6956 nreg->nlportid = info->snd_portid;
6957 list_add(&nreg->list, &rdev->beacon_registrations);
6958
6959 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01006960
6961 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07006962out_err:
6963 spin_unlock_bh(&rdev->beacon_registrations_lock);
6964 kfree(nreg);
6965 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01006966}
6967
Johannes Berg98104fde2012-06-16 00:19:54 +02006968static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
6969{
6970 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6971 struct wireless_dev *wdev = info->user_ptr[1];
6972 int err;
6973
6974 if (!rdev->ops->start_p2p_device)
6975 return -EOPNOTSUPP;
6976
6977 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
6978 return -EOPNOTSUPP;
6979
6980 if (wdev->p2p_started)
6981 return 0;
6982
6983 mutex_lock(&rdev->devlist_mtx);
6984 err = cfg80211_can_add_interface(rdev, wdev->iftype);
6985 mutex_unlock(&rdev->devlist_mtx);
6986 if (err)
6987 return err;
6988
Johannes Bergeeb126e2012-10-23 15:16:50 +02006989 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02006990 if (err)
6991 return err;
6992
6993 wdev->p2p_started = true;
6994 mutex_lock(&rdev->devlist_mtx);
6995 rdev->opencount++;
6996 mutex_unlock(&rdev->devlist_mtx);
6997
6998 return 0;
6999}
7000
7001static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7002{
7003 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7004 struct wireless_dev *wdev = info->user_ptr[1];
7005
7006 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7007 return -EOPNOTSUPP;
7008
7009 if (!rdev->ops->stop_p2p_device)
7010 return -EOPNOTSUPP;
7011
7012 if (!wdev->p2p_started)
7013 return 0;
7014
Johannes Bergeeb126e2012-10-23 15:16:50 +02007015 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007016 wdev->p2p_started = false;
7017
7018 mutex_lock(&rdev->devlist_mtx);
7019 rdev->opencount--;
7020 mutex_unlock(&rdev->devlist_mtx);
7021
7022 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7023 rdev->scan_req->aborted = true;
7024 ___cfg80211_scan_done(rdev, true);
7025 }
7026
7027 return 0;
7028}
7029
Johannes Berg4c476992010-10-04 21:36:35 +02007030#define NL80211_FLAG_NEED_WIPHY 0x01
7031#define NL80211_FLAG_NEED_NETDEV 0x02
7032#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007033#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7034#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7035 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007036#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007037/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007038#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7039 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007040
7041static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7042 struct genl_info *info)
7043{
7044 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007045 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007046 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007047 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7048
7049 if (rtnl)
7050 rtnl_lock();
7051
7052 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007053 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007054 if (IS_ERR(rdev)) {
7055 if (rtnl)
7056 rtnl_unlock();
7057 return PTR_ERR(rdev);
7058 }
7059 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007060 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7061 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007062 mutex_lock(&cfg80211_mutex);
7063 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7064 info->attrs);
7065 if (IS_ERR(wdev)) {
7066 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007067 if (rtnl)
7068 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007069 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007070 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007071
Johannes Berg89a54e42012-06-15 14:33:17 +02007072 dev = wdev->netdev;
7073 rdev = wiphy_to_dev(wdev->wiphy);
7074
Johannes Berg1bf614e2012-06-15 15:23:36 +02007075 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7076 if (!dev) {
7077 mutex_unlock(&cfg80211_mutex);
7078 if (rtnl)
7079 rtnl_unlock();
7080 return -EINVAL;
7081 }
7082
7083 info->user_ptr[1] = dev;
7084 } else {
7085 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007086 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007087
Johannes Berg1bf614e2012-06-15 15:23:36 +02007088 if (dev) {
7089 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7090 !netif_running(dev)) {
7091 mutex_unlock(&cfg80211_mutex);
7092 if (rtnl)
7093 rtnl_unlock();
7094 return -ENETDOWN;
7095 }
7096
7097 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007098 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7099 if (!wdev->p2p_started) {
7100 mutex_unlock(&cfg80211_mutex);
7101 if (rtnl)
7102 rtnl_unlock();
7103 return -ENETDOWN;
7104 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007105 }
7106
Johannes Berg89a54e42012-06-15 14:33:17 +02007107 cfg80211_lock_rdev(rdev);
7108
7109 mutex_unlock(&cfg80211_mutex);
7110
Johannes Berg4c476992010-10-04 21:36:35 +02007111 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007112 }
7113
7114 return 0;
7115}
7116
7117static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7118 struct genl_info *info)
7119{
7120 if (info->user_ptr[0])
7121 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007122 if (info->user_ptr[1]) {
7123 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7124 struct wireless_dev *wdev = info->user_ptr[1];
7125
7126 if (wdev->netdev)
7127 dev_put(wdev->netdev);
7128 } else {
7129 dev_put(info->user_ptr[1]);
7130 }
7131 }
Johannes Berg4c476992010-10-04 21:36:35 +02007132 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7133 rtnl_unlock();
7134}
7135
Johannes Berg55682962007-09-20 13:09:35 -04007136static struct genl_ops nl80211_ops[] = {
7137 {
7138 .cmd = NL80211_CMD_GET_WIPHY,
7139 .doit = nl80211_get_wiphy,
7140 .dumpit = nl80211_dump_wiphy,
7141 .policy = nl80211_policy,
7142 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007143 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007144 },
7145 {
7146 .cmd = NL80211_CMD_SET_WIPHY,
7147 .doit = nl80211_set_wiphy,
7148 .policy = nl80211_policy,
7149 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007150 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007151 },
7152 {
7153 .cmd = NL80211_CMD_GET_INTERFACE,
7154 .doit = nl80211_get_interface,
7155 .dumpit = nl80211_dump_interface,
7156 .policy = nl80211_policy,
7157 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007158 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007159 },
7160 {
7161 .cmd = NL80211_CMD_SET_INTERFACE,
7162 .doit = nl80211_set_interface,
7163 .policy = nl80211_policy,
7164 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007165 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7166 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007167 },
7168 {
7169 .cmd = NL80211_CMD_NEW_INTERFACE,
7170 .doit = nl80211_new_interface,
7171 .policy = nl80211_policy,
7172 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007173 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7174 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007175 },
7176 {
7177 .cmd = NL80211_CMD_DEL_INTERFACE,
7178 .doit = nl80211_del_interface,
7179 .policy = nl80211_policy,
7180 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007181 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007182 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007183 },
Johannes Berg41ade002007-12-19 02:03:29 +01007184 {
7185 .cmd = NL80211_CMD_GET_KEY,
7186 .doit = nl80211_get_key,
7187 .policy = nl80211_policy,
7188 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007189 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007190 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007191 },
7192 {
7193 .cmd = NL80211_CMD_SET_KEY,
7194 .doit = nl80211_set_key,
7195 .policy = nl80211_policy,
7196 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007197 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007198 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007199 },
7200 {
7201 .cmd = NL80211_CMD_NEW_KEY,
7202 .doit = nl80211_new_key,
7203 .policy = nl80211_policy,
7204 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007205 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007206 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007207 },
7208 {
7209 .cmd = NL80211_CMD_DEL_KEY,
7210 .doit = nl80211_del_key,
7211 .policy = nl80211_policy,
7212 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007213 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007214 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007215 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007216 {
7217 .cmd = NL80211_CMD_SET_BEACON,
7218 .policy = nl80211_policy,
7219 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007220 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007221 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007222 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007223 },
7224 {
Johannes Berg88600202012-02-13 15:17:18 +01007225 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007226 .policy = nl80211_policy,
7227 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007228 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007229 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007230 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007231 },
7232 {
Johannes Berg88600202012-02-13 15:17:18 +01007233 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007234 .policy = nl80211_policy,
7235 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007236 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007237 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007238 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007239 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007240 {
7241 .cmd = NL80211_CMD_GET_STATION,
7242 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007243 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007244 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007245 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7246 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007247 },
7248 {
7249 .cmd = NL80211_CMD_SET_STATION,
7250 .doit = nl80211_set_station,
7251 .policy = nl80211_policy,
7252 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007253 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007254 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007255 },
7256 {
7257 .cmd = NL80211_CMD_NEW_STATION,
7258 .doit = nl80211_new_station,
7259 .policy = nl80211_policy,
7260 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007261 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007262 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007263 },
7264 {
7265 .cmd = NL80211_CMD_DEL_STATION,
7266 .doit = nl80211_del_station,
7267 .policy = nl80211_policy,
7268 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007269 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007270 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007271 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007272 {
7273 .cmd = NL80211_CMD_GET_MPATH,
7274 .doit = nl80211_get_mpath,
7275 .dumpit = nl80211_dump_mpath,
7276 .policy = nl80211_policy,
7277 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007278 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007279 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007280 },
7281 {
7282 .cmd = NL80211_CMD_SET_MPATH,
7283 .doit = nl80211_set_mpath,
7284 .policy = nl80211_policy,
7285 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007286 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007287 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007288 },
7289 {
7290 .cmd = NL80211_CMD_NEW_MPATH,
7291 .doit = nl80211_new_mpath,
7292 .policy = nl80211_policy,
7293 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007294 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007295 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007296 },
7297 {
7298 .cmd = NL80211_CMD_DEL_MPATH,
7299 .doit = nl80211_del_mpath,
7300 .policy = nl80211_policy,
7301 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007302 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007303 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007304 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007305 {
7306 .cmd = NL80211_CMD_SET_BSS,
7307 .doit = nl80211_set_bss,
7308 .policy = nl80211_policy,
7309 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007310 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007311 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007312 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007313 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007314 .cmd = NL80211_CMD_GET_REG,
7315 .doit = nl80211_get_reg,
7316 .policy = nl80211_policy,
7317 /* can be retrieved by unprivileged users */
7318 },
7319 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007320 .cmd = NL80211_CMD_SET_REG,
7321 .doit = nl80211_set_reg,
7322 .policy = nl80211_policy,
7323 .flags = GENL_ADMIN_PERM,
7324 },
7325 {
7326 .cmd = NL80211_CMD_REQ_SET_REG,
7327 .doit = nl80211_req_set_reg,
7328 .policy = nl80211_policy,
7329 .flags = GENL_ADMIN_PERM,
7330 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007331 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007332 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7333 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007334 .policy = nl80211_policy,
7335 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007336 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007337 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007338 },
7339 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007340 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7341 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007342 .policy = nl80211_policy,
7343 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007345 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007346 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007347 {
Johannes Berg2a519312009-02-10 21:25:55 +01007348 .cmd = NL80211_CMD_TRIGGER_SCAN,
7349 .doit = nl80211_trigger_scan,
7350 .policy = nl80211_policy,
7351 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007352 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007353 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007354 },
7355 {
7356 .cmd = NL80211_CMD_GET_SCAN,
7357 .policy = nl80211_policy,
7358 .dumpit = nl80211_dump_scan,
7359 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007360 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007361 .cmd = NL80211_CMD_START_SCHED_SCAN,
7362 .doit = nl80211_start_sched_scan,
7363 .policy = nl80211_policy,
7364 .flags = GENL_ADMIN_PERM,
7365 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7366 NL80211_FLAG_NEED_RTNL,
7367 },
7368 {
7369 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7370 .doit = nl80211_stop_sched_scan,
7371 .policy = nl80211_policy,
7372 .flags = GENL_ADMIN_PERM,
7373 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7374 NL80211_FLAG_NEED_RTNL,
7375 },
7376 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007377 .cmd = NL80211_CMD_AUTHENTICATE,
7378 .doit = nl80211_authenticate,
7379 .policy = nl80211_policy,
7380 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007381 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007382 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007383 },
7384 {
7385 .cmd = NL80211_CMD_ASSOCIATE,
7386 .doit = nl80211_associate,
7387 .policy = nl80211_policy,
7388 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007389 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007390 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007391 },
7392 {
7393 .cmd = NL80211_CMD_DEAUTHENTICATE,
7394 .doit = nl80211_deauthenticate,
7395 .policy = nl80211_policy,
7396 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007397 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007398 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007399 },
7400 {
7401 .cmd = NL80211_CMD_DISASSOCIATE,
7402 .doit = nl80211_disassociate,
7403 .policy = nl80211_policy,
7404 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007405 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007406 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007407 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007408 {
7409 .cmd = NL80211_CMD_JOIN_IBSS,
7410 .doit = nl80211_join_ibss,
7411 .policy = nl80211_policy,
7412 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007413 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007414 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007415 },
7416 {
7417 .cmd = NL80211_CMD_LEAVE_IBSS,
7418 .doit = nl80211_leave_ibss,
7419 .policy = nl80211_policy,
7420 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007421 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007422 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007423 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007424#ifdef CONFIG_NL80211_TESTMODE
7425 {
7426 .cmd = NL80211_CMD_TESTMODE,
7427 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007428 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007429 .policy = nl80211_policy,
7430 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007431 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7432 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007433 },
7434#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007435 {
7436 .cmd = NL80211_CMD_CONNECT,
7437 .doit = nl80211_connect,
7438 .policy = nl80211_policy,
7439 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007440 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007441 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007442 },
7443 {
7444 .cmd = NL80211_CMD_DISCONNECT,
7445 .doit = nl80211_disconnect,
7446 .policy = nl80211_policy,
7447 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007448 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007449 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007450 },
Johannes Berg463d0182009-07-14 00:33:35 +02007451 {
7452 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7453 .doit = nl80211_wiphy_netns,
7454 .policy = nl80211_policy,
7455 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007456 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7457 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007458 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007459 {
7460 .cmd = NL80211_CMD_GET_SURVEY,
7461 .policy = nl80211_policy,
7462 .dumpit = nl80211_dump_survey,
7463 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007464 {
7465 .cmd = NL80211_CMD_SET_PMKSA,
7466 .doit = nl80211_setdel_pmksa,
7467 .policy = nl80211_policy,
7468 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007469 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007470 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007471 },
7472 {
7473 .cmd = NL80211_CMD_DEL_PMKSA,
7474 .doit = nl80211_setdel_pmksa,
7475 .policy = nl80211_policy,
7476 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007477 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007478 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007479 },
7480 {
7481 .cmd = NL80211_CMD_FLUSH_PMKSA,
7482 .doit = nl80211_flush_pmksa,
7483 .policy = nl80211_policy,
7484 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007485 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007486 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007487 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007488 {
7489 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7490 .doit = nl80211_remain_on_channel,
7491 .policy = nl80211_policy,
7492 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007493 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007494 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007495 },
7496 {
7497 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7498 .doit = nl80211_cancel_remain_on_channel,
7499 .policy = nl80211_policy,
7500 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007501 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007502 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007503 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007504 {
7505 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7506 .doit = nl80211_set_tx_bitrate_mask,
7507 .policy = nl80211_policy,
7508 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007509 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7510 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007511 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007512 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007513 .cmd = NL80211_CMD_REGISTER_FRAME,
7514 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007515 .policy = nl80211_policy,
7516 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007517 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007518 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007519 },
7520 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007521 .cmd = NL80211_CMD_FRAME,
7522 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007523 .policy = nl80211_policy,
7524 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007525 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007526 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007527 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007528 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007529 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7530 .doit = nl80211_tx_mgmt_cancel_wait,
7531 .policy = nl80211_policy,
7532 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007533 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007534 NL80211_FLAG_NEED_RTNL,
7535 },
7536 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007537 .cmd = NL80211_CMD_SET_POWER_SAVE,
7538 .doit = nl80211_set_power_save,
7539 .policy = nl80211_policy,
7540 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007541 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7542 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007543 },
7544 {
7545 .cmd = NL80211_CMD_GET_POWER_SAVE,
7546 .doit = nl80211_get_power_save,
7547 .policy = nl80211_policy,
7548 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007549 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7550 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007551 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007552 {
7553 .cmd = NL80211_CMD_SET_CQM,
7554 .doit = nl80211_set_cqm,
7555 .policy = nl80211_policy,
7556 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007557 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7558 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007559 },
Johannes Bergf444de02010-05-05 15:25:02 +02007560 {
7561 .cmd = NL80211_CMD_SET_CHANNEL,
7562 .doit = nl80211_set_channel,
7563 .policy = nl80211_policy,
7564 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007565 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7566 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007567 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007568 {
7569 .cmd = NL80211_CMD_SET_WDS_PEER,
7570 .doit = nl80211_set_wds_peer,
7571 .policy = nl80211_policy,
7572 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007573 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7574 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007575 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007576 {
7577 .cmd = NL80211_CMD_JOIN_MESH,
7578 .doit = nl80211_join_mesh,
7579 .policy = nl80211_policy,
7580 .flags = GENL_ADMIN_PERM,
7581 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7582 NL80211_FLAG_NEED_RTNL,
7583 },
7584 {
7585 .cmd = NL80211_CMD_LEAVE_MESH,
7586 .doit = nl80211_leave_mesh,
7587 .policy = nl80211_policy,
7588 .flags = GENL_ADMIN_PERM,
7589 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7590 NL80211_FLAG_NEED_RTNL,
7591 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007592#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007593 {
7594 .cmd = NL80211_CMD_GET_WOWLAN,
7595 .doit = nl80211_get_wowlan,
7596 .policy = nl80211_policy,
7597 /* can be retrieved by unprivileged users */
7598 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7599 NL80211_FLAG_NEED_RTNL,
7600 },
7601 {
7602 .cmd = NL80211_CMD_SET_WOWLAN,
7603 .doit = nl80211_set_wowlan,
7604 .policy = nl80211_policy,
7605 .flags = GENL_ADMIN_PERM,
7606 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7607 NL80211_FLAG_NEED_RTNL,
7608 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007609#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007610 {
7611 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7612 .doit = nl80211_set_rekey_data,
7613 .policy = nl80211_policy,
7614 .flags = GENL_ADMIN_PERM,
7615 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7616 NL80211_FLAG_NEED_RTNL,
7617 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007618 {
7619 .cmd = NL80211_CMD_TDLS_MGMT,
7620 .doit = nl80211_tdls_mgmt,
7621 .policy = nl80211_policy,
7622 .flags = GENL_ADMIN_PERM,
7623 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7624 NL80211_FLAG_NEED_RTNL,
7625 },
7626 {
7627 .cmd = NL80211_CMD_TDLS_OPER,
7628 .doit = nl80211_tdls_oper,
7629 .policy = nl80211_policy,
7630 .flags = GENL_ADMIN_PERM,
7631 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7632 NL80211_FLAG_NEED_RTNL,
7633 },
Johannes Berg28946da2011-11-04 11:18:12 +01007634 {
7635 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7636 .doit = nl80211_register_unexpected_frame,
7637 .policy = nl80211_policy,
7638 .flags = GENL_ADMIN_PERM,
7639 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7640 NL80211_FLAG_NEED_RTNL,
7641 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007642 {
7643 .cmd = NL80211_CMD_PROBE_CLIENT,
7644 .doit = nl80211_probe_client,
7645 .policy = nl80211_policy,
7646 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007647 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007648 NL80211_FLAG_NEED_RTNL,
7649 },
Johannes Berg5e760232011-11-04 11:18:17 +01007650 {
7651 .cmd = NL80211_CMD_REGISTER_BEACONS,
7652 .doit = nl80211_register_beacons,
7653 .policy = nl80211_policy,
7654 .flags = GENL_ADMIN_PERM,
7655 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7656 NL80211_FLAG_NEED_RTNL,
7657 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007658 {
7659 .cmd = NL80211_CMD_SET_NOACK_MAP,
7660 .doit = nl80211_set_noack_map,
7661 .policy = nl80211_policy,
7662 .flags = GENL_ADMIN_PERM,
7663 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7664 NL80211_FLAG_NEED_RTNL,
7665 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007666 {
7667 .cmd = NL80211_CMD_START_P2P_DEVICE,
7668 .doit = nl80211_start_p2p_device,
7669 .policy = nl80211_policy,
7670 .flags = GENL_ADMIN_PERM,
7671 .internal_flags = NL80211_FLAG_NEED_WDEV |
7672 NL80211_FLAG_NEED_RTNL,
7673 },
7674 {
7675 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7676 .doit = nl80211_stop_p2p_device,
7677 .policy = nl80211_policy,
7678 .flags = GENL_ADMIN_PERM,
7679 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7680 NL80211_FLAG_NEED_RTNL,
7681 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007682 {
7683 .cmd = NL80211_CMD_SET_MCAST_RATE,
7684 .doit = nl80211_set_mcast_rate,
7685 .policy = nl80211_policy,
7686 .flags = GENL_ADMIN_PERM,
7687 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7688 NL80211_FLAG_NEED_RTNL,
7689 },
Johannes Berg55682962007-09-20 13:09:35 -04007690};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007691
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007692static struct genl_multicast_group nl80211_mlme_mcgrp = {
7693 .name = "mlme",
7694};
Johannes Berg55682962007-09-20 13:09:35 -04007695
7696/* multicast groups */
7697static struct genl_multicast_group nl80211_config_mcgrp = {
7698 .name = "config",
7699};
Johannes Berg2a519312009-02-10 21:25:55 +01007700static struct genl_multicast_group nl80211_scan_mcgrp = {
7701 .name = "scan",
7702};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007703static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7704 .name = "regulatory",
7705};
Johannes Berg55682962007-09-20 13:09:35 -04007706
7707/* notification functions */
7708
7709void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7710{
7711 struct sk_buff *msg;
7712
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007713 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007714 if (!msg)
7715 return;
7716
7717 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7718 nlmsg_free(msg);
7719 return;
7720 }
7721
Johannes Berg463d0182009-07-14 00:33:35 +02007722 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7723 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007724}
7725
Johannes Berg362a4152009-05-24 16:43:15 +02007726static int nl80211_add_scan_req(struct sk_buff *msg,
7727 struct cfg80211_registered_device *rdev)
7728{
7729 struct cfg80211_scan_request *req = rdev->scan_req;
7730 struct nlattr *nest;
7731 int i;
7732
Johannes Berg667503d2009-07-07 03:56:11 +02007733 ASSERT_RDEV_LOCK(rdev);
7734
Johannes Berg362a4152009-05-24 16:43:15 +02007735 if (WARN_ON(!req))
7736 return 0;
7737
7738 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7739 if (!nest)
7740 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007741 for (i = 0; i < req->n_ssids; i++) {
7742 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7743 goto nla_put_failure;
7744 }
Johannes Berg362a4152009-05-24 16:43:15 +02007745 nla_nest_end(msg, nest);
7746
7747 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7748 if (!nest)
7749 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007750 for (i = 0; i < req->n_channels; i++) {
7751 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7752 goto nla_put_failure;
7753 }
Johannes Berg362a4152009-05-24 16:43:15 +02007754 nla_nest_end(msg, nest);
7755
David S. Miller9360ffd2012-03-29 04:41:26 -04007756 if (req->ie &&
7757 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7758 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007759
Sam Lefflered4737712012-10-11 21:03:31 -07007760 if (req->flags)
7761 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7762
Johannes Berg362a4152009-05-24 16:43:15 +02007763 return 0;
7764 nla_put_failure:
7765 return -ENOBUFS;
7766}
7767
Johannes Berga538e2d2009-06-16 19:56:42 +02007768static int nl80211_send_scan_msg(struct sk_buff *msg,
7769 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007770 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007771 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007772 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007773{
7774 void *hdr;
7775
Eric W. Biederman15e47302012-09-07 20:12:54 +00007776 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007777 if (!hdr)
7778 return -1;
7779
David S. Miller9360ffd2012-03-29 04:41:26 -04007780 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007781 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7782 wdev->netdev->ifindex)) ||
7783 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007784 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007785
Johannes Berg362a4152009-05-24 16:43:15 +02007786 /* ignore errors and send incomplete event anyway */
7787 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007788
7789 return genlmsg_end(msg, hdr);
7790
7791 nla_put_failure:
7792 genlmsg_cancel(msg, hdr);
7793 return -EMSGSIZE;
7794}
7795
Luciano Coelho807f8a82011-05-11 17:09:35 +03007796static int
7797nl80211_send_sched_scan_msg(struct sk_buff *msg,
7798 struct cfg80211_registered_device *rdev,
7799 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007800 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007801{
7802 void *hdr;
7803
Eric W. Biederman15e47302012-09-07 20:12:54 +00007804 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007805 if (!hdr)
7806 return -1;
7807
David S. Miller9360ffd2012-03-29 04:41:26 -04007808 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7809 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7810 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007811
7812 return genlmsg_end(msg, hdr);
7813
7814 nla_put_failure:
7815 genlmsg_cancel(msg, hdr);
7816 return -EMSGSIZE;
7817}
7818
Johannes Berga538e2d2009-06-16 19:56:42 +02007819void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007820 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007821{
7822 struct sk_buff *msg;
7823
Thomas Graf58050fc2012-06-28 03:57:45 +00007824 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007825 if (!msg)
7826 return;
7827
Johannes Bergfd014282012-06-18 19:17:03 +02007828 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007829 NL80211_CMD_TRIGGER_SCAN) < 0) {
7830 nlmsg_free(msg);
7831 return;
7832 }
7833
Johannes Berg463d0182009-07-14 00:33:35 +02007834 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7835 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007836}
7837
Johannes Berg2a519312009-02-10 21:25:55 +01007838void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007839 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007840{
7841 struct sk_buff *msg;
7842
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007843 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007844 if (!msg)
7845 return;
7846
Johannes Bergfd014282012-06-18 19:17:03 +02007847 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007848 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007849 nlmsg_free(msg);
7850 return;
7851 }
7852
Johannes Berg463d0182009-07-14 00:33:35 +02007853 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7854 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007855}
7856
7857void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007858 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007859{
7860 struct sk_buff *msg;
7861
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007863 if (!msg)
7864 return;
7865
Johannes Bergfd014282012-06-18 19:17:03 +02007866 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007867 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007868 nlmsg_free(msg);
7869 return;
7870 }
7871
Johannes Berg463d0182009-07-14 00:33:35 +02007872 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7873 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007874}
7875
Luciano Coelho807f8a82011-05-11 17:09:35 +03007876void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7877 struct net_device *netdev)
7878{
7879 struct sk_buff *msg;
7880
7881 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7882 if (!msg)
7883 return;
7884
7885 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7886 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7887 nlmsg_free(msg);
7888 return;
7889 }
7890
7891 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7892 nl80211_scan_mcgrp.id, GFP_KERNEL);
7893}
7894
7895void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7896 struct net_device *netdev, u32 cmd)
7897{
7898 struct sk_buff *msg;
7899
Thomas Graf58050fc2012-06-28 03:57:45 +00007900 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007901 if (!msg)
7902 return;
7903
7904 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7905 nlmsg_free(msg);
7906 return;
7907 }
7908
7909 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7910 nl80211_scan_mcgrp.id, GFP_KERNEL);
7911}
7912
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007913/*
7914 * This can happen on global regulatory changes or device specific settings
7915 * based on custom world regulatory domains.
7916 */
7917void nl80211_send_reg_change_event(struct regulatory_request *request)
7918{
7919 struct sk_buff *msg;
7920 void *hdr;
7921
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007922 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007923 if (!msg)
7924 return;
7925
7926 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7927 if (!hdr) {
7928 nlmsg_free(msg);
7929 return;
7930 }
7931
7932 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007933 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7934 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007935
David S. Miller9360ffd2012-03-29 04:41:26 -04007936 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7937 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7938 NL80211_REGDOM_TYPE_WORLD))
7939 goto nla_put_failure;
7940 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7941 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7942 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7943 goto nla_put_failure;
7944 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7945 request->intersect) {
7946 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7947 NL80211_REGDOM_TYPE_INTERSECTION))
7948 goto nla_put_failure;
7949 } else {
7950 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7951 NL80211_REGDOM_TYPE_COUNTRY) ||
7952 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7953 request->alpha2))
7954 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007955 }
7956
David S. Miller9360ffd2012-03-29 04:41:26 -04007957 if (wiphy_idx_valid(request->wiphy_idx) &&
7958 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7959 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007960
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007961 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007962
Johannes Bergbc43b282009-07-25 10:54:13 +02007963 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007964 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007965 GFP_ATOMIC);
7966 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007967
7968 return;
7969
7970nla_put_failure:
7971 genlmsg_cancel(msg, hdr);
7972 nlmsg_free(msg);
7973}
7974
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007975static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7976 struct net_device *netdev,
7977 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007978 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007979{
7980 struct sk_buff *msg;
7981 void *hdr;
7982
Johannes Berge6d6e342009-07-01 21:26:47 +02007983 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007984 if (!msg)
7985 return;
7986
7987 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7988 if (!hdr) {
7989 nlmsg_free(msg);
7990 return;
7991 }
7992
David S. Miller9360ffd2012-03-29 04:41:26 -04007993 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7994 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7995 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7996 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007997
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007998 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007999
Johannes Berg463d0182009-07-14 00:33:35 +02008000 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8001 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008002 return;
8003
8004 nla_put_failure:
8005 genlmsg_cancel(msg, hdr);
8006 nlmsg_free(msg);
8007}
8008
8009void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008010 struct net_device *netdev, const u8 *buf,
8011 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008012{
8013 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008014 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008015}
8016
8017void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8018 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008019 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008020{
Johannes Berge6d6e342009-07-01 21:26:47 +02008021 nl80211_send_mlme_event(rdev, netdev, buf, len,
8022 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008023}
8024
Jouni Malinen53b46b82009-03-27 20:53:56 +02008025void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008026 struct net_device *netdev, const u8 *buf,
8027 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008028{
8029 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008030 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008031}
8032
Jouni Malinen53b46b82009-03-27 20:53:56 +02008033void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8034 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008035 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008036{
8037 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008038 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008039}
8040
Jouni Malinencf4e5942010-12-16 00:52:40 +02008041void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8042 struct net_device *netdev, const u8 *buf,
8043 size_t len, gfp_t gfp)
8044{
8045 nl80211_send_mlme_event(rdev, netdev, buf, len,
8046 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8047}
8048
8049void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8050 struct net_device *netdev, const u8 *buf,
8051 size_t len, gfp_t gfp)
8052{
8053 nl80211_send_mlme_event(rdev, netdev, buf, len,
8054 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8055}
8056
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008057static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8058 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008059 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008060{
8061 struct sk_buff *msg;
8062 void *hdr;
8063
Johannes Berge6d6e342009-07-01 21:26:47 +02008064 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008065 if (!msg)
8066 return;
8067
8068 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8069 if (!hdr) {
8070 nlmsg_free(msg);
8071 return;
8072 }
8073
David S. Miller9360ffd2012-03-29 04:41:26 -04008074 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8075 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8076 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8077 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8078 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008079
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008080 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008081
Johannes Berg463d0182009-07-14 00:33:35 +02008082 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8083 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008084 return;
8085
8086 nla_put_failure:
8087 genlmsg_cancel(msg, hdr);
8088 nlmsg_free(msg);
8089}
8090
8091void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008092 struct net_device *netdev, const u8 *addr,
8093 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008094{
8095 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008096 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008097}
8098
8099void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008100 struct net_device *netdev, const u8 *addr,
8101 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008102{
Johannes Berge6d6e342009-07-01 21:26:47 +02008103 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8104 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008105}
8106
Samuel Ortizb23aa672009-07-01 21:26:54 +02008107void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8108 struct net_device *netdev, const u8 *bssid,
8109 const u8 *req_ie, size_t req_ie_len,
8110 const u8 *resp_ie, size_t resp_ie_len,
8111 u16 status, gfp_t gfp)
8112{
8113 struct sk_buff *msg;
8114 void *hdr;
8115
Thomas Graf58050fc2012-06-28 03:57:45 +00008116 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008117 if (!msg)
8118 return;
8119
8120 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8121 if (!hdr) {
8122 nlmsg_free(msg);
8123 return;
8124 }
8125
David S. Miller9360ffd2012-03-29 04:41:26 -04008126 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8127 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8128 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8129 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8130 (req_ie &&
8131 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8132 (resp_ie &&
8133 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8134 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008135
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008136 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008137
Johannes Berg463d0182009-07-14 00:33:35 +02008138 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8139 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008140 return;
8141
8142 nla_put_failure:
8143 genlmsg_cancel(msg, hdr);
8144 nlmsg_free(msg);
8145
8146}
8147
8148void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8149 struct net_device *netdev, const u8 *bssid,
8150 const u8 *req_ie, size_t req_ie_len,
8151 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8152{
8153 struct sk_buff *msg;
8154 void *hdr;
8155
Thomas Graf58050fc2012-06-28 03:57:45 +00008156 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008157 if (!msg)
8158 return;
8159
8160 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8161 if (!hdr) {
8162 nlmsg_free(msg);
8163 return;
8164 }
8165
David S. Miller9360ffd2012-03-29 04:41:26 -04008166 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8167 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8168 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8169 (req_ie &&
8170 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8171 (resp_ie &&
8172 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8173 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008174
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008175 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008176
Johannes Berg463d0182009-07-14 00:33:35 +02008177 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8178 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008179 return;
8180
8181 nla_put_failure:
8182 genlmsg_cancel(msg, hdr);
8183 nlmsg_free(msg);
8184
8185}
8186
8187void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8188 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02008189 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008190{
8191 struct sk_buff *msg;
8192 void *hdr;
8193
Thomas Graf58050fc2012-06-28 03:57:45 +00008194 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008195 if (!msg)
8196 return;
8197
8198 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8199 if (!hdr) {
8200 nlmsg_free(msg);
8201 return;
8202 }
8203
David S. Miller9360ffd2012-03-29 04:41:26 -04008204 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8205 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8206 (from_ap && reason &&
8207 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8208 (from_ap &&
8209 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8210 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8211 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008212
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008213 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008214
Johannes Berg463d0182009-07-14 00:33:35 +02008215 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8216 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008217 return;
8218
8219 nla_put_failure:
8220 genlmsg_cancel(msg, hdr);
8221 nlmsg_free(msg);
8222
8223}
8224
Johannes Berg04a773a2009-04-19 21:24:32 +02008225void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8226 struct net_device *netdev, const u8 *bssid,
8227 gfp_t gfp)
8228{
8229 struct sk_buff *msg;
8230 void *hdr;
8231
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008232 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008233 if (!msg)
8234 return;
8235
8236 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8237 if (!hdr) {
8238 nlmsg_free(msg);
8239 return;
8240 }
8241
David S. Miller9360ffd2012-03-29 04:41:26 -04008242 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8243 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8244 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8245 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008246
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008247 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008248
Johannes Berg463d0182009-07-14 00:33:35 +02008249 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8250 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008251 return;
8252
8253 nla_put_failure:
8254 genlmsg_cancel(msg, hdr);
8255 nlmsg_free(msg);
8256}
8257
Javier Cardonac93b5e72011-04-07 15:08:34 -07008258void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8259 struct net_device *netdev,
8260 const u8 *macaddr, const u8* ie, u8 ie_len,
8261 gfp_t gfp)
8262{
8263 struct sk_buff *msg;
8264 void *hdr;
8265
8266 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8267 if (!msg)
8268 return;
8269
8270 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8271 if (!hdr) {
8272 nlmsg_free(msg);
8273 return;
8274 }
8275
David S. Miller9360ffd2012-03-29 04:41:26 -04008276 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8277 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8278 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8279 (ie_len && ie &&
8280 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8281 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008282
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008283 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008284
8285 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8286 nl80211_mlme_mcgrp.id, gfp);
8287 return;
8288
8289 nla_put_failure:
8290 genlmsg_cancel(msg, hdr);
8291 nlmsg_free(msg);
8292}
8293
Jouni Malinena3b8b052009-03-27 21:59:49 +02008294void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8295 struct net_device *netdev, const u8 *addr,
8296 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008297 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008298{
8299 struct sk_buff *msg;
8300 void *hdr;
8301
Johannes Berge6d6e342009-07-01 21:26:47 +02008302 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008303 if (!msg)
8304 return;
8305
8306 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8307 if (!hdr) {
8308 nlmsg_free(msg);
8309 return;
8310 }
8311
David S. Miller9360ffd2012-03-29 04:41:26 -04008312 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8313 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8314 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8315 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8316 (key_id != -1 &&
8317 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8318 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8319 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008320
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008321 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008322
Johannes Berg463d0182009-07-14 00:33:35 +02008323 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8324 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008325 return;
8326
8327 nla_put_failure:
8328 genlmsg_cancel(msg, hdr);
8329 nlmsg_free(msg);
8330}
8331
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008332void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8333 struct ieee80211_channel *channel_before,
8334 struct ieee80211_channel *channel_after)
8335{
8336 struct sk_buff *msg;
8337 void *hdr;
8338 struct nlattr *nl_freq;
8339
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008340 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008341 if (!msg)
8342 return;
8343
8344 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8345 if (!hdr) {
8346 nlmsg_free(msg);
8347 return;
8348 }
8349
8350 /*
8351 * Since we are applying the beacon hint to a wiphy we know its
8352 * wiphy_idx is valid
8353 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008354 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8355 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008356
8357 /* Before */
8358 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8359 if (!nl_freq)
8360 goto nla_put_failure;
8361 if (nl80211_msg_put_channel(msg, channel_before))
8362 goto nla_put_failure;
8363 nla_nest_end(msg, nl_freq);
8364
8365 /* After */
8366 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8367 if (!nl_freq)
8368 goto nla_put_failure;
8369 if (nl80211_msg_put_channel(msg, channel_after))
8370 goto nla_put_failure;
8371 nla_nest_end(msg, nl_freq);
8372
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008373 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008374
Johannes Berg463d0182009-07-14 00:33:35 +02008375 rcu_read_lock();
8376 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8377 GFP_ATOMIC);
8378 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008379
8380 return;
8381
8382nla_put_failure:
8383 genlmsg_cancel(msg, hdr);
8384 nlmsg_free(msg);
8385}
8386
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008387static void nl80211_send_remain_on_chan_event(
8388 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008389 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008390 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008391 unsigned int duration, gfp_t gfp)
8392{
8393 struct sk_buff *msg;
8394 void *hdr;
8395
8396 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8397 if (!msg)
8398 return;
8399
8400 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8401 if (!hdr) {
8402 nlmsg_free(msg);
8403 return;
8404 }
8405
David S. Miller9360ffd2012-03-29 04:41:26 -04008406 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008407 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8408 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008409 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008410 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01008411 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
8412 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008413 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8414 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008415
David S. Miller9360ffd2012-03-29 04:41:26 -04008416 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8417 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8418 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008419
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008420 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008421
8422 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8423 nl80211_mlme_mcgrp.id, gfp);
8424 return;
8425
8426 nla_put_failure:
8427 genlmsg_cancel(msg, hdr);
8428 nlmsg_free(msg);
8429}
8430
8431void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008432 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008433 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008434 unsigned int duration, gfp_t gfp)
8435{
8436 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008437 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01008438 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008439}
8440
8441void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008442 struct cfg80211_registered_device *rdev,
8443 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01008444 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008445{
8446 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01008447 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008448}
8449
Johannes Berg98b62182009-12-23 13:15:44 +01008450void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8451 struct net_device *dev, const u8 *mac_addr,
8452 struct station_info *sinfo, gfp_t gfp)
8453{
8454 struct sk_buff *msg;
8455
Thomas Graf58050fc2012-06-28 03:57:45 +00008456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008457 if (!msg)
8458 return;
8459
John W. Linville66266b32012-03-15 13:25:41 -04008460 if (nl80211_send_station(msg, 0, 0, 0,
8461 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008462 nlmsg_free(msg);
8463 return;
8464 }
8465
8466 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8467 nl80211_mlme_mcgrp.id, gfp);
8468}
8469
Jouni Malinenec15e682011-03-23 15:29:52 +02008470void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8471 struct net_device *dev, const u8 *mac_addr,
8472 gfp_t gfp)
8473{
8474 struct sk_buff *msg;
8475 void *hdr;
8476
Thomas Graf58050fc2012-06-28 03:57:45 +00008477 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008478 if (!msg)
8479 return;
8480
8481 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8482 if (!hdr) {
8483 nlmsg_free(msg);
8484 return;
8485 }
8486
David S. Miller9360ffd2012-03-29 04:41:26 -04008487 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8488 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8489 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008490
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008491 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008492
8493 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8494 nl80211_mlme_mcgrp.id, gfp);
8495 return;
8496
8497 nla_put_failure:
8498 genlmsg_cancel(msg, hdr);
8499 nlmsg_free(msg);
8500}
8501
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308502void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8503 struct net_device *dev, const u8 *mac_addr,
8504 enum nl80211_connect_failed_reason reason,
8505 gfp_t gfp)
8506{
8507 struct sk_buff *msg;
8508 void *hdr;
8509
8510 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8511 if (!msg)
8512 return;
8513
8514 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8515 if (!hdr) {
8516 nlmsg_free(msg);
8517 return;
8518 }
8519
8520 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8521 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8522 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8523 goto nla_put_failure;
8524
8525 genlmsg_end(msg, hdr);
8526
8527 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8528 nl80211_mlme_mcgrp.id, gfp);
8529 return;
8530
8531 nla_put_failure:
8532 genlmsg_cancel(msg, hdr);
8533 nlmsg_free(msg);
8534}
8535
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008536static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8537 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008538{
8539 struct wireless_dev *wdev = dev->ieee80211_ptr;
8540 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8541 struct sk_buff *msg;
8542 void *hdr;
8543 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008544 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008545
Eric W. Biederman15e47302012-09-07 20:12:54 +00008546 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008547 return false;
8548
8549 msg = nlmsg_new(100, gfp);
8550 if (!msg)
8551 return true;
8552
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008553 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008554 if (!hdr) {
8555 nlmsg_free(msg);
8556 return true;
8557 }
8558
David S. Miller9360ffd2012-03-29 04:41:26 -04008559 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8560 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8561 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8562 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008563
8564 err = genlmsg_end(msg, hdr);
8565 if (err < 0) {
8566 nlmsg_free(msg);
8567 return true;
8568 }
8569
Eric W. Biederman15e47302012-09-07 20:12:54 +00008570 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008571 return true;
8572
8573 nla_put_failure:
8574 genlmsg_cancel(msg, hdr);
8575 nlmsg_free(msg);
8576 return true;
8577}
8578
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008579bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8580{
8581 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8582 addr, gfp);
8583}
8584
8585bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8586 const u8 *addr, gfp_t gfp)
8587{
8588 return __nl80211_unexpected_frame(dev,
8589 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8590 addr, gfp);
8591}
8592
Johannes Berg2e161f72010-08-12 15:38:38 +02008593int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008594 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008595 int freq, int sig_dbm,
8596 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008597{
Johannes Berg71bbc992012-06-15 15:30:18 +02008598 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008599 struct sk_buff *msg;
8600 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008601
8602 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8603 if (!msg)
8604 return -ENOMEM;
8605
Johannes Berg2e161f72010-08-12 15:38:38 +02008606 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008607 if (!hdr) {
8608 nlmsg_free(msg);
8609 return -ENOMEM;
8610 }
8611
David S. Miller9360ffd2012-03-29 04:41:26 -04008612 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008613 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8614 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008615 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8616 (sig_dbm &&
8617 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8618 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8619 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008620
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008621 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008622
Eric W. Biederman15e47302012-09-07 20:12:54 +00008623 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008624
8625 nla_put_failure:
8626 genlmsg_cancel(msg, hdr);
8627 nlmsg_free(msg);
8628 return -ENOBUFS;
8629}
8630
Johannes Berg2e161f72010-08-12 15:38:38 +02008631void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008632 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008633 const u8 *buf, size_t len, bool ack,
8634 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008635{
Johannes Berg71bbc992012-06-15 15:30:18 +02008636 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008637 struct sk_buff *msg;
8638 void *hdr;
8639
8640 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8641 if (!msg)
8642 return;
8643
Johannes Berg2e161f72010-08-12 15:38:38 +02008644 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008645 if (!hdr) {
8646 nlmsg_free(msg);
8647 return;
8648 }
8649
David S. Miller9360ffd2012-03-29 04:41:26 -04008650 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008651 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8652 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008653 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8654 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8655 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8656 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008657
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008658 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008659
8660 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8661 return;
8662
8663 nla_put_failure:
8664 genlmsg_cancel(msg, hdr);
8665 nlmsg_free(msg);
8666}
8667
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008668void
8669nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8670 struct net_device *netdev,
8671 enum nl80211_cqm_rssi_threshold_event rssi_event,
8672 gfp_t gfp)
8673{
8674 struct sk_buff *msg;
8675 struct nlattr *pinfoattr;
8676 void *hdr;
8677
Thomas Graf58050fc2012-06-28 03:57:45 +00008678 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008679 if (!msg)
8680 return;
8681
8682 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8683 if (!hdr) {
8684 nlmsg_free(msg);
8685 return;
8686 }
8687
David S. Miller9360ffd2012-03-29 04:41:26 -04008688 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8689 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8690 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008691
8692 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8693 if (!pinfoattr)
8694 goto nla_put_failure;
8695
David S. Miller9360ffd2012-03-29 04:41:26 -04008696 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8697 rssi_event))
8698 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008699
8700 nla_nest_end(msg, pinfoattr);
8701
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008702 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008703
8704 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8705 nl80211_mlme_mcgrp.id, gfp);
8706 return;
8707
8708 nla_put_failure:
8709 genlmsg_cancel(msg, hdr);
8710 nlmsg_free(msg);
8711}
8712
Johannes Berge5497d72011-07-05 16:35:40 +02008713void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8714 struct net_device *netdev, const u8 *bssid,
8715 const u8 *replay_ctr, gfp_t gfp)
8716{
8717 struct sk_buff *msg;
8718 struct nlattr *rekey_attr;
8719 void *hdr;
8720
Thomas Graf58050fc2012-06-28 03:57:45 +00008721 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008722 if (!msg)
8723 return;
8724
8725 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8726 if (!hdr) {
8727 nlmsg_free(msg);
8728 return;
8729 }
8730
David S. Miller9360ffd2012-03-29 04:41:26 -04008731 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8732 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8733 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8734 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008735
8736 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8737 if (!rekey_attr)
8738 goto nla_put_failure;
8739
David S. Miller9360ffd2012-03-29 04:41:26 -04008740 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8741 NL80211_REPLAY_CTR_LEN, replay_ctr))
8742 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008743
8744 nla_nest_end(msg, rekey_attr);
8745
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008746 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008747
8748 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8749 nl80211_mlme_mcgrp.id, gfp);
8750 return;
8751
8752 nla_put_failure:
8753 genlmsg_cancel(msg, hdr);
8754 nlmsg_free(msg);
8755}
8756
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008757void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8758 struct net_device *netdev, int index,
8759 const u8 *bssid, bool preauth, gfp_t gfp)
8760{
8761 struct sk_buff *msg;
8762 struct nlattr *attr;
8763 void *hdr;
8764
Thomas Graf58050fc2012-06-28 03:57:45 +00008765 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008766 if (!msg)
8767 return;
8768
8769 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8770 if (!hdr) {
8771 nlmsg_free(msg);
8772 return;
8773 }
8774
David S. Miller9360ffd2012-03-29 04:41:26 -04008775 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8776 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8777 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008778
8779 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8780 if (!attr)
8781 goto nla_put_failure;
8782
David S. Miller9360ffd2012-03-29 04:41:26 -04008783 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8784 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8785 (preauth &&
8786 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8787 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008788
8789 nla_nest_end(msg, attr);
8790
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008791 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008792
8793 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8794 nl80211_mlme_mcgrp.id, gfp);
8795 return;
8796
8797 nla_put_failure:
8798 genlmsg_cancel(msg, hdr);
8799 nlmsg_free(msg);
8800}
8801
Thomas Pedersen53145262012-04-06 13:35:47 -07008802void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8803 struct net_device *netdev, int freq,
8804 enum nl80211_channel_type type, gfp_t gfp)
8805{
8806 struct sk_buff *msg;
8807 void *hdr;
8808
Thomas Graf58050fc2012-06-28 03:57:45 +00008809 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008810 if (!msg)
8811 return;
8812
8813 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8814 if (!hdr) {
8815 nlmsg_free(msg);
8816 return;
8817 }
8818
John W. Linville7eab0f62012-04-12 14:25:14 -04008819 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8820 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8821 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8822 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008823
8824 genlmsg_end(msg, hdr);
8825
8826 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8827 nl80211_mlme_mcgrp.id, gfp);
8828 return;
8829
8830 nla_put_failure:
8831 genlmsg_cancel(msg, hdr);
8832 nlmsg_free(msg);
8833}
8834
Johannes Bergc063dbf2010-11-24 08:10:05 +01008835void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008836nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8837 struct net_device *netdev, const u8 *peer,
8838 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8839{
8840 struct sk_buff *msg;
8841 struct nlattr *pinfoattr;
8842 void *hdr;
8843
8844 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8845 if (!msg)
8846 return;
8847
8848 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8849 if (!hdr) {
8850 nlmsg_free(msg);
8851 return;
8852 }
8853
8854 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8855 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8856 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8857 goto nla_put_failure;
8858
8859 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8860 if (!pinfoattr)
8861 goto nla_put_failure;
8862
8863 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8864 goto nla_put_failure;
8865
8866 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8867 goto nla_put_failure;
8868
8869 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8870 goto nla_put_failure;
8871
8872 nla_nest_end(msg, pinfoattr);
8873
8874 genlmsg_end(msg, hdr);
8875
8876 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8877 nl80211_mlme_mcgrp.id, gfp);
8878 return;
8879
8880 nla_put_failure:
8881 genlmsg_cancel(msg, hdr);
8882 nlmsg_free(msg);
8883}
8884
8885void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008886nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8887 struct net_device *netdev, const u8 *peer,
8888 u32 num_packets, gfp_t gfp)
8889{
8890 struct sk_buff *msg;
8891 struct nlattr *pinfoattr;
8892 void *hdr;
8893
Thomas Graf58050fc2012-06-28 03:57:45 +00008894 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008895 if (!msg)
8896 return;
8897
8898 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8899 if (!hdr) {
8900 nlmsg_free(msg);
8901 return;
8902 }
8903
David S. Miller9360ffd2012-03-29 04:41:26 -04008904 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8905 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8906 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8907 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008908
8909 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8910 if (!pinfoattr)
8911 goto nla_put_failure;
8912
David S. Miller9360ffd2012-03-29 04:41:26 -04008913 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8914 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008915
8916 nla_nest_end(msg, pinfoattr);
8917
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008918 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008919
8920 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8921 nl80211_mlme_mcgrp.id, gfp);
8922 return;
8923
8924 nla_put_failure:
8925 genlmsg_cancel(msg, hdr);
8926 nlmsg_free(msg);
8927}
8928
Johannes Berg7f6cf312011-11-04 11:18:15 +01008929void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8930 u64 cookie, bool acked, gfp_t gfp)
8931{
8932 struct wireless_dev *wdev = dev->ieee80211_ptr;
8933 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8934 struct sk_buff *msg;
8935 void *hdr;
8936 int err;
8937
Beni Lev4ee3e062012-08-27 12:49:39 +03008938 trace_cfg80211_probe_status(dev, addr, cookie, acked);
8939
Thomas Graf58050fc2012-06-28 03:57:45 +00008940 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03008941
Johannes Berg7f6cf312011-11-04 11:18:15 +01008942 if (!msg)
8943 return;
8944
8945 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8946 if (!hdr) {
8947 nlmsg_free(msg);
8948 return;
8949 }
8950
David S. Miller9360ffd2012-03-29 04:41:26 -04008951 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8952 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8953 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8954 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8955 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8956 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008957
8958 err = genlmsg_end(msg, hdr);
8959 if (err < 0) {
8960 nlmsg_free(msg);
8961 return;
8962 }
8963
8964 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8965 nl80211_mlme_mcgrp.id, gfp);
8966 return;
8967
8968 nla_put_failure:
8969 genlmsg_cancel(msg, hdr);
8970 nlmsg_free(msg);
8971}
8972EXPORT_SYMBOL(cfg80211_probe_status);
8973
Johannes Berg5e760232011-11-04 11:18:17 +01008974void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8975 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07008976 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01008977{
8978 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8979 struct sk_buff *msg;
8980 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07008981 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01008982
Beni Lev4ee3e062012-08-27 12:49:39 +03008983 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
8984
Ben Greear37c73b52012-10-26 14:49:25 -07008985 spin_lock_bh(&rdev->beacon_registrations_lock);
8986 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8987 msg = nlmsg_new(len + 100, GFP_ATOMIC);
8988 if (!msg) {
8989 spin_unlock_bh(&rdev->beacon_registrations_lock);
8990 return;
8991 }
Johannes Berg5e760232011-11-04 11:18:17 +01008992
Ben Greear37c73b52012-10-26 14:49:25 -07008993 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8994 if (!hdr)
8995 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008996
Ben Greear37c73b52012-10-26 14:49:25 -07008997 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8998 (freq &&
8999 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9000 (sig_dbm &&
9001 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9002 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9003 goto nla_put_failure;
9004
9005 genlmsg_end(msg, hdr);
9006
9007 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009008 }
Ben Greear37c73b52012-10-26 14:49:25 -07009009 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009010 return;
9011
9012 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009013 spin_unlock_bh(&rdev->beacon_registrations_lock);
9014 if (hdr)
9015 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009016 nlmsg_free(msg);
9017}
9018EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9019
Jouni Malinen3475b092012-11-16 22:49:57 +02009020void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9021 enum nl80211_tdls_operation oper,
9022 u16 reason_code, gfp_t gfp)
9023{
9024 struct wireless_dev *wdev = dev->ieee80211_ptr;
9025 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9026 struct sk_buff *msg;
9027 void *hdr;
9028 int err;
9029
9030 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9031 reason_code);
9032
9033 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9034 if (!msg)
9035 return;
9036
9037 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9038 if (!hdr) {
9039 nlmsg_free(msg);
9040 return;
9041 }
9042
9043 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9044 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9045 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9046 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9047 (reason_code > 0 &&
9048 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9049 goto nla_put_failure;
9050
9051 err = genlmsg_end(msg, hdr);
9052 if (err < 0) {
9053 nlmsg_free(msg);
9054 return;
9055 }
9056
9057 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9058 nl80211_mlme_mcgrp.id, gfp);
9059 return;
9060
9061 nla_put_failure:
9062 genlmsg_cancel(msg, hdr);
9063 nlmsg_free(msg);
9064}
9065EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9066
Jouni Malinen026331c2010-02-15 12:53:10 +02009067static int nl80211_netlink_notify(struct notifier_block * nb,
9068 unsigned long state,
9069 void *_notify)
9070{
9071 struct netlink_notify *notify = _notify;
9072 struct cfg80211_registered_device *rdev;
9073 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009074 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009075
9076 if (state != NETLINK_URELEASE)
9077 return NOTIFY_DONE;
9078
9079 rcu_read_lock();
9080
Johannes Berg5e760232011-11-04 11:18:17 +01009081 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009082 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009083 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009084
9085 spin_lock_bh(&rdev->beacon_registrations_lock);
9086 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9087 list) {
9088 if (reg->nlportid == notify->portid) {
9089 list_del(&reg->list);
9090 kfree(reg);
9091 break;
9092 }
9093 }
9094 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009095 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009096
9097 rcu_read_unlock();
9098
9099 return NOTIFY_DONE;
9100}
9101
9102static struct notifier_block nl80211_netlink_notifier = {
9103 .notifier_call = nl80211_netlink_notify,
9104};
9105
Johannes Berg55682962007-09-20 13:09:35 -04009106/* initialisation/exit functions */
9107
9108int nl80211_init(void)
9109{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009110 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009111
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009112 err = genl_register_family_with_ops(&nl80211_fam,
9113 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009114 if (err)
9115 return err;
9116
Johannes Berg55682962007-09-20 13:09:35 -04009117 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9118 if (err)
9119 goto err_out;
9120
Johannes Berg2a519312009-02-10 21:25:55 +01009121 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9122 if (err)
9123 goto err_out;
9124
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009125 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9126 if (err)
9127 goto err_out;
9128
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009129 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9130 if (err)
9131 goto err_out;
9132
Johannes Bergaff89a92009-07-01 21:26:51 +02009133#ifdef CONFIG_NL80211_TESTMODE
9134 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9135 if (err)
9136 goto err_out;
9137#endif
9138
Jouni Malinen026331c2010-02-15 12:53:10 +02009139 err = netlink_register_notifier(&nl80211_netlink_notifier);
9140 if (err)
9141 goto err_out;
9142
Johannes Berg55682962007-09-20 13:09:35 -04009143 return 0;
9144 err_out:
9145 genl_unregister_family(&nl80211_fam);
9146 return err;
9147}
9148
9149void nl80211_exit(void)
9150{
Jouni Malinen026331c2010-02-15 12:53:10 +02009151 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009152 genl_unregister_family(&nl80211_fam);
9153}