blob: 6a9a1d7f51d104c6eb6a761e4a4ca126d61382dd [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg89a54e42012-06-15 14:33:17 +020049/* returns ERR_PTR values */
50static struct wireless_dev *
51__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040052{
Johannes Berg89a54e42012-06-15 14:33:17 +020053 struct cfg80211_registered_device *rdev;
54 struct wireless_dev *result = NULL;
55 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
56 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
57 u64 wdev_id;
58 int wiphy_idx = -1;
59 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040060
Johannes Berg89a54e42012-06-15 14:33:17 +020061 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040062
Johannes Berg89a54e42012-06-15 14:33:17 +020063 if (!have_ifidx && !have_wdev_id)
64 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040065
Johannes Berg89a54e42012-06-15 14:33:17 +020066 if (have_ifidx)
67 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
68 if (have_wdev_id) {
69 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
70 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040071 }
72
Johannes Berg89a54e42012-06-15 14:33:17 +020073 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
74 struct wireless_dev *wdev;
75
76 if (wiphy_net(&rdev->wiphy) != netns)
77 continue;
78
79 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
80 continue;
81
82 mutex_lock(&rdev->devlist_mtx);
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
88 }
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
92 }
93 }
94 mutex_unlock(&rdev->devlist_mtx);
95
96 if (result)
97 break;
98 }
99
100 if (result)
101 return result;
102 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400103}
104
Johannes Berga9455402012-06-15 13:32:49 +0200105static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200106__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200107{
Johannes Berg7fee4772012-06-15 14:09:58 +0200108 struct cfg80211_registered_device *rdev = NULL, *tmp;
109 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200110
111 assert_cfg80211_lock();
112
Johannes Berg878d9ec2012-06-15 14:18:32 +0200113 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200114 !attrs[NL80211_ATTR_IFINDEX] &&
115 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200116 return ERR_PTR(-EINVAL);
117
Johannes Berg878d9ec2012-06-15 14:18:32 +0200118 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200119 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200120 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200121
Johannes Berg89a54e42012-06-15 14:33:17 +0200122 if (attrs[NL80211_ATTR_WDEV]) {
123 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
124 struct wireless_dev *wdev;
125 bool found = false;
126
127 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
128 if (tmp) {
129 /* make sure wdev exists */
130 mutex_lock(&tmp->devlist_mtx);
131 list_for_each_entry(wdev, &tmp->wdev_list, list) {
132 if (wdev->identifier != (u32)wdev_id)
133 continue;
134 found = true;
135 break;
136 }
137 mutex_unlock(&tmp->devlist_mtx);
138
139 if (!found)
140 tmp = NULL;
141
142 if (rdev && tmp != rdev)
143 return ERR_PTR(-EINVAL);
144 rdev = tmp;
145 }
146 }
147
Johannes Berg878d9ec2012-06-15 14:18:32 +0200148 if (attrs[NL80211_ATTR_IFINDEX]) {
149 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200150 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200151 if (netdev) {
152 if (netdev->ieee80211_ptr)
153 tmp = wiphy_to_dev(
154 netdev->ieee80211_ptr->wiphy);
155 else
156 tmp = NULL;
157
158 dev_put(netdev);
159
160 /* not wireless device -- return error */
161 if (!tmp)
162 return ERR_PTR(-EINVAL);
163
164 /* mismatch -- return error */
165 if (rdev && tmp != rdev)
166 return ERR_PTR(-EINVAL);
167
168 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200169 }
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
171
Johannes Berg4f7eff12012-06-15 14:14:22 +0200172 if (!rdev)
173 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200174
Johannes Berg4f7eff12012-06-15 14:14:22 +0200175 if (netns != wiphy_net(&rdev->wiphy))
176 return ERR_PTR(-ENODEV);
177
178 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200179}
180
181/*
182 * This function returns a pointer to the driver
183 * that the genl_info item that is passed refers to.
184 * If successful, it returns non-NULL and also locks
185 * the driver's mutex!
186 *
187 * This means that you need to call cfg80211_unlock_rdev()
188 * before being allowed to acquire &cfg80211_mutex!
189 *
190 * This is necessary because we need to lock the global
191 * mutex to get an item off the list safely, and then
192 * we lock the rdev mutex so it doesn't go away under us.
193 *
194 * We don't want to keep cfg80211_mutex locked
195 * for all the time in order to allow requests on
196 * other interfaces to go through at the same time.
197 *
198 * The result of this can be a PTR_ERR and hence must
199 * be checked with IS_ERR() for errors.
200 */
201static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200202cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200203{
204 struct cfg80211_registered_device *rdev;
205
206 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200207 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208
209 /* if it is not an error we grab the lock on
210 * it to assure it won't be going away while
211 * we operate on it */
212 if (!IS_ERR(rdev))
213 mutex_lock(&rdev->mtx);
214
215 mutex_unlock(&cfg80211_mutex);
216
217 return rdev;
218}
219
Johannes Berg55682962007-09-20 13:09:35 -0400220/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000221static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400222 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700224 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200225 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200226 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530227 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200228 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
229 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
230 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
231 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100232 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400233
234 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
235 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
236 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100237
Eliad Pellere007b852011-11-24 18:13:56 +0200238 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
239 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100240
Johannes Bergb9454e82009-07-08 13:29:08 +0200241 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100242 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
243 .len = WLAN_MAX_KEY_LEN },
244 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
245 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
246 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200247 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200248 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100249
250 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
251 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
252 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
253 .len = IEEE80211_MAX_DATA_LEN },
254 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
255 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100256 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
257 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
258 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
259 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
260 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200263 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100264 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800265 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100266 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300267
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700268 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
269 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
270
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300271 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
272 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
273 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200274 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
275 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100276 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300277
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800278 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700279 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700280
Johannes Berg6c739412011-11-03 09:27:01 +0100281 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200282
283 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
284 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100286 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
287 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200288
289 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_SSID_LEN },
291 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
292 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200293 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300294 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300295 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300296 [NL80211_ATTR_STA_FLAGS2] = {
297 .len = sizeof(struct nl80211_sta_flag_update),
298 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300299 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300300 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
301 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200302 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
303 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
304 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200305 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100306 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100307 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
308 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100309 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
310 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200311 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200312 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
313 .len = IEEE80211_MAX_DATA_LEN },
314 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200315 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200316 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300317 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200318 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300319 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200321 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900322 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
323 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100324 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100325 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100326 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200327 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700328 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300329 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200330 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200331 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300332 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300333 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
334 .len = IEEE80211_MAX_DATA_LEN },
335 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
336 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530337 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300338 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530339 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300340 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
341 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
342 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
343 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
344 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100345 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200346 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
347 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700348 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800349 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
351 .len = NL80211_HT_CAPABILITY_LEN
352 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100353 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530354 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530355 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200356 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Johannes Berg55682962007-09-20 13:09:35 -0400357};
358
Johannes Berge31b8212010-10-05 19:39:30 +0200359/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000360static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200361 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200362 [NL80211_KEY_IDX] = { .type = NLA_U8 },
363 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200364 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200365 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
366 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200367 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100368 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
369};
370
371/* policy for the key default flags */
372static const struct nla_policy
373nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
374 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
375 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200376};
377
Johannes Bergff1b6e62011-05-04 15:37:28 +0200378/* policy for WoWLAN attributes */
379static const struct nla_policy
380nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
381 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
382 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
383 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
384 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb132011-07-13 10:48:55 +0200385 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
386 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
387 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
388 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200389};
390
Johannes Berge5497d72011-07-05 16:35:40 +0200391/* policy for GTK rekey offload attributes */
392static const struct nla_policy
393nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
394 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
395 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
396 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
397};
398
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300399static const struct nla_policy
400nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200401 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300402 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700403 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300404};
405
Holger Schuriga0438972009-11-11 11:30:02 +0100406/* ifidx get helper */
407static int nl80211_get_ifidx(struct netlink_callback *cb)
408{
409 int res;
410
411 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
412 nl80211_fam.attrbuf, nl80211_fam.maxattr,
413 nl80211_policy);
414 if (res)
415 return res;
416
417 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
418 return -EINVAL;
419
420 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
421 if (!res)
422 return -EINVAL;
423 return res;
424}
425
Johannes Berg67748892010-10-04 21:14:06 +0200426static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
427 struct netlink_callback *cb,
428 struct cfg80211_registered_device **rdev,
429 struct net_device **dev)
430{
431 int ifidx = cb->args[0];
432 int err;
433
434 if (!ifidx)
435 ifidx = nl80211_get_ifidx(cb);
436 if (ifidx < 0)
437 return ifidx;
438
439 cb->args[0] = ifidx;
440
441 rtnl_lock();
442
443 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
444 if (!*dev) {
445 err = -ENODEV;
446 goto out_rtnl;
447 }
448
449 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100450 if (IS_ERR(*rdev)) {
451 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200452 goto out_rtnl;
453 }
454
455 return 0;
456 out_rtnl:
457 rtnl_unlock();
458 return err;
459}
460
461static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
462{
463 cfg80211_unlock_rdev(rdev);
464 rtnl_unlock();
465}
466
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100467/* IE validation */
468static bool is_valid_ie_attr(const struct nlattr *attr)
469{
470 const u8 *pos;
471 int len;
472
473 if (!attr)
474 return true;
475
476 pos = nla_data(attr);
477 len = nla_len(attr);
478
479 while (len) {
480 u8 elemlen;
481
482 if (len < 2)
483 return false;
484 len -= 2;
485
486 elemlen = pos[1];
487 if (elemlen > len)
488 return false;
489
490 len -= elemlen;
491 pos += 2 + elemlen;
492 }
493
494 return true;
495}
496
Johannes Berg55682962007-09-20 13:09:35 -0400497/* message building helper */
498static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
499 int flags, u8 cmd)
500{
501 /* since there is no private header just add the generic one */
502 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
503}
504
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400505static int nl80211_msg_put_channel(struct sk_buff *msg,
506 struct ieee80211_channel *chan)
507{
David S. Miller9360ffd2012-03-29 04:41:26 -0400508 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
509 chan->center_freq))
510 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400511
David S. Miller9360ffd2012-03-29 04:41:26 -0400512 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
513 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
514 goto nla_put_failure;
515 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
516 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
517 goto nla_put_failure;
518 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
519 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
520 goto nla_put_failure;
521 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
522 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
523 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400524
David S. Miller9360ffd2012-03-29 04:41:26 -0400525 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
526 DBM_TO_MBM(chan->max_power)))
527 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400528
529 return 0;
530
531 nla_put_failure:
532 return -ENOBUFS;
533}
534
Johannes Berg55682962007-09-20 13:09:35 -0400535/* netlink command implementations */
536
Johannes Bergb9454e82009-07-08 13:29:08 +0200537struct key_parse {
538 struct key_params p;
539 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200540 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200541 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100542 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200543};
544
545static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
546{
547 struct nlattr *tb[NL80211_KEY_MAX + 1];
548 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
549 nl80211_key_policy);
550 if (err)
551 return err;
552
553 k->def = !!tb[NL80211_KEY_DEFAULT];
554 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
555
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100556 if (k->def) {
557 k->def_uni = true;
558 k->def_multi = true;
559 }
560 if (k->defmgmt)
561 k->def_multi = true;
562
Johannes Bergb9454e82009-07-08 13:29:08 +0200563 if (tb[NL80211_KEY_IDX])
564 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
565
566 if (tb[NL80211_KEY_DATA]) {
567 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
568 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
569 }
570
571 if (tb[NL80211_KEY_SEQ]) {
572 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
573 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
574 }
575
576 if (tb[NL80211_KEY_CIPHER])
577 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
578
Johannes Berge31b8212010-10-05 19:39:30 +0200579 if (tb[NL80211_KEY_TYPE]) {
580 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
581 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
582 return -EINVAL;
583 }
584
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100585 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
586 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100587 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
588 tb[NL80211_KEY_DEFAULT_TYPES],
589 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100590 if (err)
591 return err;
592
593 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
594 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
595 }
596
Johannes Bergb9454e82009-07-08 13:29:08 +0200597 return 0;
598}
599
600static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
601{
602 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
603 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
604 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
605 }
606
607 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
608 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
609 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
610 }
611
612 if (info->attrs[NL80211_ATTR_KEY_IDX])
613 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
614
615 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
616 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
617
618 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
619 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
620
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100621 if (k->def) {
622 k->def_uni = true;
623 k->def_multi = true;
624 }
625 if (k->defmgmt)
626 k->def_multi = true;
627
Johannes Berge31b8212010-10-05 19:39:30 +0200628 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
629 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
630 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
631 return -EINVAL;
632 }
633
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100634 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
635 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
636 int err = nla_parse_nested(
637 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
638 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
639 nl80211_key_default_policy);
640 if (err)
641 return err;
642
643 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
644 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
645 }
646
Johannes Bergb9454e82009-07-08 13:29:08 +0200647 return 0;
648}
649
650static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
651{
652 int err;
653
654 memset(k, 0, sizeof(*k));
655 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200656 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200657
658 if (info->attrs[NL80211_ATTR_KEY])
659 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
660 else
661 err = nl80211_parse_key_old(info, k);
662
663 if (err)
664 return err;
665
666 if (k->def && k->defmgmt)
667 return -EINVAL;
668
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100669 if (k->defmgmt) {
670 if (k->def_uni || !k->def_multi)
671 return -EINVAL;
672 }
673
Johannes Bergb9454e82009-07-08 13:29:08 +0200674 if (k->idx != -1) {
675 if (k->defmgmt) {
676 if (k->idx < 4 || k->idx > 5)
677 return -EINVAL;
678 } else if (k->def) {
679 if (k->idx < 0 || k->idx > 3)
680 return -EINVAL;
681 } else {
682 if (k->idx < 0 || k->idx > 5)
683 return -EINVAL;
684 }
685 }
686
687 return 0;
688}
689
Johannes Bergfffd0932009-07-08 14:22:54 +0200690static struct cfg80211_cached_keys *
691nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
692 struct nlattr *keys)
693{
694 struct key_parse parse;
695 struct nlattr *key;
696 struct cfg80211_cached_keys *result;
697 int rem, err, def = 0;
698
699 result = kzalloc(sizeof(*result), GFP_KERNEL);
700 if (!result)
701 return ERR_PTR(-ENOMEM);
702
703 result->def = -1;
704 result->defmgmt = -1;
705
706 nla_for_each_nested(key, keys, rem) {
707 memset(&parse, 0, sizeof(parse));
708 parse.idx = -1;
709
710 err = nl80211_parse_key_new(key, &parse);
711 if (err)
712 goto error;
713 err = -EINVAL;
714 if (!parse.p.key)
715 goto error;
716 if (parse.idx < 0 || parse.idx > 4)
717 goto error;
718 if (parse.def) {
719 if (def)
720 goto error;
721 def = 1;
722 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100723 if (!parse.def_uni || !parse.def_multi)
724 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200725 } else if (parse.defmgmt)
726 goto error;
727 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200728 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200729 if (err)
730 goto error;
731 result->params[parse.idx].cipher = parse.p.cipher;
732 result->params[parse.idx].key_len = parse.p.key_len;
733 result->params[parse.idx].key = result->data[parse.idx];
734 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
735 }
736
737 return result;
738 error:
739 kfree(result);
740 return ERR_PTR(err);
741}
742
743static int nl80211_key_allowed(struct wireless_dev *wdev)
744{
745 ASSERT_WDEV_LOCK(wdev);
746
Johannes Bergfffd0932009-07-08 14:22:54 +0200747 switch (wdev->iftype) {
748 case NL80211_IFTYPE_AP:
749 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200750 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700751 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 break;
753 case NL80211_IFTYPE_ADHOC:
754 if (!wdev->current_bss)
755 return -ENOLINK;
756 break;
757 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200758 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200759 if (wdev->sme_state != CFG80211_SME_CONNECTED)
760 return -ENOLINK;
761 break;
762 default:
763 return -EINVAL;
764 }
765
766 return 0;
767}
768
Johannes Berg7527a782011-05-13 10:58:57 +0200769static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
770{
771 struct nlattr *nl_modes = nla_nest_start(msg, attr);
772 int i;
773
774 if (!nl_modes)
775 goto nla_put_failure;
776
777 i = 0;
778 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400779 if ((ifmodes & 1) && nla_put_flag(msg, i))
780 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200781 ifmodes >>= 1;
782 i++;
783 }
784
785 nla_nest_end(msg, nl_modes);
786 return 0;
787
788nla_put_failure:
789 return -ENOBUFS;
790}
791
792static int nl80211_put_iface_combinations(struct wiphy *wiphy,
793 struct sk_buff *msg)
794{
795 struct nlattr *nl_combis;
796 int i, j;
797
798 nl_combis = nla_nest_start(msg,
799 NL80211_ATTR_INTERFACE_COMBINATIONS);
800 if (!nl_combis)
801 goto nla_put_failure;
802
803 for (i = 0; i < wiphy->n_iface_combinations; i++) {
804 const struct ieee80211_iface_combination *c;
805 struct nlattr *nl_combi, *nl_limits;
806
807 c = &wiphy->iface_combinations[i];
808
809 nl_combi = nla_nest_start(msg, i + 1);
810 if (!nl_combi)
811 goto nla_put_failure;
812
813 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
814 if (!nl_limits)
815 goto nla_put_failure;
816
817 for (j = 0; j < c->n_limits; j++) {
818 struct nlattr *nl_limit;
819
820 nl_limit = nla_nest_start(msg, j + 1);
821 if (!nl_limit)
822 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400823 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
824 c->limits[j].max))
825 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200826 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
827 c->limits[j].types))
828 goto nla_put_failure;
829 nla_nest_end(msg, nl_limit);
830 }
831
832 nla_nest_end(msg, nl_limits);
833
David S. Miller9360ffd2012-03-29 04:41:26 -0400834 if (c->beacon_int_infra_match &&
835 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
836 goto nla_put_failure;
837 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
838 c->num_different_channels) ||
839 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
840 c->max_interfaces))
841 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200842
843 nla_nest_end(msg, nl_combi);
844 }
845
846 nla_nest_end(msg, nl_combis);
847
848 return 0;
849nla_put_failure:
850 return -ENOBUFS;
851}
852
Johannes Berg55682962007-09-20 13:09:35 -0400853static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
854 struct cfg80211_registered_device *dev)
855{
856 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100857 struct nlattr *nl_bands, *nl_band;
858 struct nlattr *nl_freqs, *nl_freq;
859 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100860 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100861 enum ieee80211_band band;
862 struct ieee80211_channel *chan;
863 struct ieee80211_rate *rate;
864 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200865 const struct ieee80211_txrx_stypes *mgmt_stypes =
866 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400867
868 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
869 if (!hdr)
870 return -1;
871
David S. Miller9360ffd2012-03-29 04:41:26 -0400872 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
873 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
874 nla_put_u32(msg, NL80211_ATTR_GENERATION,
875 cfg80211_rdev_list_generation) ||
876 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
877 dev->wiphy.retry_short) ||
878 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
879 dev->wiphy.retry_long) ||
880 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
881 dev->wiphy.frag_threshold) ||
882 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
883 dev->wiphy.rts_threshold) ||
884 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
885 dev->wiphy.coverage_class) ||
886 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
887 dev->wiphy.max_scan_ssids) ||
888 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
889 dev->wiphy.max_sched_scan_ssids) ||
890 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
891 dev->wiphy.max_scan_ie_len) ||
892 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
893 dev->wiphy.max_sched_scan_ie_len) ||
894 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
895 dev->wiphy.max_match_sets))
896 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200897
David S. Miller9360ffd2012-03-29 04:41:26 -0400898 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
899 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
900 goto nla_put_failure;
901 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
902 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
903 goto nla_put_failure;
904 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
905 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
906 goto nla_put_failure;
907 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
908 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
909 goto nla_put_failure;
910 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
911 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
912 goto nla_put_failure;
913 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
914 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
915 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200916
David S. Miller9360ffd2012-03-29 04:41:26 -0400917 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
918 sizeof(u32) * dev->wiphy.n_cipher_suites,
919 dev->wiphy.cipher_suites))
920 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100921
David S. Miller9360ffd2012-03-29 04:41:26 -0400922 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
923 dev->wiphy.max_num_pmkids))
924 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530925
David S. Miller9360ffd2012-03-29 04:41:26 -0400926 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
927 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
928 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200929
David S. Miller9360ffd2012-03-29 04:41:26 -0400930 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
931 dev->wiphy.available_antennas_tx) ||
932 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
933 dev->wiphy.available_antennas_rx))
934 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100935
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
937 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
938 dev->wiphy.probe_resp_offload))
939 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200940
Bruno Randolf7f531e02010-12-16 11:30:22 +0900941 if ((dev->wiphy.available_antennas_tx ||
942 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900943 u32 tx_ant = 0, rx_ant = 0;
944 int res;
945 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
946 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
948 tx_ant) ||
949 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
950 rx_ant))
951 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900952 }
953 }
954
Johannes Berg7527a782011-05-13 10:58:57 +0200955 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
956 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700957 goto nla_put_failure;
958
Johannes Bergee688b002008-01-24 19:38:39 +0100959 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
960 if (!nl_bands)
961 goto nla_put_failure;
962
963 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
964 if (!dev->wiphy.bands[band])
965 continue;
966
967 nl_band = nla_nest_start(msg, band);
968 if (!nl_band)
969 goto nla_put_failure;
970
Johannes Bergd51626d2008-10-09 12:20:13 +0200971 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400972 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
973 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
974 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
975 &dev->wiphy.bands[band]->ht_cap.mcs) ||
976 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
977 dev->wiphy.bands[band]->ht_cap.cap) ||
978 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
979 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
980 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
981 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
982 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200983
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000984 /* add VHT info */
985 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
986 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
987 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
988 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
989 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
990 dev->wiphy.bands[band]->vht_cap.cap)))
991 goto nla_put_failure;
992
Johannes Bergee688b002008-01-24 19:38:39 +0100993 /* add frequencies */
994 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
995 if (!nl_freqs)
996 goto nla_put_failure;
997
998 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
999 nl_freq = nla_nest_start(msg, i);
1000 if (!nl_freq)
1001 goto nla_put_failure;
1002
1003 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001004
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001005 if (nl80211_msg_put_channel(msg, chan))
1006 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001007
Johannes Bergee688b002008-01-24 19:38:39 +01001008 nla_nest_end(msg, nl_freq);
1009 }
1010
1011 nla_nest_end(msg, nl_freqs);
1012
1013 /* add bitrates */
1014 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1015 if (!nl_rates)
1016 goto nla_put_failure;
1017
1018 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1019 nl_rate = nla_nest_start(msg, i);
1020 if (!nl_rate)
1021 goto nla_put_failure;
1022
1023 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001024 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1025 rate->bitrate))
1026 goto nla_put_failure;
1027 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1028 nla_put_flag(msg,
1029 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1030 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001031
1032 nla_nest_end(msg, nl_rate);
1033 }
1034
1035 nla_nest_end(msg, nl_rates);
1036
1037 nla_nest_end(msg, nl_band);
1038 }
1039 nla_nest_end(msg, nl_bands);
1040
Johannes Berg8fdc6212009-03-14 09:34:01 +01001041 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1042 if (!nl_cmds)
1043 goto nla_put_failure;
1044
1045 i = 0;
1046#define CMD(op, n) \
1047 do { \
1048 if (dev->ops->op) { \
1049 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001050 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1051 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001052 } \
1053 } while (0)
1054
1055 CMD(add_virtual_intf, NEW_INTERFACE);
1056 CMD(change_virtual_intf, SET_INTERFACE);
1057 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001058 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001059 CMD(add_station, NEW_STATION);
1060 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001061 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001062 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001063 CMD(auth, AUTHENTICATE);
1064 CMD(assoc, ASSOCIATE);
1065 CMD(deauth, DEAUTHENTICATE);
1066 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001067 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001068 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001069 CMD(set_pmksa, SET_PMKSA);
1070 CMD(del_pmksa, DEL_PMKSA);
1071 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001072 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1073 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001074 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001075 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001076 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001077 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001078 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001079 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1080 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001081 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001082 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001083 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001084 i++;
1085 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1086 goto nla_put_failure;
1087 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001088 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001089 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1090 CMD(tdls_mgmt, TDLS_MGMT);
1091 CMD(tdls_oper, TDLS_OPER);
1092 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001093 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1094 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001095 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001096 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001097 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1098 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001099 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1100 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001101 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001102
Kalle Valo4745fc02011-11-17 19:06:10 +02001103#ifdef CONFIG_NL80211_TESTMODE
1104 CMD(testmode_cmd, TESTMODE);
1105#endif
1106
Johannes Berg8fdc6212009-03-14 09:34:01 +01001107#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001108
Johannes Berg6829c872009-07-02 09:13:27 +02001109 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001110 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001111 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1112 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001113 }
1114
Johannes Berg6829c872009-07-02 09:13:27 +02001115 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001116 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001117 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1118 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001119 }
1120
Johannes Berg8fdc6212009-03-14 09:34:01 +01001121 nla_nest_end(msg, nl_cmds);
1122
Johannes Berg7c4ef712011-11-18 15:33:48 +01001123 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001124 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1125 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1126 dev->wiphy.max_remain_on_channel_duration))
1127 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001128
David S. Miller9360ffd2012-03-29 04:41:26 -04001129 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1130 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1131 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001132
Johannes Berg2e161f72010-08-12 15:38:38 +02001133 if (mgmt_stypes) {
1134 u16 stypes;
1135 struct nlattr *nl_ftypes, *nl_ifs;
1136 enum nl80211_iftype ift;
1137
1138 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1139 if (!nl_ifs)
1140 goto nla_put_failure;
1141
1142 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1143 nl_ftypes = nla_nest_start(msg, ift);
1144 if (!nl_ftypes)
1145 goto nla_put_failure;
1146 i = 0;
1147 stypes = mgmt_stypes[ift].tx;
1148 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001149 if ((stypes & 1) &&
1150 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1151 (i << 4) | IEEE80211_FTYPE_MGMT))
1152 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001153 stypes >>= 1;
1154 i++;
1155 }
1156 nla_nest_end(msg, nl_ftypes);
1157 }
1158
Johannes Berg74b70a42010-08-24 12:15:53 +02001159 nla_nest_end(msg, nl_ifs);
1160
Johannes Berg2e161f72010-08-12 15:38:38 +02001161 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1162 if (!nl_ifs)
1163 goto nla_put_failure;
1164
1165 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1166 nl_ftypes = nla_nest_start(msg, ift);
1167 if (!nl_ftypes)
1168 goto nla_put_failure;
1169 i = 0;
1170 stypes = mgmt_stypes[ift].rx;
1171 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001172 if ((stypes & 1) &&
1173 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1174 (i << 4) | IEEE80211_FTYPE_MGMT))
1175 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001176 stypes >>= 1;
1177 i++;
1178 }
1179 nla_nest_end(msg, nl_ftypes);
1180 }
1181 nla_nest_end(msg, nl_ifs);
1182 }
1183
Johannes Bergdfb89c52012-06-27 09:23:48 +02001184#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001185 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1186 struct nlattr *nl_wowlan;
1187
1188 nl_wowlan = nla_nest_start(msg,
1189 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1190 if (!nl_wowlan)
1191 goto nla_put_failure;
1192
David S. Miller9360ffd2012-03-29 04:41:26 -04001193 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1194 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1195 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1196 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1197 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1198 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1199 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1200 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1201 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1202 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1203 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1204 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1205 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1206 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1207 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1208 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1209 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001210 if (dev->wiphy.wowlan.n_patterns) {
1211 struct nl80211_wowlan_pattern_support pat = {
1212 .max_patterns = dev->wiphy.wowlan.n_patterns,
1213 .min_pattern_len =
1214 dev->wiphy.wowlan.pattern_min_len,
1215 .max_pattern_len =
1216 dev->wiphy.wowlan.pattern_max_len,
1217 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001218 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1219 sizeof(pat), &pat))
1220 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001221 }
1222
1223 nla_nest_end(msg, nl_wowlan);
1224 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001225#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001226
Johannes Berg7527a782011-05-13 10:58:57 +02001227 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1228 dev->wiphy.software_iftypes))
1229 goto nla_put_failure;
1230
1231 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1232 goto nla_put_failure;
1233
David S. Miller9360ffd2012-03-29 04:41:26 -04001234 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1235 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1236 dev->wiphy.ap_sme_capa))
1237 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001238
David S. Miller9360ffd2012-03-29 04:41:26 -04001239 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1240 dev->wiphy.features))
1241 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001242
David S. Miller9360ffd2012-03-29 04:41:26 -04001243 if (dev->wiphy.ht_capa_mod_mask &&
1244 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1245 sizeof(*dev->wiphy.ht_capa_mod_mask),
1246 dev->wiphy.ht_capa_mod_mask))
1247 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001248
Johannes Berg55682962007-09-20 13:09:35 -04001249 return genlmsg_end(msg, hdr);
1250
1251 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001252 genlmsg_cancel(msg, hdr);
1253 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001254}
1255
1256static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1257{
1258 int idx = 0;
1259 int start = cb->args[0];
1260 struct cfg80211_registered_device *dev;
1261
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001262 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001263 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001264 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1265 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001266 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001267 continue;
1268 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1269 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001270 dev) < 0) {
1271 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001272 break;
Julius Volzb4637272008-07-08 14:02:19 +02001273 }
Johannes Berg55682962007-09-20 13:09:35 -04001274 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001275 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001276
1277 cb->args[0] = idx;
1278
1279 return skb->len;
1280}
1281
1282static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1283{
1284 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001285 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001286
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001288 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001289 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001290
Johannes Berg4c476992010-10-04 21:36:35 +02001291 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1292 nlmsg_free(msg);
1293 return -ENOBUFS;
1294 }
Johannes Berg55682962007-09-20 13:09:35 -04001295
Johannes Berg134e6372009-07-10 09:51:34 +00001296 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001297}
1298
Jouni Malinen31888482008-10-30 16:59:24 +02001299static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1300 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1301 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1302 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1303 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1304 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1305};
1306
1307static int parse_txq_params(struct nlattr *tb[],
1308 struct ieee80211_txq_params *txq_params)
1309{
Johannes Berga3304b02012-03-28 11:04:24 +02001310 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001311 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1312 !tb[NL80211_TXQ_ATTR_AIFS])
1313 return -EINVAL;
1314
Johannes Berga3304b02012-03-28 11:04:24 +02001315 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001316 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1317 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1318 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1319 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1320
Johannes Berga3304b02012-03-28 11:04:24 +02001321 if (txq_params->ac >= NL80211_NUM_ACS)
1322 return -EINVAL;
1323
Jouni Malinen31888482008-10-30 16:59:24 +02001324 return 0;
1325}
1326
Johannes Bergf444de02010-05-05 15:25:02 +02001327static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1328{
1329 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001330 * You can only set the channel explicitly for WDS interfaces,
1331 * all others have their channel managed via their respective
1332 * "establish a connection" command (connect, join, ...)
1333 *
1334 * For AP/GO and mesh mode, the channel can be set with the
1335 * channel userspace API, but is only stored and passed to the
1336 * low-level driver when the AP starts or the mesh is joined.
1337 * This is for backward compatibility, userspace can also give
1338 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001339 *
1340 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001341 * whatever else is going on, so they have their own special
1342 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001343 */
1344 return !wdev ||
1345 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001346 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001347 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1348 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001349}
1350
Johannes Bergcd6c6592012-05-10 21:27:18 +02001351static bool nl80211_valid_channel_type(struct genl_info *info,
1352 enum nl80211_channel_type *channel_type)
1353{
1354 enum nl80211_channel_type tmp;
1355
1356 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1357 return false;
1358
1359 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1360 if (tmp != NL80211_CHAN_NO_HT &&
1361 tmp != NL80211_CHAN_HT20 &&
1362 tmp != NL80211_CHAN_HT40PLUS &&
1363 tmp != NL80211_CHAN_HT40MINUS)
1364 return false;
1365
1366 if (channel_type)
1367 *channel_type = tmp;
1368
1369 return true;
1370}
1371
Johannes Bergf444de02010-05-05 15:25:02 +02001372static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1373 struct wireless_dev *wdev,
1374 struct genl_info *info)
1375{
Johannes Bergaa430da2012-05-16 23:50:18 +02001376 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001377 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1378 u32 freq;
1379 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001380 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1381
1382 if (wdev)
1383 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001384
1385 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1386 return -EINVAL;
1387
1388 if (!nl80211_can_set_dev_channel(wdev))
1389 return -EOPNOTSUPP;
1390
Johannes Bergcd6c6592012-05-10 21:27:18 +02001391 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1392 !nl80211_valid_channel_type(info, &channel_type))
1393 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001394
1395 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1396
1397 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001398 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001399 case NL80211_IFTYPE_AP:
1400 case NL80211_IFTYPE_P2P_GO:
1401 if (wdev->beacon_interval) {
1402 result = -EBUSY;
1403 break;
1404 }
1405 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1406 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1407 channel,
1408 channel_type)) {
1409 result = -EINVAL;
1410 break;
1411 }
1412 wdev->preset_chan = channel;
1413 wdev->preset_chantype = channel_type;
1414 result = 0;
1415 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001416 case NL80211_IFTYPE_MESH_POINT:
1417 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1418 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001419 case NL80211_IFTYPE_MONITOR:
1420 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1421 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001422 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001423 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001424 }
1425 mutex_unlock(&rdev->devlist_mtx);
1426
1427 return result;
1428}
1429
1430static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1431{
Johannes Berg4c476992010-10-04 21:36:35 +02001432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1433 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001434
Johannes Berg4c476992010-10-04 21:36:35 +02001435 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001436}
1437
Bill Jordane8347eb2010-10-01 13:54:28 -04001438static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1439{
Johannes Berg43b19952010-10-07 13:10:30 +02001440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1441 struct net_device *dev = info->user_ptr[1];
1442 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001443 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001444
1445 if (!info->attrs[NL80211_ATTR_MAC])
1446 return -EINVAL;
1447
Johannes Berg43b19952010-10-07 13:10:30 +02001448 if (netif_running(dev))
1449 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001450
Johannes Berg43b19952010-10-07 13:10:30 +02001451 if (!rdev->ops->set_wds_peer)
1452 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001453
Johannes Berg43b19952010-10-07 13:10:30 +02001454 if (wdev->iftype != NL80211_IFTYPE_WDS)
1455 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001456
1457 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001458 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001459}
1460
1461
Johannes Berg55682962007-09-20 13:09:35 -04001462static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1463{
1464 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001465 struct net_device *netdev = NULL;
1466 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001467 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001468 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001469 u32 changed;
1470 u8 retry_short = 0, retry_long = 0;
1471 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001472 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001473
Johannes Bergf444de02010-05-05 15:25:02 +02001474 /*
1475 * Try to find the wiphy and netdev. Normally this
1476 * function shouldn't need the netdev, but this is
1477 * done for backward compatibility -- previously
1478 * setting the channel was done per wiphy, but now
1479 * it is per netdev. Previous userland like hostapd
1480 * also passed a netdev to set_wiphy, so that it is
1481 * possible to let that go to the right netdev!
1482 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001483 mutex_lock(&cfg80211_mutex);
1484
Johannes Bergf444de02010-05-05 15:25:02 +02001485 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1486 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1487
1488 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1489 if (netdev && netdev->ieee80211_ptr) {
1490 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1491 mutex_lock(&rdev->mtx);
1492 } else
1493 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001494 }
1495
Johannes Bergf444de02010-05-05 15:25:02 +02001496 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001497 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1498 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001499 if (IS_ERR(rdev)) {
1500 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001501 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001502 }
1503 wdev = NULL;
1504 netdev = NULL;
1505 result = 0;
1506
1507 mutex_lock(&rdev->mtx);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001508 } else if (nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
Johannes Bergf444de02010-05-05 15:25:02 +02001509 wdev = netdev->ieee80211_ptr;
1510 else
1511 wdev = NULL;
1512
1513 /*
1514 * end workaround code, by now the rdev is available
1515 * and locked, and wdev may or may not be NULL.
1516 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001517
1518 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001519 result = cfg80211_dev_rename(
1520 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001521
1522 mutex_unlock(&cfg80211_mutex);
1523
1524 if (result)
1525 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001526
Jouni Malinen31888482008-10-30 16:59:24 +02001527 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1528 struct ieee80211_txq_params txq_params;
1529 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1530
1531 if (!rdev->ops->set_txq_params) {
1532 result = -EOPNOTSUPP;
1533 goto bad_res;
1534 }
1535
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001536 if (!netdev) {
1537 result = -EINVAL;
1538 goto bad_res;
1539 }
1540
Johannes Berg133a3ff2011-11-03 14:50:13 +01001541 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1542 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1543 result = -EINVAL;
1544 goto bad_res;
1545 }
1546
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001547 if (!netif_running(netdev)) {
1548 result = -ENETDOWN;
1549 goto bad_res;
1550 }
1551
Jouni Malinen31888482008-10-30 16:59:24 +02001552 nla_for_each_nested(nl_txq_params,
1553 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1554 rem_txq_params) {
1555 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1556 nla_data(nl_txq_params),
1557 nla_len(nl_txq_params),
1558 txq_params_policy);
1559 result = parse_txq_params(tb, &txq_params);
1560 if (result)
1561 goto bad_res;
1562
1563 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001564 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001565 &txq_params);
1566 if (result)
1567 goto bad_res;
1568 }
1569 }
1570
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001571 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001572 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001573 if (result)
1574 goto bad_res;
1575 }
1576
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001577 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1578 enum nl80211_tx_power_setting type;
1579 int idx, mbm = 0;
1580
1581 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001582 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001583 goto bad_res;
1584 }
1585
1586 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1587 type = nla_get_u32(info->attrs[idx]);
1588
1589 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1590 (type != NL80211_TX_POWER_AUTOMATIC)) {
1591 result = -EINVAL;
1592 goto bad_res;
1593 }
1594
1595 if (type != NL80211_TX_POWER_AUTOMATIC) {
1596 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1597 mbm = nla_get_u32(info->attrs[idx]);
1598 }
1599
1600 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1601 if (result)
1602 goto bad_res;
1603 }
1604
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001605 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1606 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1607 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001608 if ((!rdev->wiphy.available_antennas_tx &&
1609 !rdev->wiphy.available_antennas_rx) ||
1610 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001611 result = -EOPNOTSUPP;
1612 goto bad_res;
1613 }
1614
1615 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1616 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1617
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001618 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001619 * available antenna masks, except for the "all" mask */
1620 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1621 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001622 result = -EINVAL;
1623 goto bad_res;
1624 }
1625
Bruno Randolf7f531e02010-12-16 11:30:22 +09001626 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1627 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001628
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001629 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1630 if (result)
1631 goto bad_res;
1632 }
1633
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001634 changed = 0;
1635
1636 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1637 retry_short = nla_get_u8(
1638 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1639 if (retry_short == 0) {
1640 result = -EINVAL;
1641 goto bad_res;
1642 }
1643 changed |= WIPHY_PARAM_RETRY_SHORT;
1644 }
1645
1646 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1647 retry_long = nla_get_u8(
1648 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1649 if (retry_long == 0) {
1650 result = -EINVAL;
1651 goto bad_res;
1652 }
1653 changed |= WIPHY_PARAM_RETRY_LONG;
1654 }
1655
1656 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1657 frag_threshold = nla_get_u32(
1658 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1659 if (frag_threshold < 256) {
1660 result = -EINVAL;
1661 goto bad_res;
1662 }
1663 if (frag_threshold != (u32) -1) {
1664 /*
1665 * Fragments (apart from the last one) are required to
1666 * have even length. Make the fragmentation code
1667 * simpler by stripping LSB should someone try to use
1668 * odd threshold value.
1669 */
1670 frag_threshold &= ~0x1;
1671 }
1672 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1673 }
1674
1675 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1676 rts_threshold = nla_get_u32(
1677 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1678 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1679 }
1680
Lukáš Turek81077e82009-12-21 22:50:47 +01001681 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1682 coverage_class = nla_get_u8(
1683 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1684 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1685 }
1686
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001687 if (changed) {
1688 u8 old_retry_short, old_retry_long;
1689 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001690 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001691
1692 if (!rdev->ops->set_wiphy_params) {
1693 result = -EOPNOTSUPP;
1694 goto bad_res;
1695 }
1696
1697 old_retry_short = rdev->wiphy.retry_short;
1698 old_retry_long = rdev->wiphy.retry_long;
1699 old_frag_threshold = rdev->wiphy.frag_threshold;
1700 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001701 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001702
1703 if (changed & WIPHY_PARAM_RETRY_SHORT)
1704 rdev->wiphy.retry_short = retry_short;
1705 if (changed & WIPHY_PARAM_RETRY_LONG)
1706 rdev->wiphy.retry_long = retry_long;
1707 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1708 rdev->wiphy.frag_threshold = frag_threshold;
1709 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1710 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001711 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1712 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001713
1714 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1715 if (result) {
1716 rdev->wiphy.retry_short = old_retry_short;
1717 rdev->wiphy.retry_long = old_retry_long;
1718 rdev->wiphy.frag_threshold = old_frag_threshold;
1719 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001720 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001721 }
1722 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001723
Johannes Berg306d6112008-12-08 12:39:04 +01001724 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001725 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001726 if (netdev)
1727 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001728 return result;
1729}
1730
Johannes Berg71bbc992012-06-15 15:30:18 +02001731static inline u64 wdev_id(struct wireless_dev *wdev)
1732{
1733 return (u64)wdev->identifier |
1734 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1735}
Johannes Berg55682962007-09-20 13:09:35 -04001736
1737static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001738 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001739 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001740{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001741 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001742 void *hdr;
1743
1744 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1745 if (!hdr)
1746 return -1;
1747
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001748 if (dev &&
1749 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1750 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1751 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dev->dev_addr)))
1752 goto nla_put_failure;
1753
1754 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1755 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001756 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001757 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1758 rdev->devlist_generation ^
1759 (cfg80211_rdev_list_generation << 2)))
1760 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001761
Michal Kazior2e165b82012-06-29 12:47:06 +02001762 if (rdev->monitor_channel) {
1763 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1764 rdev->monitor_channel->center_freq) ||
1765 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1766 rdev->monitor_channel_type))
John W. Linville59ef43e2012-04-18 14:17:13 -04001767 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001768 }
1769
Johannes Berg55682962007-09-20 13:09:35 -04001770 return genlmsg_end(msg, hdr);
1771
1772 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001773 genlmsg_cancel(msg, hdr);
1774 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001775}
1776
1777static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1778{
1779 int wp_idx = 0;
1780 int if_idx = 0;
1781 int wp_start = cb->args[0];
1782 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001783 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001784 struct wireless_dev *wdev;
1785
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001786 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001787 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1788 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001789 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001790 if (wp_idx < wp_start) {
1791 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001792 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001793 }
Johannes Berg55682962007-09-20 13:09:35 -04001794 if_idx = 0;
1795
Johannes Bergf5ea9122009-08-07 16:17:38 +02001796 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001797 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001798 if (if_idx < if_start) {
1799 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001800 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001801 }
Johannes Berg55682962007-09-20 13:09:35 -04001802 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1803 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001804 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001805 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001806 goto out;
1807 }
1808 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001809 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001810 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001811
1812 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001813 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001814 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001815 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001816
1817 cb->args[0] = wp_idx;
1818 cb->args[1] = if_idx;
1819
1820 return skb->len;
1821}
1822
1823static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1824{
1825 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001826 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001827 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001828
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001829 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001830 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001831 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001832
Johannes Bergd7264052009-04-19 16:23:20 +02001833 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001834 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001835 nlmsg_free(msg);
1836 return -ENOBUFS;
1837 }
Johannes Berg55682962007-09-20 13:09:35 -04001838
Johannes Berg134e6372009-07-10 09:51:34 +00001839 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001840}
1841
Michael Wu66f7ac52008-01-31 19:48:22 +01001842static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1843 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1844 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1845 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1846 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1847 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1848};
1849
1850static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1851{
1852 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1853 int flag;
1854
1855 *mntrflags = 0;
1856
1857 if (!nla)
1858 return -EINVAL;
1859
1860 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1861 nla, mntr_flags_policy))
1862 return -EINVAL;
1863
1864 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1865 if (flags[flag])
1866 *mntrflags |= (1<<flag);
1867
1868 return 0;
1869}
1870
Johannes Berg9bc383d2009-11-19 11:55:19 +01001871static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001872 struct net_device *netdev, u8 use_4addr,
1873 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001874{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001875 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001876 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001877 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001878 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001879 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001880
1881 switch (iftype) {
1882 case NL80211_IFTYPE_AP_VLAN:
1883 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1884 return 0;
1885 break;
1886 case NL80211_IFTYPE_STATION:
1887 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1888 return 0;
1889 break;
1890 default:
1891 break;
1892 }
1893
1894 return -EOPNOTSUPP;
1895}
1896
Johannes Berg55682962007-09-20 13:09:35 -04001897static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1898{
Johannes Berg4c476992010-10-04 21:36:35 +02001899 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001900 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001901 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001902 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001903 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001904 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001905 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001906
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001907 memset(&params, 0, sizeof(params));
1908
Johannes Berg04a773a2009-04-19 21:24:32 +02001909 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001910
Johannes Berg723b0382008-09-16 20:22:09 +02001911 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001912 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001913 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001914 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001915 if (ntype > NL80211_IFTYPE_MAX)
1916 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001917 }
1918
Johannes Berg92ffe052008-09-16 20:39:36 +02001919 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001920 struct wireless_dev *wdev = dev->ieee80211_ptr;
1921
Johannes Berg4c476992010-10-04 21:36:35 +02001922 if (ntype != NL80211_IFTYPE_MESH_POINT)
1923 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001924 if (netif_running(dev))
1925 return -EBUSY;
1926
1927 wdev_lock(wdev);
1928 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1929 IEEE80211_MAX_MESH_ID_LEN);
1930 wdev->mesh_id_up_len =
1931 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1932 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1933 wdev->mesh_id_up_len);
1934 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001935 }
1936
Felix Fietkau8b787642009-11-10 18:53:10 +01001937 if (info->attrs[NL80211_ATTR_4ADDR]) {
1938 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1939 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001940 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001941 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001942 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001943 } else {
1944 params.use_4addr = -1;
1945 }
1946
Johannes Berg92ffe052008-09-16 20:39:36 +02001947 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001948 if (ntype != NL80211_IFTYPE_MONITOR)
1949 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001950 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1951 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001952 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001953 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001954
1955 flags = &_flags;
1956 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001957 }
Johannes Berg3b858752009-03-12 09:55:09 +01001958
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001959 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001960 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001961 else
1962 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001963
Johannes Berg9bc383d2009-11-19 11:55:19 +01001964 if (!err && params.use_4addr != -1)
1965 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1966
Johannes Berg55682962007-09-20 13:09:35 -04001967 return err;
1968}
1969
1970static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1971{
Johannes Berg4c476992010-10-04 21:36:35 +02001972 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001973 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02001974 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02001975 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04001976 int err;
1977 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001978 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001979
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001980 memset(&params, 0, sizeof(params));
1981
Johannes Berg55682962007-09-20 13:09:35 -04001982 if (!info->attrs[NL80211_ATTR_IFNAME])
1983 return -EINVAL;
1984
1985 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1986 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1987 if (type > NL80211_IFTYPE_MAX)
1988 return -EINVAL;
1989 }
1990
Johannes Berg79c97e92009-07-07 03:56:12 +02001991 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001992 !(rdev->wiphy.interface_modes & (1 << type)))
1993 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001994
Johannes Berg9bc383d2009-11-19 11:55:19 +01001995 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001996 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001997 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001998 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001999 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002000 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002001
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002002 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2003 if (!msg)
2004 return -ENOMEM;
2005
Michael Wu66f7ac52008-01-31 19:48:22 +01002006 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2007 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2008 &flags);
Johannes Berg84efbb82012-06-16 00:00:26 +02002009 wdev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01002010 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002011 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002012 if (IS_ERR(wdev)) {
2013 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002014 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002015 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002016
Johannes Berg29cbe682010-12-03 09:20:44 +01002017 if (type == NL80211_IFTYPE_MESH_POINT &&
2018 info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002019 wdev_lock(wdev);
2020 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2021 IEEE80211_MAX_MESH_ID_LEN);
2022 wdev->mesh_id_up_len =
2023 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2024 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2025 wdev->mesh_id_up_len);
2026 wdev_unlock(wdev);
2027 }
2028
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002029 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
2030 rdev, wdev) < 0) {
2031 nlmsg_free(msg);
2032 return -ENOBUFS;
2033 }
2034
2035 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002036}
2037
2038static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2039{
Johannes Berg4c476992010-10-04 21:36:35 +02002040 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002041 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002042
Johannes Berg4c476992010-10-04 21:36:35 +02002043 if (!rdev->ops->del_virtual_intf)
2044 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002045
Johannes Berg84efbb82012-06-16 00:00:26 +02002046 /*
2047 * If we remove a wireless device without a netdev then clear
2048 * user_ptr[1] so that nl80211_post_doit won't dereference it
2049 * to check if it needs to do dev_put(). Otherwise it crashes
2050 * since the wdev has been freed, unlike with a netdev where
2051 * we need the dev_put() for the netdev to really be freed.
2052 */
2053 if (!wdev->netdev)
2054 info->user_ptr[1] = NULL;
2055
2056 return rdev->ops->del_virtual_intf(&rdev->wiphy, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002057}
2058
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002059static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2060{
2061 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2062 struct net_device *dev = info->user_ptr[1];
2063 u16 noack_map;
2064
2065 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2066 return -EINVAL;
2067
2068 if (!rdev->ops->set_noack_map)
2069 return -EOPNOTSUPP;
2070
2071 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2072
2073 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
2074}
2075
Johannes Berg41ade002007-12-19 02:03:29 +01002076struct get_key_cookie {
2077 struct sk_buff *msg;
2078 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002079 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002080};
2081
2082static void get_key_callback(void *c, struct key_params *params)
2083{
Johannes Bergb9454e82009-07-08 13:29:08 +02002084 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002085 struct get_key_cookie *cookie = c;
2086
David S. Miller9360ffd2012-03-29 04:41:26 -04002087 if ((params->key &&
2088 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2089 params->key_len, params->key)) ||
2090 (params->seq &&
2091 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2092 params->seq_len, params->seq)) ||
2093 (params->cipher &&
2094 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2095 params->cipher)))
2096 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002097
Johannes Bergb9454e82009-07-08 13:29:08 +02002098 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2099 if (!key)
2100 goto nla_put_failure;
2101
David S. Miller9360ffd2012-03-29 04:41:26 -04002102 if ((params->key &&
2103 nla_put(cookie->msg, NL80211_KEY_DATA,
2104 params->key_len, params->key)) ||
2105 (params->seq &&
2106 nla_put(cookie->msg, NL80211_KEY_SEQ,
2107 params->seq_len, params->seq)) ||
2108 (params->cipher &&
2109 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2110 params->cipher)))
2111 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002112
David S. Miller9360ffd2012-03-29 04:41:26 -04002113 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2114 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002115
2116 nla_nest_end(cookie->msg, key);
2117
Johannes Berg41ade002007-12-19 02:03:29 +01002118 return;
2119 nla_put_failure:
2120 cookie->error = 1;
2121}
2122
2123static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2124{
Johannes Berg4c476992010-10-04 21:36:35 +02002125 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002126 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002127 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002128 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002129 const u8 *mac_addr = NULL;
2130 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002131 struct get_key_cookie cookie = {
2132 .error = 0,
2133 };
2134 void *hdr;
2135 struct sk_buff *msg;
2136
2137 if (info->attrs[NL80211_ATTR_KEY_IDX])
2138 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2139
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002140 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002141 return -EINVAL;
2142
2143 if (info->attrs[NL80211_ATTR_MAC])
2144 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2145
Johannes Berge31b8212010-10-05 19:39:30 +02002146 pairwise = !!mac_addr;
2147 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2148 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2149 if (kt >= NUM_NL80211_KEYTYPES)
2150 return -EINVAL;
2151 if (kt != NL80211_KEYTYPE_GROUP &&
2152 kt != NL80211_KEYTYPE_PAIRWISE)
2153 return -EINVAL;
2154 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2155 }
2156
Johannes Berg4c476992010-10-04 21:36:35 +02002157 if (!rdev->ops->get_key)
2158 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002159
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002160 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002161 if (!msg)
2162 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002163
2164 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2165 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002166 if (IS_ERR(hdr))
2167 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002168
2169 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002170 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002171
David S. Miller9360ffd2012-03-29 04:41:26 -04002172 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2173 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2174 goto nla_put_failure;
2175 if (mac_addr &&
2176 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2177 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002178
Johannes Berge31b8212010-10-05 19:39:30 +02002179 if (pairwise && mac_addr &&
2180 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2181 return -ENOENT;
2182
2183 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
2184 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002185
2186 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002187 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002188
2189 if (cookie.error)
2190 goto nla_put_failure;
2191
2192 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002193 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002194
2195 nla_put_failure:
2196 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002197 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002198 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002199 return err;
2200}
2201
2202static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2203{
Johannes Berg4c476992010-10-04 21:36:35 +02002204 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002205 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002206 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002207 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002208
Johannes Bergb9454e82009-07-08 13:29:08 +02002209 err = nl80211_parse_key(info, &key);
2210 if (err)
2211 return err;
2212
2213 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002214 return -EINVAL;
2215
Johannes Bergb9454e82009-07-08 13:29:08 +02002216 /* only support setting default key */
2217 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002218 return -EINVAL;
2219
Johannes Bergfffd0932009-07-08 14:22:54 +02002220 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002221
2222 if (key.def) {
2223 if (!rdev->ops->set_default_key) {
2224 err = -EOPNOTSUPP;
2225 goto out;
2226 }
2227
2228 err = nl80211_key_allowed(dev->ieee80211_ptr);
2229 if (err)
2230 goto out;
2231
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002232 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2233 key.def_uni, key.def_multi);
2234
2235 if (err)
2236 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002237
Johannes Berg3d23e342009-09-29 23:27:28 +02002238#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002239 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002240#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002241 } else {
2242 if (key.def_uni || !key.def_multi) {
2243 err = -EINVAL;
2244 goto out;
2245 }
2246
2247 if (!rdev->ops->set_default_mgmt_key) {
2248 err = -EOPNOTSUPP;
2249 goto out;
2250 }
2251
2252 err = nl80211_key_allowed(dev->ieee80211_ptr);
2253 if (err)
2254 goto out;
2255
2256 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2257 dev, key.idx);
2258 if (err)
2259 goto out;
2260
2261#ifdef CONFIG_CFG80211_WEXT
2262 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2263#endif
2264 }
2265
2266 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002267 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002268
Johannes Berg41ade002007-12-19 02:03:29 +01002269 return err;
2270}
2271
2272static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2273{
Johannes Berg4c476992010-10-04 21:36:35 +02002274 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002275 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002276 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002277 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002278 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002279
Johannes Bergb9454e82009-07-08 13:29:08 +02002280 err = nl80211_parse_key(info, &key);
2281 if (err)
2282 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002283
Johannes Bergb9454e82009-07-08 13:29:08 +02002284 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002285 return -EINVAL;
2286
Johannes Berg41ade002007-12-19 02:03:29 +01002287 if (info->attrs[NL80211_ATTR_MAC])
2288 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2289
Johannes Berge31b8212010-10-05 19:39:30 +02002290 if (key.type == -1) {
2291 if (mac_addr)
2292 key.type = NL80211_KEYTYPE_PAIRWISE;
2293 else
2294 key.type = NL80211_KEYTYPE_GROUP;
2295 }
2296
2297 /* for now */
2298 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2299 key.type != NL80211_KEYTYPE_GROUP)
2300 return -EINVAL;
2301
Johannes Berg4c476992010-10-04 21:36:35 +02002302 if (!rdev->ops->add_key)
2303 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002304
Johannes Berge31b8212010-10-05 19:39:30 +02002305 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2306 key.type == NL80211_KEYTYPE_PAIRWISE,
2307 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002308 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002309
2310 wdev_lock(dev->ieee80211_ptr);
2311 err = nl80211_key_allowed(dev->ieee80211_ptr);
2312 if (!err)
2313 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002314 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002315 mac_addr, &key.p);
2316 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002317
Johannes Berg41ade002007-12-19 02:03:29 +01002318 return err;
2319}
2320
2321static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2322{
Johannes Berg4c476992010-10-04 21:36:35 +02002323 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002324 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002325 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002326 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002327 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002328
Johannes Bergb9454e82009-07-08 13:29:08 +02002329 err = nl80211_parse_key(info, &key);
2330 if (err)
2331 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002332
2333 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->del_key)
2349 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002350
Johannes Bergfffd0932009-07-08 14:22:54 +02002351 wdev_lock(dev->ieee80211_ptr);
2352 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002353
2354 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2355 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2356 err = -ENOENT;
2357
Johannes Bergfffd0932009-07-08 14:22:54 +02002358 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002359 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2360 key.type == NL80211_KEYTYPE_PAIRWISE,
2361 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002362
Johannes Berg3d23e342009-09-29 23:27:28 +02002363#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002364 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002365 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002366 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002367 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002368 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2369 }
2370#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002371 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002372
Johannes Berg41ade002007-12-19 02:03:29 +01002373 return err;
2374}
2375
Johannes Berg88600202012-02-13 15:17:18 +01002376static int nl80211_parse_beacon(struct genl_info *info,
2377 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002378{
Johannes Berg88600202012-02-13 15:17:18 +01002379 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002380
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002381 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2382 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2383 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2384 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002385 return -EINVAL;
2386
Johannes Berg88600202012-02-13 15:17:18 +01002387 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002388
Johannes Berged1b6cc2007-12-19 02:03:32 +01002389 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002390 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2391 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2392 if (!bcn->head_len)
2393 return -EINVAL;
2394 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002395 }
2396
2397 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002398 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2399 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002400 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002401 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002402 }
2403
Johannes Berg4c476992010-10-04 21:36:35 +02002404 if (!haveinfo)
2405 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002406
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002407 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002408 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2409 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002410 }
2411
2412 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002413 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002414 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002415 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002416 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2417 }
2418
2419 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002420 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002421 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002422 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002423 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2424 }
2425
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002426 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002427 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002428 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002429 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002430 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2431 }
2432
Johannes Berg88600202012-02-13 15:17:18 +01002433 return 0;
2434}
2435
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002436static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2437 struct cfg80211_ap_settings *params)
2438{
2439 struct wireless_dev *wdev;
2440 bool ret = false;
2441
2442 mutex_lock(&rdev->devlist_mtx);
2443
Johannes Berg89a54e42012-06-15 14:33:17 +02002444 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002445 if (wdev->iftype != NL80211_IFTYPE_AP &&
2446 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2447 continue;
2448
2449 if (!wdev->preset_chan)
2450 continue;
2451
2452 params->channel = wdev->preset_chan;
2453 params->channel_type = wdev->preset_chantype;
2454 ret = true;
2455 break;
2456 }
2457
2458 mutex_unlock(&rdev->devlist_mtx);
2459
2460 return ret;
2461}
2462
Johannes Berg88600202012-02-13 15:17:18 +01002463static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2464{
2465 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2466 struct net_device *dev = info->user_ptr[1];
2467 struct wireless_dev *wdev = dev->ieee80211_ptr;
2468 struct cfg80211_ap_settings params;
2469 int err;
2470
2471 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2472 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2473 return -EOPNOTSUPP;
2474
2475 if (!rdev->ops->start_ap)
2476 return -EOPNOTSUPP;
2477
2478 if (wdev->beacon_interval)
2479 return -EALREADY;
2480
2481 memset(&params, 0, sizeof(params));
2482
2483 /* these are required for START_AP */
2484 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2485 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2486 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2487 return -EINVAL;
2488
2489 err = nl80211_parse_beacon(info, &params.beacon);
2490 if (err)
2491 return err;
2492
2493 params.beacon_interval =
2494 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2495 params.dtim_period =
2496 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2497
2498 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2499 if (err)
2500 return err;
2501
2502 /*
2503 * In theory, some of these attributes should be required here
2504 * but since they were not used when the command was originally
2505 * added, keep them optional for old user space programs to let
2506 * them continue to work with drivers that do not need the
2507 * additional information -- drivers must check!
2508 */
2509 if (info->attrs[NL80211_ATTR_SSID]) {
2510 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2511 params.ssid_len =
2512 nla_len(info->attrs[NL80211_ATTR_SSID]);
2513 if (params.ssid_len == 0 ||
2514 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2515 return -EINVAL;
2516 }
2517
2518 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2519 params.hidden_ssid = nla_get_u32(
2520 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2521 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2522 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2523 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2524 return -EINVAL;
2525 }
2526
2527 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2528
2529 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2530 params.auth_type = nla_get_u32(
2531 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2532 if (!nl80211_valid_auth_type(params.auth_type))
2533 return -EINVAL;
2534 } else
2535 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2536
2537 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2538 NL80211_MAX_NR_CIPHER_SUITES);
2539 if (err)
2540 return err;
2541
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302542 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2543 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2544 return -EOPNOTSUPP;
2545 params.inactivity_timeout = nla_get_u16(
2546 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2547 }
2548
Johannes Bergaa430da2012-05-16 23:50:18 +02002549 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2550 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2551
2552 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2553 !nl80211_valid_channel_type(info, &channel_type))
2554 return -EINVAL;
2555
2556 params.channel = rdev_freq_to_chan(rdev,
2557 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2558 channel_type);
2559 if (!params.channel)
2560 return -EINVAL;
2561 params.channel_type = channel_type;
2562 } else if (wdev->preset_chan) {
2563 params.channel = wdev->preset_chan;
2564 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002565 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002566 return -EINVAL;
2567
2568 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2569 params.channel_type))
2570 return -EINVAL;
2571
Michal Kaziore4e32452012-06-29 12:47:08 +02002572 mutex_lock(&rdev->devlist_mtx);
2573 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2574 CHAN_MODE_SHARED);
2575 mutex_unlock(&rdev->devlist_mtx);
2576
2577 if (err)
2578 return err;
2579
Johannes Berg88600202012-02-13 15:17:18 +01002580 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002581 if (!err) {
2582 wdev->preset_chan = params.channel;
2583 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002584 wdev->beacon_interval = params.beacon_interval;
Michal Kaziorf4489eb2012-06-29 12:46:58 +02002585 wdev->channel = params.channel;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002586 }
Johannes Berg56d18932011-05-09 18:41:15 +02002587 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002588}
2589
Johannes Berg88600202012-02-13 15:17:18 +01002590static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2591{
2592 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2593 struct net_device *dev = info->user_ptr[1];
2594 struct wireless_dev *wdev = dev->ieee80211_ptr;
2595 struct cfg80211_beacon_data params;
2596 int err;
2597
2598 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2599 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2600 return -EOPNOTSUPP;
2601
2602 if (!rdev->ops->change_beacon)
2603 return -EOPNOTSUPP;
2604
2605 if (!wdev->beacon_interval)
2606 return -EINVAL;
2607
2608 err = nl80211_parse_beacon(info, &params);
2609 if (err)
2610 return err;
2611
2612 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2613}
2614
2615static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002616{
Johannes Berg4c476992010-10-04 21:36:35 +02002617 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2618 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002619
Michal Kazior60771782012-06-29 12:46:56 +02002620 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002621}
2622
Johannes Berg5727ef12007-12-19 02:03:34 +01002623static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2624 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2625 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2626 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002627 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002628 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002629 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002630};
2631
Johannes Bergeccb8e82009-05-11 21:57:56 +03002632static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002633 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002634 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002635{
2636 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002637 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002638 int flag;
2639
Johannes Bergeccb8e82009-05-11 21:57:56 +03002640 /*
2641 * Try parsing the new attribute first so userspace
2642 * can specify both for older kernels.
2643 */
2644 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2645 if (nla) {
2646 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002647
Johannes Bergeccb8e82009-05-11 21:57:56 +03002648 sta_flags = nla_data(nla);
2649 params->sta_flags_mask = sta_flags->mask;
2650 params->sta_flags_set = sta_flags->set;
2651 if ((params->sta_flags_mask |
2652 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2653 return -EINVAL;
2654 return 0;
2655 }
2656
2657 /* if present, parse the old attribute */
2658
2659 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002660 if (!nla)
2661 return 0;
2662
2663 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2664 nla, sta_flags_policy))
2665 return -EINVAL;
2666
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002667 /*
2668 * Only allow certain flags for interface types so that
2669 * other attributes are silently ignored. Remember that
2670 * this is backward compatibility code with old userspace
2671 * and shouldn't be hit in other cases anyway.
2672 */
2673 switch (iftype) {
2674 case NL80211_IFTYPE_AP:
2675 case NL80211_IFTYPE_AP_VLAN:
2676 case NL80211_IFTYPE_P2P_GO:
2677 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2678 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2679 BIT(NL80211_STA_FLAG_WME) |
2680 BIT(NL80211_STA_FLAG_MFP);
2681 break;
2682 case NL80211_IFTYPE_P2P_CLIENT:
2683 case NL80211_IFTYPE_STATION:
2684 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2685 BIT(NL80211_STA_FLAG_TDLS_PEER);
2686 break;
2687 case NL80211_IFTYPE_MESH_POINT:
2688 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2689 BIT(NL80211_STA_FLAG_MFP) |
2690 BIT(NL80211_STA_FLAG_AUTHORIZED);
2691 default:
2692 return -EINVAL;
2693 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002694
Johannes Berg3383b5a2012-05-10 20:14:43 +02002695 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2696 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002697 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002698
Johannes Berg3383b5a2012-05-10 20:14:43 +02002699 /* no longer support new API additions in old API */
2700 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2701 return -EINVAL;
2702 }
2703 }
2704
Johannes Berg5727ef12007-12-19 02:03:34 +01002705 return 0;
2706}
2707
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002708static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2709 int attr)
2710{
2711 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002712 u32 bitrate;
2713 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002714
2715 rate = nla_nest_start(msg, attr);
2716 if (!rate)
2717 goto nla_put_failure;
2718
2719 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2720 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002721 /* report 16-bit bitrate only if we can */
2722 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002723 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002724 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2725 (bitrate_compat > 0 &&
2726 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002727 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2728 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2729 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2730 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2731 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2732 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2733 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002734
2735 nla_nest_end(msg, rate);
2736 return true;
2737
2738nla_put_failure:
2739 return false;
2740}
2741
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002742static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002743 int flags,
2744 struct cfg80211_registered_device *rdev,
2745 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002746 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002747{
2748 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002749 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002750
2751 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2752 if (!hdr)
2753 return -1;
2754
David S. Miller9360ffd2012-03-29 04:41:26 -04002755 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2756 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2757 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2758 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002759
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002760 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2761 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002762 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002763 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2764 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2765 sinfo->connected_time))
2766 goto nla_put_failure;
2767 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2768 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2769 sinfo->inactive_time))
2770 goto nla_put_failure;
2771 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2772 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2773 sinfo->rx_bytes))
2774 goto nla_put_failure;
2775 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2776 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2777 sinfo->tx_bytes))
2778 goto nla_put_failure;
2779 if ((sinfo->filled & STATION_INFO_LLID) &&
2780 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2781 goto nla_put_failure;
2782 if ((sinfo->filled & STATION_INFO_PLID) &&
2783 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2784 goto nla_put_failure;
2785 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2786 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2787 sinfo->plink_state))
2788 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002789 switch (rdev->wiphy.signal_type) {
2790 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002791 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2792 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2793 sinfo->signal))
2794 goto nla_put_failure;
2795 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2796 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2797 sinfo->signal_avg))
2798 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002799 break;
2800 default:
2801 break;
2802 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002803 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002804 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2805 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002806 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002807 }
2808 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2809 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2810 NL80211_STA_INFO_RX_BITRATE))
2811 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002812 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002813 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2814 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2815 sinfo->rx_packets))
2816 goto nla_put_failure;
2817 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2818 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2819 sinfo->tx_packets))
2820 goto nla_put_failure;
2821 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2822 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2823 sinfo->tx_retries))
2824 goto nla_put_failure;
2825 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2826 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2827 sinfo->tx_failed))
2828 goto nla_put_failure;
2829 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2830 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2831 sinfo->beacon_loss_count))
2832 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002833 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2834 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2835 if (!bss_param)
2836 goto nla_put_failure;
2837
David S. Miller9360ffd2012-03-29 04:41:26 -04002838 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2839 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2840 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2841 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2842 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2843 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2844 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2845 sinfo->bss_param.dtim_period) ||
2846 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2847 sinfo->bss_param.beacon_interval))
2848 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002849
2850 nla_nest_end(msg, bss_param);
2851 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002852 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2853 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2854 sizeof(struct nl80211_sta_flag_update),
2855 &sinfo->sta_flags))
2856 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002857 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2858 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2859 sinfo->t_offset))
2860 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002861 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002862
David S. Miller9360ffd2012-03-29 04:41:26 -04002863 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2864 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2865 sinfo->assoc_req_ies))
2866 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002867
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002868 return genlmsg_end(msg, hdr);
2869
2870 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002871 genlmsg_cancel(msg, hdr);
2872 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002873}
2874
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002875static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002876 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002877{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002878 struct station_info sinfo;
2879 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002880 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002881 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002882 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002883 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002884
Johannes Berg67748892010-10-04 21:14:06 +02002885 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2886 if (err)
2887 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002888
2889 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002890 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002891 goto out_err;
2892 }
2893
Johannes Bergbba95fe2008-07-29 13:22:51 +02002894 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002895 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002896 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2897 mac_addr, &sinfo);
2898 if (err == -ENOENT)
2899 break;
2900 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002901 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002902
2903 if (nl80211_send_station(skb,
2904 NETLINK_CB(cb->skb).pid,
2905 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002906 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002907 &sinfo) < 0)
2908 goto out;
2909
2910 sta_idx++;
2911 }
2912
2913
2914 out:
2915 cb->args[1] = sta_idx;
2916 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002917 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002918 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002919
2920 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002921}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002922
Johannes Berg5727ef12007-12-19 02:03:34 +01002923static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2924{
Johannes Berg4c476992010-10-04 21:36:35 +02002925 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2926 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002927 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002928 struct sk_buff *msg;
2929 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002930 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002931
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002932 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002933
2934 if (!info->attrs[NL80211_ATTR_MAC])
2935 return -EINVAL;
2936
2937 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2938
Johannes Berg4c476992010-10-04 21:36:35 +02002939 if (!rdev->ops->get_station)
2940 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002941
Johannes Berg79c97e92009-07-07 03:56:12 +02002942 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002943 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002944 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002945
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002946 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002947 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002948 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002949
2950 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002951 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002952 nlmsg_free(msg);
2953 return -ENOBUFS;
2954 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002955
Johannes Berg4c476992010-10-04 21:36:35 +02002956 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002957}
2958
2959/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002960 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002961 */
Johannes Berg80b99892011-11-18 16:23:01 +01002962static struct net_device *get_vlan(struct genl_info *info,
2963 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002964{
Johannes Berg463d0182009-07-14 00:33:35 +02002965 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002966 struct net_device *v;
2967 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002968
Johannes Berg80b99892011-11-18 16:23:01 +01002969 if (!vlanattr)
2970 return NULL;
2971
2972 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2973 if (!v)
2974 return ERR_PTR(-ENODEV);
2975
2976 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2977 ret = -EINVAL;
2978 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002979 }
Johannes Berg80b99892011-11-18 16:23:01 +01002980
2981 if (!netif_running(v)) {
2982 ret = -ENETDOWN;
2983 goto error;
2984 }
2985
2986 return v;
2987 error:
2988 dev_put(v);
2989 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002990}
2991
2992static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2993{
Johannes Berg4c476992010-10-04 21:36:35 +02002994 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002995 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002996 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002997 struct station_parameters params;
2998 u8 *mac_addr = NULL;
2999
3000 memset(&params, 0, sizeof(params));
3001
3002 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003003 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003004
3005 if (info->attrs[NL80211_ATTR_STA_AID])
3006 return -EINVAL;
3007
3008 if (!info->attrs[NL80211_ATTR_MAC])
3009 return -EINVAL;
3010
3011 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3012
3013 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3014 params.supported_rates =
3015 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3016 params.supported_rates_len =
3017 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3018 }
3019
3020 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3021 params.listen_interval =
3022 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3023
Jouni Malinen36aedc92008-08-25 11:58:58 +03003024 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3025 params.ht_capa =
3026 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3027
Johannes Bergbdd90d52011-12-14 12:20:27 +01003028 if (!rdev->ops->change_station)
3029 return -EOPNOTSUPP;
3030
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003031 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003032 return -EINVAL;
3033
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003034 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3035 params.plink_action =
3036 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3037
Javier Cardona9c3990a2011-05-03 16:57:11 -07003038 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3039 params.plink_state =
3040 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3041
Johannes Berga97f4422009-06-18 17:23:43 +02003042 switch (dev->ieee80211_ptr->iftype) {
3043 case NL80211_IFTYPE_AP:
3044 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003045 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003046 /* disallow mesh-specific things */
3047 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003048 return -EINVAL;
3049
3050 /* TDLS can't be set, ... */
3051 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3052 return -EINVAL;
3053 /*
3054 * ... but don't bother the driver with it. This works around
3055 * a hostapd/wpa_supplicant issue -- it always includes the
3056 * TLDS_PEER flag in the mask even for AP mode.
3057 */
3058 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3059
3060 /* accept only the listed bits */
3061 if (params.sta_flags_mask &
3062 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3063 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3064 BIT(NL80211_STA_FLAG_WME) |
3065 BIT(NL80211_STA_FLAG_MFP)))
3066 return -EINVAL;
3067
3068 /* must be last in here for error handling */
3069 params.vlan = get_vlan(info, rdev);
3070 if (IS_ERR(params.vlan))
3071 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003072 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003073 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003074 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003075 /*
3076 * Don't allow userspace to change the TDLS_PEER flag,
3077 * but silently ignore attempts to change it since we
3078 * don't have state here to verify that it doesn't try
3079 * to change the flag.
3080 */
3081 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003082 /* fall through */
3083 case NL80211_IFTYPE_ADHOC:
3084 /* disallow things sta doesn't support */
3085 if (params.plink_action)
3086 return -EINVAL;
3087 if (params.ht_capa)
3088 return -EINVAL;
3089 if (params.listen_interval >= 0)
3090 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003091 /* reject any changes other than AUTHORIZED */
3092 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3093 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003094 break;
3095 case NL80211_IFTYPE_MESH_POINT:
3096 /* disallow things mesh doesn't support */
3097 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003098 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003099 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003100 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003101 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003102 return -EINVAL;
3103 /*
3104 * No special handling for TDLS here -- the userspace
3105 * mesh code doesn't have this bug.
3106 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003107 if (params.sta_flags_mask &
3108 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003109 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003110 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003111 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003112 break;
3113 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003114 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003115 }
3116
Johannes Bergbdd90d52011-12-14 12:20:27 +01003117 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003118
Johannes Berg79c97e92009-07-07 03:56:12 +02003119 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003120
Johannes Berg5727ef12007-12-19 02:03:34 +01003121 if (params.vlan)
3122 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003123
Johannes Berg5727ef12007-12-19 02:03:34 +01003124 return err;
3125}
3126
Eliad Pellerc75786c2011-08-23 14:37:46 +03003127static struct nla_policy
3128nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3129 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3130 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3131};
3132
Johannes Berg5727ef12007-12-19 02:03:34 +01003133static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3134{
Johannes Berg4c476992010-10-04 21:36:35 +02003135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003136 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003137 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003138 struct station_parameters params;
3139 u8 *mac_addr = NULL;
3140
3141 memset(&params, 0, sizeof(params));
3142
3143 if (!info->attrs[NL80211_ATTR_MAC])
3144 return -EINVAL;
3145
Johannes Berg5727ef12007-12-19 02:03:34 +01003146 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3147 return -EINVAL;
3148
3149 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3150 return -EINVAL;
3151
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003152 if (!info->attrs[NL80211_ATTR_STA_AID])
3153 return -EINVAL;
3154
Johannes Berg5727ef12007-12-19 02:03:34 +01003155 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3156 params.supported_rates =
3157 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3158 params.supported_rates_len =
3159 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3160 params.listen_interval =
3161 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003162
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003163 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3164 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3165 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003166
Jouni Malinen36aedc92008-08-25 11:58:58 +03003167 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3168 params.ht_capa =
3169 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003170
Javier Cardona96b78df2011-04-07 15:08:33 -07003171 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3172 params.plink_action =
3173 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3174
Johannes Bergbdd90d52011-12-14 12:20:27 +01003175 if (!rdev->ops->add_station)
3176 return -EOPNOTSUPP;
3177
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003178 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003179 return -EINVAL;
3180
Johannes Bergbdd90d52011-12-14 12:20:27 +01003181 switch (dev->ieee80211_ptr->iftype) {
3182 case NL80211_IFTYPE_AP:
3183 case NL80211_IFTYPE_AP_VLAN:
3184 case NL80211_IFTYPE_P2P_GO:
3185 /* parse WME attributes if sta is WME capable */
3186 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3187 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3188 info->attrs[NL80211_ATTR_STA_WME]) {
3189 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3190 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003191
Johannes Bergbdd90d52011-12-14 12:20:27 +01003192 nla = info->attrs[NL80211_ATTR_STA_WME];
3193 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3194 nl80211_sta_wme_policy);
3195 if (err)
3196 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003197
Johannes Bergbdd90d52011-12-14 12:20:27 +01003198 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3199 params.uapsd_queues =
3200 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3201 if (params.uapsd_queues &
3202 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3203 return -EINVAL;
3204
3205 if (tb[NL80211_STA_WME_MAX_SP])
3206 params.max_sp =
3207 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3208
3209 if (params.max_sp &
3210 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3211 return -EINVAL;
3212
3213 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3214 }
3215 /* TDLS peers cannot be added */
3216 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003217 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003218 /* but don't bother the driver with it */
3219 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003220
Johannes Bergbdd90d52011-12-14 12:20:27 +01003221 /* must be last in here for error handling */
3222 params.vlan = get_vlan(info, rdev);
3223 if (IS_ERR(params.vlan))
3224 return PTR_ERR(params.vlan);
3225 break;
3226 case NL80211_IFTYPE_MESH_POINT:
3227 /* TDLS peers cannot be added */
3228 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003229 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003230 break;
3231 case NL80211_IFTYPE_STATION:
3232 /* Only TDLS peers can be added */
3233 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3234 return -EINVAL;
3235 /* Can only add if TDLS ... */
3236 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3237 return -EOPNOTSUPP;
3238 /* ... with external setup is supported */
3239 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3240 return -EOPNOTSUPP;
3241 break;
3242 default:
3243 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003244 }
3245
Johannes Bergbdd90d52011-12-14 12:20:27 +01003246 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003247
Johannes Berg79c97e92009-07-07 03:56:12 +02003248 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003249
Johannes Berg5727ef12007-12-19 02:03:34 +01003250 if (params.vlan)
3251 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003252 return err;
3253}
3254
3255static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3256{
Johannes Berg4c476992010-10-04 21:36:35 +02003257 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3258 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003259 u8 *mac_addr = NULL;
3260
3261 if (info->attrs[NL80211_ATTR_MAC])
3262 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3263
Johannes Berge80cf852009-05-11 14:43:13 +02003264 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003265 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003266 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003267 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3268 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003269
Johannes Berg4c476992010-10-04 21:36:35 +02003270 if (!rdev->ops->del_station)
3271 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003272
Johannes Berg4c476992010-10-04 21:36:35 +02003273 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003274}
3275
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003276static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3277 int flags, struct net_device *dev,
3278 u8 *dst, u8 *next_hop,
3279 struct mpath_info *pinfo)
3280{
3281 void *hdr;
3282 struct nlattr *pinfoattr;
3283
3284 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3285 if (!hdr)
3286 return -1;
3287
David S. Miller9360ffd2012-03-29 04:41:26 -04003288 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3289 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3290 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3291 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3292 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003293
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003294 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3295 if (!pinfoattr)
3296 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003297 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3298 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3299 pinfo->frame_qlen))
3300 goto nla_put_failure;
3301 if (((pinfo->filled & MPATH_INFO_SN) &&
3302 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3303 ((pinfo->filled & MPATH_INFO_METRIC) &&
3304 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3305 pinfo->metric)) ||
3306 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3307 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3308 pinfo->exptime)) ||
3309 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3310 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3311 pinfo->flags)) ||
3312 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3313 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3314 pinfo->discovery_timeout)) ||
3315 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3316 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3317 pinfo->discovery_retries)))
3318 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003319
3320 nla_nest_end(msg, pinfoattr);
3321
3322 return genlmsg_end(msg, hdr);
3323
3324 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003325 genlmsg_cancel(msg, hdr);
3326 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003327}
3328
3329static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003330 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003331{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003332 struct mpath_info pinfo;
3333 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003334 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003335 u8 dst[ETH_ALEN];
3336 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003337 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003338 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003339
Johannes Berg67748892010-10-04 21:14:06 +02003340 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3341 if (err)
3342 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003343
3344 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003345 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003346 goto out_err;
3347 }
3348
Jouni Malineneec60b02009-03-20 21:21:19 +02003349 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3350 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003351 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003352 }
3353
Johannes Bergbba95fe2008-07-29 13:22:51 +02003354 while (1) {
3355 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3356 dst, next_hop, &pinfo);
3357 if (err == -ENOENT)
3358 break;
3359 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003360 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003361
3362 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3363 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3364 netdev, dst, next_hop,
3365 &pinfo) < 0)
3366 goto out;
3367
3368 path_idx++;
3369 }
3370
3371
3372 out:
3373 cb->args[1] = path_idx;
3374 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003375 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003376 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003377 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003378}
3379
3380static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3381{
Johannes Berg4c476992010-10-04 21:36:35 +02003382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003383 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003384 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003385 struct mpath_info pinfo;
3386 struct sk_buff *msg;
3387 u8 *dst = NULL;
3388 u8 next_hop[ETH_ALEN];
3389
3390 memset(&pinfo, 0, sizeof(pinfo));
3391
3392 if (!info->attrs[NL80211_ATTR_MAC])
3393 return -EINVAL;
3394
3395 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3396
Johannes Berg4c476992010-10-04 21:36:35 +02003397 if (!rdev->ops->get_mpath)
3398 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003399
Johannes Berg4c476992010-10-04 21:36:35 +02003400 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3401 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003402
Johannes Berg79c97e92009-07-07 03:56:12 +02003403 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003404 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003405 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003406
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003407 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003408 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003409 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003410
3411 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003412 dev, dst, next_hop, &pinfo) < 0) {
3413 nlmsg_free(msg);
3414 return -ENOBUFS;
3415 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003416
Johannes Berg4c476992010-10-04 21:36:35 +02003417 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003418}
3419
3420static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3421{
Johannes Berg4c476992010-10-04 21:36:35 +02003422 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3423 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003424 u8 *dst = NULL;
3425 u8 *next_hop = NULL;
3426
3427 if (!info->attrs[NL80211_ATTR_MAC])
3428 return -EINVAL;
3429
3430 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3431 return -EINVAL;
3432
3433 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3434 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3435
Johannes Berg4c476992010-10-04 21:36:35 +02003436 if (!rdev->ops->change_mpath)
3437 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003438
Johannes Berg4c476992010-10-04 21:36:35 +02003439 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3440 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003441
Johannes Berg4c476992010-10-04 21:36:35 +02003442 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003443}
Johannes Berg4c476992010-10-04 21:36:35 +02003444
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003445static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3446{
Johannes Berg4c476992010-10-04 21:36:35 +02003447 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3448 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003449 u8 *dst = NULL;
3450 u8 *next_hop = NULL;
3451
3452 if (!info->attrs[NL80211_ATTR_MAC])
3453 return -EINVAL;
3454
3455 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3456 return -EINVAL;
3457
3458 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3459 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3460
Johannes Berg4c476992010-10-04 21:36:35 +02003461 if (!rdev->ops->add_mpath)
3462 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003463
Johannes Berg4c476992010-10-04 21:36:35 +02003464 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3465 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003466
Johannes Berg4c476992010-10-04 21:36:35 +02003467 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003468}
3469
3470static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3471{
Johannes Berg4c476992010-10-04 21:36:35 +02003472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3473 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003474 u8 *dst = NULL;
3475
3476 if (info->attrs[NL80211_ATTR_MAC])
3477 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3478
Johannes Berg4c476992010-10-04 21:36:35 +02003479 if (!rdev->ops->del_mpath)
3480 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003481
Johannes Berg4c476992010-10-04 21:36:35 +02003482 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003483}
3484
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003485static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3486{
Johannes Berg4c476992010-10-04 21:36:35 +02003487 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3488 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003489 struct bss_parameters params;
3490
3491 memset(&params, 0, sizeof(params));
3492 /* default to not changing parameters */
3493 params.use_cts_prot = -1;
3494 params.use_short_preamble = -1;
3495 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003496 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003497 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003498
3499 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3500 params.use_cts_prot =
3501 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3502 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3503 params.use_short_preamble =
3504 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3505 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3506 params.use_short_slot_time =
3507 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003508 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3509 params.basic_rates =
3510 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3511 params.basic_rates_len =
3512 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3513 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003514 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3515 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003516 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3517 params.ht_opmode =
3518 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003519
Johannes Berg4c476992010-10-04 21:36:35 +02003520 if (!rdev->ops->change_bss)
3521 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003522
Johannes Berg074ac8d2010-09-16 14:58:22 +02003523 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003524 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3525 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003526
Johannes Berg4c476992010-10-04 21:36:35 +02003527 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003528}
3529
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003530static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003531 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3532 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3533 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3534 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3535 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3536 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3537};
3538
3539static int parse_reg_rule(struct nlattr *tb[],
3540 struct ieee80211_reg_rule *reg_rule)
3541{
3542 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3543 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3544
3545 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3546 return -EINVAL;
3547 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3548 return -EINVAL;
3549 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3550 return -EINVAL;
3551 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3552 return -EINVAL;
3553 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3554 return -EINVAL;
3555
3556 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3557
3558 freq_range->start_freq_khz =
3559 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3560 freq_range->end_freq_khz =
3561 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3562 freq_range->max_bandwidth_khz =
3563 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3564
3565 power_rule->max_eirp =
3566 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3567
3568 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3569 power_rule->max_antenna_gain =
3570 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3571
3572 return 0;
3573}
3574
3575static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3576{
3577 int r;
3578 char *data = NULL;
3579
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003580 /*
3581 * You should only get this when cfg80211 hasn't yet initialized
3582 * completely when built-in to the kernel right between the time
3583 * window between nl80211_init() and regulatory_init(), if that is
3584 * even possible.
3585 */
3586 mutex_lock(&cfg80211_mutex);
3587 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003588 mutex_unlock(&cfg80211_mutex);
3589 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003590 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003591 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003592
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003593 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3594 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003595
3596 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3597
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003598 r = regulatory_hint_user(data);
3599
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003600 return r;
3601}
3602
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003603static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003604 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003605{
Johannes Berg4c476992010-10-04 21:36:35 +02003606 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003607 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003608 struct wireless_dev *wdev = dev->ieee80211_ptr;
3609 struct mesh_config cur_params;
3610 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003611 void *hdr;
3612 struct nlattr *pinfoattr;
3613 struct sk_buff *msg;
3614
Johannes Berg29cbe682010-12-03 09:20:44 +01003615 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3616 return -EOPNOTSUPP;
3617
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003618 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003619 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003620
Johannes Berg29cbe682010-12-03 09:20:44 +01003621 wdev_lock(wdev);
3622 /* If not connected, get default parameters */
3623 if (!wdev->mesh_id_len)
3624 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3625 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003626 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003627 &cur_params);
3628 wdev_unlock(wdev);
3629
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003630 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003631 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003632
3633 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003634 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003635 if (!msg)
3636 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003637 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003638 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003639 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003640 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003641 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003642 if (!pinfoattr)
3643 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003644 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3645 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3646 cur_params.dot11MeshRetryTimeout) ||
3647 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3648 cur_params.dot11MeshConfirmTimeout) ||
3649 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3650 cur_params.dot11MeshHoldingTimeout) ||
3651 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3652 cur_params.dot11MeshMaxPeerLinks) ||
3653 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3654 cur_params.dot11MeshMaxRetries) ||
3655 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3656 cur_params.dot11MeshTTL) ||
3657 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3658 cur_params.element_ttl) ||
3659 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3660 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003661 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3662 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003663 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3664 cur_params.dot11MeshHWMPmaxPREQretries) ||
3665 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3666 cur_params.path_refresh_time) ||
3667 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3668 cur_params.min_discovery_timeout) ||
3669 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3670 cur_params.dot11MeshHWMPactivePathTimeout) ||
3671 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3672 cur_params.dot11MeshHWMPpreqMinInterval) ||
3673 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3674 cur_params.dot11MeshHWMPperrMinInterval) ||
3675 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3676 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3677 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3678 cur_params.dot11MeshHWMPRootMode) ||
3679 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3680 cur_params.dot11MeshHWMPRannInterval) ||
3681 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3682 cur_params.dot11MeshGateAnnouncementProtocol) ||
3683 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3684 cur_params.dot11MeshForwarding) ||
3685 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003686 cur_params.rssi_threshold) ||
3687 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003688 cur_params.ht_opmode) ||
3689 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3690 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3691 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003692 cur_params.dot11MeshHWMProotInterval) ||
3693 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3694 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003695 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003696 nla_nest_end(msg, pinfoattr);
3697 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003698 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003699
Johannes Berg3b858752009-03-12 09:55:09 +01003700 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003701 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003702 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003703 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003704 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003705}
3706
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003707static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003708 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3709 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3710 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3711 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3712 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3713 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003714 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003715 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003716 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003717 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3718 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3719 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3720 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3721 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003722 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003723 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003724 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003725 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003726 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003727 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003728 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3729 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003730 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3731 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003732 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003733};
3734
Javier Cardonac80d5452010-12-16 17:37:49 -08003735static const struct nla_policy
3736 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003737 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003738 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3739 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003740 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003741 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003742 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003743 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003744};
3745
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003746static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003747 struct mesh_config *cfg,
3748 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003749{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003750 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003751 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003752
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003753#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3754do {\
3755 if (table[attr_num]) {\
3756 cfg->param = nla_fn(table[attr_num]); \
3757 mask |= (1 << (attr_num - 1)); \
3758 } \
3759} while (0);\
3760
3761
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003762 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003763 return -EINVAL;
3764 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003765 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003766 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003767 return -EINVAL;
3768
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003769 /* This makes sure that there aren't more than 32 mesh config
3770 * parameters (otherwise our bitfield scheme would not work.) */
3771 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3772
3773 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003774 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003775 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3776 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003777 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003778 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3779 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003780 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003781 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3782 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003783 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003784 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3785 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003786 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003787 mask, NL80211_MESHCONF_MAX_RETRIES,
3788 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003789 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003790 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003791 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003792 mask, NL80211_MESHCONF_ELEMENT_TTL,
3793 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003794 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003795 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3796 nla_get_u8);
3797 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3798 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3799 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003800 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003801 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3802 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003803 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003804 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3805 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003806 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003807 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3808 nla_get_u16);
3809 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3810 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3811 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003812 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003813 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3814 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003815 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003816 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3817 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003818 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003819 dot11MeshHWMPnetDiameterTraversalTime, mask,
3820 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3821 nla_get_u16);
3822 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3823 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3824 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3825 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3826 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003827 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003828 dot11MeshGateAnnouncementProtocol, mask,
3829 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3830 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003831 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003832 mask, NL80211_MESHCONF_FORWARDING,
3833 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003834 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003835 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3836 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003837 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003838 mask, NL80211_MESHCONF_HT_OPMODE,
3839 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003840 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3841 mask,
3842 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3843 nla_get_u32);
3844 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3845 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3846 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003847 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3848 dot11MeshHWMPconfirmationInterval, mask,
3849 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3850 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003851 if (mask_out)
3852 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003853
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003854 return 0;
3855
3856#undef FILL_IN_MESH_PARAM_IF_SET
3857}
3858
Javier Cardonac80d5452010-12-16 17:37:49 -08003859static int nl80211_parse_mesh_setup(struct genl_info *info,
3860 struct mesh_setup *setup)
3861{
3862 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3863
3864 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3865 return -EINVAL;
3866 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3867 info->attrs[NL80211_ATTR_MESH_SETUP],
3868 nl80211_mesh_setup_params_policy))
3869 return -EINVAL;
3870
Javier Cardonad299a1f2012-03-31 11:31:33 -07003871 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3872 setup->sync_method =
3873 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3874 IEEE80211_SYNC_METHOD_VENDOR :
3875 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3876
Javier Cardonac80d5452010-12-16 17:37:49 -08003877 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3878 setup->path_sel_proto =
3879 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3880 IEEE80211_PATH_PROTOCOL_VENDOR :
3881 IEEE80211_PATH_PROTOCOL_HWMP;
3882
3883 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3884 setup->path_metric =
3885 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3886 IEEE80211_PATH_METRIC_VENDOR :
3887 IEEE80211_PATH_METRIC_AIRTIME;
3888
Javier Cardona581a8b02011-04-07 15:08:27 -07003889
3890 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003891 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003892 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003893 if (!is_valid_ie_attr(ieattr))
3894 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003895 setup->ie = nla_data(ieattr);
3896 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003897 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003898 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3899 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003900
3901 return 0;
3902}
3903
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003904static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003905 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003906{
3907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3908 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003909 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003910 struct mesh_config cfg;
3911 u32 mask;
3912 int err;
3913
Johannes Berg29cbe682010-12-03 09:20:44 +01003914 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3915 return -EOPNOTSUPP;
3916
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003917 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003918 return -EOPNOTSUPP;
3919
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003920 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003921 if (err)
3922 return err;
3923
Johannes Berg29cbe682010-12-03 09:20:44 +01003924 wdev_lock(wdev);
3925 if (!wdev->mesh_id_len)
3926 err = -ENOLINK;
3927
3928 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003929 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003930 mask, &cfg);
3931
3932 wdev_unlock(wdev);
3933
3934 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003935}
3936
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003937static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3938{
3939 struct sk_buff *msg;
3940 void *hdr = NULL;
3941 struct nlattr *nl_reg_rules;
3942 unsigned int i;
3943 int err = -EINVAL;
3944
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003945 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003946
3947 if (!cfg80211_regdomain)
3948 goto out;
3949
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003950 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003951 if (!msg) {
3952 err = -ENOBUFS;
3953 goto out;
3954 }
3955
3956 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3957 NL80211_CMD_GET_REG);
3958 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003959 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003960
David S. Miller9360ffd2012-03-29 04:41:26 -04003961 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3962 cfg80211_regdomain->alpha2) ||
3963 (cfg80211_regdomain->dfs_region &&
3964 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3965 cfg80211_regdomain->dfs_region)))
3966 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003967
3968 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3969 if (!nl_reg_rules)
3970 goto nla_put_failure;
3971
3972 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3973 struct nlattr *nl_reg_rule;
3974 const struct ieee80211_reg_rule *reg_rule;
3975 const struct ieee80211_freq_range *freq_range;
3976 const struct ieee80211_power_rule *power_rule;
3977
3978 reg_rule = &cfg80211_regdomain->reg_rules[i];
3979 freq_range = &reg_rule->freq_range;
3980 power_rule = &reg_rule->power_rule;
3981
3982 nl_reg_rule = nla_nest_start(msg, i);
3983 if (!nl_reg_rule)
3984 goto nla_put_failure;
3985
David S. Miller9360ffd2012-03-29 04:41:26 -04003986 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3987 reg_rule->flags) ||
3988 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3989 freq_range->start_freq_khz) ||
3990 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3991 freq_range->end_freq_khz) ||
3992 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3993 freq_range->max_bandwidth_khz) ||
3994 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3995 power_rule->max_antenna_gain) ||
3996 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3997 power_rule->max_eirp))
3998 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003999
4000 nla_nest_end(msg, nl_reg_rule);
4001 }
4002
4003 nla_nest_end(msg, nl_reg_rules);
4004
4005 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004006 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004007 goto out;
4008
4009nla_put_failure:
4010 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004011put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004012 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004013 err = -EMSGSIZE;
4014out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004015 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004016 return err;
4017}
4018
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004019static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4020{
4021 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4022 struct nlattr *nl_reg_rule;
4023 char *alpha2 = NULL;
4024 int rem_reg_rules = 0, r = 0;
4025 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004026 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004027 struct ieee80211_regdomain *rd = NULL;
4028
4029 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4030 return -EINVAL;
4031
4032 if (!info->attrs[NL80211_ATTR_REG_RULES])
4033 return -EINVAL;
4034
4035 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4036
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004037 if (info->attrs[NL80211_ATTR_DFS_REGION])
4038 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4039
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004040 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4041 rem_reg_rules) {
4042 num_rules++;
4043 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004044 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004045 }
4046
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004047 mutex_lock(&cfg80211_mutex);
4048
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004049 if (!reg_is_valid_request(alpha2)) {
4050 r = -EINVAL;
4051 goto bad_reg;
4052 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004053
4054 size_of_regd = sizeof(struct ieee80211_regdomain) +
4055 (num_rules * sizeof(struct ieee80211_reg_rule));
4056
4057 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004058 if (!rd) {
4059 r = -ENOMEM;
4060 goto bad_reg;
4061 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004062
4063 rd->n_reg_rules = num_rules;
4064 rd->alpha2[0] = alpha2[0];
4065 rd->alpha2[1] = alpha2[1];
4066
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004067 /*
4068 * Disable DFS master mode if the DFS region was
4069 * not supported or known on this kernel.
4070 */
4071 if (reg_supported_dfs_region(dfs_region))
4072 rd->dfs_region = dfs_region;
4073
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004074 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4075 rem_reg_rules) {
4076 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4077 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4078 reg_rule_policy);
4079 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4080 if (r)
4081 goto bad_reg;
4082
4083 rule_idx++;
4084
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004085 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4086 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004087 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004088 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004089 }
4090
4091 BUG_ON(rule_idx != num_rules);
4092
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004093 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004094
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004095 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004096
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004097 return r;
4098
Johannes Bergd2372b32008-10-24 20:32:20 +02004099 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004100 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004101 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004102 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004103}
4104
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004105static int validate_scan_freqs(struct nlattr *freqs)
4106{
4107 struct nlattr *attr1, *attr2;
4108 int n_channels = 0, tmp1, tmp2;
4109
4110 nla_for_each_nested(attr1, freqs, tmp1) {
4111 n_channels++;
4112 /*
4113 * Some hardware has a limited channel list for
4114 * scanning, and it is pretty much nonsensical
4115 * to scan for a channel twice, so disallow that
4116 * and don't require drivers to check that the
4117 * channel list they get isn't longer than what
4118 * they can scan, as long as they can scan all
4119 * the channels they registered at once.
4120 */
4121 nla_for_each_nested(attr2, freqs, tmp2)
4122 if (attr1 != attr2 &&
4123 nla_get_u32(attr1) == nla_get_u32(attr2))
4124 return 0;
4125 }
4126
4127 return n_channels;
4128}
4129
Johannes Berg2a519312009-02-10 21:25:55 +01004130static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4131{
Johannes Berg4c476992010-10-04 21:36:35 +02004132 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4133 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004134 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004135 struct nlattr *attr;
4136 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004137 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004138 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004139
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004140 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4141 return -EINVAL;
4142
Johannes Berg79c97e92009-07-07 03:56:12 +02004143 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004144
Johannes Berg4c476992010-10-04 21:36:35 +02004145 if (!rdev->ops->scan)
4146 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004147
Johannes Berg4c476992010-10-04 21:36:35 +02004148 if (rdev->scan_req)
4149 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004150
4151 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004152 n_channels = validate_scan_freqs(
4153 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004154 if (!n_channels)
4155 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004156 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004157 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004158 n_channels = 0;
4159
Johannes Berg2a519312009-02-10 21:25:55 +01004160 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4161 if (wiphy->bands[band])
4162 n_channels += wiphy->bands[band]->n_channels;
4163 }
4164
4165 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4166 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4167 n_ssids++;
4168
Johannes Berg4c476992010-10-04 21:36:35 +02004169 if (n_ssids > wiphy->max_scan_ssids)
4170 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004171
Jouni Malinen70692ad2009-02-16 19:39:13 +02004172 if (info->attrs[NL80211_ATTR_IE])
4173 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4174 else
4175 ie_len = 0;
4176
Johannes Berg4c476992010-10-04 21:36:35 +02004177 if (ie_len > wiphy->max_scan_ie_len)
4178 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004179
Johannes Berg2a519312009-02-10 21:25:55 +01004180 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004181 + sizeof(*request->ssids) * n_ssids
4182 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004183 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004184 if (!request)
4185 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004186
Johannes Berg2a519312009-02-10 21:25:55 +01004187 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004188 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004189 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004190 if (ie_len) {
4191 if (request->ssids)
4192 request->ie = (void *)(request->ssids + n_ssids);
4193 else
4194 request->ie = (void *)(request->channels + n_channels);
4195 }
Johannes Berg2a519312009-02-10 21:25:55 +01004196
Johannes Berg584991d2009-11-02 13:32:03 +01004197 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004198 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4199 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004200 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004201 struct ieee80211_channel *chan;
4202
4203 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4204
4205 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004206 err = -EINVAL;
4207 goto out_free;
4208 }
Johannes Berg584991d2009-11-02 13:32:03 +01004209
4210 /* ignore disabled channels */
4211 if (chan->flags & IEEE80211_CHAN_DISABLED)
4212 continue;
4213
4214 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004215 i++;
4216 }
4217 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004218 enum ieee80211_band band;
4219
Johannes Berg2a519312009-02-10 21:25:55 +01004220 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004221 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4222 int j;
4223 if (!wiphy->bands[band])
4224 continue;
4225 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004226 struct ieee80211_channel *chan;
4227
4228 chan = &wiphy->bands[band]->channels[j];
4229
4230 if (chan->flags & IEEE80211_CHAN_DISABLED)
4231 continue;
4232
4233 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004234 i++;
4235 }
4236 }
4237 }
4238
Johannes Berg584991d2009-11-02 13:32:03 +01004239 if (!i) {
4240 err = -EINVAL;
4241 goto out_free;
4242 }
4243
4244 request->n_channels = i;
4245
Johannes Berg2a519312009-02-10 21:25:55 +01004246 i = 0;
4247 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4248 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004249 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004250 err = -EINVAL;
4251 goto out_free;
4252 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004253 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004254 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004255 i++;
4256 }
4257 }
4258
Jouni Malinen70692ad2009-02-16 19:39:13 +02004259 if (info->attrs[NL80211_ATTR_IE]) {
4260 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004261 memcpy((void *)request->ie,
4262 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004263 request->ie_len);
4264 }
4265
Johannes Berg34850ab2011-07-18 18:08:35 +02004266 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004267 if (wiphy->bands[i])
4268 request->rates[i] =
4269 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004270
4271 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4272 nla_for_each_nested(attr,
4273 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4274 tmp) {
4275 enum ieee80211_band band = nla_type(attr);
4276
Dan Carpenter84404622011-07-29 11:52:18 +03004277 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004278 err = -EINVAL;
4279 goto out_free;
4280 }
4281 err = ieee80211_get_ratemask(wiphy->bands[band],
4282 nla_data(attr),
4283 nla_len(attr),
4284 &request->rates[band]);
4285 if (err)
4286 goto out_free;
4287 }
4288 }
4289
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304290 request->no_cck =
4291 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4292
Johannes Berg463d0182009-07-14 00:33:35 +02004293 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004294 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004295
Johannes Berg79c97e92009-07-07 03:56:12 +02004296 rdev->scan_req = request;
4297 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004298
Johannes Berg463d0182009-07-14 00:33:35 +02004299 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02004300 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02004301 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02004302 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004303 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004304 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004305 kfree(request);
4306 }
Johannes Berg3b858752009-03-12 09:55:09 +01004307
Johannes Berg2a519312009-02-10 21:25:55 +01004308 return err;
4309}
4310
Luciano Coelho807f8a82011-05-11 17:09:35 +03004311static int nl80211_start_sched_scan(struct sk_buff *skb,
4312 struct genl_info *info)
4313{
4314 struct cfg80211_sched_scan_request *request;
4315 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4316 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004317 struct nlattr *attr;
4318 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004319 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004320 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004321 enum ieee80211_band band;
4322 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004323 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004324
4325 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4326 !rdev->ops->sched_scan_start)
4327 return -EOPNOTSUPP;
4328
4329 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4330 return -EINVAL;
4331
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004332 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4333 return -EINVAL;
4334
4335 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4336 if (interval == 0)
4337 return -EINVAL;
4338
Luciano Coelho807f8a82011-05-11 17:09:35 +03004339 wiphy = &rdev->wiphy;
4340
4341 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4342 n_channels = validate_scan_freqs(
4343 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4344 if (!n_channels)
4345 return -EINVAL;
4346 } else {
4347 n_channels = 0;
4348
4349 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4350 if (wiphy->bands[band])
4351 n_channels += wiphy->bands[band]->n_channels;
4352 }
4353
4354 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4355 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4356 tmp)
4357 n_ssids++;
4358
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004359 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004360 return -EINVAL;
4361
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004362 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4363 nla_for_each_nested(attr,
4364 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4365 tmp)
4366 n_match_sets++;
4367
4368 if (n_match_sets > wiphy->max_match_sets)
4369 return -EINVAL;
4370
Luciano Coelho807f8a82011-05-11 17:09:35 +03004371 if (info->attrs[NL80211_ATTR_IE])
4372 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4373 else
4374 ie_len = 0;
4375
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004376 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004377 return -EINVAL;
4378
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004379 mutex_lock(&rdev->sched_scan_mtx);
4380
4381 if (rdev->sched_scan_req) {
4382 err = -EINPROGRESS;
4383 goto out;
4384 }
4385
Luciano Coelho807f8a82011-05-11 17:09:35 +03004386 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004387 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004388 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004389 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004390 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004391 if (!request) {
4392 err = -ENOMEM;
4393 goto out;
4394 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004395
4396 if (n_ssids)
4397 request->ssids = (void *)&request->channels[n_channels];
4398 request->n_ssids = n_ssids;
4399 if (ie_len) {
4400 if (request->ssids)
4401 request->ie = (void *)(request->ssids + n_ssids);
4402 else
4403 request->ie = (void *)(request->channels + n_channels);
4404 }
4405
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004406 if (n_match_sets) {
4407 if (request->ie)
4408 request->match_sets = (void *)(request->ie + ie_len);
4409 else if (request->ssids)
4410 request->match_sets =
4411 (void *)(request->ssids + n_ssids);
4412 else
4413 request->match_sets =
4414 (void *)(request->channels + n_channels);
4415 }
4416 request->n_match_sets = n_match_sets;
4417
Luciano Coelho807f8a82011-05-11 17:09:35 +03004418 i = 0;
4419 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4420 /* user specified, bail out if channel not found */
4421 nla_for_each_nested(attr,
4422 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4423 tmp) {
4424 struct ieee80211_channel *chan;
4425
4426 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4427
4428 if (!chan) {
4429 err = -EINVAL;
4430 goto out_free;
4431 }
4432
4433 /* ignore disabled channels */
4434 if (chan->flags & IEEE80211_CHAN_DISABLED)
4435 continue;
4436
4437 request->channels[i] = chan;
4438 i++;
4439 }
4440 } else {
4441 /* all channels */
4442 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4443 int j;
4444 if (!wiphy->bands[band])
4445 continue;
4446 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4447 struct ieee80211_channel *chan;
4448
4449 chan = &wiphy->bands[band]->channels[j];
4450
4451 if (chan->flags & IEEE80211_CHAN_DISABLED)
4452 continue;
4453
4454 request->channels[i] = chan;
4455 i++;
4456 }
4457 }
4458 }
4459
4460 if (!i) {
4461 err = -EINVAL;
4462 goto out_free;
4463 }
4464
4465 request->n_channels = i;
4466
4467 i = 0;
4468 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4469 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4470 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004471 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004472 err = -EINVAL;
4473 goto out_free;
4474 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004475 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004476 memcpy(request->ssids[i].ssid, nla_data(attr),
4477 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004478 i++;
4479 }
4480 }
4481
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004482 i = 0;
4483 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4484 nla_for_each_nested(attr,
4485 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4486 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004487 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004488
4489 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4490 nla_data(attr), nla_len(attr),
4491 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004492 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004493 if (ssid) {
4494 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4495 err = -EINVAL;
4496 goto out_free;
4497 }
4498 memcpy(request->match_sets[i].ssid.ssid,
4499 nla_data(ssid), nla_len(ssid));
4500 request->match_sets[i].ssid.ssid_len =
4501 nla_len(ssid);
4502 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004503 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4504 if (rssi)
4505 request->rssi_thold = nla_get_u32(rssi);
4506 else
4507 request->rssi_thold =
4508 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004509 i++;
4510 }
4511 }
4512
Luciano Coelho807f8a82011-05-11 17:09:35 +03004513 if (info->attrs[NL80211_ATTR_IE]) {
4514 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4515 memcpy((void *)request->ie,
4516 nla_data(info->attrs[NL80211_ATTR_IE]),
4517 request->ie_len);
4518 }
4519
4520 request->dev = dev;
4521 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004522 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004523
4524 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4525 if (!err) {
4526 rdev->sched_scan_req = request;
4527 nl80211_send_sched_scan(rdev, dev,
4528 NL80211_CMD_START_SCHED_SCAN);
4529 goto out;
4530 }
4531
4532out_free:
4533 kfree(request);
4534out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004535 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004536 return err;
4537}
4538
4539static int nl80211_stop_sched_scan(struct sk_buff *skb,
4540 struct genl_info *info)
4541{
4542 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004543 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004544
4545 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4546 !rdev->ops->sched_scan_stop)
4547 return -EOPNOTSUPP;
4548
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004549 mutex_lock(&rdev->sched_scan_mtx);
4550 err = __cfg80211_stop_sched_scan(rdev, false);
4551 mutex_unlock(&rdev->sched_scan_mtx);
4552
4553 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004554}
4555
Johannes Berg9720bb32011-06-21 09:45:33 +02004556static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4557 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004558 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004559 struct wireless_dev *wdev,
4560 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004561{
Johannes Berg48ab9052009-07-10 18:42:31 +02004562 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004563 void *hdr;
4564 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004565
4566 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004567
Johannes Berg9720bb32011-06-21 09:45:33 +02004568 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004569 NL80211_CMD_NEW_SCAN_RESULTS);
4570 if (!hdr)
4571 return -1;
4572
Johannes Berg9720bb32011-06-21 09:45:33 +02004573 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4574
David S. Miller9360ffd2012-03-29 04:41:26 -04004575 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4576 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4577 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004578
4579 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4580 if (!bss)
4581 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004582 if ((!is_zero_ether_addr(res->bssid) &&
4583 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4584 (res->information_elements && res->len_information_elements &&
4585 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4586 res->len_information_elements,
4587 res->information_elements)) ||
4588 (res->beacon_ies && res->len_beacon_ies &&
4589 res->beacon_ies != res->information_elements &&
4590 nla_put(msg, NL80211_BSS_BEACON_IES,
4591 res->len_beacon_ies, res->beacon_ies)))
4592 goto nla_put_failure;
4593 if (res->tsf &&
4594 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4595 goto nla_put_failure;
4596 if (res->beacon_interval &&
4597 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4598 goto nla_put_failure;
4599 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4600 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4601 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4602 jiffies_to_msecs(jiffies - intbss->ts)))
4603 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004604
Johannes Berg77965c92009-02-18 18:45:06 +01004605 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004606 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004607 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4608 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004609 break;
4610 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004611 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4612 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004613 break;
4614 default:
4615 break;
4616 }
4617
Johannes Berg48ab9052009-07-10 18:42:31 +02004618 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004619 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004620 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004621 if (intbss == wdev->current_bss &&
4622 nla_put_u32(msg, NL80211_BSS_STATUS,
4623 NL80211_BSS_STATUS_ASSOCIATED))
4624 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004625 break;
4626 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004627 if (intbss == wdev->current_bss &&
4628 nla_put_u32(msg, NL80211_BSS_STATUS,
4629 NL80211_BSS_STATUS_IBSS_JOINED))
4630 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004631 break;
4632 default:
4633 break;
4634 }
4635
Johannes Berg2a519312009-02-10 21:25:55 +01004636 nla_nest_end(msg, bss);
4637
4638 return genlmsg_end(msg, hdr);
4639
4640 nla_put_failure:
4641 genlmsg_cancel(msg, hdr);
4642 return -EMSGSIZE;
4643}
4644
4645static int nl80211_dump_scan(struct sk_buff *skb,
4646 struct netlink_callback *cb)
4647{
Johannes Berg48ab9052009-07-10 18:42:31 +02004648 struct cfg80211_registered_device *rdev;
4649 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004650 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004651 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004652 int start = cb->args[1], idx = 0;
4653 int err;
4654
Johannes Berg67748892010-10-04 21:14:06 +02004655 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4656 if (err)
4657 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004658
Johannes Berg48ab9052009-07-10 18:42:31 +02004659 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004660
Johannes Berg48ab9052009-07-10 18:42:31 +02004661 wdev_lock(wdev);
4662 spin_lock_bh(&rdev->bss_lock);
4663 cfg80211_bss_expire(rdev);
4664
Johannes Berg9720bb32011-06-21 09:45:33 +02004665 cb->seq = rdev->bss_generation;
4666
Johannes Berg48ab9052009-07-10 18:42:31 +02004667 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004668 if (++idx <= start)
4669 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004670 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004671 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004672 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004673 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004674 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004675 }
4676 }
4677
Johannes Berg48ab9052009-07-10 18:42:31 +02004678 spin_unlock_bh(&rdev->bss_lock);
4679 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004680
4681 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004682 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004683
Johannes Berg67748892010-10-04 21:14:06 +02004684 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004685}
4686
Holger Schurig61fa7132009-11-11 12:25:40 +01004687static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4688 int flags, struct net_device *dev,
4689 struct survey_info *survey)
4690{
4691 void *hdr;
4692 struct nlattr *infoattr;
4693
Holger Schurig61fa7132009-11-11 12:25:40 +01004694 hdr = nl80211hdr_put(msg, pid, seq, flags,
4695 NL80211_CMD_NEW_SURVEY_RESULTS);
4696 if (!hdr)
4697 return -ENOMEM;
4698
David S. Miller9360ffd2012-03-29 04:41:26 -04004699 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4700 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004701
4702 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4703 if (!infoattr)
4704 goto nla_put_failure;
4705
David S. Miller9360ffd2012-03-29 04:41:26 -04004706 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4707 survey->channel->center_freq))
4708 goto nla_put_failure;
4709
4710 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4711 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4712 goto nla_put_failure;
4713 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4714 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4715 goto nla_put_failure;
4716 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4717 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4718 survey->channel_time))
4719 goto nla_put_failure;
4720 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4721 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4722 survey->channel_time_busy))
4723 goto nla_put_failure;
4724 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4725 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4726 survey->channel_time_ext_busy))
4727 goto nla_put_failure;
4728 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4729 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4730 survey->channel_time_rx))
4731 goto nla_put_failure;
4732 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4733 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4734 survey->channel_time_tx))
4735 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004736
4737 nla_nest_end(msg, infoattr);
4738
4739 return genlmsg_end(msg, hdr);
4740
4741 nla_put_failure:
4742 genlmsg_cancel(msg, hdr);
4743 return -EMSGSIZE;
4744}
4745
4746static int nl80211_dump_survey(struct sk_buff *skb,
4747 struct netlink_callback *cb)
4748{
4749 struct survey_info survey;
4750 struct cfg80211_registered_device *dev;
4751 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004752 int survey_idx = cb->args[1];
4753 int res;
4754
Johannes Berg67748892010-10-04 21:14:06 +02004755 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4756 if (res)
4757 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004758
4759 if (!dev->ops->dump_survey) {
4760 res = -EOPNOTSUPP;
4761 goto out_err;
4762 }
4763
4764 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004765 struct ieee80211_channel *chan;
4766
Holger Schurig61fa7132009-11-11 12:25:40 +01004767 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4768 &survey);
4769 if (res == -ENOENT)
4770 break;
4771 if (res)
4772 goto out_err;
4773
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004774 /* Survey without a channel doesn't make sense */
4775 if (!survey.channel) {
4776 res = -EINVAL;
4777 goto out;
4778 }
4779
4780 chan = ieee80211_get_channel(&dev->wiphy,
4781 survey.channel->center_freq);
4782 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4783 survey_idx++;
4784 continue;
4785 }
4786
Holger Schurig61fa7132009-11-11 12:25:40 +01004787 if (nl80211_send_survey(skb,
4788 NETLINK_CB(cb->skb).pid,
4789 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4790 netdev,
4791 &survey) < 0)
4792 goto out;
4793 survey_idx++;
4794 }
4795
4796 out:
4797 cb->args[1] = survey_idx;
4798 res = skb->len;
4799 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004800 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004801 return res;
4802}
4803
Jouni Malinen255e7372009-03-20 21:21:17 +02004804static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4805{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004806 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004807}
4808
Samuel Ortizb23aa672009-07-01 21:26:54 +02004809static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4810{
4811 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4812 NL80211_WPA_VERSION_2));
4813}
4814
Jouni Malinen636a5d32009-03-19 13:39:22 +02004815static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4816{
Johannes Berg4c476992010-10-04 21:36:35 +02004817 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4818 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004819 struct ieee80211_channel *chan;
4820 const u8 *bssid, *ssid, *ie = NULL;
4821 int err, ssid_len, ie_len = 0;
4822 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004823 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004824 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004825
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004826 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4827 return -EINVAL;
4828
4829 if (!info->attrs[NL80211_ATTR_MAC])
4830 return -EINVAL;
4831
Jouni Malinen17780922009-03-27 20:52:47 +02004832 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4833 return -EINVAL;
4834
Johannes Berg19957bb2009-07-02 17:20:43 +02004835 if (!info->attrs[NL80211_ATTR_SSID])
4836 return -EINVAL;
4837
4838 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4839 return -EINVAL;
4840
Johannes Bergfffd0932009-07-08 14:22:54 +02004841 err = nl80211_parse_key(info, &key);
4842 if (err)
4843 return err;
4844
4845 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004846 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4847 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004848 if (!key.p.key || !key.p.key_len)
4849 return -EINVAL;
4850 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4851 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4852 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4853 key.p.key_len != WLAN_KEY_LEN_WEP104))
4854 return -EINVAL;
4855 if (key.idx > 4)
4856 return -EINVAL;
4857 } else {
4858 key.p.key_len = 0;
4859 key.p.key = NULL;
4860 }
4861
Johannes Bergafea0b72010-08-10 09:46:42 +02004862 if (key.idx >= 0) {
4863 int i;
4864 bool ok = false;
4865 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4866 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4867 ok = true;
4868 break;
4869 }
4870 }
Johannes Berg4c476992010-10-04 21:36:35 +02004871 if (!ok)
4872 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004873 }
4874
Johannes Berg4c476992010-10-04 21:36:35 +02004875 if (!rdev->ops->auth)
4876 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004877
Johannes Berg074ac8d2010-09-16 14:58:22 +02004878 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004879 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4880 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004881
Johannes Berg19957bb2009-07-02 17:20:43 +02004882 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004883 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004884 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004885 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4886 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004887
Johannes Berg19957bb2009-07-02 17:20:43 +02004888 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4889 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4890
4891 if (info->attrs[NL80211_ATTR_IE]) {
4892 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4893 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4894 }
4895
4896 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004897 if (!nl80211_valid_auth_type(auth_type))
4898 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004899
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004900 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4901
Johannes Berg95de8172012-01-20 13:55:25 +01004902 /*
4903 * Since we no longer track auth state, ignore
4904 * requests to only change local state.
4905 */
4906 if (local_state_change)
4907 return 0;
4908
Johannes Berg4c476992010-10-04 21:36:35 +02004909 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4910 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004911 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004912}
4913
Johannes Bergc0692b82010-08-27 14:26:53 +03004914static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4915 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004916 struct cfg80211_crypto_settings *settings,
4917 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004918{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004919 memset(settings, 0, sizeof(*settings));
4920
Samuel Ortizb23aa672009-07-01 21:26:54 +02004921 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4922
Johannes Bergc0692b82010-08-27 14:26:53 +03004923 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4924 u16 proto;
4925 proto = nla_get_u16(
4926 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4927 settings->control_port_ethertype = cpu_to_be16(proto);
4928 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4929 proto != ETH_P_PAE)
4930 return -EINVAL;
4931 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4932 settings->control_port_no_encrypt = true;
4933 } else
4934 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4935
Samuel Ortizb23aa672009-07-01 21:26:54 +02004936 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4937 void *data;
4938 int len, i;
4939
4940 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4941 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4942 settings->n_ciphers_pairwise = len / sizeof(u32);
4943
4944 if (len % sizeof(u32))
4945 return -EINVAL;
4946
Johannes Berg3dc27d22009-07-02 21:36:37 +02004947 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004948 return -EINVAL;
4949
4950 memcpy(settings->ciphers_pairwise, data, len);
4951
4952 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004953 if (!cfg80211_supported_cipher_suite(
4954 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004955 settings->ciphers_pairwise[i]))
4956 return -EINVAL;
4957 }
4958
4959 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4960 settings->cipher_group =
4961 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004962 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4963 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004964 return -EINVAL;
4965 }
4966
4967 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4968 settings->wpa_versions =
4969 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4970 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4971 return -EINVAL;
4972 }
4973
4974 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4975 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004976 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004977
4978 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4979 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4980 settings->n_akm_suites = len / sizeof(u32);
4981
4982 if (len % sizeof(u32))
4983 return -EINVAL;
4984
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004985 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4986 return -EINVAL;
4987
Samuel Ortizb23aa672009-07-01 21:26:54 +02004988 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004989 }
4990
4991 return 0;
4992}
4993
Jouni Malinen636a5d32009-03-19 13:39:22 +02004994static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4995{
Johannes Berg4c476992010-10-04 21:36:35 +02004996 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4997 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004998 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004999 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005000 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005001 int err, ssid_len, ie_len = 0;
5002 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005003 u32 flags = 0;
5004 struct ieee80211_ht_cap *ht_capa = NULL;
5005 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005006
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005007 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5008 return -EINVAL;
5009
5010 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005011 !info->attrs[NL80211_ATTR_SSID] ||
5012 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005013 return -EINVAL;
5014
Johannes Berg4c476992010-10-04 21:36:35 +02005015 if (!rdev->ops->assoc)
5016 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005017
Johannes Berg074ac8d2010-09-16 14:58:22 +02005018 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005019 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5020 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005021
Johannes Berg19957bb2009-07-02 17:20:43 +02005022 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005023
Johannes Berg19957bb2009-07-02 17:20:43 +02005024 chan = ieee80211_get_channel(&rdev->wiphy,
5025 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005026 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5027 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005028
Johannes Berg19957bb2009-07-02 17:20:43 +02005029 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5030 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005031
5032 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005033 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5034 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005035 }
5036
Jouni Malinendc6382c2009-05-06 22:09:37 +03005037 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005038 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005039 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005040 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005041 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005042 else if (mfp != NL80211_MFP_NO)
5043 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005044 }
5045
Johannes Berg3e5d7642009-07-07 14:37:26 +02005046 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5047 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5048
Ben Greear7e7c8922011-11-18 11:31:59 -08005049 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5050 flags |= ASSOC_REQ_DISABLE_HT;
5051
5052 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5053 ht_capa_mask =
5054 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5055
5056 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5057 if (!ht_capa_mask)
5058 return -EINVAL;
5059 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5060 }
5061
Johannes Bergc0692b82010-08-27 14:26:53 +03005062 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005063 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005064 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5065 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005066 &crypto, flags, ht_capa,
5067 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005068
Jouni Malinen636a5d32009-03-19 13:39:22 +02005069 return err;
5070}
5071
5072static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5073{
Johannes Berg4c476992010-10-04 21:36:35 +02005074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5075 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005076 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005077 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005078 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005079 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005080
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005081 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5082 return -EINVAL;
5083
5084 if (!info->attrs[NL80211_ATTR_MAC])
5085 return -EINVAL;
5086
5087 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5088 return -EINVAL;
5089
Johannes Berg4c476992010-10-04 21:36:35 +02005090 if (!rdev->ops->deauth)
5091 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005092
Johannes Berg074ac8d2010-09-16 14:58:22 +02005093 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005094 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5095 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005096
Johannes Berg19957bb2009-07-02 17:20:43 +02005097 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005098
Johannes Berg19957bb2009-07-02 17:20:43 +02005099 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5100 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005101 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005102 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005103 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005104
5105 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005106 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5107 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005108 }
5109
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005110 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5111
Johannes Berg4c476992010-10-04 21:36:35 +02005112 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5113 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005114}
5115
5116static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5117{
Johannes Berg4c476992010-10-04 21:36:35 +02005118 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5119 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005120 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005121 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005122 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005123 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005124
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005125 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5126 return -EINVAL;
5127
5128 if (!info->attrs[NL80211_ATTR_MAC])
5129 return -EINVAL;
5130
5131 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5132 return -EINVAL;
5133
Johannes Berg4c476992010-10-04 21:36:35 +02005134 if (!rdev->ops->disassoc)
5135 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005136
Johannes Berg074ac8d2010-09-16 14:58:22 +02005137 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005138 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5139 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005140
Johannes Berg19957bb2009-07-02 17:20:43 +02005141 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005142
Johannes Berg19957bb2009-07-02 17:20:43 +02005143 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5144 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005145 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005146 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005147 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005148
5149 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005150 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5151 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005152 }
5153
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005154 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5155
Johannes Berg4c476992010-10-04 21:36:35 +02005156 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5157 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005158}
5159
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005160static bool
5161nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5162 int mcast_rate[IEEE80211_NUM_BANDS],
5163 int rateval)
5164{
5165 struct wiphy *wiphy = &rdev->wiphy;
5166 bool found = false;
5167 int band, i;
5168
5169 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5170 struct ieee80211_supported_band *sband;
5171
5172 sband = wiphy->bands[band];
5173 if (!sband)
5174 continue;
5175
5176 for (i = 0; i < sband->n_bitrates; i++) {
5177 if (sband->bitrates[i].bitrate == rateval) {
5178 mcast_rate[band] = i + 1;
5179 found = true;
5180 break;
5181 }
5182 }
5183 }
5184
5185 return found;
5186}
5187
Johannes Berg04a773a2009-04-19 21:24:32 +02005188static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5189{
Johannes Berg4c476992010-10-04 21:36:35 +02005190 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5191 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005192 struct cfg80211_ibss_params ibss;
5193 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005194 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005195 int err;
5196
Johannes Berg8e30bc52009-04-22 17:45:38 +02005197 memset(&ibss, 0, sizeof(ibss));
5198
Johannes Berg04a773a2009-04-19 21:24:32 +02005199 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5200 return -EINVAL;
5201
5202 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5203 !info->attrs[NL80211_ATTR_SSID] ||
5204 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5205 return -EINVAL;
5206
Johannes Berg8e30bc52009-04-22 17:45:38 +02005207 ibss.beacon_interval = 100;
5208
5209 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5210 ibss.beacon_interval =
5211 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5212 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5213 return -EINVAL;
5214 }
5215
Johannes Berg4c476992010-10-04 21:36:35 +02005216 if (!rdev->ops->join_ibss)
5217 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005218
Johannes Berg4c476992010-10-04 21:36:35 +02005219 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5220 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005221
Johannes Berg79c97e92009-07-07 03:56:12 +02005222 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005223
Johannes Berg39193492011-09-16 13:45:25 +02005224 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005225 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005226
5227 if (!is_valid_ether_addr(ibss.bssid))
5228 return -EINVAL;
5229 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005230 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5231 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5232
5233 if (info->attrs[NL80211_ATTR_IE]) {
5234 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5235 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5236 }
5237
Alexander Simon54858ee2011-11-30 16:56:32 +01005238 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5239 enum nl80211_channel_type channel_type;
5240
Johannes Bergcd6c6592012-05-10 21:27:18 +02005241 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee2011-11-30 16:56:32 +01005242 return -EINVAL;
5243
5244 if (channel_type != NL80211_CHAN_NO_HT &&
5245 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5246 return -EINVAL;
5247
5248 ibss.channel_type = channel_type;
5249 } else {
5250 ibss.channel_type = NL80211_CHAN_NO_HT;
5251 }
5252
5253 ibss.channel = rdev_freq_to_chan(rdev,
5254 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5255 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005256 if (!ibss.channel ||
5257 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005258 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5259 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005260
Alexander Simon54858ee2011-11-30 16:56:32 +01005261 /* Both channels should be able to initiate communication */
5262 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5263 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5264 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5265 ibss.channel_type))
5266 return -EINVAL;
5267
Johannes Berg04a773a2009-04-19 21:24:32 +02005268 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005269 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005270
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005271 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5272 u8 *rates =
5273 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5274 int n_rates =
5275 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5276 struct ieee80211_supported_band *sband =
5277 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005278
Johannes Berg34850ab2011-07-18 18:08:35 +02005279 err = ieee80211_get_ratemask(sband, rates, n_rates,
5280 &ibss.basic_rates);
5281 if (err)
5282 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005283 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005284
5285 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5286 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5287 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5288 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005289
Johannes Berg4c476992010-10-04 21:36:35 +02005290 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5291 connkeys = nl80211_parse_connkeys(rdev,
5292 info->attrs[NL80211_ATTR_KEYS]);
5293 if (IS_ERR(connkeys))
5294 return PTR_ERR(connkeys);
5295 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005296
Antonio Quartulli267335d2012-01-31 20:25:47 +01005297 ibss.control_port =
5298 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5299
Johannes Berg4c476992010-10-04 21:36:35 +02005300 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005301 if (err)
5302 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005303 return err;
5304}
5305
5306static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5307{
Johannes Berg4c476992010-10-04 21:36:35 +02005308 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5309 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005310
Johannes Berg4c476992010-10-04 21:36:35 +02005311 if (!rdev->ops->leave_ibss)
5312 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005313
Johannes Berg4c476992010-10-04 21:36:35 +02005314 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5315 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005316
Johannes Berg4c476992010-10-04 21:36:35 +02005317 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005318}
5319
Johannes Bergaff89a92009-07-01 21:26:51 +02005320#ifdef CONFIG_NL80211_TESTMODE
5321static struct genl_multicast_group nl80211_testmode_mcgrp = {
5322 .name = "testmode",
5323};
5324
5325static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5326{
Johannes Berg4c476992010-10-04 21:36:35 +02005327 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005328 int err;
5329
5330 if (!info->attrs[NL80211_ATTR_TESTDATA])
5331 return -EINVAL;
5332
Johannes Bergaff89a92009-07-01 21:26:51 +02005333 err = -EOPNOTSUPP;
5334 if (rdev->ops->testmode_cmd) {
5335 rdev->testmode_info = info;
5336 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5337 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5338 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5339 rdev->testmode_info = NULL;
5340 }
5341
Johannes Bergaff89a92009-07-01 21:26:51 +02005342 return err;
5343}
5344
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005345static int nl80211_testmode_dump(struct sk_buff *skb,
5346 struct netlink_callback *cb)
5347{
Johannes Berg00918d32011-12-13 17:22:05 +01005348 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005349 int err;
5350 long phy_idx;
5351 void *data = NULL;
5352 int data_len = 0;
5353
5354 if (cb->args[0]) {
5355 /*
5356 * 0 is a valid index, but not valid for args[0],
5357 * so we need to offset by 1.
5358 */
5359 phy_idx = cb->args[0] - 1;
5360 } else {
5361 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5362 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5363 nl80211_policy);
5364 if (err)
5365 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005366
Johannes Berg2bd7e352012-06-15 14:23:16 +02005367 mutex_lock(&cfg80211_mutex);
5368 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5369 nl80211_fam.attrbuf);
5370 if (IS_ERR(rdev)) {
5371 mutex_unlock(&cfg80211_mutex);
5372 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005373 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005374 phy_idx = rdev->wiphy_idx;
5375 rdev = NULL;
5376 mutex_unlock(&cfg80211_mutex);
5377
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005378 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5379 cb->args[1] =
5380 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5381 }
5382
5383 if (cb->args[1]) {
5384 data = nla_data((void *)cb->args[1]);
5385 data_len = nla_len((void *)cb->args[1]);
5386 }
5387
5388 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005389 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5390 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005391 mutex_unlock(&cfg80211_mutex);
5392 return -ENOENT;
5393 }
Johannes Berg00918d32011-12-13 17:22:05 +01005394 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005395 mutex_unlock(&cfg80211_mutex);
5396
Johannes Berg00918d32011-12-13 17:22:05 +01005397 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005398 err = -EOPNOTSUPP;
5399 goto out_err;
5400 }
5401
5402 while (1) {
5403 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5404 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5405 NL80211_CMD_TESTMODE);
5406 struct nlattr *tmdata;
5407
David S. Miller9360ffd2012-03-29 04:41:26 -04005408 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005409 genlmsg_cancel(skb, hdr);
5410 break;
5411 }
5412
5413 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5414 if (!tmdata) {
5415 genlmsg_cancel(skb, hdr);
5416 break;
5417 }
Johannes Berg00918d32011-12-13 17:22:05 +01005418 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5419 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005420 nla_nest_end(skb, tmdata);
5421
5422 if (err == -ENOBUFS || err == -ENOENT) {
5423 genlmsg_cancel(skb, hdr);
5424 break;
5425 } else if (err) {
5426 genlmsg_cancel(skb, hdr);
5427 goto out_err;
5428 }
5429
5430 genlmsg_end(skb, hdr);
5431 }
5432
5433 err = skb->len;
5434 /* see above */
5435 cb->args[0] = phy_idx + 1;
5436 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005437 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005438 return err;
5439}
5440
Johannes Bergaff89a92009-07-01 21:26:51 +02005441static struct sk_buff *
5442__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5443 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5444{
5445 struct sk_buff *skb;
5446 void *hdr;
5447 struct nlattr *data;
5448
5449 skb = nlmsg_new(approxlen + 100, gfp);
5450 if (!skb)
5451 return NULL;
5452
5453 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5454 if (!hdr) {
5455 kfree_skb(skb);
5456 return NULL;
5457 }
5458
David S. Miller9360ffd2012-03-29 04:41:26 -04005459 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5460 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005461 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5462
5463 ((void **)skb->cb)[0] = rdev;
5464 ((void **)skb->cb)[1] = hdr;
5465 ((void **)skb->cb)[2] = data;
5466
5467 return skb;
5468
5469 nla_put_failure:
5470 kfree_skb(skb);
5471 return NULL;
5472}
5473
5474struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5475 int approxlen)
5476{
5477 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5478
5479 if (WARN_ON(!rdev->testmode_info))
5480 return NULL;
5481
5482 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5483 rdev->testmode_info->snd_pid,
5484 rdev->testmode_info->snd_seq,
5485 GFP_KERNEL);
5486}
5487EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5488
5489int cfg80211_testmode_reply(struct sk_buff *skb)
5490{
5491 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5492 void *hdr = ((void **)skb->cb)[1];
5493 struct nlattr *data = ((void **)skb->cb)[2];
5494
5495 if (WARN_ON(!rdev->testmode_info)) {
5496 kfree_skb(skb);
5497 return -EINVAL;
5498 }
5499
5500 nla_nest_end(skb, data);
5501 genlmsg_end(skb, hdr);
5502 return genlmsg_reply(skb, rdev->testmode_info);
5503}
5504EXPORT_SYMBOL(cfg80211_testmode_reply);
5505
5506struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5507 int approxlen, gfp_t gfp)
5508{
5509 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5510
5511 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5512}
5513EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5514
5515void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5516{
5517 void *hdr = ((void **)skb->cb)[1];
5518 struct nlattr *data = ((void **)skb->cb)[2];
5519
5520 nla_nest_end(skb, data);
5521 genlmsg_end(skb, hdr);
5522 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5523}
5524EXPORT_SYMBOL(cfg80211_testmode_event);
5525#endif
5526
Samuel Ortizb23aa672009-07-01 21:26:54 +02005527static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5528{
Johannes Berg4c476992010-10-04 21:36:35 +02005529 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5530 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005531 struct cfg80211_connect_params connect;
5532 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005533 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005534 int err;
5535
5536 memset(&connect, 0, sizeof(connect));
5537
5538 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5539 return -EINVAL;
5540
5541 if (!info->attrs[NL80211_ATTR_SSID] ||
5542 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5543 return -EINVAL;
5544
5545 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5546 connect.auth_type =
5547 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5548 if (!nl80211_valid_auth_type(connect.auth_type))
5549 return -EINVAL;
5550 } else
5551 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5552
5553 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5554
Johannes Bergc0692b82010-08-27 14:26:53 +03005555 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005556 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005557 if (err)
5558 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005559
Johannes Berg074ac8d2010-09-16 14:58:22 +02005560 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005561 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5562 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005563
Johannes Berg79c97e92009-07-07 03:56:12 +02005564 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005565
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305566 connect.bg_scan_period = -1;
5567 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5568 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5569 connect.bg_scan_period =
5570 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5571 }
5572
Samuel Ortizb23aa672009-07-01 21:26:54 +02005573 if (info->attrs[NL80211_ATTR_MAC])
5574 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5575 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5576 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5577
5578 if (info->attrs[NL80211_ATTR_IE]) {
5579 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5580 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5581 }
5582
5583 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5584 connect.channel =
5585 ieee80211_get_channel(wiphy,
5586 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5587 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005588 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5589 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005590 }
5591
Johannes Bergfffd0932009-07-08 14:22:54 +02005592 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5593 connkeys = nl80211_parse_connkeys(rdev,
5594 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005595 if (IS_ERR(connkeys))
5596 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005597 }
5598
Ben Greear7e7c8922011-11-18 11:31:59 -08005599 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5600 connect.flags |= ASSOC_REQ_DISABLE_HT;
5601
5602 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5603 memcpy(&connect.ht_capa_mask,
5604 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5605 sizeof(connect.ht_capa_mask));
5606
5607 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5608 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5609 return -EINVAL;
5610 memcpy(&connect.ht_capa,
5611 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5612 sizeof(connect.ht_capa));
5613 }
5614
Johannes Bergfffd0932009-07-08 14:22:54 +02005615 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005616 if (err)
5617 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005618 return err;
5619}
5620
5621static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5622{
Johannes Berg4c476992010-10-04 21:36:35 +02005623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5624 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005625 u16 reason;
5626
5627 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5628 reason = WLAN_REASON_DEAUTH_LEAVING;
5629 else
5630 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5631
5632 if (reason == 0)
5633 return -EINVAL;
5634
Johannes Berg074ac8d2010-09-16 14:58:22 +02005635 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005636 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5637 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005638
Johannes Berg4c476992010-10-04 21:36:35 +02005639 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005640}
5641
Johannes Berg463d0182009-07-14 00:33:35 +02005642static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5643{
Johannes Berg4c476992010-10-04 21:36:35 +02005644 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005645 struct net *net;
5646 int err;
5647 u32 pid;
5648
5649 if (!info->attrs[NL80211_ATTR_PID])
5650 return -EINVAL;
5651
5652 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5653
Johannes Berg463d0182009-07-14 00:33:35 +02005654 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005655 if (IS_ERR(net))
5656 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005657
5658 err = 0;
5659
5660 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005661 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5662 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005663
Johannes Berg463d0182009-07-14 00:33:35 +02005664 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005665 return err;
5666}
5667
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005668static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5669{
Johannes Berg4c476992010-10-04 21:36:35 +02005670 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005671 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5672 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005673 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005674 struct cfg80211_pmksa pmksa;
5675
5676 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5677
5678 if (!info->attrs[NL80211_ATTR_MAC])
5679 return -EINVAL;
5680
5681 if (!info->attrs[NL80211_ATTR_PMKID])
5682 return -EINVAL;
5683
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005684 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5685 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5686
Johannes Berg074ac8d2010-09-16 14:58:22 +02005687 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005688 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5689 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005690
5691 switch (info->genlhdr->cmd) {
5692 case NL80211_CMD_SET_PMKSA:
5693 rdev_ops = rdev->ops->set_pmksa;
5694 break;
5695 case NL80211_CMD_DEL_PMKSA:
5696 rdev_ops = rdev->ops->del_pmksa;
5697 break;
5698 default:
5699 WARN_ON(1);
5700 break;
5701 }
5702
Johannes Berg4c476992010-10-04 21:36:35 +02005703 if (!rdev_ops)
5704 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005705
Johannes Berg4c476992010-10-04 21:36:35 +02005706 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005707}
5708
5709static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5710{
Johannes Berg4c476992010-10-04 21:36:35 +02005711 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5712 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005713
Johannes Berg074ac8d2010-09-16 14:58:22 +02005714 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005715 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5716 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005717
Johannes Berg4c476992010-10-04 21:36:35 +02005718 if (!rdev->ops->flush_pmksa)
5719 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005720
Johannes Berg4c476992010-10-04 21:36:35 +02005721 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005722}
5723
Arik Nemtsov109086c2011-09-28 14:12:50 +03005724static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5725{
5726 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5727 struct net_device *dev = info->user_ptr[1];
5728 u8 action_code, dialog_token;
5729 u16 status_code;
5730 u8 *peer;
5731
5732 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5733 !rdev->ops->tdls_mgmt)
5734 return -EOPNOTSUPP;
5735
5736 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5737 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5738 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5739 !info->attrs[NL80211_ATTR_IE] ||
5740 !info->attrs[NL80211_ATTR_MAC])
5741 return -EINVAL;
5742
5743 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5744 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5745 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5746 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5747
5748 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5749 dialog_token, status_code,
5750 nla_data(info->attrs[NL80211_ATTR_IE]),
5751 nla_len(info->attrs[NL80211_ATTR_IE]));
5752}
5753
5754static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5755{
5756 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5757 struct net_device *dev = info->user_ptr[1];
5758 enum nl80211_tdls_operation operation;
5759 u8 *peer;
5760
5761 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5762 !rdev->ops->tdls_oper)
5763 return -EOPNOTSUPP;
5764
5765 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5766 !info->attrs[NL80211_ATTR_MAC])
5767 return -EINVAL;
5768
5769 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5770 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5771
5772 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5773}
5774
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005775static int nl80211_remain_on_channel(struct sk_buff *skb,
5776 struct genl_info *info)
5777{
Johannes Berg4c476992010-10-04 21:36:35 +02005778 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005779 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005780 struct ieee80211_channel *chan;
5781 struct sk_buff *msg;
5782 void *hdr;
5783 u64 cookie;
5784 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5785 u32 freq, duration;
5786 int err;
5787
5788 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5789 !info->attrs[NL80211_ATTR_DURATION])
5790 return -EINVAL;
5791
5792 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5793
Johannes Berg7c4ef712011-11-18 15:33:48 +01005794 if (!rdev->ops->remain_on_channel ||
5795 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005796 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005797
Johannes Bergebf348f2012-06-01 12:50:54 +02005798 /*
5799 * We should be on that channel for at least a minimum amount of
5800 * time (10ms) but no longer than the driver supports.
5801 */
5802 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5803 duration > rdev->wiphy.max_remain_on_channel_duration)
5804 return -EINVAL;
5805
Johannes Bergcd6c6592012-05-10 21:27:18 +02005806 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5807 !nl80211_valid_channel_type(info, &channel_type))
5808 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005809
5810 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5811 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005812 if (chan == NULL)
5813 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005814
5815 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005816 if (!msg)
5817 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005818
5819 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5820 NL80211_CMD_REMAIN_ON_CHANNEL);
5821
5822 if (IS_ERR(hdr)) {
5823 err = PTR_ERR(hdr);
5824 goto free_msg;
5825 }
5826
Johannes Berg71bbc992012-06-15 15:30:18 +02005827 err = rdev->ops->remain_on_channel(&rdev->wiphy, wdev, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005828 channel_type, duration, &cookie);
5829
5830 if (err)
5831 goto free_msg;
5832
David S. Miller9360ffd2012-03-29 04:41:26 -04005833 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5834 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005835
5836 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005837
5838 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005839
5840 nla_put_failure:
5841 err = -ENOBUFS;
5842 free_msg:
5843 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005844 return err;
5845}
5846
5847static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5848 struct genl_info *info)
5849{
Johannes Berg4c476992010-10-04 21:36:35 +02005850 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005851 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005852 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005853
5854 if (!info->attrs[NL80211_ATTR_COOKIE])
5855 return -EINVAL;
5856
Johannes Berg4c476992010-10-04 21:36:35 +02005857 if (!rdev->ops->cancel_remain_on_channel)
5858 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005859
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005860 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5861
Johannes Berg71bbc992012-06-15 15:30:18 +02005862 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005863}
5864
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005865static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5866 u8 *rates, u8 rates_len)
5867{
5868 u8 i;
5869 u32 mask = 0;
5870
5871 for (i = 0; i < rates_len; i++) {
5872 int rate = (rates[i] & 0x7f) * 5;
5873 int ridx;
5874 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5875 struct ieee80211_rate *srate =
5876 &sband->bitrates[ridx];
5877 if (rate == srate->bitrate) {
5878 mask |= 1 << ridx;
5879 break;
5880 }
5881 }
5882 if (ridx == sband->n_bitrates)
5883 return 0; /* rate not found */
5884 }
5885
5886 return mask;
5887}
5888
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005889static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5890 u8 *rates, u8 rates_len,
5891 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5892{
5893 u8 i;
5894
5895 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5896
5897 for (i = 0; i < rates_len; i++) {
5898 int ridx, rbit;
5899
5900 ridx = rates[i] / 8;
5901 rbit = BIT(rates[i] % 8);
5902
5903 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005904 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005905 return false;
5906
5907 /* check availability */
5908 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5909 mcs[ridx] |= rbit;
5910 else
5911 return false;
5912 }
5913
5914 return true;
5915}
5916
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005917static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005918 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5919 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005920 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5921 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005922};
5923
5924static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5925 struct genl_info *info)
5926{
5927 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005928 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005929 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005930 int rem, i;
5931 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005932 struct nlattr *tx_rates;
5933 struct ieee80211_supported_band *sband;
5934
5935 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5936 return -EINVAL;
5937
Johannes Berg4c476992010-10-04 21:36:35 +02005938 if (!rdev->ops->set_bitrate_mask)
5939 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005940
5941 memset(&mask, 0, sizeof(mask));
5942 /* Default to all rates enabled */
5943 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5944 sband = rdev->wiphy.bands[i];
5945 mask.control[i].legacy =
5946 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005947 if (sband)
5948 memcpy(mask.control[i].mcs,
5949 sband->ht_cap.mcs.rx_mask,
5950 sizeof(mask.control[i].mcs));
5951 else
5952 memset(mask.control[i].mcs, 0,
5953 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005954 }
5955
5956 /*
5957 * The nested attribute uses enum nl80211_band as the index. This maps
5958 * directly to the enum ieee80211_band values used in cfg80211.
5959 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005960 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005961 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5962 {
5963 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005964 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5965 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005966 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005967 if (sband == NULL)
5968 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005969 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5970 nla_len(tx_rates), nl80211_txattr_policy);
5971 if (tb[NL80211_TXRATE_LEGACY]) {
5972 mask.control[band].legacy = rateset_to_mask(
5973 sband,
5974 nla_data(tb[NL80211_TXRATE_LEGACY]),
5975 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305976 if ((mask.control[band].legacy == 0) &&
5977 nla_len(tb[NL80211_TXRATE_LEGACY]))
5978 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005979 }
5980 if (tb[NL80211_TXRATE_MCS]) {
5981 if (!ht_rateset_to_mask(
5982 sband,
5983 nla_data(tb[NL80211_TXRATE_MCS]),
5984 nla_len(tb[NL80211_TXRATE_MCS]),
5985 mask.control[band].mcs))
5986 return -EINVAL;
5987 }
5988
5989 if (mask.control[band].legacy == 0) {
5990 /* don't allow empty legacy rates if HT
5991 * is not even supported. */
5992 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5993 return -EINVAL;
5994
5995 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5996 if (mask.control[band].mcs[i])
5997 break;
5998
5999 /* legacy and mcs rates may not be both empty */
6000 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006001 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006002 }
6003 }
6004
Johannes Berg4c476992010-10-04 21:36:35 +02006005 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006006}
6007
Johannes Berg2e161f72010-08-12 15:38:38 +02006008static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006009{
Johannes Berg4c476992010-10-04 21:36:35 +02006010 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006011 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006012 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006013
6014 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6015 return -EINVAL;
6016
Johannes Berg2e161f72010-08-12 15:38:38 +02006017 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6018 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006019
Johannes Berg71bbc992012-06-15 15:30:18 +02006020 switch (wdev->iftype) {
6021 case NL80211_IFTYPE_STATION:
6022 case NL80211_IFTYPE_ADHOC:
6023 case NL80211_IFTYPE_P2P_CLIENT:
6024 case NL80211_IFTYPE_AP:
6025 case NL80211_IFTYPE_AP_VLAN:
6026 case NL80211_IFTYPE_MESH_POINT:
6027 case NL80211_IFTYPE_P2P_GO:
6028 break;
6029 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006030 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006031 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006032
6033 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006034 if (!rdev->ops->mgmt_tx)
6035 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006036
Johannes Berg71bbc992012-06-15 15:30:18 +02006037 return cfg80211_mlme_register_mgmt(wdev, info->snd_pid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006038 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6039 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006040}
6041
Johannes Berg2e161f72010-08-12 15:38:38 +02006042static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006043{
Johannes Berg4c476992010-10-04 21:36:35 +02006044 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006045 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006046 struct ieee80211_channel *chan;
6047 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02006048 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02006049 u32 freq;
6050 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006051 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006052 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006053 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006054 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006055 bool offchan, no_cck, dont_wait_for_ack;
6056
6057 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006058
6059 if (!info->attrs[NL80211_ATTR_FRAME] ||
6060 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6061 return -EINVAL;
6062
Johannes Berg4c476992010-10-04 21:36:35 +02006063 if (!rdev->ops->mgmt_tx)
6064 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006065
Johannes Berg71bbc992012-06-15 15:30:18 +02006066 switch (wdev->iftype) {
6067 case NL80211_IFTYPE_STATION:
6068 case NL80211_IFTYPE_ADHOC:
6069 case NL80211_IFTYPE_P2P_CLIENT:
6070 case NL80211_IFTYPE_AP:
6071 case NL80211_IFTYPE_AP_VLAN:
6072 case NL80211_IFTYPE_MESH_POINT:
6073 case NL80211_IFTYPE_P2P_GO:
6074 break;
6075 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006076 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006077 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006078
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006079 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006080 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006081 return -EINVAL;
6082 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006083
6084 /*
6085 * We should wait on the channel for at least a minimum amount
6086 * of time (10ms) but no longer than the driver supports.
6087 */
6088 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6089 wait > rdev->wiphy.max_remain_on_channel_duration)
6090 return -EINVAL;
6091
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006092 }
6093
Jouni Malinen026331c2010-02-15 12:53:10 +02006094 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02006095 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02006096 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02006097 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02006098 }
6099
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006100 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6101
Johannes Berg7c4ef712011-11-18 15:33:48 +01006102 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6103 return -EINVAL;
6104
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306105 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6106
Jouni Malinen026331c2010-02-15 12:53:10 +02006107 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6108 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006109 if (chan == NULL)
6110 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006111
Johannes Berge247bd902011-11-04 11:18:21 +01006112 if (!dont_wait_for_ack) {
6113 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6114 if (!msg)
6115 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006116
Johannes Berge247bd902011-11-04 11:18:21 +01006117 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6118 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006119
Johannes Berge247bd902011-11-04 11:18:21 +01006120 if (IS_ERR(hdr)) {
6121 err = PTR_ERR(hdr);
6122 goto free_msg;
6123 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006124 }
Johannes Berge247bd902011-11-04 11:18:21 +01006125
Johannes Berg71bbc992012-06-15 15:30:18 +02006126 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, channel_type,
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006127 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006128 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6129 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006130 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006131 if (err)
6132 goto free_msg;
6133
Johannes Berge247bd902011-11-04 11:18:21 +01006134 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006135 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6136 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006137
Johannes Berge247bd902011-11-04 11:18:21 +01006138 genlmsg_end(msg, hdr);
6139 return genlmsg_reply(msg, info);
6140 }
6141
6142 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006143
6144 nla_put_failure:
6145 err = -ENOBUFS;
6146 free_msg:
6147 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006148 return err;
6149}
6150
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006151static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6152{
6153 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006154 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006155 u64 cookie;
6156
6157 if (!info->attrs[NL80211_ATTR_COOKIE])
6158 return -EINVAL;
6159
6160 if (!rdev->ops->mgmt_tx_cancel_wait)
6161 return -EOPNOTSUPP;
6162
Johannes Berg71bbc992012-06-15 15:30:18 +02006163 switch (wdev->iftype) {
6164 case NL80211_IFTYPE_STATION:
6165 case NL80211_IFTYPE_ADHOC:
6166 case NL80211_IFTYPE_P2P_CLIENT:
6167 case NL80211_IFTYPE_AP:
6168 case NL80211_IFTYPE_AP_VLAN:
6169 case NL80211_IFTYPE_P2P_GO:
6170 break;
6171 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006172 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006173 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006174
6175 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6176
Johannes Berg71bbc992012-06-15 15:30:18 +02006177 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006178}
6179
Kalle Valoffb9eb32010-02-17 17:58:10 +02006180static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6181{
Johannes Berg4c476992010-10-04 21:36:35 +02006182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006183 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006184 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006185 u8 ps_state;
6186 bool state;
6187 int err;
6188
Johannes Berg4c476992010-10-04 21:36:35 +02006189 if (!info->attrs[NL80211_ATTR_PS_STATE])
6190 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006191
6192 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6193
Johannes Berg4c476992010-10-04 21:36:35 +02006194 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6195 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006196
6197 wdev = dev->ieee80211_ptr;
6198
Johannes Berg4c476992010-10-04 21:36:35 +02006199 if (!rdev->ops->set_power_mgmt)
6200 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006201
6202 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6203
6204 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006205 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006206
Johannes Berg4c476992010-10-04 21:36:35 +02006207 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
6208 wdev->ps_timeout);
6209 if (!err)
6210 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006211 return err;
6212}
6213
6214static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6215{
Johannes Berg4c476992010-10-04 21:36:35 +02006216 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006217 enum nl80211_ps_state ps_state;
6218 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006219 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006220 struct sk_buff *msg;
6221 void *hdr;
6222 int err;
6223
Kalle Valoffb9eb32010-02-17 17:58:10 +02006224 wdev = dev->ieee80211_ptr;
6225
Johannes Berg4c476992010-10-04 21:36:35 +02006226 if (!rdev->ops->set_power_mgmt)
6227 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006228
6229 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006230 if (!msg)
6231 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006232
6233 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6234 NL80211_CMD_GET_POWER_SAVE);
6235 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006236 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006237 goto free_msg;
6238 }
6239
6240 if (wdev->ps)
6241 ps_state = NL80211_PS_ENABLED;
6242 else
6243 ps_state = NL80211_PS_DISABLED;
6244
David S. Miller9360ffd2012-03-29 04:41:26 -04006245 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6246 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006247
6248 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006249 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006250
Johannes Berg4c476992010-10-04 21:36:35 +02006251 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006252 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006253 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006254 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006255 return err;
6256}
6257
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006258static struct nla_policy
6259nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6260 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6261 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6262 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
6263};
6264
6265static int nl80211_set_cqm_rssi(struct genl_info *info,
6266 s32 threshold, u32 hysteresis)
6267{
Johannes Berg4c476992010-10-04 21:36:35 +02006268 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006269 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006270 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006271
6272 if (threshold > 0)
6273 return -EINVAL;
6274
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006275 wdev = dev->ieee80211_ptr;
6276
Johannes Berg4c476992010-10-04 21:36:35 +02006277 if (!rdev->ops->set_cqm_rssi_config)
6278 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006279
Johannes Berg074ac8d2010-09-16 14:58:22 +02006280 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006281 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6282 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006283
Johannes Berg4c476992010-10-04 21:36:35 +02006284 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6285 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006286}
6287
6288static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6289{
6290 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6291 struct nlattr *cqm;
6292 int err;
6293
6294 cqm = info->attrs[NL80211_ATTR_CQM];
6295 if (!cqm) {
6296 err = -EINVAL;
6297 goto out;
6298 }
6299
6300 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6301 nl80211_attr_cqm_policy);
6302 if (err)
6303 goto out;
6304
6305 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6306 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6307 s32 threshold;
6308 u32 hysteresis;
6309 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6310 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6311 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6312 } else
6313 err = -EINVAL;
6314
6315out:
6316 return err;
6317}
6318
Johannes Berg29cbe682010-12-03 09:20:44 +01006319static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6320{
6321 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6322 struct net_device *dev = info->user_ptr[1];
6323 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006324 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006325 int err;
6326
6327 /* start with default */
6328 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006329 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006330
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006331 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006332 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006333 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006334 if (err)
6335 return err;
6336 }
6337
6338 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6339 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6340 return -EINVAL;
6341
Javier Cardonac80d5452010-12-16 17:37:49 -08006342 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6343 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6344
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006345 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6346 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6347 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6348 return -EINVAL;
6349
Javier Cardonac80d5452010-12-16 17:37:49 -08006350 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6351 /* parse additional setup parameters if given */
6352 err = nl80211_parse_mesh_setup(info, &setup);
6353 if (err)
6354 return err;
6355 }
6356
Johannes Bergcc1d2802012-05-16 23:50:20 +02006357 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6358 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6359
6360 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6361 !nl80211_valid_channel_type(info, &channel_type))
6362 return -EINVAL;
6363
6364 setup.channel = rdev_freq_to_chan(rdev,
6365 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6366 channel_type);
6367 if (!setup.channel)
6368 return -EINVAL;
6369 setup.channel_type = channel_type;
6370 } else {
6371 /* cfg80211_join_mesh() will sort it out */
6372 setup.channel = NULL;
6373 }
6374
Javier Cardonac80d5452010-12-16 17:37:49 -08006375 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006376}
6377
6378static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6379{
6380 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6381 struct net_device *dev = info->user_ptr[1];
6382
6383 return cfg80211_leave_mesh(rdev, dev);
6384}
6385
Johannes Bergdfb89c52012-06-27 09:23:48 +02006386#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006387static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6388{
6389 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6390 struct sk_buff *msg;
6391 void *hdr;
6392
6393 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6394 return -EOPNOTSUPP;
6395
6396 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6397 if (!msg)
6398 return -ENOMEM;
6399
6400 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6401 NL80211_CMD_GET_WOWLAN);
6402 if (!hdr)
6403 goto nla_put_failure;
6404
6405 if (rdev->wowlan) {
6406 struct nlattr *nl_wowlan;
6407
6408 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6409 if (!nl_wowlan)
6410 goto nla_put_failure;
6411
David S. Miller9360ffd2012-03-29 04:41:26 -04006412 if ((rdev->wowlan->any &&
6413 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6414 (rdev->wowlan->disconnect &&
6415 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6416 (rdev->wowlan->magic_pkt &&
6417 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6418 (rdev->wowlan->gtk_rekey_failure &&
6419 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6420 (rdev->wowlan->eap_identity_req &&
6421 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6422 (rdev->wowlan->four_way_handshake &&
6423 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6424 (rdev->wowlan->rfkill_release &&
6425 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6426 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006427 if (rdev->wowlan->n_patterns) {
6428 struct nlattr *nl_pats, *nl_pat;
6429 int i, pat_len;
6430
6431 nl_pats = nla_nest_start(msg,
6432 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6433 if (!nl_pats)
6434 goto nla_put_failure;
6435
6436 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6437 nl_pat = nla_nest_start(msg, i + 1);
6438 if (!nl_pat)
6439 goto nla_put_failure;
6440 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006441 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6442 DIV_ROUND_UP(pat_len, 8),
6443 rdev->wowlan->patterns[i].mask) ||
6444 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6445 pat_len,
6446 rdev->wowlan->patterns[i].pattern))
6447 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006448 nla_nest_end(msg, nl_pat);
6449 }
6450 nla_nest_end(msg, nl_pats);
6451 }
6452
6453 nla_nest_end(msg, nl_wowlan);
6454 }
6455
6456 genlmsg_end(msg, hdr);
6457 return genlmsg_reply(msg, info);
6458
6459nla_put_failure:
6460 nlmsg_free(msg);
6461 return -ENOBUFS;
6462}
6463
6464static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6465{
6466 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6467 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6468 struct cfg80211_wowlan no_triggers = {};
6469 struct cfg80211_wowlan new_triggers = {};
6470 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6471 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006472 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006473
6474 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6475 return -EOPNOTSUPP;
6476
6477 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6478 goto no_triggers;
6479
6480 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6481 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6482 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6483 nl80211_wowlan_policy);
6484 if (err)
6485 return err;
6486
6487 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6488 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6489 return -EINVAL;
6490 new_triggers.any = true;
6491 }
6492
6493 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6494 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6495 return -EINVAL;
6496 new_triggers.disconnect = true;
6497 }
6498
6499 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6500 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6501 return -EINVAL;
6502 new_triggers.magic_pkt = true;
6503 }
6504
Johannes Berg77dbbb132011-07-13 10:48:55 +02006505 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6506 return -EINVAL;
6507
6508 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6509 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6510 return -EINVAL;
6511 new_triggers.gtk_rekey_failure = true;
6512 }
6513
6514 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6515 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6516 return -EINVAL;
6517 new_triggers.eap_identity_req = true;
6518 }
6519
6520 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6521 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6522 return -EINVAL;
6523 new_triggers.four_way_handshake = true;
6524 }
6525
6526 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6527 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6528 return -EINVAL;
6529 new_triggers.rfkill_release = true;
6530 }
6531
Johannes Bergff1b6e62011-05-04 15:37:28 +02006532 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6533 struct nlattr *pat;
6534 int n_patterns = 0;
6535 int rem, pat_len, mask_len;
6536 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6537
6538 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6539 rem)
6540 n_patterns++;
6541 if (n_patterns > wowlan->n_patterns)
6542 return -EINVAL;
6543
6544 new_triggers.patterns = kcalloc(n_patterns,
6545 sizeof(new_triggers.patterns[0]),
6546 GFP_KERNEL);
6547 if (!new_triggers.patterns)
6548 return -ENOMEM;
6549
6550 new_triggers.n_patterns = n_patterns;
6551 i = 0;
6552
6553 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6554 rem) {
6555 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6556 nla_data(pat), nla_len(pat), NULL);
6557 err = -EINVAL;
6558 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6559 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6560 goto error;
6561 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6562 mask_len = DIV_ROUND_UP(pat_len, 8);
6563 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6564 mask_len)
6565 goto error;
6566 if (pat_len > wowlan->pattern_max_len ||
6567 pat_len < wowlan->pattern_min_len)
6568 goto error;
6569
6570 new_triggers.patterns[i].mask =
6571 kmalloc(mask_len + pat_len, GFP_KERNEL);
6572 if (!new_triggers.patterns[i].mask) {
6573 err = -ENOMEM;
6574 goto error;
6575 }
6576 new_triggers.patterns[i].pattern =
6577 new_triggers.patterns[i].mask + mask_len;
6578 memcpy(new_triggers.patterns[i].mask,
6579 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6580 mask_len);
6581 new_triggers.patterns[i].pattern_len = pat_len;
6582 memcpy(new_triggers.patterns[i].pattern,
6583 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6584 pat_len);
6585 i++;
6586 }
6587 }
6588
6589 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6590 struct cfg80211_wowlan *ntrig;
6591 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6592 GFP_KERNEL);
6593 if (!ntrig) {
6594 err = -ENOMEM;
6595 goto error;
6596 }
6597 cfg80211_rdev_free_wowlan(rdev);
6598 rdev->wowlan = ntrig;
6599 } else {
6600 no_triggers:
6601 cfg80211_rdev_free_wowlan(rdev);
6602 rdev->wowlan = NULL;
6603 }
6604
Johannes Berg6d525632012-04-04 15:05:25 +02006605 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6606 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6607
Johannes Bergff1b6e62011-05-04 15:37:28 +02006608 return 0;
6609 error:
6610 for (i = 0; i < new_triggers.n_patterns; i++)
6611 kfree(new_triggers.patterns[i].mask);
6612 kfree(new_triggers.patterns);
6613 return err;
6614}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006615#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006616
Johannes Berge5497d72011-07-05 16:35:40 +02006617static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6618{
6619 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6620 struct net_device *dev = info->user_ptr[1];
6621 struct wireless_dev *wdev = dev->ieee80211_ptr;
6622 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6623 struct cfg80211_gtk_rekey_data rekey_data;
6624 int err;
6625
6626 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6627 return -EINVAL;
6628
6629 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6630 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6631 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6632 nl80211_rekey_policy);
6633 if (err)
6634 return err;
6635
6636 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6637 return -ERANGE;
6638 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6639 return -ERANGE;
6640 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6641 return -ERANGE;
6642
6643 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6644 NL80211_KEK_LEN);
6645 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6646 NL80211_KCK_LEN);
6647 memcpy(rekey_data.replay_ctr,
6648 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6649 NL80211_REPLAY_CTR_LEN);
6650
6651 wdev_lock(wdev);
6652 if (!wdev->current_bss) {
6653 err = -ENOTCONN;
6654 goto out;
6655 }
6656
6657 if (!rdev->ops->set_rekey_data) {
6658 err = -EOPNOTSUPP;
6659 goto out;
6660 }
6661
6662 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6663 out:
6664 wdev_unlock(wdev);
6665 return err;
6666}
6667
Johannes Berg28946da2011-11-04 11:18:12 +01006668static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6669 struct genl_info *info)
6670{
6671 struct net_device *dev = info->user_ptr[1];
6672 struct wireless_dev *wdev = dev->ieee80211_ptr;
6673
6674 if (wdev->iftype != NL80211_IFTYPE_AP &&
6675 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6676 return -EINVAL;
6677
6678 if (wdev->ap_unexpected_nlpid)
6679 return -EBUSY;
6680
6681 wdev->ap_unexpected_nlpid = info->snd_pid;
6682 return 0;
6683}
6684
Johannes Berg7f6cf312011-11-04 11:18:15 +01006685static int nl80211_probe_client(struct sk_buff *skb,
6686 struct genl_info *info)
6687{
6688 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6689 struct net_device *dev = info->user_ptr[1];
6690 struct wireless_dev *wdev = dev->ieee80211_ptr;
6691 struct sk_buff *msg;
6692 void *hdr;
6693 const u8 *addr;
6694 u64 cookie;
6695 int err;
6696
6697 if (wdev->iftype != NL80211_IFTYPE_AP &&
6698 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6699 return -EOPNOTSUPP;
6700
6701 if (!info->attrs[NL80211_ATTR_MAC])
6702 return -EINVAL;
6703
6704 if (!rdev->ops->probe_client)
6705 return -EOPNOTSUPP;
6706
6707 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6708 if (!msg)
6709 return -ENOMEM;
6710
6711 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6712 NL80211_CMD_PROBE_CLIENT);
6713
6714 if (IS_ERR(hdr)) {
6715 err = PTR_ERR(hdr);
6716 goto free_msg;
6717 }
6718
6719 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6720
6721 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6722 if (err)
6723 goto free_msg;
6724
David S. Miller9360ffd2012-03-29 04:41:26 -04006725 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6726 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006727
6728 genlmsg_end(msg, hdr);
6729
6730 return genlmsg_reply(msg, info);
6731
6732 nla_put_failure:
6733 err = -ENOBUFS;
6734 free_msg:
6735 nlmsg_free(msg);
6736 return err;
6737}
6738
Johannes Berg5e760232011-11-04 11:18:17 +01006739static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6740{
6741 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6742
6743 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6744 return -EOPNOTSUPP;
6745
6746 if (rdev->ap_beacons_nlpid)
6747 return -EBUSY;
6748
6749 rdev->ap_beacons_nlpid = info->snd_pid;
6750
6751 return 0;
6752}
6753
Johannes Berg4c476992010-10-04 21:36:35 +02006754#define NL80211_FLAG_NEED_WIPHY 0x01
6755#define NL80211_FLAG_NEED_NETDEV 0x02
6756#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006757#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6758#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6759 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02006760#define NL80211_FLAG_NEED_WDEV 0x10
6761/* If a netdev is associated, it must be UP */
6762#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
6763 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006764
6765static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6766 struct genl_info *info)
6767{
6768 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02006769 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006770 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02006771 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6772
6773 if (rtnl)
6774 rtnl_lock();
6775
6776 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02006777 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02006778 if (IS_ERR(rdev)) {
6779 if (rtnl)
6780 rtnl_unlock();
6781 return PTR_ERR(rdev);
6782 }
6783 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02006784 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
6785 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02006786 mutex_lock(&cfg80211_mutex);
6787 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
6788 info->attrs);
6789 if (IS_ERR(wdev)) {
6790 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02006791 if (rtnl)
6792 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02006793 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02006794 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006795
Johannes Berg89a54e42012-06-15 14:33:17 +02006796 dev = wdev->netdev;
6797 rdev = wiphy_to_dev(wdev->wiphy);
6798
Johannes Berg1bf614e2012-06-15 15:23:36 +02006799 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6800 if (!dev) {
6801 mutex_unlock(&cfg80211_mutex);
6802 if (rtnl)
6803 rtnl_unlock();
6804 return -EINVAL;
6805 }
6806
6807 info->user_ptr[1] = dev;
6808 } else {
6809 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02006810 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006811
Johannes Berg1bf614e2012-06-15 15:23:36 +02006812 if (dev) {
6813 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6814 !netif_running(dev)) {
6815 mutex_unlock(&cfg80211_mutex);
6816 if (rtnl)
6817 rtnl_unlock();
6818 return -ENETDOWN;
6819 }
6820
6821 dev_hold(dev);
6822 }
6823
Johannes Berg89a54e42012-06-15 14:33:17 +02006824 cfg80211_lock_rdev(rdev);
6825
6826 mutex_unlock(&cfg80211_mutex);
6827
Johannes Berg4c476992010-10-04 21:36:35 +02006828 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006829 }
6830
6831 return 0;
6832}
6833
6834static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6835 struct genl_info *info)
6836{
6837 if (info->user_ptr[0])
6838 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02006839 if (info->user_ptr[1]) {
6840 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
6841 struct wireless_dev *wdev = info->user_ptr[1];
6842
6843 if (wdev->netdev)
6844 dev_put(wdev->netdev);
6845 } else {
6846 dev_put(info->user_ptr[1]);
6847 }
6848 }
Johannes Berg4c476992010-10-04 21:36:35 +02006849 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6850 rtnl_unlock();
6851}
6852
Johannes Berg55682962007-09-20 13:09:35 -04006853static struct genl_ops nl80211_ops[] = {
6854 {
6855 .cmd = NL80211_CMD_GET_WIPHY,
6856 .doit = nl80211_get_wiphy,
6857 .dumpit = nl80211_dump_wiphy,
6858 .policy = nl80211_policy,
6859 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006860 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006861 },
6862 {
6863 .cmd = NL80211_CMD_SET_WIPHY,
6864 .doit = nl80211_set_wiphy,
6865 .policy = nl80211_policy,
6866 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006867 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006868 },
6869 {
6870 .cmd = NL80211_CMD_GET_INTERFACE,
6871 .doit = nl80211_get_interface,
6872 .dumpit = nl80211_dump_interface,
6873 .policy = nl80211_policy,
6874 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02006875 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006876 },
6877 {
6878 .cmd = NL80211_CMD_SET_INTERFACE,
6879 .doit = nl80211_set_interface,
6880 .policy = nl80211_policy,
6881 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006882 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6883 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006884 },
6885 {
6886 .cmd = NL80211_CMD_NEW_INTERFACE,
6887 .doit = nl80211_new_interface,
6888 .policy = nl80211_policy,
6889 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006890 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6891 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006892 },
6893 {
6894 .cmd = NL80211_CMD_DEL_INTERFACE,
6895 .doit = nl80211_del_interface,
6896 .policy = nl80211_policy,
6897 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02006898 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02006899 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006900 },
Johannes Berg41ade002007-12-19 02:03:29 +01006901 {
6902 .cmd = NL80211_CMD_GET_KEY,
6903 .doit = nl80211_get_key,
6904 .policy = nl80211_policy,
6905 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006906 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006907 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006908 },
6909 {
6910 .cmd = NL80211_CMD_SET_KEY,
6911 .doit = nl80211_set_key,
6912 .policy = nl80211_policy,
6913 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006914 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006915 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006916 },
6917 {
6918 .cmd = NL80211_CMD_NEW_KEY,
6919 .doit = nl80211_new_key,
6920 .policy = nl80211_policy,
6921 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006922 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006923 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006924 },
6925 {
6926 .cmd = NL80211_CMD_DEL_KEY,
6927 .doit = nl80211_del_key,
6928 .policy = nl80211_policy,
6929 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006930 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006931 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006932 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006933 {
6934 .cmd = NL80211_CMD_SET_BEACON,
6935 .policy = nl80211_policy,
6936 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006937 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006938 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006939 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006940 },
6941 {
Johannes Berg88600202012-02-13 15:17:18 +01006942 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006943 .policy = nl80211_policy,
6944 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006945 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006946 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006947 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006948 },
6949 {
Johannes Berg88600202012-02-13 15:17:18 +01006950 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006951 .policy = nl80211_policy,
6952 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006953 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006954 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006955 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006956 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006957 {
6958 .cmd = NL80211_CMD_GET_STATION,
6959 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006960 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006961 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006962 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6963 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006964 },
6965 {
6966 .cmd = NL80211_CMD_SET_STATION,
6967 .doit = nl80211_set_station,
6968 .policy = nl80211_policy,
6969 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006970 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006971 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006972 },
6973 {
6974 .cmd = NL80211_CMD_NEW_STATION,
6975 .doit = nl80211_new_station,
6976 .policy = nl80211_policy,
6977 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006978 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006979 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006980 },
6981 {
6982 .cmd = NL80211_CMD_DEL_STATION,
6983 .doit = nl80211_del_station,
6984 .policy = nl80211_policy,
6985 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006986 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006987 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006988 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006989 {
6990 .cmd = NL80211_CMD_GET_MPATH,
6991 .doit = nl80211_get_mpath,
6992 .dumpit = nl80211_dump_mpath,
6993 .policy = nl80211_policy,
6994 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006995 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006996 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006997 },
6998 {
6999 .cmd = NL80211_CMD_SET_MPATH,
7000 .doit = nl80211_set_mpath,
7001 .policy = nl80211_policy,
7002 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007003 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007004 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007005 },
7006 {
7007 .cmd = NL80211_CMD_NEW_MPATH,
7008 .doit = nl80211_new_mpath,
7009 .policy = nl80211_policy,
7010 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007011 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007012 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007013 },
7014 {
7015 .cmd = NL80211_CMD_DEL_MPATH,
7016 .doit = nl80211_del_mpath,
7017 .policy = nl80211_policy,
7018 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007019 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007020 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007021 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007022 {
7023 .cmd = NL80211_CMD_SET_BSS,
7024 .doit = nl80211_set_bss,
7025 .policy = nl80211_policy,
7026 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007027 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007028 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007029 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007030 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007031 .cmd = NL80211_CMD_GET_REG,
7032 .doit = nl80211_get_reg,
7033 .policy = nl80211_policy,
7034 /* can be retrieved by unprivileged users */
7035 },
7036 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007037 .cmd = NL80211_CMD_SET_REG,
7038 .doit = nl80211_set_reg,
7039 .policy = nl80211_policy,
7040 .flags = GENL_ADMIN_PERM,
7041 },
7042 {
7043 .cmd = NL80211_CMD_REQ_SET_REG,
7044 .doit = nl80211_req_set_reg,
7045 .policy = nl80211_policy,
7046 .flags = GENL_ADMIN_PERM,
7047 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007048 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007049 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7050 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007051 .policy = nl80211_policy,
7052 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007053 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007054 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007055 },
7056 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007057 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7058 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007059 .policy = nl80211_policy,
7060 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007061 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007062 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007063 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007064 {
Johannes Berg2a519312009-02-10 21:25:55 +01007065 .cmd = NL80211_CMD_TRIGGER_SCAN,
7066 .doit = nl80211_trigger_scan,
7067 .policy = nl80211_policy,
7068 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007069 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007070 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007071 },
7072 {
7073 .cmd = NL80211_CMD_GET_SCAN,
7074 .policy = nl80211_policy,
7075 .dumpit = nl80211_dump_scan,
7076 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007077 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007078 .cmd = NL80211_CMD_START_SCHED_SCAN,
7079 .doit = nl80211_start_sched_scan,
7080 .policy = nl80211_policy,
7081 .flags = GENL_ADMIN_PERM,
7082 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7083 NL80211_FLAG_NEED_RTNL,
7084 },
7085 {
7086 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7087 .doit = nl80211_stop_sched_scan,
7088 .policy = nl80211_policy,
7089 .flags = GENL_ADMIN_PERM,
7090 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7091 NL80211_FLAG_NEED_RTNL,
7092 },
7093 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007094 .cmd = NL80211_CMD_AUTHENTICATE,
7095 .doit = nl80211_authenticate,
7096 .policy = nl80211_policy,
7097 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007098 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007099 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007100 },
7101 {
7102 .cmd = NL80211_CMD_ASSOCIATE,
7103 .doit = nl80211_associate,
7104 .policy = nl80211_policy,
7105 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007106 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007107 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007108 },
7109 {
7110 .cmd = NL80211_CMD_DEAUTHENTICATE,
7111 .doit = nl80211_deauthenticate,
7112 .policy = nl80211_policy,
7113 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007114 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007115 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007116 },
7117 {
7118 .cmd = NL80211_CMD_DISASSOCIATE,
7119 .doit = nl80211_disassociate,
7120 .policy = nl80211_policy,
7121 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007122 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007123 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007124 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007125 {
7126 .cmd = NL80211_CMD_JOIN_IBSS,
7127 .doit = nl80211_join_ibss,
7128 .policy = nl80211_policy,
7129 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007130 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007131 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007132 },
7133 {
7134 .cmd = NL80211_CMD_LEAVE_IBSS,
7135 .doit = nl80211_leave_ibss,
7136 .policy = nl80211_policy,
7137 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007138 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007139 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007140 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007141#ifdef CONFIG_NL80211_TESTMODE
7142 {
7143 .cmd = NL80211_CMD_TESTMODE,
7144 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007145 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007146 .policy = nl80211_policy,
7147 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007148 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7149 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007150 },
7151#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007152 {
7153 .cmd = NL80211_CMD_CONNECT,
7154 .doit = nl80211_connect,
7155 .policy = nl80211_policy,
7156 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007157 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007158 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007159 },
7160 {
7161 .cmd = NL80211_CMD_DISCONNECT,
7162 .doit = nl80211_disconnect,
7163 .policy = nl80211_policy,
7164 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007165 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007166 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007167 },
Johannes Berg463d0182009-07-14 00:33:35 +02007168 {
7169 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7170 .doit = nl80211_wiphy_netns,
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 Berg463d0182009-07-14 00:33:35 +02007175 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007176 {
7177 .cmd = NL80211_CMD_GET_SURVEY,
7178 .policy = nl80211_policy,
7179 .dumpit = nl80211_dump_survey,
7180 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007181 {
7182 .cmd = NL80211_CMD_SET_PMKSA,
7183 .doit = nl80211_setdel_pmksa,
7184 .policy = nl80211_policy,
7185 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007186 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007187 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007188 },
7189 {
7190 .cmd = NL80211_CMD_DEL_PMKSA,
7191 .doit = nl80211_setdel_pmksa,
7192 .policy = nl80211_policy,
7193 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007194 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007195 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007196 },
7197 {
7198 .cmd = NL80211_CMD_FLUSH_PMKSA,
7199 .doit = nl80211_flush_pmksa,
7200 .policy = nl80211_policy,
7201 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007202 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007203 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007204 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007205 {
7206 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7207 .doit = nl80211_remain_on_channel,
7208 .policy = nl80211_policy,
7209 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007210 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007211 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007212 },
7213 {
7214 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7215 .doit = nl80211_cancel_remain_on_channel,
7216 .policy = nl80211_policy,
7217 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007218 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007219 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007220 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007221 {
7222 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7223 .doit = nl80211_set_tx_bitrate_mask,
7224 .policy = nl80211_policy,
7225 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007226 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7227 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007228 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007229 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007230 .cmd = NL80211_CMD_REGISTER_FRAME,
7231 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007232 .policy = nl80211_policy,
7233 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007234 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007235 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007236 },
7237 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007238 .cmd = NL80211_CMD_FRAME,
7239 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007240 .policy = nl80211_policy,
7241 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007242 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007243 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007244 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007245 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007246 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7247 .doit = nl80211_tx_mgmt_cancel_wait,
7248 .policy = nl80211_policy,
7249 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007250 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007251 NL80211_FLAG_NEED_RTNL,
7252 },
7253 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007254 .cmd = NL80211_CMD_SET_POWER_SAVE,
7255 .doit = nl80211_set_power_save,
7256 .policy = nl80211_policy,
7257 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007258 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7259 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007260 },
7261 {
7262 .cmd = NL80211_CMD_GET_POWER_SAVE,
7263 .doit = nl80211_get_power_save,
7264 .policy = nl80211_policy,
7265 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007266 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7267 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007268 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007269 {
7270 .cmd = NL80211_CMD_SET_CQM,
7271 .doit = nl80211_set_cqm,
7272 .policy = nl80211_policy,
7273 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007274 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7275 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007276 },
Johannes Bergf444de02010-05-05 15:25:02 +02007277 {
7278 .cmd = NL80211_CMD_SET_CHANNEL,
7279 .doit = nl80211_set_channel,
7280 .policy = nl80211_policy,
7281 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007282 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7283 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007284 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007285 {
7286 .cmd = NL80211_CMD_SET_WDS_PEER,
7287 .doit = nl80211_set_wds_peer,
7288 .policy = nl80211_policy,
7289 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007290 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7291 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007292 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007293 {
7294 .cmd = NL80211_CMD_JOIN_MESH,
7295 .doit = nl80211_join_mesh,
7296 .policy = nl80211_policy,
7297 .flags = GENL_ADMIN_PERM,
7298 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7299 NL80211_FLAG_NEED_RTNL,
7300 },
7301 {
7302 .cmd = NL80211_CMD_LEAVE_MESH,
7303 .doit = nl80211_leave_mesh,
7304 .policy = nl80211_policy,
7305 .flags = GENL_ADMIN_PERM,
7306 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7307 NL80211_FLAG_NEED_RTNL,
7308 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007309#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007310 {
7311 .cmd = NL80211_CMD_GET_WOWLAN,
7312 .doit = nl80211_get_wowlan,
7313 .policy = nl80211_policy,
7314 /* can be retrieved by unprivileged users */
7315 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7316 NL80211_FLAG_NEED_RTNL,
7317 },
7318 {
7319 .cmd = NL80211_CMD_SET_WOWLAN,
7320 .doit = nl80211_set_wowlan,
7321 .policy = nl80211_policy,
7322 .flags = GENL_ADMIN_PERM,
7323 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7324 NL80211_FLAG_NEED_RTNL,
7325 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007326#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007327 {
7328 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7329 .doit = nl80211_set_rekey_data,
7330 .policy = nl80211_policy,
7331 .flags = GENL_ADMIN_PERM,
7332 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7333 NL80211_FLAG_NEED_RTNL,
7334 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007335 {
7336 .cmd = NL80211_CMD_TDLS_MGMT,
7337 .doit = nl80211_tdls_mgmt,
7338 .policy = nl80211_policy,
7339 .flags = GENL_ADMIN_PERM,
7340 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7341 NL80211_FLAG_NEED_RTNL,
7342 },
7343 {
7344 .cmd = NL80211_CMD_TDLS_OPER,
7345 .doit = nl80211_tdls_oper,
7346 .policy = nl80211_policy,
7347 .flags = GENL_ADMIN_PERM,
7348 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7349 NL80211_FLAG_NEED_RTNL,
7350 },
Johannes Berg28946da2011-11-04 11:18:12 +01007351 {
7352 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7353 .doit = nl80211_register_unexpected_frame,
7354 .policy = nl80211_policy,
7355 .flags = GENL_ADMIN_PERM,
7356 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7357 NL80211_FLAG_NEED_RTNL,
7358 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007359 {
7360 .cmd = NL80211_CMD_PROBE_CLIENT,
7361 .doit = nl80211_probe_client,
7362 .policy = nl80211_policy,
7363 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007364 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007365 NL80211_FLAG_NEED_RTNL,
7366 },
Johannes Berg5e760232011-11-04 11:18:17 +01007367 {
7368 .cmd = NL80211_CMD_REGISTER_BEACONS,
7369 .doit = nl80211_register_beacons,
7370 .policy = nl80211_policy,
7371 .flags = GENL_ADMIN_PERM,
7372 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7373 NL80211_FLAG_NEED_RTNL,
7374 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007375 {
7376 .cmd = NL80211_CMD_SET_NOACK_MAP,
7377 .doit = nl80211_set_noack_map,
7378 .policy = nl80211_policy,
7379 .flags = GENL_ADMIN_PERM,
7380 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7381 NL80211_FLAG_NEED_RTNL,
7382 },
7383
Johannes Berg55682962007-09-20 13:09:35 -04007384};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007385
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007386static struct genl_multicast_group nl80211_mlme_mcgrp = {
7387 .name = "mlme",
7388};
Johannes Berg55682962007-09-20 13:09:35 -04007389
7390/* multicast groups */
7391static struct genl_multicast_group nl80211_config_mcgrp = {
7392 .name = "config",
7393};
Johannes Berg2a519312009-02-10 21:25:55 +01007394static struct genl_multicast_group nl80211_scan_mcgrp = {
7395 .name = "scan",
7396};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007397static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7398 .name = "regulatory",
7399};
Johannes Berg55682962007-09-20 13:09:35 -04007400
7401/* notification functions */
7402
7403void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7404{
7405 struct sk_buff *msg;
7406
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007407 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007408 if (!msg)
7409 return;
7410
7411 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7412 nlmsg_free(msg);
7413 return;
7414 }
7415
Johannes Berg463d0182009-07-14 00:33:35 +02007416 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7417 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007418}
7419
Johannes Berg362a4152009-05-24 16:43:15 +02007420static int nl80211_add_scan_req(struct sk_buff *msg,
7421 struct cfg80211_registered_device *rdev)
7422{
7423 struct cfg80211_scan_request *req = rdev->scan_req;
7424 struct nlattr *nest;
7425 int i;
7426
Johannes Berg667503d2009-07-07 03:56:11 +02007427 ASSERT_RDEV_LOCK(rdev);
7428
Johannes Berg362a4152009-05-24 16:43:15 +02007429 if (WARN_ON(!req))
7430 return 0;
7431
7432 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7433 if (!nest)
7434 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007435 for (i = 0; i < req->n_ssids; i++) {
7436 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7437 goto nla_put_failure;
7438 }
Johannes Berg362a4152009-05-24 16:43:15 +02007439 nla_nest_end(msg, nest);
7440
7441 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7442 if (!nest)
7443 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007444 for (i = 0; i < req->n_channels; i++) {
7445 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7446 goto nla_put_failure;
7447 }
Johannes Berg362a4152009-05-24 16:43:15 +02007448 nla_nest_end(msg, nest);
7449
David S. Miller9360ffd2012-03-29 04:41:26 -04007450 if (req->ie &&
7451 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7452 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007453
7454 return 0;
7455 nla_put_failure:
7456 return -ENOBUFS;
7457}
7458
Johannes Berga538e2d2009-06-16 19:56:42 +02007459static int nl80211_send_scan_msg(struct sk_buff *msg,
7460 struct cfg80211_registered_device *rdev,
7461 struct net_device *netdev,
7462 u32 pid, u32 seq, int flags,
7463 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007464{
7465 void *hdr;
7466
7467 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7468 if (!hdr)
7469 return -1;
7470
David S. Miller9360ffd2012-03-29 04:41:26 -04007471 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7472 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7473 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007474
Johannes Berg362a4152009-05-24 16:43:15 +02007475 /* ignore errors and send incomplete event anyway */
7476 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007477
7478 return genlmsg_end(msg, hdr);
7479
7480 nla_put_failure:
7481 genlmsg_cancel(msg, hdr);
7482 return -EMSGSIZE;
7483}
7484
Luciano Coelho807f8a82011-05-11 17:09:35 +03007485static int
7486nl80211_send_sched_scan_msg(struct sk_buff *msg,
7487 struct cfg80211_registered_device *rdev,
7488 struct net_device *netdev,
7489 u32 pid, u32 seq, int flags, u32 cmd)
7490{
7491 void *hdr;
7492
7493 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7494 if (!hdr)
7495 return -1;
7496
David S. Miller9360ffd2012-03-29 04:41:26 -04007497 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7498 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7499 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007500
7501 return genlmsg_end(msg, hdr);
7502
7503 nla_put_failure:
7504 genlmsg_cancel(msg, hdr);
7505 return -EMSGSIZE;
7506}
7507
Johannes Berga538e2d2009-06-16 19:56:42 +02007508void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7509 struct net_device *netdev)
7510{
7511 struct sk_buff *msg;
7512
7513 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7514 if (!msg)
7515 return;
7516
7517 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7518 NL80211_CMD_TRIGGER_SCAN) < 0) {
7519 nlmsg_free(msg);
7520 return;
7521 }
7522
Johannes Berg463d0182009-07-14 00:33:35 +02007523 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7524 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007525}
7526
Johannes Berg2a519312009-02-10 21:25:55 +01007527void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7528 struct net_device *netdev)
7529{
7530 struct sk_buff *msg;
7531
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007532 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007533 if (!msg)
7534 return;
7535
Johannes Berga538e2d2009-06-16 19:56:42 +02007536 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7537 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007538 nlmsg_free(msg);
7539 return;
7540 }
7541
Johannes Berg463d0182009-07-14 00:33:35 +02007542 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7543 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007544}
7545
7546void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7547 struct net_device *netdev)
7548{
7549 struct sk_buff *msg;
7550
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007551 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007552 if (!msg)
7553 return;
7554
Johannes Berga538e2d2009-06-16 19:56:42 +02007555 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7556 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007557 nlmsg_free(msg);
7558 return;
7559 }
7560
Johannes Berg463d0182009-07-14 00:33:35 +02007561 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7562 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007563}
7564
Luciano Coelho807f8a82011-05-11 17:09:35 +03007565void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7566 struct net_device *netdev)
7567{
7568 struct sk_buff *msg;
7569
7570 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7571 if (!msg)
7572 return;
7573
7574 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7575 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7576 nlmsg_free(msg);
7577 return;
7578 }
7579
7580 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7581 nl80211_scan_mcgrp.id, GFP_KERNEL);
7582}
7583
7584void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7585 struct net_device *netdev, u32 cmd)
7586{
7587 struct sk_buff *msg;
7588
7589 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7590 if (!msg)
7591 return;
7592
7593 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7594 nlmsg_free(msg);
7595 return;
7596 }
7597
7598 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7599 nl80211_scan_mcgrp.id, GFP_KERNEL);
7600}
7601
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007602/*
7603 * This can happen on global regulatory changes or device specific settings
7604 * based on custom world regulatory domains.
7605 */
7606void nl80211_send_reg_change_event(struct regulatory_request *request)
7607{
7608 struct sk_buff *msg;
7609 void *hdr;
7610
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007611 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007612 if (!msg)
7613 return;
7614
7615 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7616 if (!hdr) {
7617 nlmsg_free(msg);
7618 return;
7619 }
7620
7621 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007622 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7623 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007624
David S. Miller9360ffd2012-03-29 04:41:26 -04007625 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7626 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7627 NL80211_REGDOM_TYPE_WORLD))
7628 goto nla_put_failure;
7629 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7630 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7631 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7632 goto nla_put_failure;
7633 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7634 request->intersect) {
7635 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7636 NL80211_REGDOM_TYPE_INTERSECTION))
7637 goto nla_put_failure;
7638 } else {
7639 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7640 NL80211_REGDOM_TYPE_COUNTRY) ||
7641 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7642 request->alpha2))
7643 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007644 }
7645
David S. Miller9360ffd2012-03-29 04:41:26 -04007646 if (wiphy_idx_valid(request->wiphy_idx) &&
7647 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7648 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007649
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007650 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007651
Johannes Bergbc43b282009-07-25 10:54:13 +02007652 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007653 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007654 GFP_ATOMIC);
7655 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007656
7657 return;
7658
7659nla_put_failure:
7660 genlmsg_cancel(msg, hdr);
7661 nlmsg_free(msg);
7662}
7663
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007664static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7665 struct net_device *netdev,
7666 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007667 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007668{
7669 struct sk_buff *msg;
7670 void *hdr;
7671
Johannes Berge6d6e342009-07-01 21:26:47 +02007672 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007673 if (!msg)
7674 return;
7675
7676 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7677 if (!hdr) {
7678 nlmsg_free(msg);
7679 return;
7680 }
7681
David S. Miller9360ffd2012-03-29 04:41:26 -04007682 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7683 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7684 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7685 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007686
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007687 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007688
Johannes Berg463d0182009-07-14 00:33:35 +02007689 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7690 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007691 return;
7692
7693 nla_put_failure:
7694 genlmsg_cancel(msg, hdr);
7695 nlmsg_free(msg);
7696}
7697
7698void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007699 struct net_device *netdev, const u8 *buf,
7700 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007701{
7702 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007703 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007704}
7705
7706void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7707 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007708 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007709{
Johannes Berge6d6e342009-07-01 21:26:47 +02007710 nl80211_send_mlme_event(rdev, netdev, buf, len,
7711 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007712}
7713
Jouni Malinen53b46b82009-03-27 20:53:56 +02007714void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007715 struct net_device *netdev, const u8 *buf,
7716 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007717{
7718 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007719 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007720}
7721
Jouni Malinen53b46b82009-03-27 20:53:56 +02007722void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7723 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007724 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007725{
7726 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007727 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007728}
7729
Jouni Malinencf4e5942010-12-16 00:52:40 +02007730void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7731 struct net_device *netdev, const u8 *buf,
7732 size_t len, gfp_t gfp)
7733{
7734 nl80211_send_mlme_event(rdev, netdev, buf, len,
7735 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7736}
7737
7738void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7739 struct net_device *netdev, const u8 *buf,
7740 size_t len, gfp_t gfp)
7741{
7742 nl80211_send_mlme_event(rdev, netdev, buf, len,
7743 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7744}
7745
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007746static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7747 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007748 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007749{
7750 struct sk_buff *msg;
7751 void *hdr;
7752
Johannes Berge6d6e342009-07-01 21:26:47 +02007753 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007754 if (!msg)
7755 return;
7756
7757 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7758 if (!hdr) {
7759 nlmsg_free(msg);
7760 return;
7761 }
7762
David S. Miller9360ffd2012-03-29 04:41:26 -04007763 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7764 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7765 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7766 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7767 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007768
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007769 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007770
Johannes Berg463d0182009-07-14 00:33:35 +02007771 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7772 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007773 return;
7774
7775 nla_put_failure:
7776 genlmsg_cancel(msg, hdr);
7777 nlmsg_free(msg);
7778}
7779
7780void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007781 struct net_device *netdev, const u8 *addr,
7782 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007783{
7784 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007785 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007786}
7787
7788void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007789 struct net_device *netdev, const u8 *addr,
7790 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007791{
Johannes Berge6d6e342009-07-01 21:26:47 +02007792 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7793 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007794}
7795
Samuel Ortizb23aa672009-07-01 21:26:54 +02007796void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7797 struct net_device *netdev, const u8 *bssid,
7798 const u8 *req_ie, size_t req_ie_len,
7799 const u8 *resp_ie, size_t resp_ie_len,
7800 u16 status, gfp_t gfp)
7801{
7802 struct sk_buff *msg;
7803 void *hdr;
7804
7805 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7806 if (!msg)
7807 return;
7808
7809 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7810 if (!hdr) {
7811 nlmsg_free(msg);
7812 return;
7813 }
7814
David S. Miller9360ffd2012-03-29 04:41:26 -04007815 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7816 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7817 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7818 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7819 (req_ie &&
7820 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7821 (resp_ie &&
7822 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7823 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007824
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007825 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007826
Johannes Berg463d0182009-07-14 00:33:35 +02007827 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7828 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007829 return;
7830
7831 nla_put_failure:
7832 genlmsg_cancel(msg, hdr);
7833 nlmsg_free(msg);
7834
7835}
7836
7837void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7838 struct net_device *netdev, const u8 *bssid,
7839 const u8 *req_ie, size_t req_ie_len,
7840 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7841{
7842 struct sk_buff *msg;
7843 void *hdr;
7844
7845 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7846 if (!msg)
7847 return;
7848
7849 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7850 if (!hdr) {
7851 nlmsg_free(msg);
7852 return;
7853 }
7854
David S. Miller9360ffd2012-03-29 04:41:26 -04007855 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7856 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7857 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7858 (req_ie &&
7859 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7860 (resp_ie &&
7861 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7862 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007863
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007864 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007865
Johannes Berg463d0182009-07-14 00:33:35 +02007866 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7867 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007868 return;
7869
7870 nla_put_failure:
7871 genlmsg_cancel(msg, hdr);
7872 nlmsg_free(msg);
7873
7874}
7875
7876void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7877 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02007878 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007879{
7880 struct sk_buff *msg;
7881 void *hdr;
7882
Johannes Berg667503d2009-07-07 03:56:11 +02007883 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007884 if (!msg)
7885 return;
7886
7887 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7888 if (!hdr) {
7889 nlmsg_free(msg);
7890 return;
7891 }
7892
David S. Miller9360ffd2012-03-29 04:41:26 -04007893 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7894 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7895 (from_ap && reason &&
7896 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7897 (from_ap &&
7898 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7899 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7900 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007901
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007902 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007903
Johannes Berg463d0182009-07-14 00:33:35 +02007904 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7905 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007906 return;
7907
7908 nla_put_failure:
7909 genlmsg_cancel(msg, hdr);
7910 nlmsg_free(msg);
7911
7912}
7913
Johannes Berg04a773a2009-04-19 21:24:32 +02007914void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7915 struct net_device *netdev, const u8 *bssid,
7916 gfp_t gfp)
7917{
7918 struct sk_buff *msg;
7919 void *hdr;
7920
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007921 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007922 if (!msg)
7923 return;
7924
7925 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7926 if (!hdr) {
7927 nlmsg_free(msg);
7928 return;
7929 }
7930
David S. Miller9360ffd2012-03-29 04:41:26 -04007931 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7932 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7933 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7934 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007935
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007936 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007937
Johannes Berg463d0182009-07-14 00:33:35 +02007938 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7939 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007940 return;
7941
7942 nla_put_failure:
7943 genlmsg_cancel(msg, hdr);
7944 nlmsg_free(msg);
7945}
7946
Javier Cardonac93b5e72011-04-07 15:08:34 -07007947void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7948 struct net_device *netdev,
7949 const u8 *macaddr, const u8* ie, u8 ie_len,
7950 gfp_t gfp)
7951{
7952 struct sk_buff *msg;
7953 void *hdr;
7954
7955 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7956 if (!msg)
7957 return;
7958
7959 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7960 if (!hdr) {
7961 nlmsg_free(msg);
7962 return;
7963 }
7964
David S. Miller9360ffd2012-03-29 04:41:26 -04007965 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7966 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7967 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7968 (ie_len && ie &&
7969 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7970 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007971
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007972 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007973
7974 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7975 nl80211_mlme_mcgrp.id, gfp);
7976 return;
7977
7978 nla_put_failure:
7979 genlmsg_cancel(msg, hdr);
7980 nlmsg_free(msg);
7981}
7982
Jouni Malinena3b8b052009-03-27 21:59:49 +02007983void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7984 struct net_device *netdev, const u8 *addr,
7985 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007986 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007987{
7988 struct sk_buff *msg;
7989 void *hdr;
7990
Johannes Berge6d6e342009-07-01 21:26:47 +02007991 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007992 if (!msg)
7993 return;
7994
7995 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7996 if (!hdr) {
7997 nlmsg_free(msg);
7998 return;
7999 }
8000
David S. Miller9360ffd2012-03-29 04:41:26 -04008001 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8002 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8003 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8004 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8005 (key_id != -1 &&
8006 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8007 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8008 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008009
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008010 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008011
Johannes Berg463d0182009-07-14 00:33:35 +02008012 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8013 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008014 return;
8015
8016 nla_put_failure:
8017 genlmsg_cancel(msg, hdr);
8018 nlmsg_free(msg);
8019}
8020
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008021void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8022 struct ieee80211_channel *channel_before,
8023 struct ieee80211_channel *channel_after)
8024{
8025 struct sk_buff *msg;
8026 void *hdr;
8027 struct nlattr *nl_freq;
8028
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008029 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008030 if (!msg)
8031 return;
8032
8033 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8034 if (!hdr) {
8035 nlmsg_free(msg);
8036 return;
8037 }
8038
8039 /*
8040 * Since we are applying the beacon hint to a wiphy we know its
8041 * wiphy_idx is valid
8042 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008043 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8044 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008045
8046 /* Before */
8047 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8048 if (!nl_freq)
8049 goto nla_put_failure;
8050 if (nl80211_msg_put_channel(msg, channel_before))
8051 goto nla_put_failure;
8052 nla_nest_end(msg, nl_freq);
8053
8054 /* After */
8055 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8056 if (!nl_freq)
8057 goto nla_put_failure;
8058 if (nl80211_msg_put_channel(msg, channel_after))
8059 goto nla_put_failure;
8060 nla_nest_end(msg, nl_freq);
8061
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008062 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008063
Johannes Berg463d0182009-07-14 00:33:35 +02008064 rcu_read_lock();
8065 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8066 GFP_ATOMIC);
8067 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008068
8069 return;
8070
8071nla_put_failure:
8072 genlmsg_cancel(msg, hdr);
8073 nlmsg_free(msg);
8074}
8075
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008076static void nl80211_send_remain_on_chan_event(
8077 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008078 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008079 struct ieee80211_channel *chan,
8080 enum nl80211_channel_type channel_type,
8081 unsigned int duration, gfp_t gfp)
8082{
8083 struct sk_buff *msg;
8084 void *hdr;
8085
8086 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8087 if (!msg)
8088 return;
8089
8090 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8091 if (!hdr) {
8092 nlmsg_free(msg);
8093 return;
8094 }
8095
David S. Miller9360ffd2012-03-29 04:41:26 -04008096 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008097 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8098 wdev->netdev->ifindex)) ||
8099 nla_put_u32(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008100 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8101 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8102 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8103 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008104
David S. Miller9360ffd2012-03-29 04:41:26 -04008105 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8106 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8107 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008108
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008109 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008110
8111 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8112 nl80211_mlme_mcgrp.id, gfp);
8113 return;
8114
8115 nla_put_failure:
8116 genlmsg_cancel(msg, hdr);
8117 nlmsg_free(msg);
8118}
8119
8120void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008121 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008122 struct ieee80211_channel *chan,
8123 enum nl80211_channel_type channel_type,
8124 unsigned int duration, gfp_t gfp)
8125{
8126 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008127 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008128 channel_type, duration, gfp);
8129}
8130
8131void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008132 struct cfg80211_registered_device *rdev,
8133 struct wireless_dev *wdev,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008134 u64 cookie, struct ieee80211_channel *chan,
8135 enum nl80211_channel_type channel_type, gfp_t gfp)
8136{
8137 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008138 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008139 channel_type, 0, gfp);
8140}
8141
Johannes Berg98b62182009-12-23 13:15:44 +01008142void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8143 struct net_device *dev, const u8 *mac_addr,
8144 struct station_info *sinfo, gfp_t gfp)
8145{
8146 struct sk_buff *msg;
8147
8148 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8149 if (!msg)
8150 return;
8151
John W. Linville66266b32012-03-15 13:25:41 -04008152 if (nl80211_send_station(msg, 0, 0, 0,
8153 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008154 nlmsg_free(msg);
8155 return;
8156 }
8157
8158 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8159 nl80211_mlme_mcgrp.id, gfp);
8160}
8161
Jouni Malinenec15e682011-03-23 15:29:52 +02008162void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8163 struct net_device *dev, const u8 *mac_addr,
8164 gfp_t gfp)
8165{
8166 struct sk_buff *msg;
8167 void *hdr;
8168
8169 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8170 if (!msg)
8171 return;
8172
8173 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8174 if (!hdr) {
8175 nlmsg_free(msg);
8176 return;
8177 }
8178
David S. Miller9360ffd2012-03-29 04:41:26 -04008179 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8180 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8181 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008182
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008183 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008184
8185 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8186 nl80211_mlme_mcgrp.id, gfp);
8187 return;
8188
8189 nla_put_failure:
8190 genlmsg_cancel(msg, hdr);
8191 nlmsg_free(msg);
8192}
8193
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008194static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8195 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008196{
8197 struct wireless_dev *wdev = dev->ieee80211_ptr;
8198 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8199 struct sk_buff *msg;
8200 void *hdr;
8201 int err;
8202 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
8203
8204 if (!nlpid)
8205 return false;
8206
8207 msg = nlmsg_new(100, gfp);
8208 if (!msg)
8209 return true;
8210
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008211 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008212 if (!hdr) {
8213 nlmsg_free(msg);
8214 return true;
8215 }
8216
David S. Miller9360ffd2012-03-29 04:41:26 -04008217 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8218 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8219 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8220 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008221
8222 err = genlmsg_end(msg, hdr);
8223 if (err < 0) {
8224 nlmsg_free(msg);
8225 return true;
8226 }
8227
8228 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8229 return true;
8230
8231 nla_put_failure:
8232 genlmsg_cancel(msg, hdr);
8233 nlmsg_free(msg);
8234 return true;
8235}
8236
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008237bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8238{
8239 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8240 addr, gfp);
8241}
8242
8243bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8244 const u8 *addr, gfp_t gfp)
8245{
8246 return __nl80211_unexpected_frame(dev,
8247 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8248 addr, gfp);
8249}
8250
Johannes Berg2e161f72010-08-12 15:38:38 +02008251int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008252 struct wireless_dev *wdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01008253 int freq, int sig_dbm,
8254 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008255{
Johannes Berg71bbc992012-06-15 15:30:18 +02008256 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008257 struct sk_buff *msg;
8258 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008259
8260 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8261 if (!msg)
8262 return -ENOMEM;
8263
Johannes Berg2e161f72010-08-12 15:38:38 +02008264 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008265 if (!hdr) {
8266 nlmsg_free(msg);
8267 return -ENOMEM;
8268 }
8269
David S. Miller9360ffd2012-03-29 04:41:26 -04008270 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008271 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8272 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008273 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8274 (sig_dbm &&
8275 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8276 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8277 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008278
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008279 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008280
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008281 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008282
8283 nla_put_failure:
8284 genlmsg_cancel(msg, hdr);
8285 nlmsg_free(msg);
8286 return -ENOBUFS;
8287}
8288
Johannes Berg2e161f72010-08-12 15:38:38 +02008289void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008290 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008291 const u8 *buf, size_t len, bool ack,
8292 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008293{
Johannes Berg71bbc992012-06-15 15:30:18 +02008294 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008295 struct sk_buff *msg;
8296 void *hdr;
8297
8298 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8299 if (!msg)
8300 return;
8301
Johannes Berg2e161f72010-08-12 15:38:38 +02008302 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008303 if (!hdr) {
8304 nlmsg_free(msg);
8305 return;
8306 }
8307
David S. Miller9360ffd2012-03-29 04:41:26 -04008308 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008309 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8310 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008311 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8312 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8313 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8314 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008315
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008316 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008317
8318 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8319 return;
8320
8321 nla_put_failure:
8322 genlmsg_cancel(msg, hdr);
8323 nlmsg_free(msg);
8324}
8325
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008326void
8327nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8328 struct net_device *netdev,
8329 enum nl80211_cqm_rssi_threshold_event rssi_event,
8330 gfp_t gfp)
8331{
8332 struct sk_buff *msg;
8333 struct nlattr *pinfoattr;
8334 void *hdr;
8335
8336 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8337 if (!msg)
8338 return;
8339
8340 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8341 if (!hdr) {
8342 nlmsg_free(msg);
8343 return;
8344 }
8345
David S. Miller9360ffd2012-03-29 04:41:26 -04008346 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8347 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8348 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008349
8350 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8351 if (!pinfoattr)
8352 goto nla_put_failure;
8353
David S. Miller9360ffd2012-03-29 04:41:26 -04008354 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8355 rssi_event))
8356 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008357
8358 nla_nest_end(msg, pinfoattr);
8359
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008360 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008361
8362 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8363 nl80211_mlme_mcgrp.id, gfp);
8364 return;
8365
8366 nla_put_failure:
8367 genlmsg_cancel(msg, hdr);
8368 nlmsg_free(msg);
8369}
8370
Johannes Berge5497d72011-07-05 16:35:40 +02008371void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8372 struct net_device *netdev, const u8 *bssid,
8373 const u8 *replay_ctr, gfp_t gfp)
8374{
8375 struct sk_buff *msg;
8376 struct nlattr *rekey_attr;
8377 void *hdr;
8378
8379 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8380 if (!msg)
8381 return;
8382
8383 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8384 if (!hdr) {
8385 nlmsg_free(msg);
8386 return;
8387 }
8388
David S. Miller9360ffd2012-03-29 04:41:26 -04008389 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8390 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8391 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8392 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008393
8394 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8395 if (!rekey_attr)
8396 goto nla_put_failure;
8397
David S. Miller9360ffd2012-03-29 04:41:26 -04008398 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8399 NL80211_REPLAY_CTR_LEN, replay_ctr))
8400 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008401
8402 nla_nest_end(msg, rekey_attr);
8403
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008404 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008405
8406 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8407 nl80211_mlme_mcgrp.id, gfp);
8408 return;
8409
8410 nla_put_failure:
8411 genlmsg_cancel(msg, hdr);
8412 nlmsg_free(msg);
8413}
8414
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008415void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8416 struct net_device *netdev, int index,
8417 const u8 *bssid, bool preauth, gfp_t gfp)
8418{
8419 struct sk_buff *msg;
8420 struct nlattr *attr;
8421 void *hdr;
8422
8423 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8424 if (!msg)
8425 return;
8426
8427 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8428 if (!hdr) {
8429 nlmsg_free(msg);
8430 return;
8431 }
8432
David S. Miller9360ffd2012-03-29 04:41:26 -04008433 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8434 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8435 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008436
8437 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8438 if (!attr)
8439 goto nla_put_failure;
8440
David S. Miller9360ffd2012-03-29 04:41:26 -04008441 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8442 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8443 (preauth &&
8444 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8445 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008446
8447 nla_nest_end(msg, attr);
8448
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008449 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008450
8451 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8452 nl80211_mlme_mcgrp.id, gfp);
8453 return;
8454
8455 nla_put_failure:
8456 genlmsg_cancel(msg, hdr);
8457 nlmsg_free(msg);
8458}
8459
Thomas Pedersen53145262012-04-06 13:35:47 -07008460void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8461 struct net_device *netdev, int freq,
8462 enum nl80211_channel_type type, gfp_t gfp)
8463{
8464 struct sk_buff *msg;
8465 void *hdr;
8466
8467 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8468 if (!msg)
8469 return;
8470
8471 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8472 if (!hdr) {
8473 nlmsg_free(msg);
8474 return;
8475 }
8476
John W. Linville7eab0f62012-04-12 14:25:14 -04008477 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8478 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8479 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8480 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008481
8482 genlmsg_end(msg, hdr);
8483
8484 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8485 nl80211_mlme_mcgrp.id, gfp);
8486 return;
8487
8488 nla_put_failure:
8489 genlmsg_cancel(msg, hdr);
8490 nlmsg_free(msg);
8491}
8492
Johannes Bergc063dbf2010-11-24 08:10:05 +01008493void
8494nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8495 struct net_device *netdev, const u8 *peer,
8496 u32 num_packets, gfp_t gfp)
8497{
8498 struct sk_buff *msg;
8499 struct nlattr *pinfoattr;
8500 void *hdr;
8501
8502 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8503 if (!msg)
8504 return;
8505
8506 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8507 if (!hdr) {
8508 nlmsg_free(msg);
8509 return;
8510 }
8511
David S. Miller9360ffd2012-03-29 04:41:26 -04008512 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8513 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8514 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8515 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008516
8517 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8518 if (!pinfoattr)
8519 goto nla_put_failure;
8520
David S. Miller9360ffd2012-03-29 04:41:26 -04008521 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8522 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008523
8524 nla_nest_end(msg, pinfoattr);
8525
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008526 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008527
8528 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8529 nl80211_mlme_mcgrp.id, gfp);
8530 return;
8531
8532 nla_put_failure:
8533 genlmsg_cancel(msg, hdr);
8534 nlmsg_free(msg);
8535}
8536
Johannes Berg7f6cf312011-11-04 11:18:15 +01008537void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8538 u64 cookie, bool acked, gfp_t gfp)
8539{
8540 struct wireless_dev *wdev = dev->ieee80211_ptr;
8541 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8542 struct sk_buff *msg;
8543 void *hdr;
8544 int err;
8545
8546 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8547 if (!msg)
8548 return;
8549
8550 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8551 if (!hdr) {
8552 nlmsg_free(msg);
8553 return;
8554 }
8555
David S. Miller9360ffd2012-03-29 04:41:26 -04008556 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8557 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8558 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8559 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8560 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8561 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008562
8563 err = genlmsg_end(msg, hdr);
8564 if (err < 0) {
8565 nlmsg_free(msg);
8566 return;
8567 }
8568
8569 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8570 nl80211_mlme_mcgrp.id, gfp);
8571 return;
8572
8573 nla_put_failure:
8574 genlmsg_cancel(msg, hdr);
8575 nlmsg_free(msg);
8576}
8577EXPORT_SYMBOL(cfg80211_probe_status);
8578
Johannes Berg5e760232011-11-04 11:18:17 +01008579void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8580 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008581 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008582{
8583 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8584 struct sk_buff *msg;
8585 void *hdr;
8586 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8587
8588 if (!nlpid)
8589 return;
8590
8591 msg = nlmsg_new(len + 100, gfp);
8592 if (!msg)
8593 return;
8594
8595 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8596 if (!hdr) {
8597 nlmsg_free(msg);
8598 return;
8599 }
8600
David S. Miller9360ffd2012-03-29 04:41:26 -04008601 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8602 (freq &&
8603 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8604 (sig_dbm &&
8605 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8606 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8607 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008608
8609 genlmsg_end(msg, hdr);
8610
8611 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8612 return;
8613
8614 nla_put_failure:
8615 genlmsg_cancel(msg, hdr);
8616 nlmsg_free(msg);
8617}
8618EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8619
Jouni Malinen026331c2010-02-15 12:53:10 +02008620static int nl80211_netlink_notify(struct notifier_block * nb,
8621 unsigned long state,
8622 void *_notify)
8623{
8624 struct netlink_notify *notify = _notify;
8625 struct cfg80211_registered_device *rdev;
8626 struct wireless_dev *wdev;
8627
8628 if (state != NETLINK_URELEASE)
8629 return NOTIFY_DONE;
8630
8631 rcu_read_lock();
8632
Johannes Berg5e760232011-11-04 11:18:17 +01008633 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008634 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008635 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e760232011-11-04 11:18:17 +01008636 if (rdev->ap_beacons_nlpid == notify->pid)
8637 rdev->ap_beacons_nlpid = 0;
8638 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008639
8640 rcu_read_unlock();
8641
8642 return NOTIFY_DONE;
8643}
8644
8645static struct notifier_block nl80211_netlink_notifier = {
8646 .notifier_call = nl80211_netlink_notify,
8647};
8648
Johannes Berg55682962007-09-20 13:09:35 -04008649/* initialisation/exit functions */
8650
8651int nl80211_init(void)
8652{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008653 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008654
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008655 err = genl_register_family_with_ops(&nl80211_fam,
8656 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008657 if (err)
8658 return err;
8659
Johannes Berg55682962007-09-20 13:09:35 -04008660 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8661 if (err)
8662 goto err_out;
8663
Johannes Berg2a519312009-02-10 21:25:55 +01008664 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8665 if (err)
8666 goto err_out;
8667
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008668 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8669 if (err)
8670 goto err_out;
8671
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008672 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8673 if (err)
8674 goto err_out;
8675
Johannes Bergaff89a92009-07-01 21:26:51 +02008676#ifdef CONFIG_NL80211_TESTMODE
8677 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8678 if (err)
8679 goto err_out;
8680#endif
8681
Jouni Malinen026331c2010-02-15 12:53:10 +02008682 err = netlink_register_notifier(&nl80211_netlink_notifier);
8683 if (err)
8684 goto err_out;
8685
Johannes Berg55682962007-09-20 13:09:35 -04008686 return 0;
8687 err_out:
8688 genl_unregister_family(&nl80211_fam);
8689 return err;
8690}
8691
8692void nl80211_exit(void)
8693{
Jouni Malinen026331c2010-02-15 12:53:10 +02008694 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008695 genl_unregister_family(&nl80211_fam);
8696}